RendererOptionsWidget

class menpowidgets.options.RendererOptionsWidget(options_tabs, labels, axes_x_limits=None, axes_y_limits=None, render_function=None, style='minimal', tabs_style='minimal')[source]

Bases: MenpoWidget

Creates a widget for selecting rendering options. The widget consists of the following objects from ipywidgets and menpowidgets.tools:

No Object Property (self.) Description
1

LineOptionsWidget

MarkerOptionsWidget

ImageOptionsWidget

NumberingOptionsWidget

ZoomOneScaleWidget

ZoomTwoScalesWidget

AxesOptionsWidget

LegendOptionsWidget

GridOptionsWidget

options_widgets

list that

contains the

rendering

sub-options

widgets

2 Tab suboptions_tab Contains all 2

Note that:

  • To update the state of the widget, please refer to the set_widget_state() method.

  • The widget has memory about the properties of the objects that are passed into it through set_widget_state(). Each object has a unique key id assigned through get_key(). Then, the options that correspond to each key are stored in the self.default_options dict.

  • The selected values of the current object object are stored in the self.selected_values trait.

  • When an unseen image object is passed in (i.e. a key that is not included in the self.default_options dict), it gets the following initial options by default:

    • lines
      • render_lines = True
      • line_width = 1
      • line_style = '-
      • line_colour = ['red'] if labels is None else colours
    • markers
      • render_markers = True
      • marker_size = 5
      • marker_style = 'o'
      • marker_face_colour = ['red'] if labels is None else colours
      • marker_edge_colour = ['black'] if labels is None else colours
      • marker_edge_width = 1

    where colours = sample_colours_from_colourmap(len(labels), 'jet')

    • image
      • interpolation = 'bilinear'
      • cmap_name = None
      • alpha = 1.
    • numbering
      • render_numbering = False
      • numbers_font_name = 'sans-serif'
      • numbers_font_size = 10
      • numbers_font_style = 'normal'
      • numbers_font_weight = 'normal'
      • numbers_font_colour = ['black']
      • numbers_horizontal_align = 'center'
      • numbers_vertical_align = 'bottom'
    • zoom_one = 1.
    • zoom_two = [1., 1.]
    • axes
      • render_axes = False
      • axes_font_name = 'sans-serif'
      • axes_font_size = 10
      • axes_font_style = 'normal'
      • axes_font_weight = 'normal'
      • axes_x_ticks = None
      • axes_y_ticks = None
      • axes_x_limits = axes_x_limits
      • axes_y_limits = axes_y_limits
    • legend
      • render_legend = False
      • legend_title = ''
      • legend_font_name = 'sans-serif'
      • legend_font_style = 'normal'
      • legend_font_size = 10
      • legend_font_weight = 'normal'
      • legend_marker_scale = 1.
      • legend_location = 2
      • legend_bbox_to_anchor = (1.05, 1.)
      • legend_border_axes_pad = 1.
      • legend_n_columns = 1
      • legend_horizontal_spacing = 1.
      • legend_vertical_spacing = 1.
      • legend_border = True
      • legend_border_padding = 0.5
      • legend_shadow = False
      • legend_rounded_corners = False
    • grid
      • render_grid = False
      • grid_line_width = 0.5
      • grid_line_style = '--'
  • To set the styling of this widget please refer to the style() and predefined_style() methods.

  • To update the handler callback function of the widget, please refer to the replace_render_function() method.

