Created
February 20, 2015 19:10
-
-
Save fhs/f5bbd57bfea000ffa555 to your computer and use it in GitHub Desktop.
some ipython magic commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
def loadnpz_impl(self, params=''): | |
"""Load a npz file into user namespace. | |
%loadnpz <filename.npz> | |
""" | |
import numpy as np | |
ip = get_ipython() | |
args = params.split() | |
if len(args) == 0: | |
return | |
filename, = args | |
ip.push(dict(np.load(filename)), interactive=True) | |
def loadmat_impl(self, params=''): | |
"""Load a MATLAB file into user namespace. | |
%loadmat [-s] <filename.mat> | |
The -s option squeezes unit matrix dimensions. | |
""" | |
from scipy.io import loadmat | |
ip = get_ipython() | |
args = params.split() | |
if len(args) == 0: | |
return | |
if args[0] == '-s': | |
squeeze_me = True | |
_, filename = args | |
else: | |
squeeze_me = False | |
filename, = args | |
ip.push(loadmat(filename, squeeze_me=squeeze_me), interactive=True) | |
def savemat_impl(self, params=''): | |
"""Save interactive variables to a MATLAB file. | |
%savemat <filename.mat> | |
- Saves all interactive variables. | |
%savemat <filename.mat> <var1 var2 ...> | |
- Saves selected variables var1, var2, etc. only. | |
""" | |
from scipy.io import savemat | |
ip = get_ipython() | |
args = params.split() | |
if len(args) == 0: | |
return | |
filename, vars = args[0], args[1:] | |
if len(vars) == 0: | |
sel = dict((k, ip.user_ns[k]) for k in ip.magic('%who_ls')) | |
print("Saving variables:", list(sel.keys())) | |
savemat(filename, sel) | |
else: | |
sel = dict((k, ip.user_ns[k]) for k in vars) | |
print("Saving variables:", list(sel.keys())) | |
savemat(filename, sel) | |
def imshow_impl(self, params=''): | |
"""Shows given image in matplotlib. | |
%imshow [options] name ... | |
-t <title> Set title of all figures. The default title | |
is the name of the image. | |
-c <cmap> Set colormap of all figures (e.g. gray). | |
""" | |
ip = get_ipython() | |
opts, args = ip.parse_options(params, 'c:t:') | |
names = args.split() | |
cmap = cm.jet | |
if 'c' in opts: | |
cmap = cm.get_cmap(name=opts.c) | |
if cmap is None: | |
print("invalid colormap:", opts.c) | |
return | |
titles = names | |
if 't' in opts: | |
titles = len(names)*[opts.t] | |
for name, tl in zip(names, titles): | |
img = np.squeeze(ip.user_ns[name]) | |
figure() | |
imshow(img, interpolation='nearest', cmap=cmap) | |
colorbar() | |
title(tl) | |
if __name__ == '__main__': | |
ip = get_ipython() | |
ip.define_magic('loadmat', loadmat_impl) | |
ip.define_magic('loadnpz', loadnpz_impl) | |
ip.define_magic('savemat', savemat_impl) | |
ip.define_magic('imshow', imshow_impl) | |
# clean up ipython's namespace | |
del ip, savemat_impl, loadmat_impl, imshow_impl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment