Friday, October 26, 2018

Matplotlib Tight Layouts and Plots with Insets

For normal plots or subplots, the tight_layout command does a pretty good job of keeping things from overlapping, and managing the bounding box of the overall figure.

However, if you have plot with insets etc. then tight_layout can throw a tantrum. Something like: "ValueError: max() arg is an empty sequence".

For such plots, if your axis label gets cut, and you don't want to push the label too close to the axis with something like:

ax1.xaxis.labelpad = -10

then, you can use the bbox_inches = "tight" flag when saving your figure to ensure your axis labels are not clipped.

plt.savefig('xyz.pdf',  bbox_inches = "tight")

Wednesday, October 24, 2018

Image Processing with Python

Basic
  • matplotlib can read png and jpg files as numpy objects.
  • imageio is a newer library that can read and write to a variety of image formats.
  • scipy.ndimage provides some additional functionality for manipulating the image arrays.
Intermediate
  • scikit-image is a library that offers a toolbox comparable to Matlab’s image processing toolbox.
Advanced

The following libraries provide more advanced functions for image manipulation.
Some resources on using Python’s basic image processing capabilities.

Sunday, October 21, 2018

The Secretary Problem

I've been obsessed with the Secretary Problem since I first heard about it. I even wrote about it over 7 years ago, when I used it to fashion a lab exercise in one of my classes.

Here is a nice old article (1989) "Who Solved the Secretary Problem" by Thomas Ferguson. If that link doesn't work, here is the stable url from JSTOR. It allows you to read up to six articles per month without any subscription.

Thursday, October 18, 2018

Jupyter Notebooks: Interacting with Python Files

You can make functions defined inside a python file [myFunctions.py] visible in a Jupyter Notebook by simply importing it as a module.

For example suppose myFunctions.py contains:

$ cat myFunctions.py

def func1():

def func2():

etc.

You can use the functions by importing the python file as a module.

from myFunctions import *

This makes func1() and func2() visible inside the Jupyter notebook.

This feature can be helpful in reducing clutter by moving large walls of code out of the notebook, which can then retain a simpler look and feel.

There are two potential issues:

  • when you make changes to python file, they are not immediately reflected in the notebook
  • if your python file has any "script"-like commands outside the function definitions, they are executed when the file is imported as a module

The first issue can be taken care of by the magic command %autoreload.

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: from myFunctions import func1

In [4]: func1()
Out[4]: 42

In [5]: # open myFunctions.py in an editor and change func1() to return 43

In [6]: func1()
Out[6]: 43

The second issue can be resolved by decorating the "script" commands with an appropriate if statement, which ensures that those commands are not executed unless the file is executed directly.

$ cat myFunctions.py

def func1():

def func(2):

#  
# Main Driver
# This part is not run when imported as a module
#
if __name__ == '__main__':
    some script commands
    print('something')

Monday, October 8, 2018

The Nature of Code

I accidentally stumbled on Daniel Shiffman's "The Nature of Code" online book. It is the ideal programming 102 book - something interesting to fool around with once you've learned the first few essentials of coding.
The goal of this book is simple. We want to take a look at something that naturally occurs in our physical world, then determine how we can write code to simulate that occurrence.
I love this real-world slant. The applications cover physics, biology, agent-based modeling, automata, neural networks etc.

Give it a look.