Parameters:
  • options_tabs (list of str) –

    List that defines the ordering of the options tabs. Possible values are:

    Value Returned object
    'lines' LineOptionsWidget
    'markers' MarkerOptionsWidget
    'numbering' NumberingOptionsWidget
    'zoom_one' ZoomOneScaleWidget
    'zoom_two' ZoomTwoScalesWidget
    'legend' LegendOptionsWidget
    'grid' GridOptionsWidget
    'image' ImageOptionsWidget
    'axes' AxesOptionsWidget
  • labels (list or None, optional) – The list of labels used in all ColourSelectionWidget objects.
  • axes_x_limits (float or (float, float) or None, optional) – The limits of the x axis. If float, then it sets padding on the right and left as a percentage of the rendered object’s width. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically.
  • axes_y_limits ((float, float) tuple or None, optional) – The limits of the y axis. If float, then it sets padding on the top and bottom as a percentage of the rendered object’s height. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically.
  • render_function (callable or None, optional) –

    The render function that is executed when a widgets’ value changes. It must have signature render_function(change) where change is a dict with the following keys:

    • type : The type of notification (normally 'change').
    • owner : the HasTraits instance
    • old : the old value of the modified trait attribute
    • new : the new value of the modified trait attribute
    • name : the name of the modified trait attribute.

    If None, then nothing is assigned.

  • style (str (see below), optional) –

    Sets a predefined style at the widget. Possible options are:

    Style Description
    'minimal' Simple black and white style
    'success' Green-based style
    'info' Blue-based style
    'warning' Yellow-based style
    'danger' Red-based style
    '' No style
  • tabs_style (str (see below), optional) –

    Sets a predefined style at the tabs of the widget. Possible options are:

    Style Description
    'minimal' Simple black and white style
    'success' Green-based style
    'info' Blue-based style
    'warning' Yellow-based style
    'danger' Red-based style
    '' No style

Example

Let’s create a rendering options widget and then update its state. Firstly, we need to import it:

>>> from menpowidgets.options import RendererOptionsWidget

Let’s set some initial options:

>>> options_tabs = ['markers', 'lines', 'grid']
>>> labels = ['jaw', 'eyes']

Now let’s define a render function that will get called on every widget change and will dynamically print the selected marker face colour and line width:

>>> from menpo.visualize import print_dynamic
>>> def render_function(change):
>>>     s = "Marker face colour: {}, Line width: {}".format(
>>>         wid.selected_values['markers']['marker_face_colour'],
>>>         wid.selected_values['lines']['line_width'])
>>>     print_dynamic(s)

Create the widget with the initial options and display it:

>>> wid = RendererOptionsWidget(options_tabs, labels=labels,
>>>                             render_function=render_function,
>>>                             style='info')
>>> wid

By playing around, the printed message gets updated. The style of the widget can be changed as:

>>> wid.predefined_style('minimal', 'info')

Finally, let’s change the widget status with a new set of labels:

>>> wid.set_widget_state(labels=['1'], allow_callback=True)

Remember that the widget is mnemonic, i.e. it remembers the objects it has seen and their corresponding options. These can be retrieved as:

>>> wid.default_options
add_callbacks()[source]

Function that adds the handler callback functions in all the widget components, which are necessary for the internal functionality.

add_render_function(render_function)

Method that adds the provided render_function() as a callback handler to the selected_values trait of the widget. The given function is also stored in self._render_function.

Parameters:render_function (callable or None, optional) –

The render function that behaves as a callback handler of the selected_values trait for the change event. Its signature can be render_function() or render_function(change), where change is a dict with the following keys:

  • owner : the HasTraits instance
  • old : the old value of the modified trait attribute
  • new : the new value of the modified trait attribute
  • name : the name of the modified trait attribute.
  • type : 'change'

If None, then nothing is added.

add_traits(**traits)

Dynamically add trait attributes to the Widget.

call_render_function(old_value, new_value, type_value='change')

Method that calls the existing render_function() callback handler.

Parameters:
  • old_value (int or float or dict or list or tuple) – The old selected_values value.
  • new_value (int or float or dict or list or tuple) – The new selected_values value.
  • type_value (str, optional) – The trait event type.
close()

Close method.

Closes the underlying comm. When the comm is closed, all of the widget views are automatically removed from the front-end.

get_default_options(labels)[source]

