Keyboard Maestro macro to open .pickle file in IPython

I often want to view the contents of .pickle files on my computer. I like doing this in the IPython REPL, so I would manually open a new Terminal window, switch to my data science virtualenv, import pickle, and then load the .pickle file in question into a variable. Sounds like a prime target for automation, eh?

Here's a Keyboard Maestro macro that will take the selected .pickle file in Finder and open it up in IPython (inside iTerm2), assigning it to the variable x.

Here's what it looks like (the macro is triggered through LaunchBar with this custom action):

After the macro runs, I type in x to show the object in the Pickle file (in this case, it's just {"hello": "world"}.

Here's what the macro looks like in Keyboard Maestro:

Here's the code:

tell application "Keyboard Maestro Engine"
--- GET VARIABLE ---
-- IF KM Variable does NOT exist, the AS Variable will be set to empty string --
set the_path to getvariable "Path"
end tell
tell application "iTerm"
activate
create window with default profile
try
set _session to current session of current window
on error
set _term to (make new terminal)
tell _term
launch session "Default"
set _session to current session
end tell
end try
tell _session
write text "pyenv data && ipython -i --c=\"import pickle; x = pickle.load(open('"& the_path & "', 'rb'))\""
end tell
end tell

Note that this requires your Python virtualenv to be set up like this and you have to have a "data" virtualenv. If either of these things aren't true, you'll need to modify the shell commands in the AppleScript accordingly.

SciPy: Using GridSpec to Display Box Plots Under Histograms

Here's how to get output like this using matplotlib and its GridSpec class:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gs

%matplotlib inline

df = pd.DataFrame([
        np.random.randn(1000),
        np.random.random_integers(1, 2, 1000)
    ]).T
df.columns = ["value", "group"]


fig = plt.figure(figsize=(10, 6))
grid = gs.GridSpec(2, 2, height_ratios=[3, 1]) 
for i, g in enumerate([1, 2]):
    
    subset = df['value'][df['group'] == g]
    
    # Histogram
    ax = plt.subplot(grid[i])
    ax.hist(list(subset), color='k', alpha=0.4)
    ax.set_title("Group %s" % g)
    
    # Box plot
    ax2 = plt.subplot(grid[i+2])
    pd.DataFrame(subset).boxplot(vert=False, return_type='axes')
    ax2.set_yticklabels([''])

fig.tight_layout()
fig.subplots_adjust(top=0.85)
fig.suptitle('Example', fontsize=20)

None # Don't display the last thing -- `%matplotlib inline` will display the graphs no matter what.


It looks like Gist embed is broken here on PostHaven for IPython notebooks, so the embed below may look strange:

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
view raw gridspec.ipynb hosted with ❤ by GitHub

Installing Python 3 Alongside Python 2 on OS X (Yosemite)

This was actually pretty easy. Here’s what I did:

  1. Opened a new Terminal window that wasn’t in a virtualenv.
  2. Ran brew update
  3. Ran brew install python3
  4. cd ~/.virtualenvs (the folder where my virtualenvs are)
  5. virtualenv -p `which python3` data3 to create a new virtualenv using Python 3
  6. workon data3 (this is from virtualenvwrapper)
  7. pip install "ipython[all]"

Update: I've been trying out pyenv, which is forked from my much-beloved rbenv. It seems to work fine with virtualenv via pyenv-virtualenv, but I could not get pyenv-virtualenvwrapper to work. I'm using this now to run an updated version of Python 2 without messing with my system Python, and it seems to work fine.

Hide Code When Sharing IPython Notebooks

When sharing IPython notebooks with non-programmers, I don't want to cause confusion by including a bunch of code.

To this end, I wrote a snippet of Python and JavaScript that can be put in a code cell at the top of a notebook. When a notebook with this code is exported as HTML or viewed with nbviewer.ipython.org, code blocks will be hidden by default. A "Toggle code" button is also added in case you want to see the hidden code.

Here is the code snippet to put in the cell at the top of the notebook:

import IPython.core.display as di
# This line will hide code by default when the notebook is exported as HTML
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)
# This line will add a button to toggle visibility of code blocks, for use with the HTML export version
di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True)
view raw gistfile1.py hosted with ❤ by GitHub

And here is an example of how this works in nbviewer.ipython.org.

(The source code for this example and the accompanying HTML output are available here.)