Show More
@@ -4,7 +4,7 b'' | |||||
4 | octavemagic |
|
4 | octavemagic | |
5 | =========== |
|
5 | =========== | |
6 |
|
6 | |||
7 |
Magic |
|
7 | Magics for interacting with Octave via oct2py. | |
8 |
|
8 | |||
9 | Usage |
|
9 | Usage | |
10 | ===== |
|
10 | ===== | |
@@ -30,17 +30,15 b' Usage' | |||||
30 | # the file COPYING, distributed as part of this software. |
|
30 | # the file COPYING, distributed as part of this software. | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | import sys |
|
|||
34 | import tempfile |
|
33 | import tempfile | |
35 | from glob import glob |
|
34 | from glob import glob | |
36 | from shutil import rmtree |
|
35 | from shutil import rmtree | |
37 | from getopt import getopt |
|
|||
38 |
|
36 | |||
39 | import numpy as np |
|
37 | import numpy as np | |
40 | import oct2py |
|
38 | import oct2py | |
41 |
|
39 | |||
42 | from IPython.core.displaypub import publish_display_data |
|
40 | from IPython.core.displaypub import publish_display_data | |
43 |
from IPython.core.magic import (Magics, magics_class, |
|
41 | from IPython.core.magic import (Magics, magics_class, line_magic, | |
44 | line_cell_magic) |
|
42 | line_cell_magic) | |
45 | from IPython.testing.skipdoctest import skip_doctest |
|
43 | from IPython.testing.skipdoctest import skip_doctest | |
46 | from IPython.core.magic_arguments import ( |
|
44 | from IPython.core.magic_arguments import ( | |
@@ -117,6 +115,7 b' class OctaveMagics(Magics):' | |||||
117 | ''' |
|
115 | ''' | |
118 | outputs = line.split(' ') |
|
116 | outputs = line.split(' ') | |
119 | for output in outputs: |
|
117 | for output in outputs: | |
|
118 | output = unicode_to_str(output) | |||
120 | self.shell.push({output: self.oct.get(output)}) |
|
119 | self.shell.push({output: self.oct.get(output)}) | |
121 |
|
120 | |||
122 |
|
121 | |||
@@ -131,6 +130,10 b' class OctaveMagics(Magics):' | |||||
131 | help='Names of variables to be pulled from Octave after executing cell body. Multiple names can be passed, separated by commas with no whitespace.' |
|
130 | help='Names of variables to be pulled from Octave after executing cell body. Multiple names can be passed, separated by commas with no whitespace.' | |
132 | ) |
|
131 | ) | |
133 | @argument( |
|
132 | @argument( | |
|
133 | '-s', '--size', action='append', | |||
|
134 | help='Pixel size of plots. Default is "-s 400,250".' | |||
|
135 | ) | |||
|
136 | @argument( | |||
134 | 'code', |
|
137 | 'code', | |
135 | nargs='*', |
|
138 | nargs='*', | |
136 | ) |
|
139 | ) | |
@@ -190,10 +193,15 b' class OctaveMagics(Magics):' | |||||
190 |
|
193 | |||
191 | if args.input: |
|
194 | if args.input: | |
192 | for input in ','.join(args.input).split(','): |
|
195 | for input in ','.join(args.input).split(','): | |
|
196 | input = unicode_to_str(input) | |||
193 | self.oct.put(input, self.shell.user_ns[input]) |
|
197 | self.oct.put(input, self.shell.user_ns[input]) | |
194 |
|
198 | |||
195 | # generate plots in a temporary directory |
|
199 | # generate plots in a temporary directory | |
196 | plot_dir = tempfile.mkdtemp() |
|
200 | plot_dir = tempfile.mkdtemp() | |
|
201 | if args.size is not None: | |||
|
202 | size = args.size[0] | |||
|
203 | else: | |||
|
204 | size = '400,240' | |||
197 |
|
205 | |||
198 | pre_call = ''' |
|
206 | pre_call = ''' | |
199 | global __ipy_figures = []; |
|
207 | global __ipy_figures = []; | |
@@ -202,6 +210,7 b' class OctaveMagics(Magics):' | |||||
202 | function fig_create(src, event) |
|
210 | function fig_create(src, event) | |
203 | global __ipy_figures; |
|
211 | global __ipy_figures; | |
204 | __ipy_figures(size(__ipy_figures) + 1) = src; |
|
212 | __ipy_figures(size(__ipy_figures) + 1) = src; | |
|
213 | set(src, "visible", "off"); | |||
205 | end |
|
214 | end | |
206 |
|
215 | |||
207 | set(0, 'DefaultFigureCreateFcn', @fig_create); |
|
216 | set(0, 'DefaultFigureCreateFcn', @fig_create); | |
@@ -219,11 +228,13 b' class OctaveMagics(Magics):' | |||||
219 | end |
|
228 | end | |
220 |
|
229 | |||
221 | for f = __ipy_figures |
|
230 | for f = __ipy_figures | |
222 | outfile = sprintf('%s/__ipy_oct_fig_%%03d.png', f) |
|
231 | outfile = sprintf('%(plot_dir)s/__ipy_oct_fig_%%03d.png', f); | |
223 | print(f, outfile, '-dpng') |
|
232 | print(f, outfile, '-dpng', '-S%(size)s'); | |
224 | end |
|
233 | end | |
225 |
|
234 | |||
226 | ''' % plot_dir |
|
235 | # close all; | |
|
236 | ||||
|
237 | ''' % {'plot_dir': plot_dir, 'size': size} | |||
227 |
|
238 | |||
228 | code = ' '.join((pre_call, code, post_call)) |
|
239 | code = ' '.join((pre_call, code, post_call)) | |
229 | try: |
|
240 | try: | |
@@ -266,7 +277,7 b' class OctaveMagics(Magics):' | |||||
266 |
|
277 | |||
267 | # Unfortunately, Octave doesn't have a "None" object, |
|
278 | # Unfortunately, Octave doesn't have a "None" object, | |
268 | # so we can't return any NaN outputs |
|
279 | # so we can't return any NaN outputs | |
269 |
if np.isnan( |
|
280 | if np.isscalar(ans) and np.isnan(ans): | |
270 | ans = None |
|
281 | ans = None | |
271 |
|
282 | |||
272 | return ans |
|
283 | return ans |
General Comments 0
You need to be logged in to leave comments.
Login now