Function that returns a dict with default options given a list of labels. The function returns the dict of options but also updates the self.default_options dict.

Parameters:labels (list or None, optional) – The list of labels used in all ColourSelectionWidget objects
Returns:default_options (dict) – A dict with the default options. It contains:
  • lines : (dict) It has the following keys:
    • render_lines : (bool) Whether to render the lines.
    • line_width : (float) The width of the lines.
    • line_style : (str) The style of the lines.
    • line_colour : (list) The colour per label.
  • markers : (dict) It has the following keys:
    • render_markers : (bool) Whether to render the markers.
    • marker_size : (int) The size of the markers.
    • marker_style : (str) The style of the markers.
    • marker_face_colour : (list) The face colour per label.
    • marker_edge_colour : (list) The edge colour per label.
    • marker_edge_width : (float) The edge width of the markers.

If the object is not seen before by the widget, then it automatically gets the following default options:

  • lines
    • render_lines = True
    • line_width = 1
    • line_style = '-
    • line_colour = ['red'] if labels is None else colours
  • markers
    • render_markers = True
    • marker_size = 5
    • marker_style = 'o'
    • marker_face_colour = ['red'] if labels is None else colours
    • marker_edge_colour = ['black'] if labels is None else colours
    • marker_edge_width = 1

where colours = sample_colours_from_colourmap(len(labels), 'jet')

get_key(labels)[source]

Function that returns a unique key based on the provided labels.

Parameters:labels (list or None, optional) – The list of labels used in all ColourSelectionWidget objects
Returns:key (str) – The key that has the format '{labels}'.
has_trait(name)

Returns True if the object has a trait with the specified name.

initialise_global_options(axes_x_limits, axes_y_limits)[source]

Function that returns a dict with global options, i.e. options that do not depend on labels. The functions updates self.global_options dict with:

  • image : (dict) It has the following keys:
    • interpolation : (str) The interpolation method.
    • cmap_name : (str) The colourmap.
    • alpha : (float) The alpha transparency value.
  • numbering : (dict) It has the following keys:
    • render_numbering : (bool) Flag for rendering the numbers.
    • numbers_font_name : (str) The font name.
    • numbers_font_size : (int) The font size.
    • numbers_font_style : (str) The font style.
    • numbers_font_weight : (str) The font weight.
    • numbers_font_colour : (list) The font colour.
    • numbers_horizontal_align : (str) The horizontal alignment.
    • numbers_vertical_align : (str) The vertical alignment.
  • zoom_one : (float) The zoom value.
  • zoom_two : (list of float) The zoom values.
  • axes : (dict) It has the following keys:
    • render_axes : (bool) Flag for rendering the axes.
    • axes_font_name : (str) The axes font name.
    • axes_font_size : (int) The axes font size.
    • axes_font_style : (str) The axes font style
    • axes_font_weight : (str) The font weight.
    • axes_x_ticks : (list or None) The x ticks.
    • axes_y_ticks : (list or None) The y ticks.
    • axes_x_limits : (float or [float, float] or None) The x limits.
    • axes_y_limits : (float or [float, float] or None) The y limits.
  • legend : (dict) It has the following keys:
    • render_legend : (bool) Flag for rendering the legend.
    • legend_title : (str) The legend title.
    • legend_font_name : (str) The font name.
    • legend_font_style : (str) The font style.
    • legend_font_size : (str) The font size.
    • legend_font_weight : (str) The font weight.
    • legend_marker_scale : (float) The marker scale.
    • legend_location : (int) The legend location.
    • legend_bbox_to_anchor : (tuple) Bbox to anchor.
    • legend_border_axes_pad : (float) Border axes pad.
    • legend_n_columns : (int) The number of columns.
    • legend_horizontal_spacing : (float) Horizontal spacing.
    • legend_vertical_spacing : (float) Vetical spacing.
    • legend_border : (bool) Flag for adding border to the legend
    • legend_border_padding : (float) The border padding
    • legend_shadow : (bool) Flag for adding shadow to the legend
    • legend_rounded_corners : (bool) Flag for adding rounded corners to the legend.
  • gird : (dict) It has the following keys:
    • render_grid : (bool) Flag for rendering the grid.
    • grid_line_width : (int) The line width.
    • grid_line_style : (str) The line style.

