From c66fef1fbf56d8eacb283288014b6fa73e1f9ab1 Mon Sep 17 00:00:00 2001 From: anibalsolon Date: Mon, 10 Sep 2018 11:52:33 -0400 Subject: [PATCH] widget parameters --- brainsprite_widget/traits.py | 38 ++++++++++++++++++++++++++++++++++ brainsprite_widget/widget.py | 40 ++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 brainsprite_widget/traits.py diff --git a/brainsprite_widget/traits.py b/brainsprite_widget/traits.py new file mode 100644 index 0000000..e9323a9 --- /dev/null +++ b/brainsprite_widget/traits.py @@ -0,0 +1,38 @@ +import re +import six + +from traitlets import TraitType + + +class Point3D(TraitType): + default_value = {'x': 0, 'y': 0, 'z': 0} + info_text = 'a 3D point' + + def validate(self, obj, value): + if isinstance(value, dict): + if all(a in value for a in ['x', 'y', 'z']) and \ + all(isinstance(value[a], int) in value for a in ['x', 'y', 'z']): + return value + self.error(obj, value) + + +class Color(TraitType): + """A trait for unicode strings.""" + + default_value = '#000000' + info_text = 'a hex color string' + + def validate(self, obj, value): + + decoded = value + if isinstance(value, bytes): + try: + decoded = value.decode('ascii', 'strict') + except UnicodeDecodeError: + self.error(obj, value) + + if isinstance(decoded, six.text_type) and \ + re.match(r'^#(?:[0-9a-fA-F]{3}){1,2}$', decoded): + return decoded + + self.error(obj, value) \ No newline at end of file diff --git a/brainsprite_widget/widget.py b/brainsprite_widget/widget.py index 9011bd5..e2f7708 100644 --- a/brainsprite_widget/widget.py +++ b/brainsprite_widget/widget.py @@ -1,7 +1,8 @@ import nibabel as nb import ipywidgets as widgets -from traitlets import Unicode, Bytes +from traitlets import Unicode, Bytes, Bool, Dict, Int, Float from .brainsprite import view_stat_map +from .traits import Point3D, Color @widgets.register class BrainspriteWidget(widgets.DOMWidget): @@ -14,6 +15,41 @@ class BrainspriteWidget(widgets.DOMWidget): _model_module_version = Unicode('^0.1.0').tag(sync=True) sprite = Bytes(help='Sprite image').tag(sync=True) - def __init__(self, image): + nan_values = Bool(help='NaN image values, i.e. unable to read values').tag(sync=True) + smooth = Bool(help='Smoothing of the main slices').tag(sync=True) + coordinates = Bool(help='Show slice numbers').tag(sync=True) + origin = Point3D(help='Origin').tag(sync=True) + voxel_size = Int(help='Voxel size').tag(sync=True) + + background = Color(help='Background color for the canvas').tag(sync=True) + + colorbar_height = Float(help='Colorbar size parameters').tag(sync=True) + + font_size = Float(help='Font size').tag(sync=True) + font_color = Color(help='Font color').tag(sync=True) + decimals = Int(help='Number of decimals displayed', min=0).tag(sync=True) + + crosshair = Bool(help='Show crosshair').tag(sync=True) + crosshair_color = Color(help='Crosshair color').tag(sync=True) + crosshair_size = Float(help='Crosshair size', min=0).tag(sync=True) + + def __init__(self, image, nan_values, smooth, coordinates, origin, + voxel_size, background, colorbar_height, font_size, font_color, + decimals, crosshair, crosshair_color, crosshair_size): + super(BrainspriteWidget, self).__init__() + self.sprite = view_stat_map(image) + self.nan_values = nan_values + self.smooth = smooth + self.coordinates = coordinates + self.origin = origin + self.voxel_size = voxel_size + self.background = background + self.colorbar_height = colorbar_height + self.font_size = font_size + self.font_color = font_color + self.decimals = decimals + self.crosshair = crosshair + self.crosshair_color = crosshair_color + self.crosshair_size = crosshair_size \ No newline at end of file