Skip to content
This repository has been archived by the owner on Apr 4, 2020. It is now read-only.

Commit

Permalink
Improve methods for plot formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Aug 24, 2014
1 parent 388d026 commit aab6097
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Release History
- Add option to show plotting gui with `-g` flag in ScilabMagic.
- Add ability to specify the Scilab executable as a keyword argument to
the Scilab2Py object.
- Add specifications for plot saving instead of displaying plots to `eval` and
- Add specifications for plot saving instead of displaying plots to `eval` and
dynamic functions.


Expand Down
2 changes: 1 addition & 1 deletion scilab2py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


__title__ = 'scilab2py'
__version__ = '0.2'
__version__ = '0.3'
__author__ = 'Steven Silvester'
__license__ = 'MIT'
__copyright__ = 'Copyright 2014 Steven Silvester'
Expand Down
54 changes: 32 additions & 22 deletions scilab2py/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ def pull(self, var, verbose=False, timeout=None):
return data

def eval(self, cmds, verbose=False, timeout=None, log=True,
plot_dir=None, plot_name='plot', plot_format='png'):
plot_dir=None, plot_name='plot', plot_format='png',
plot_width=620, plot_height=590):
"""
Perform Scilab command or commands.
Expand All @@ -196,6 +197,10 @@ def eval(self, cmds, verbose=False, timeout=None, log=True,
xxx is the `plot_format`.
plot_format: str, optional
The format in which to save the plot (PNG by default).
plot_width: int, optional
The plot with in pixels.
plot_height: int, optional
The plot height in pixels.
Returns
-------
Expand Down Expand Up @@ -223,33 +228,40 @@ def eval(self, cmds, verbose=False, timeout=None, log=True,
if timeout is None:
timeout = self.timeout

post_call = ''
force_ans = ''
for cmd in cmds:

match = re.match('([a-z][a-zA-Z0-9_]*) *=', cmd)
if match and not cmd.strip().endswith(';'):
post_call = 'ans = %s' % match.groups()[0]
force_ans = 'ans = %s' % match.groups()[0]
break

match = re.match('([a-z][a-zA-Z0-9_]*)\Z', cmd.strip())
if match and not cmd.strip().endswith(';'):
post_call = 'ans = %s' % match.groups()[0]
force_ans = 'ans = %s' % match.groups()[0]
break

cmds.append(post_call)
cmds.append(force_ans)

pre_call = ''
post_call = ''

pre_call += '''
h = gdf();
h.figure_position = [0, 0];
h.figure_size = [%(plot_width)s,%(plot_height)s];
h.axes_size = [%(plot_width)s * 0.98, %(plot_height)s * 0.8];
''' % locals()

if not plot_dir is None:
plot_dir = plot_dir.replace("\\", "/")

spec = '%(plot_dir)s/%(plot_name)s*.%(plot_format)s' % locals()
existing = glob.glob(spec)
plot_offset = len(existing)

plot_call = '''
h = gdf();
h.figure_position = [0, 0];
h.toolbar_visible = 'off';
h.menubar_visible = 'off';
h.infobar_visible = 'off';
pre_call += '''
close all;
function handle_all_fig()
ids_array=winsid();
for i=1:length(ids_array)
Expand All @@ -269,11 +281,11 @@ def eval(self, cmds, verbose=False, timeout=None, log=True,
endfunction
''' % locals()

cmds.insert(0, plot_call)
post_call += 'handle_all_fig();'

try:
resp = self._session.evaluate(cmds, verbose, log, self.logger,
timeout=timeout)
timeout=timeout, pre_call=pre_call, post_call=post_call)
except KeyboardInterrupt:
if os.name == 'nt':
self.restart()
Expand All @@ -293,9 +305,6 @@ def eval(self, cmds, verbose=False, timeout=None, log=True,
elif log:
self.logger.debug(resp)

if not plot_dir is None:
self._session.evaluate(['handle_all_fig();'], verbose, log, self.logger, timeout=timeout)

if not resp in ['', []]:
return resp

Expand Down Expand Up @@ -373,9 +382,7 @@ def _call(self, func, *inputs, **kwargs):
Scilab2PyError
If the function call is unsucessful.
"""
verbose = kwargs.pop('verbose', False)
nout = kwargs.pop('nout', get_nout())
timeout = kwargs.pop('timeout', self.timeout)
argout_list = ['ans']
if '=' in func:
nout = 0
Expand Down Expand Up @@ -623,7 +630,8 @@ def set_timeout(self, timeout=None):
timeout = int(1e6)
self.timeout = timeout

def evaluate(self, cmds, verbose=True, log=True, logger=None, timeout=None):
def evaluate(self, cmds, verbose=True, log=True, logger=None, timeout=None,
pre_call='', post_call=''):
"""Perform the low-level interaction with an Scilab Session
"""

Expand Down Expand Up @@ -662,6 +670,7 @@ def evaluate(self, cmds, verbose=True, log=True, logger=None, timeout=None):
self.logger.debug(expr)

output = """
%s;
clear("ans");
disp(char(2));
if execstr("%s", "errcatch") <> 0 then
Expand All @@ -687,8 +696,9 @@ def evaluate(self, cmds, verbose=True, log=True, logger=None, timeout=None):
end
clear("last_ans")
end
disp(char(3))
end""" % (expr, self.outfile)
end
%s
disp(char(3))""" % (pre_call, expr, self.outfile, post_call)

if len(cmds) == 5:
main_line = cmds[2].strip()
Expand Down
13 changes: 12 additions & 1 deletion scilab2py/ipython/scilabmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def scilab_pull(self, line):
'-f', '--format', action='store',
help='Plot format (png, svg or jpg).'
)
@argument(
'-s', '--size', action='store',
help='Pixel size of plots, "width,height". Default is "-s 400,250".'
)
@argument(
'-g', '--gui', action='store_true', default=False,
help='Show a gui for plots. Default is False'
Expand Down Expand Up @@ -261,9 +265,16 @@ def scilab(self, line, cell=None, local_ns=None):
if args.gui:
plot_dir = None

if args.size is not None:
size = args.size
else:
size = '400,240'
plot_width, plot_height = [int(s) for s in size.split(',')]

try:
text_output = str(self._sci.eval(code, plot_dir=plot_dir, plot_format=plot_format,
verbose=False))
plot_width=plot_width, plot_height=plot_height,
verbose=False))
except (scilab2py.Scilab2PyError) as exception:
msg = str(exception)
if 'Scilab Syntax Error' in msg:
Expand Down
2 changes: 1 addition & 1 deletion scilab2py/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_using_closed_session(self):
sci.exit()
test.assert_raises(Scilab2PyError, sci.eval, 'ones')

def test_pause(self):
def test_xpause(self):
self.assertRaises(Scilab2PyError,
lambda: self.sci.xpause(10e6, timeout=3))

Expand Down

0 comments on commit aab6097

Please sign in to comment.