PatchOptionsWidget¶
-
class
menpowidgets.options.
PatchOptionsWidget
(n_patches, n_offsets, render_function=None, style='minimal', subwidgets_style='minimal')[source]¶ Bases:
MenpoWidget
Creates a widget for selecting patches options when rendering a patch-based image. The widget consists of the following objects from ipywidgets and menpowidgets.tools:
No Object Property (self.) Description 1 Dropdown offset_dropdown Offset index 2 Checkbox render_centers_checkbox Render centers flag 3 Checkbox render_patches_checkbox Render patches flag 4 ToggleButton background_toggle Background colour 5 Latex background_title Background title 6 SlicingCommandWidget
slicing_wid Patch index selector 7 LineOptionsWidget
bboxes_line_options_wid Bboxes options 8 HBox background_box Contains 5, 4 9 Box render_checkboxes_box Contains 2, 3 10 HBox render_box Contains 8, 9 11 VBox offset_patches_box Contains 6, 1, 10 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 patches object has a unique key id assigned throughget_key()
. Then, the options that correspond to each key are stored in theself.default_options
dict. - The selected values of the current patches object are stored in the
self.selected_values
trait. It is a dict with the following keys:patches_indices
: (list or int) The selected patches (e.g.list(range(n_patches))
).offset_index
: (int) The selected offsetbackground
: (str) The background colour (e.g.'white'
).render_patches
: (bool) Whether to render the patches.render_patches_bboxes
: (bool) Whether to render boxes around the patches.bboxes_line_colour
: (list) The boxes line colour (e.g.['red']
)bboxes_line_style
: (str) The boxes line style (e.g.'-'
).bboxes_line_width
: (float) The boxes line width (e.g.1
).render_centers
: (bool) Whether to render the patches’ centers.
- When an unseen patches 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:patches_indices = list(range(n_patches))
offset_index = 0
background = 'white'
render_patches = True
render_patches_bboxes = True
bboxes_line_colour = ['red']
bboxes_line_style = '-'
bboxes_line_width = 1
render_centers = True
- To set the styling of this widget please refer to the
style()
andpredefined_style()
methods. - To update the handler callback function of the widget, please refer to the
replace_render_function()
method.
Parameters: - n_patches (int) – The number of patches of the initial object.
- n_offsets (int) – The number of offsets of the initial object.
- render_function (callable or
None
, optional) –The render function that is executed when a widgets’ value changes. It must have signature
render_function(change)
wherechange
is a dict with the following keys:type
: The type of notification (normally'change'
).owner
: the HasTraits instanceold
: the old value of the modified trait attributenew
: the new value of the modified trait attributename
: 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 - subwidgets_style (str (see below), optional) –
Sets a predefined style at the widget’s patches and bboxes options. 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 patches widget and then update its state. Firstly, we need to import it:
>>> from menpowidgets.options import PatchOptionsWidget
Now let’s define a render function that will get called on every widget change and will dynamically print the selected patches and bboxes flag:
>>> from menpo.visualize import print_dynamic >>> def render_function(change): >>> s = "Patches: {}, BBoxes: {}".format( >>> wid.selected_values['patches']['indices'], >>> wid.selected_values['bboxes']['render_lines']) >>> print_dynamic(s)
Create the widget with some initial options and display it:
>>> wid = PatchOptionsWidget(n_patches=68, n_offsets=5, >>> render_function=render_function, >>> style='info', subwidgets_style='danger') >>> wid
By playing around with the widget, printed message gets updated. Finally, let’s change the widget status with a new set of options:
>>> wid.set_widget_state(n_patches=49, n_offsets=1, allow_callback=False)
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()
orrender_function(change)
, wherechange
is a dict with the following keys:owner
: the HasTraits instanceold
: the old value of the modified trait attributenew
: the new value of the modified trait attributename
: 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
(n_patches, n_offsets)[source]¶ Function that returns a dict with default options given the properties of a patches object, i.e. n_patches and n_offsets. The function returns the dict of options but also updates the
self.default_options
dict.Parameters: - n_patches (int) – The number of patches.
- n_offsets (int) – The number of offsets.
Returns: default_options (dict) – A dict with the default options. It contains:
patches_indices
: (list or int) The selected patches.offset_index
: (int) The selected offset.background
: (str) The background colour.render_patches
: (bool) Whether to render the patches.render_patches_bboxes
: (bool) Whether to render boxes around the patches.bboxes_line_colour
: (list) The boxes line colour.bboxes_line_style
: (str) The boxes line style.bboxes_line_width
: (float) The boxes line width.render_centers
: (bool) Whether to render the patches centers
If the object is not seen before by the widget, then it automatically gets the following default options:
patches_indices = list(range(n_patches))
offset_index = 0
background = 'white'
render_patches = True
render_patches_bboxes = True
bboxes_line_colour = ['red']
bboxes_line_style = '-'
bboxes_line_width = 1
render_centers = True
-
get_key
(n_patches, n_offsets)[source]¶ Function that returns a unique key based on the properties of the provided patches object.
Parameters: - n_patches (int) – The number of patches.
- n_offsets (int) – The number of offsets.
Returns: key (str) – The key that has the format
'{n_patches}_{n_offsets}'
.
-
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)
, wherechange```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.
- handler (callable) – A callable that is called when a trait changes. Its
signature should be
-
predefined_style
(style, subwidgets_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 - subwidgets_style (str (see below)) –
Sub-widgets (patches and bounding boxes) 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
- style (str (see below)) –
-
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()
orrender_function(change)
, wherechange
is a dict with the following keys:owner
: the HasTraits instanceold
: the old value of the modified trait attributenew
: the new value of the modified trait attributename
: the name of the modified trait attribute.type
:'change'
If
None
, then nothing is added.
-
set_widget_state
(n_patches, n_offsets, allow_callback=True)[source]¶ Method that updates the state of the widget, if the key generated with
get_key()
based on the provided n_patches and n_offsets is different than the current key based onself.n_patches
andself.n_offsets
.Parameters: - n_patches (int) – The number of patches.
- n_offsets (int) – The number of offsets.
- 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='dashed', border_width=1, border_radius=0, padding=0, margin=0, font_family='', font_size=None, font_style='', font_weight='', bboxes_box_style=None, bboxes_border_visible=False, bboxes_border_colour='black', bboxes_border_style='solid', bboxes_border_width=1, bboxes_border_radius=0, bboxes_padding=0, bboxes_margin=0, patches_box_style=None, patches_border_visible=False, patches_border_colour='black', patches_border_style='solid', patches_border_width=1, patches_border_radius=0, patches_padding=0, patches_margin=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'
- bboxes_box_style (str or
None
(see below), optional) –Style options for the bounding boxes:
‘success’, ‘info’, ‘warning’, ‘danger’, ‘’, None - bboxes_border_visible (bool, optional) – Defines whether to draw the border line around the bounding boxes options.
- bboxes_border_colour (str, optional) – The color of the border around the bounding boxes options.
- bboxes_border_style (str, optional) – The line style of the border around the bounding boxes options.
- bboxes_border_width (float, optional) – The line width of the border around the bounding boxes options.
- bboxes_border_radius (float, optional) – The radius of the corners of the box of the bounding boxes options.
- bboxes_padding (float, optional) – The padding around the bounding boxes options.
- bboxes_margin (float, optional) – The margin around the bounding boxes options.
- patches_box_style (str or
None
(see below), optional) –Style options of the patches and offset options:
‘success’, ‘info’, ‘warning’, ‘danger’, ‘’, None - patches_border_visible (bool, optional) – Defines whether to draw the border line around the patches and offset options.
- patches_border_colour (str, optional) – The color of the border around the patches and offset options.
- patches_border_style (str, optional) – The line style of the border around the patches and offset options.
- patches_border_width (float, optional) – The line width of the border around the patches and offset options.
- patches_border_radius (float, optional) – The radius of the corners of the box of the patches and offset options.
- patches_padding (float, optional) – The padding around the patches and offset options.
- patches_margin (float, optional) – The margin around the patches and offset options.
- box_style (str or
-
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.
- To update the state of the widget, please refer to the