If the object is not seen before by the widget, then it automatically gets the following default options:

  • image
    • interpolation = 'bilinear'
    • cmap_name = None
    • alpha = 1.
  • numbering
    • render_numbering = False
    • numbers_font_name = 'sans-serif'
    • numbers_font_size = 10
    • numbers_font_style = 'normal'
    • numbers_font_weight = 'normal'
    • numbers_font_colour = ['black']
    • numbers_horizontal_align = 'center'
    • numbers_vertical_align = 'bottom'
  • zoom_one = 1.
  • zoom_two = [1., 1.]
  • axes
    • render_axes = False
    • axes_font_name = 'sans-serif'
    • axes_font_size = 10
    • axes_font_style = 'normal'
    • axes_font_weight = 'normal'
    • axes_x_ticks = None
    • axes_y_ticks = None
    • axes_x_limits = axes_x_limits
    • axes_y_limits = axes_y_limits
  • legend
    • render_legend = False
    • legend_title = ''
    • legend_font_name = 'sans-serif'
    • legend_font_style = 'normal'
    • legend_font_size = 10
    • legend_font_weight = 'normal'
    • legend_marker_scale = 1.
    • legend_location = 2
    • legend_bbox_to_anchor = (1.05, 1.)
    • legend_border_axes_pad = 1.
    • legend_n_columns = 1
    • legend_horizontal_spacing = 1.
    • legend_vertical_spacing = 1.
    • legend_border = True
    • legend_border_padding = 0.5
    • legend_shadow = False
    • legend_rounded_corners = False
  • grid
    • render_grid = False
    • grid_line_width = 0.5
    • grid_line_style = '--'
Parameters:
  • axes_x_limits (float or (float, float) or None, optional) – The limits of the x axis. If float, then it sets padding on the right and left as a percentage of the rendered object’s width. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically.
  • axes_y_limits ((float, float) tuple or None, optional) – The limits of the y axis. If float, then it sets padding on the top and bottom as a percentage of the rendered object’s height. If tuple or list, then it defines the axis limits. If None, then the limits are set automatically.
observe(handler, names=traitlets.All, type='change')

Setup a handler to be called when a trait changes.

This is used to setup dynamic notifications of trait changes.

