LinearModelParametersWidget

class menpowidgets.options.LinearModelParametersWidget(n_parameters, render_function=None, mode='multiple', params_str='', params_bounds=(-3.0, 3.0), params_step=0.1, plot_variance_visible=True, plot_variance_function=None, animation_visible=True, loop_enabled=False, interval=0.0, interval_step=0.05, animation_step=0.5, style='minimal', continuous_update=False)[source]

Bases: MenpoWidget

Creates a widget for selecting parameters values when visualizing a linear model (e.g. PCA model). The widget has options for animating through various parameters values. It consists of the following objects from ipywidgets:

No Object Property (self.) Description
1 Button plot_button The plot variance button
2 Button reset_button The reset button
3 HBox plot_and_reset Contains 1, 2
4 ToggleButton play_stop_toggle The play/stop button
5 Button fast_forward_button Increase speed
6 Button fast_backward_button Decrease speed
7 ToggleButton loop_toggle Repeat mode
8 HBox animation_buttons Contains 4, 5, 6, 7
9 HBox buttons_box Contains 3, 8

If mode = 'single', then:

No Object Property (self.) Description
4 FloatSlider slider The parameter value slider
5 Dropdown dropdown_params The parameter selector
6 HBox parameters_wid Contains 4, 5

If mode = 'multiple', then:

No Object Property (self.) Description
7 FloatSlider sliders list of all sliders
8 VBox parameters_wid Contains all 7

Note that:

Parameters:
  • n_parameters (int) – The list of initial parameters values.
  • 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.

  • mode ({'single', 'multiple'}, optional) – If 'single', only a single slider is constructed along with a dropdown menu that allows the parameter selection. If 'multiple', a slider is constructed for each parameter.
  • params_str (str, optional) – The string that will be used as description of the slider(s). The final description has the form "{}{}".format(params_str, p), where p is the parameter number.
  • params_bounds ((float, float), optional) – The minimum and maximum bounds, in std units, for the sliders.
  • params_step (float, optional) – The step, in std units, of the sliders.
  • plot_variance_visible (bool, optional) – Defines whether the button for plotting the variance will be visible upon construction.
  • plot_variance_function (callable or None, optional) – The plot function that is executed when the plot variance button is clicked. If None, then nothing is assigned.
  • animation_visible (bool, optional) – Defines whether the animation options will be visible.
  • loop_enabled (bool, optional) – If True, then the repeat mode of the animation is enabled.
  • interval (float, optional) – The interval between the animation progress in seconds.
  • interval_step (float, optional) – The interval step (in seconds) that is applied when fast forward/backward buttons are pressed.
  • animation_step (float, optional) – The parameters step that is applied when animation is enabled.
  • 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
  • continuous_update (bool, optional) – If True, then the render function is called while moving a slider’s handle. If False, then the the function is called only when the handle (mouse click) is released.

Example

Let’s create a linear model parameters values widget and then update its state. Firstly, we need to import it:

>>> from menpowidgets.options import LinearModelParametersWidget

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

>>> from menpo.visualize import print_dynamic
>>> def render_function(change):
>>>     s = "Selected parameters: {}".format(wid.selected_values)
>>>     print_dynamic(s)

Create the widget with some initial options and display it:

>>> wid = LinearModelParametersWidget(n_parameters=5,
>>>                                   render_function=render_function,
>>>                                   params_str='Parameter ',
>>>                                   mode='multiple',
>>>                                   params_bounds=(-3., 3.),
>>>                                   plot_variance_visible=True,
>>>                                   style='info')
>>> wid

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

>>> wid.set_widget_state(n_parameters=10, params_str='',
>>>                      params_step=0.1, params_bounds=(-10, 10),
>>>                      plot_variance_visible=False,
>>>                      allow_callback=True)
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.

add_variance_function(variance_function)[source]

Method that adds a variance_function() to the Variance button of the widget. The given function is also stored in self._variance_function.

Parameters:variance_function (callable or None, optional) – The variance function that behaves as a callback. If None, then nothing is added.
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)[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
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.

remove_variance_function()[source]

Method that removes the current self._variance_function() from the Variance button of the widget and sets self._variance_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.

replace_variance_function(variance_function)[source]

Method that replaces the current self._variance_function() of the Variance button of the widget with the given variance_function().

Parameters:variance_function (callable or None, optional) – The variance function that behaves as a callback. If None, then nothing happens.
set_widget_state(n_parameters=None, params_str=None, params_bounds=None, params_step=None, plot_variance_visible=True, animation_step=0.5, allow_callback=True)[source]

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

Parameters:
  • n_parameters (int) – The list of initial parameters values.
  • params_str (str, optional) – The string that will be used as description of the slider(s). The final description has the form "{}{}".format(params_str, p), where p is the parameter number.
  • params_bounds ((float, float), optional) – The minimum and maximum bounds, in std units, for the sliders.
  • params_step (float, optional) – The step, in std units, of the sliders.
  • plot_variance_visible (bool, optional) – Defines whether the button for plotting the variance will be visible upon construction.
  • animation_step (float, optional) – The parameters step that is applied when animation is enabled.
  • 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='', slider_width='', slider_handle_colour=None, slider_bar_colour=None, buttons_style='')[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'
    
  • slider_width (str, optional) – The width of the slider(s).
  • slider_handle_colour (str, optional) – The colour of the handle(s) of the slider(s).
  • slider_bar_colour (str, optional) – The bar colour of the slider(s).
  • buttons_style (str or None (see below), optional) –

    Style options:

    ‘success’, ‘info’, ‘warning’, ‘danger’, ‘primary’, ‘’, None
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 notifications.

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.