IterativeResultOptionsWidget

class menpowidgets.menpofit.options.IterativeResultOptionsWidget(has_gt_shape, has_initial_shape, has_image, n_shapes, has_costs, render_function=None, tab_update_function=None, displacements_function=None, errors_function=None, costs_function=None, style='minimal', tabs_style='minimal')[source]

Bases: MenpoWidget

Creates a widget for selecting options when visualizing an iterative fitting result. The widget consists of the following parts from ipywidgets and menpowidgets.tools:

No Object Property (self.) Description
1 RadioButtons mode Subplot mode
2 Checkbox render_image Image rendering
3 VBox mode_render_image_box Contains 1, 2
4 Latex shape_buttons[0] ‘Shape:’ str
5 ToggleButton shape_buttons[1] Initial shape
6 ToggleButton shape_buttons[2] Final shape
7 ToggleButton shape_buttons[3] Ground truth
8 HBox result_box Contains 4-7
9 RadioButtons iterations_mode

‘Animation’ or

‘Static’

10 AnimationOptionsWidget index_animation Animation wid
11 SlicingCommandWidget index_slicing Slicing wid
12 Button plot_errors_button Errors plot
13 Button plot_displacements_button Displacements
14 Button plot_costs_button Costs plot
15 HBox buttons_box Contains 12-14
16 VBox index_buttons_box 10,11,15
17 HBox mode_index_buttons_box Contains 9, 16
18 Latex no_iterations_text No iterations
19 VBox iterations_box Contains 17, 18
20 Tab result_iterations_tab Contains 8, 19

Note that:

Parameters:
  • has_gt_shape (bool) – Whether the fitting result object has the ground truth shape.
  • has_initial_shape (bool) – Whether the fitting result object has the initial shape.
  • has_image (bool) – Whether the fitting result object has the image.
  • n_shapes (int or None) – The total number of shapes. If None, then it is assumed that no iteration shapes are available.
  • has_costs (bool) – Whether the fitting result object has costs attached.
  • 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.

  • tab_update_function (callable or None, optional) – A function that gets called when switching between the ‘Result’ and ‘Iterations’ tabs. If None, then nothing is assigned.
  • displacements_function (callable or None, optional) – The function that is executed when the ‘Displacements’ button is pressed. It must have signature displacements_function(name). If None, then nothing is assigned and the button is invisible.
  • errors_function (callable or None, optional) – The function that is executed when the ‘Errors’ button is pressed. It must have signature errors_function(name). If None, then nothing is assigned and the button is invisible.
  • costs_function (callable or None, optional) – The function that is executed when the ‘Errors’ button is pressed. It must have signature displacements_function(name). If None, then nothing is assigned and the button is invisible.
  • 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 tab widgets. 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 an iterative result options widget and then update its state. Firstly, we need to import it:

>>> from menpowidgets.menpofit.options import IterativeResultOptionsWidget

Now let’s define a render function that will get called on every widget change and will print the selected options:

>>> def render_function(change):
>>>     print(wid.selected_values)

Let’s also define a plot function that will get called when one of the ‘Errors’, ‘Costs’ or ‘Displacements’ buttons is pressed:

>>> def plot_function(name):
>>>     print(name)

Create the widget with some initial options and display it:

>>> wid = IterativeResultOptionsWidget(
>>>         has_gt_shape=True, has_initial_shape=True, has_image=True,
>>>         n_shapes=20, has_costs=True, render_function=render_function,
>>>         displacements_function=plot_function,
>>>         errors_function=plot_function, costs_function=plot_function,
>>>         style='info', tabs_style='danger')
>>> wid

By changing the various widgets, the printed message gets updated. Finally, let’s change the widget status with a new set of options:

>>> wid.set_widget_state(has_gt_shape=False, has_initial_shape=True,
>>>                      has_image=True, n_shapes=None, has_costs=False,
>>>                      allow_callback=True)
add_callbacks()[source]

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

add_costs_function(costs_function)[source]

Method that adds the provided costs_function as a callback handler to the click event of self.plot_costs_button. The given function is also stored in self._costs_function.

Parameters:costs_function (callable or None, optional) – The function that behaves as a callback handler of the click event of self.plot_costs_button. Its signature is costs_function(name). If None, then nothing is added.
add_displacements_function(displacements_function)[source]

Method that adds the provided displacements_function as a callback handler to the click event of self.plot_displacements_button. The given function is also stored in self._displacements_function.

Parameters:displacements_function (callable or None, optional) – The function that behaves as a callback handler of the click event of self.plot_displacements_button. Its signature is displacements_function(name). If None, then nothing is added.
add_errors_function(errors_function)[source]

Method that adds the provided errors_function as a callback handler to the click event of self.plot_errors_button. The given function is also stored in self._errors_function.

Parameters:errors_function (callable or None, optional) – The function that behaves as a callback handler of the click event of self.plot_errors_button. Its signature is errors_function(name). If None, then nothing is added.
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.

has_trait(name)

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

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)[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_costs_function()[source]

Method that removes the current self._costs_function as a callback handler to the click event of self.plot_costs_button and sets self._costs_function = None.

remove_displacements_function()[source]

Method that removes the current self._displacements_function as a callback handler to the click event of self.plot_displacements_button and sets self._displacements_function = None.

remove_errors_function()[source]

Method that removes the current self._errors_function as a callback handler to the click event of self.plot_errors_button and sets self._errors_function = None.

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_costs_function(costs_function)[source]

Method that replaces the current self._costs_function with the given costs_function as a callback handler to the click event of self.plot_costs_button.

Parameters:costs_function (callable or None, optional) – The function that behaves as a callback handler of the click event of self.plot_costs_button. Its signature is costs_function(name). If None, then nothing is added.
replace_displacements_function(displacements_function)[source]

Method that replaces the current self._displacements_function with the given displacements_function as a callback handler to the click event of self.plot_displacements_button.

Parameters:displacements_function (callable or None, optional) – The function that behaves as a callback handler of the click event of self.plot_displacements_button. Its signature is displacements_function(name). If None, then nothing is added.
replace_errors_function(errors_function)[source]

Method that replaces the current self._errors_function with the given errors_function as a callback handler to the click event of self.plot_errors_button.

Parameters:errors_function (callable or None, optional) – The function that behaves as a callback handler of the click event of self.plot_errors_button. Its signature is errors_function(name). If None, then nothing is added.
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_visibility()[source]

Function that sets the visibility of the various components of the widget, depending on the properties of the current image object, i.e. self.n_channels and self.image_is_masked.

set_widget_state(has_gt_shape, has_initial_shape, has_image, n_shapes, has_costs, allow_callback=True)[source]

Method that updates the state of the widget with a new set of values.

Parameters:
  • has_gt_shape (bool) – Whether the fitting result object has the ground truth shape.
  • has_initial_shape (bool) – Whether the fitting result object has the initial shape.
  • has_image (bool) – Whether the fitting result object has the image.
  • n_shapes (int or None) – The total number of shapes. If None, then it is assumed that no iteration shapes are available.
  • has_costs (bool) – Whether the fitting result object has the costs attached.
  • 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, margin=0, font_family='', font_size=None, font_style='', font_weight='', buttons_style='', tabs_box_style=None, tabs_border_visible=False, tabs_border_colour='black', tabs_border_style='solid', tabs_border_width=1, tabs_border_radius=0)[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.
  • 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'
    
  • buttons_style (str or None (see below), optional) –

    Style options:

    ‘success’, ‘info’, ‘warning’, ‘danger’, ‘primary’, ‘’, None
  • 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 colour 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 border around the tab widgets.
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.