Parameters:
  • handler (callable) – A callable that is called when a trait changes. Its signature should be handler(change), where change```is a dictionary. The change dictionary at least holds a 'type' key. * ``type: the type of notification. Other keys may be passed depending on the value of ‘type’. In the case where type is ‘change’, we also have the following keys: * owner : the HasTraits instance * old : the old value of the modified trait attribute * new : the new value of the modified trait attribute * name : the name of the modified trait attribute.
  • names (list, str, All) – If names is All, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.
  • type (str, All (default: 'change')) – The type of notification to filter by. If equal to All, then all notifications are passed to the observe handler.
predefined_style(style, tabs_style='minimal')[source]

Function that sets a predefined style on the widget.

Parameters:
  • style (str (see below)) –

    Style options:

    Style Description
    'minimal' Simple black and white style
    'success' Green-based style
    'info' Blue-based style
    'warning' Yellow-based style
    'danger' Red-based style
    '' No style
  • tabs_style (str (see below)) –

    Tabs style options:

    Style Description
    'minimal' Simple black and white style
    'success' Green-based style
    'info' Blue-based style
    'warning' Yellow-based style
    'danger' Red-based style
    '' No style
remove_callbacks()[source]

Function that removes all the internal handler callback functions.

remove_render_function()

Method that removes the current self._render_function() as a callback handler to the selected_values trait of the widget and sets self._render_function = None.

replace_render_function(render_function)

Method that replaces the current self._render_function() with the given render_function() as a callback handler to the selected_values trait of the widget.

Parameters:render_function (callable or None, optional) –

The render function that behaves as a callback handler of the selected_values trait for the change event. Its signature can be render_function() or render_function(change), where change is a dict with the following keys:

  • owner : the HasTraits instance
  • old : the old value of the modified trait attribute
  • new : the new value of the modified trait attribute
  • name : the name of the modified trait attribute.
  • type : 'change'

If None, then nothing is added.

set_widget_state(labels, allow_callback=True)[source]

Method that updates the state of the widget, if the provided labels are different than self.labels.

Parameters:
  • labels (list or None, optional) – The list of labels used in all ColourSelectionWidget objects
  • allow_callback (bool, optional) – If True, it allows triggering of any callback functions.
style(box_style=None, border_visible=False, border_colour='black', border_style='solid', border_width=1, border_radius=0, padding='0.2cm', margin=0, tabs_box_style=None, tabs_border_visible=True, tabs_border_colour='black', tabs_border_style='solid', tabs_border_width=1, tabs_border_radius=1, tabs_padding=0, tabs_margin=0, font_family='', font_size=None, font_style='', font_weight='')[source]

Function that defines the styling of the widget.

Parameters:
  • box_style (str or None (see below), optional) –

    Possible widget style options:

    'success', 'info', 'warning', 'danger', '', None
    
  • border_visible (bool, optional) – Defines whether to draw the border line around the widget.
  • border_colour (str, optional) – The colour of the border around the widget.
  • border_style (str, optional) – The line style of the border around the widget.
  • border_width (float, optional) – The line width of the border around the widget.
  • border_radius (float, optional) – The radius of the border around the widget.
  • padding (float, optional) – The padding around the widget.
  • margin (float, optional) – The margin around the widget.
  • tabs_box_style (str or None (see below), optional) –

    Possible tab widgets style options:

    'success', 'info', 'warning', 'danger', '', None
    
  • tabs_border_visible (bool, optional) – Defines whether to draw the border line around the tab widgets.
  • tabs_border_colour (str, optional) – The color of the border around the tab widgets.
  • tabs_border_style (str, optional) – The line style of the border around the tab widgets.
  • tabs_border_width (float, optional) – The line width of the border around the tab widgets.
  • tabs_border_radius (float, optional) – The radius of the corners of the box of the tab widgets.
  • tabs_padding (float, optional) – The padding around the tab widgets.
  • tabs_margin (float, optional) – The margin around the tab widgets.
  • font_family (str (see below), optional) –

    The font family to be used. Example options:

    'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace',
    'helvetica'
    
  • font_size (int, optional) – The font size.
  • font_style (str (see below), optional) –

    The font style. Example options:

    'normal', 'italic', 'oblique'
    
  • font_weight (See Below, optional) –

    The font weight. Example options:

    'ultralight', 'light', 'normal', 'regular', 'book', 'medium',
    'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy',
    'extra bold', 'black'
    
trait_names(**metadata)

Get a list of all the names of this class’ traits.

traits(**metadata)

Get a dict of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.

unobserve(handler, names=traitlets.All, type='change')

Remove a trait change handler.

This is used to unregister handlers to trait change notificiations.

Parameters:
  • handler (callable) – The callable called when a trait attribute changes.
  • names (list, str, All (default: All)) – The names of the traits for which the specified handler should be uninstalled. If names is All, the specified handler is uninstalled from the list of notifiers corresponding to all changes.
  • type (str or All (default: 'change')) – The type of notification to filter by. If All, the specified handler is uninstalled from the list of notifiers corresponding to all types.
unobserve_all(name=traitlets.All)

Remove trait change handlers of any type for the specified name. If name is not specified, removes all trait notifiers.