def test_style_opts_intermediate_explicit(self): line = "Layout style(string='foo' test=3, b=True )" expected= {'Layout':{ 'style': Options(string='foo', test=3, b=True)}} self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_1(self): line = "Layout plot[fig_inches=(3,3) foo='bar baz'] Layout (string='foo')" expected= {'Layout': {'plot': Options(foo='bar baz', fig_inches=(3, 3)), 'style': Options(string='foo')}} self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_with_space(self): "Space in the tuple, see issue #77" line = "Layout [fig_inches=(3, 3) title_format='foo bar']" expected= {'Layout': {'plot': Options(title_format='foo bar', fig_inches=(3, 3))}} self.assertEqual(OptsSpec.parse(line), expected)
def test_norm_opts_simple_explicit_1(self): line = "Layout norm{+axiswise}" expected = { 'Layout': { 'norm': Options(axiswise=True, framewise=False) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_with_space_explicit(self): line = "Layout plot[fig_inches=(3, 3) title='foo bar']" expected = { 'Layout': { 'plot': Options(title='foo bar', fig_inches=(3, 3)) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_advanced(self): line = "Layout (string='foo' test=3, b=True color=Cycle(values=[1,2]))" expected= {'Layout':{ 'style': Options(string='foo', test=3, b=True, color=Cycle(values=[1,2]))}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_cycle_list(self): line = "Curve (color=Cycle(values=['r', 'g', 'b']))" expected = { 'Curve': { 'style': Options(color=Cycle(values=['r', 'g', 'b'])) } } self.assertEqual(OptsSpec.parse(line, {'Cycle': Cycle}), expected)
def test_plot_opts_simple(self): line = "Layout [fig_inches=(3,3) title_format='foo bar']" expected = { 'Layout': { 'plot': Options(title_format='foo bar', fig_inches=(3, 3)) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_multiple_paths(self): line = "Image Curve [fig_inches=(3, 3) title_format='foo bar']" expected = {'Image': {'plot': Options(title_format='foo bar', fig_inches=(3, 3))}, 'Curve': {'plot': Options(title_format='foo bar', fig_inches=(3, 3))}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_multiple_paths(self): line = "Image Curve (color='beautiful')" expected = {'Image': {'style': Options(color='beautiful')}, 'Curve': {'style': Options(color='beautiful')}} self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_1(self): line = "Layout plot[fig_inches=(3,3) foo='bar baz'] Layout (string='foo')" expected = { 'Layout': { 'plot': Options(foo='bar baz', fig_inches=(3, 3)), 'style': Options(string='foo') } } self.assertEqual(OptsSpec.parse(line), expected)
def test_norm_opts_multiple_paths(self): line = "Image Curve {+axiswise +framewise}" expected = {'Image': {'norm': Options(axiswise=True, framewise=True)}, 'Curve': {'norm': Options(axiswise=True, framewise=True)}} self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_two_types_2(self): line = "Layout plot[fig_inches=(3, 3)] Image (string='foo') [foo='bar baz']" expected= {'Layout': {'plot': Options(fig_inches=(3, 3))}, 'Image': { 'style': Options(string='foo'), 'plot': Options(foo='bar baz')}} self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_with_space(self): "Space in the tuple, see issue #77" line = "Layout [fig_inches=(3, 3) title='foo bar']" expected = { 'Layout': { 'plot': Options(title='foo bar', fig_inches=(3, 3)) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_multiple_paths(self): line = "Image Curve [fig_inches=(3, 3) title='foo bar']" expected = { 'Image': { 'plot': Options(title='foo bar', fig_inches=(3, 3)) }, 'Curve': { 'plot': Options(title='foo bar', fig_inches=(3, 3)) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_norm_opts_multiple_paths(self): line = "Image Curve {+axiswise +framewise}" expected = { 'Image': { 'norm': Options(axiswise=True, framewise=True) }, 'Curve': { 'norm': Options(axiswise=True, framewise=True) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_multiple_paths(self): line = "Image Curve (color='beautiful')" expected = { 'Image': { 'style': Options(color='beautiful') }, 'Curve': { 'style': Options(color='beautiful') } } self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_dict_without_space(self): line = "Curve (fontsize={'xlabel': 10,'title': 20})" expected = { 'Curve': { 'style': Options(fontsize={ 'xlabel': 10, 'title': 20 }) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_dict_without_space(self): line = "Curve [fontsize=dict(xlabel=10,title=20)]" expected = { 'Curve': { 'plot': Options(fontsize={ 'xlabel': 10, 'title': 20 }) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_two_types_2(self): line = "Layout plot[fig_inches=(3, 3)] Image (string='foo') [foo='bar baz']" expected = { 'Layout': { 'plot': Options(fig_inches=(3, 3)) }, 'Image': { 'style': Options(string='foo'), 'plot': Options(foo='bar baz') } } self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_advanced(self): line = "Layout (string='foo' test=3, b=True color=Cycle(values=[1,2]))" expected = { 'Layout': { 'style': Options(string='foo', test=3, b=True, color=Cycle(values=[1, 2])) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_multiple_paths_merge(self): line = "Image Curve [fig_inches=(3, 3)] (c='b') Image (s=3)" expected = { 'Image': { 'plot': Options(fig_inches=(3, 3)), 'style': Options(c='b', s=3) }, 'Curve': { 'plot': Options(fig_inches=(3, 3)), 'style': Options(c='b') } } self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_multiple_paths_merge(self): line = "Image Curve [fig_inches=(3, 3)] (c='b') Image (s=3)" expected = {'Image': {'plot': Options(fig_inches=(3, 3)), 'style': Options(c='b', s=3)}, 'Curve': {'plot': Options(fig_inches=(3, 3)), 'style': Options(c='b')}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_cycle_function(self): # Explicitly compare because list of arrays do not compare correctly import numpy as np np.random.seed(42) line = "Curve (color=Cycle(values=list(np.random.rand(3,3))))" options = OptsSpec.parse(line, {'np': np, 'Cycle': Cycle}) self.assertTrue('Curve' in options) self.assertTrue('style' in options['Curve']) self.assertTrue('color' in options['Curve']['style'].kwargs) self.assertTrue(isinstance(options['Curve']['style'].kwargs['color'], Cycle)) values = np.array([[ 0.37454012, 0.95071431, 0.73199394], [ 0.59865848, 0.15601864, 0.15599452], [ 0.05808361, 0.86617615, 0.60111501]]) self.assertEqual(np.array(options['Curve']['style'].kwargs['color'].values), values)
def test_plot_opts_multiple_paths_2(self): line = "Image Curve Layout Overlay[fig_inches=(3, 3) title='foo bar']" expected = {'Image': {'plot': Options(title='foo bar', fig_inches=(3, 3))}, 'Curve': {'plot': Options(title='foo bar', fig_inches=(3, 3))}, 'Layout': {'plot': Options(title='foo bar', fig_inches=(3, 3))}, 'Overlay': {'plot': Options(title='foo bar', fig_inches=(3, 3))}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_cycle_function(self): # Explicitly compare because list of arrays do not compare correctly import numpy as np np.random.seed(42) line = "Curve (color=Cycle(values=list(np.random.rand(3,3))))" options = OptsSpec.parse(line, {'np': np, 'Cycle': Cycle}) self.assertTrue('Curve' in options) self.assertTrue('style' in options['Curve']) self.assertTrue('color' in options['Curve']['style'].kwargs) self.assertTrue( isinstance(options['Curve']['style'].kwargs['color'], Cycle)) values = np.array([[0.37454012, 0.95071431, 0.73199394], [0.59865848, 0.15601864, 0.15599452], [0.05808361, 0.86617615, 0.60111501]]) self.assertEqual( np.array(options['Curve']['style'].kwargs['color'].values), values)
def test_plot_opts_multiple_paths_2(self): line = "Image Curve Layout Overlay[fig_inches=(3, 3) title_format='foo bar']" expected = { 'Image': { 'plot': Options(title_format='foo bar', fig_inches=(3, 3)) }, 'Curve': { 'plot': Options(title_format='foo bar', fig_inches=(3, 3)) }, 'Layout': { 'plot': Options(title_format='foo bar', fig_inches=(3, 3)) }, 'Overlay': { 'plot': Options(title_format='foo bar', fig_inches=(3, 3)) } } self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_multiple_paths(self): line = "Image Curve {+framewise} [fig_inches=(3, 3) title='foo bar'] (c='b') Layout [string='foo'] Overlay" expected = { 'Image': { 'norm': Options(framewise=True, axiswise=False), 'plot': Options(title='foo bar', fig_inches=(3, 3)), 'style': Options(c='b') }, 'Curve': { 'norm': Options(framewise=True, axiswise=False), 'plot': Options(title='foo bar', fig_inches=(3, 3)), 'style': Options(c='b') }, 'Layout': { 'plot': Options(string='foo') }, 'Overlay': {} } self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_multiple_paths(self): line = "Image Curve {+framewise} [fig_inches=(3, 3) title_format='foo bar'] (c='b') Layout [string='foo'] Overlay" expected = {'Image': {'norm': Options(framewise=True, axiswise=False), 'plot': Options(title_format='foo bar', fig_inches=(3, 3)), 'style': Options(c='b')}, 'Curve': {'norm': Options(framewise=True, axiswise=False), 'plot': Options(title_format='foo bar', fig_inches=(3, 3)), 'style': Options(c='b')}, 'Layout': {'plot': Options(string='foo')}, 'Overlay': {}} self.assertEqual(OptsSpec.parse(line), expected)
def opts(self, line='', cell=None): """ The opts line/cell magic with tab-completion. %%opts [ [path] [normalization] [plotting options] [style options]]+ path: A dotted type.group.label specification (e.g. Image.Grayscale.Photo) normalization: List of normalization options delimited by braces. One of | -axiswise | -framewise | +axiswise | +framewise | E.g. { +axiswise +framewise } plotting options: List of plotting option keywords delimited by square brackets. E.g. [show_title=False] style options: List of style option keywords delimited by parentheses. E.g. (lw=10 marker='+') Note that commas between keywords are optional (not recommended) and that keywords must end in '=' without a separating space. More information may be found in the class docstring of util.parser.OptsSpec. """ line, cell = self._partition_lines(line, cell) try: spec = OptsSpec.parse(line, ns=self.shell.user_ns) except SyntaxError: display(HTML("<b>Invalid syntax</b>: Consult <tt>%%opts?</tt> for more information.")) return # Make sure the specified elements exist in the loaded backends available_elements = set() for backend in Store.loaded_backends(): available_elements |= set(Store.options(backend).children) spec_elements = set(k.split('.')[0] for k in spec.keys()) unknown_elements = spec_elements - available_elements if unknown_elements: msg = ("<b>WARNING:</b> Unknown elements {unknown} not registered " "with any of the loaded backends.") display(HTML(msg.format(unknown=', '.join(unknown_elements)))) if cell: self.register_custom_spec(spec) # Process_element is invoked when the cell is run. self.shell.run_cell(cell, store_history=STORE_HISTORY) else: errmsg = StoreOptions.validation_error_message(spec) if errmsg: OptsMagic.error_message = None sys.stderr.write(errmsg) if self.strict: display(HTML('Options specification will not be applied.')) return with options_policy(skip_invalid=True, warn_on_skip=False): StoreOptions.apply_customizations(spec, Store.options()) OptsMagic.error_message = None
def test_plot_opts_nested_brackets(self): line = "Curve [title_format=', '.join(('A', 'B'))]" expected = {'Curve': {'plot': Options(title_format='A, B')}} self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_dict_without_space(self): line = "Curve [fontsize=dict(xlabel=10,title=20)]" expected = {'Curve': {'plot': Options(fontsize={'xlabel': 10, 'title': 20})}} self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_with_space_explicit(self): line = "Layout plot[fig_inches=(3, 3) title_format='foo bar']" expected= {'Layout': {'plot': Options(title_format='foo bar', fig_inches=(3, 3))}} self.assertEqual(OptsSpec.parse(line), expected)
def test_plot_opts_simple(self): line = "Layout [fig_inches=(3,3) title='foo bar']" expected= {'Layout': {'plot': Options(title='foo bar', fig_inches=(3, 3))}} self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_multiple_paths_merge_precedence(self): line = "Image (s=0, c='b') Image (s=3)" expected = {'Image': {'style': Options(c='b', s=3)}} self.assertEqual(OptsSpec.parse(line), expected)
def test_combined_multiple_paths_merge_precedence(self): line = "Image (s=0, c='b') Image (s=3)" expected = {'Image': {'style': Options(c='b', s=3)}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_intermediate_explicit(self): line = "Layout style(string='foo' test=3, b=True )" expected = {'Layout': {'style': Options(string='foo', test=3, b=True)}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_simple(self): line = "Layout (string='foo')" expected= {'Layout':{ 'style': Options(string='foo')}} self.assertEqual(OptsSpec.parse(line), expected)
def test_norm_opts_simple_explicit_2(self): line = "Layout norm{+axiswise +framewise}" expected= {'Layout': {'norm': Options(axiswise=True, framewise=True)}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_cycle_list(self): line = "Curve (color=Cycle(values=['r', 'g', 'b']))" expected = {'Curve': {'style': Options(color=Cycle(values=['r', 'g', 'b']))}} self.assertEqual(OptsSpec.parse(line, {'Cycle': Cycle}), expected)
def test_style_opts_simple_explicit(self): line = "Layout style(string='foo')" expected = {'Layout': {'style': Options(string='foo')}} self.assertEqual(OptsSpec.parse(line), expected)
def test_style_opts_dict_without_space(self): line = "Curve (fontsize={'xlabel': 10,'title': 20})" expected = {'Curve': {'style': Options(fontsize={'xlabel': 10, 'title': 20})}} self.assertEqual(OptsSpec.parse(line), expected)
def opts(self, line='', cell=None): """ The opts line/cell magic with tab-completion. %%opts [ [path] [normalization] [plotting options] [style options]]+ path: A dotted type.group.label specification (e.g. Image.Grayscale.Photo) normalization: List of normalization options delimited by braces. One of | -axiswise | -framewise | +axiswise | +framewise | E.g. { +axiswise +framewise } plotting options: List of plotting option keywords delimited by square brackets. E.g. [show_title=False] style options: List of style option keywords delimited by parentheses. E.g. (lw=10 marker='+') Note that commas between keywords are optional (not recommended) and that keywords must end in '=' without a separating space. More information may be found in the class docstring of util.parser.OptsSpec. """ line, cell = self._partition_lines(line, cell) try: spec = OptsSpec.parse(line, ns=self.shell.user_ns) except SyntaxError: display(HTML("<b>Invalid syntax</b>: Consult <tt>%%opts?</tt> for more information.")) return # Make sure the specified elements exist in the loaded backends available_elements = set() for backend in Store.loaded_backends(): available_elements |= set(Store.options(backend).children) spec_elements = set(k.split('.')[0] for k in spec.keys()) unknown_elements = spec_elements - available_elements if unknown_elements: msg = ("<b>WARNING:</b> Unknown elements {unknown} not registered " "with any of the loaded backends.") display(HTML(msg.format(unknown=', '.join(unknown_elements)))) if cell: self.register_custom_spec(spec) # Process_element is invoked when the cell is run. self.shell.run_cell(cell, store_history=STORE_HISTORY) else: errmsg = StoreOptions.validation_error_message(spec) if errmsg: OptsMagic.error_message = None sys.stderr.write(errmsg) if self.strict: display(HTML('Options specification will not be applied.')) return with options_policy(skip_invalid=True, warn_on_skip=False): StoreOptions.apply_customizations(spec, Store.options()) OptsMagic.error_message = None
def test_plot_opts_nested_brackets(self): line = "Curve [title=', '.join(('A', 'B'))]" expected = {'Curve': {'plot': Options(title='A, B')}} self.assertEqual(OptsSpec.parse(line), expected)