Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [Feature] Display parameters #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions brainsprite_widget/traits.py
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 38 additions & 2 deletions brainsprite_widget/widget.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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