class SubsetFacetState(State): log = CallbackProperty(False) v_min = CallbackProperty(0.) v_max = CallbackProperty(1.) steps = CallbackProperty(5) data = SelectionCallbackProperty() att = SelectionCallbackProperty() cmap = CallbackProperty() def __init__(self, data_collection): super(SubsetFacetState, self).__init__() self.data_helper = DataCollectionComboHelper(self, 'data', data_collection) self.att_helper = ComponentIDComboHelper(self, 'att') self.lim_helper = StateAttributeLimitsHelper(self, attribute='att', lower='v_min', upper='v_max', log='log') self.add_callback('data', self._on_data_change) self._on_data_change() def _on_data_change(self, *args, **kwargs): self.att_helper.set_multiple_data( [] if self.data is None else [self.data])
def __init__(self, viewer_state=None, layer=None, **kwargs): self.vz_att_helper = ComponentIDComboHelper(self, 'vz_att', numeric=True, categorical=False) super(Scatter3dLayerState, self).__init__(viewer_state=viewer_state, layer=layer)
def __init__(self, **kwargs): super(HistogramViewerState, self).__init__() self.hist_helper = StateAttributeHistogramHelper( self, 'x_att', lower='hist_x_min', upper='hist_x_max', n_bin='hist_n_bin', common_n_bin='common_n_bin') self.x_lim_helper = StateAttributeLimitsHelper(self, 'x_att', lower='x_min', upper='x_max', log='x_log') self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', pixel_coord=True, world_coord=True) self.update_from_dict(kwargs) # This should be added after update_from_dict since we don't want to # influence the restoring of sessions. self.add_callback('hist_x_min', self.update_view_to_bins) self.add_callback('hist_x_max', self.update_view_to_bins) self.add_callback('x_log', self._reset_x_limits, priority=1000)
def __init__(self, layer=None, viewer_state=None, **kwargs): super(ProfileLayerState, self).__init__(layer=layer, viewer_state=viewer_state) self.attribute_att_helper = ComponentIDComboHelper(self, 'attribute', numeric=True, categorical=False) percentile_display = { 100: 'Min/Max', 99.5: '99.5%', 99: '99%', 95: '95%', 90: '90%', 'Custom': 'Custom' } ProfileLayerState.percentile.set_choices( self, [100, 99.5, 99, 95, 90, 'Custom']) ProfileLayerState.percentile.set_display_func(self, percentile_display.get) self.add_callback('layer', self._update_attribute, priority=1000) self.add_callback('layer', self._update_profile, priority=1000) self.add_callback('attribute', self._update_profile, priority=1000) if layer is not None: self._update_attribute() self.update_from_dict(kwargs)
class SaveDataState(State): data = SelectionCallbackProperty() component = SelectionCallbackProperty() exporter = SelectionCallbackProperty() def __init__(self, data_collection=None): super(SaveDataState, self).__init__() self.data_helper = DataCollectionComboHelper(self, 'data', data_collection) self.component_helper = ComponentIDComboHelper(self, 'component', data_collection=data_collection) self.add_callback('data', self._on_data_change) self._on_data_change() self._sync_data_exporters() def _sync_data_exporters(self): exporters = list(config.data_exporter) def display_func(exporter): if exporter.extension == '': return "{0} (*)".format(exporter.label) else: return "{0} ({1})".format(exporter.label, ' '.join('*.' + ext for ext in exporter.extension)) SaveDataState.exporter.set_choices(self, exporters) SaveDataState.exporter.set_display_func(self, display_func) def _on_data_change(self, event=None): self.component_helper.set_multiple_data([self.data])
def __init__(self, **kwargs): super(ScatterViewerState, self).__init__() self.limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper(self, attribute='x_att', lower='x_min', upper='x_max', log='x_log', margin=0.05, limits_cache=self.limits_cache) self.y_lim_helper = StateAttributeLimitsHelper(self, attribute='y_att', lower='y_min', upper='y_max', log='y_log', margin=0.05, limits_cache=self.limits_cache) self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', pixel_coord=True, world_coord=True) self.y_att_helper = ComponentIDComboHelper(self, 'y_att', pixel_coord=True, world_coord=True) self.update_from_dict(kwargs) self.add_callback('x_log', self._reset_x_limits) self.add_callback('y_log', self._reset_y_limits)
def __init__(self, layer=None, **kwargs): self._sync_markersize = None super(ScatterLayerState, self).__init__(layer=layer) if self.layer is not None: self.color = self.layer.style.color self.size = self.layer.style.markersize self.alpha = self.layer.style.alpha self.size_att_helper = ComponentIDComboHelper(self, 'size_attribute') self.cmap_att_helper = ComponentIDComboHelper(self, 'cmap_attribute') self.size_lim_helper = StateAttributeLimitsHelper(self, attribute='size_attribute', lower='size_vmin', upper='size_vmax', cache=self.size_limits_cache) self.cmap_lim_helper = StateAttributeLimitsHelper(self, attribute='cmap_attribute', lower='cmap_vmin', upper='cmap_vmax', cache=self.cmap_limits_cache) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() self.cmap = colormaps.members[0][1] self.update_from_dict(kwargs)
def __init__(self, *args, **kwargs): super(TutorialViewerState, self).__init__(*args, **kwargs) self._x_att_helper = ComponentIDComboHelper(self, 'x_att') self._y_att_helper = ComponentIDComboHelper(self, 'y_att') self.add_callback('layers', self._on_layers_change) self.add_callback('x_att', self._on_attribute_change) self.add_callback('y_att', self._on_attribute_change)
class SpecvizLayerState(LayerState): """ """ color = CallbackProperty(docstring='The color used to display the data') alpha = CallbackProperty(docstring='The transparency used to display the data') linewidth = CallbackProperty(1, docstring='The width of the line for the data') attribute = SelectionCallbackProperty(docstring='The attribute to use for the spectrum') statistic = SelectionCallbackProperty(docstring='The statistic to use to collapse data') def __init__(self, viewer_state=None, **kwargs): super(SpecvizLayerState, self).__init__(viewer_state=viewer_state, **kwargs) self.color = self.layer.style.color self.alpha = self.layer.style.alpha self._sync_color = keep_in_sync(self, 'color', self.layer.style, 'color') self._sync_alpha = keep_in_sync(self, 'alpha', self.layer.style, 'alpha') self._att_helper = ComponentIDComboHelper(self, 'attribute') self.add_callback('layer', self._on_layer_change) self._on_layer_change() SpecvizLayerState.statistic.set_choices(self, list(FUNCTIONS)) SpecvizLayerState.statistic.set_display_func(self, FUNCTIONS.get) def _on_layer_change(self, *args): if self.layer is None: self._att_helper.set_multiple_data([]) else: self._att_helper.set_multiple_data([self.layer])
def __init__(self, **kwargs): super(WWTDataViewerState, self).__init__() WWTDataViewerState.mode.set_choices(self, ['Sky'] + MODES_3D + MODES_BODIES) WWTDataViewerState.frame.set_choices(self, CELESTIAL_FRAMES) WWTDataViewerState.alt_unit.set_choices(self, [str(x) for x in ALT_UNITS]) WWTDataViewerState.alt_type.set_choices(self, ALT_TYPES) self.add_callback('imagery_layers', self._update_imagery_layers) self.lon_att_helper = ComponentIDComboHelper(self, 'lon_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False) self.lat_att_helper = ComponentIDComboHelper(self, 'lat_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False) self.alt_att_helper = ComponentIDComboHelper(self, 'alt_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False, none='None') self.add_callback('layers', self._on_layers_changed) self._on_layers_changed() self.update_from_dict(kwargs)
def __init__(self, **kwargs): super(ImageViewerState, self).__init__() self.limits_cache = {} # NOTE: we don't need to use StateAttributeLimitsHelper here because # we can simply call reset_limits below when x/y attributes change. # Using StateAttributeLimitsHelper makes things a lot slower. self.ref_data_helper = ManualDataComboHelper(self, 'reference_data') self.xw_att_helper = ComponentIDComboHelper(self, 'x_att_world', numeric=False, categorical=False) self.yw_att_helper = ComponentIDComboHelper(self, 'y_att_world', numeric=False, categorical=False) self.add_callback('reference_data', self._reference_data_changed, priority=1000) self.add_callback('layers', self._layers_changed, priority=1000) self.add_callback('x_att', self._on_xatt_change, priority=500) self.add_callback('y_att', self._on_yatt_change, priority=500) self.add_callback('x_att_world', self._on_xatt_world_change, priority=1000) self.add_callback('y_att_world', self._on_yatt_world_change, priority=1000) aspect_display = {'equal': 'Square Pixels', 'auto': 'Automatic'} ImageViewerState.aspect.set_choices(self, ['equal', 'auto']) ImageViewerState.aspect.set_display_func(self, aspect_display.get) ImageViewerState.color_mode.set_choices(self, ['Colormaps', 'One color per layer']) self.update_from_dict(kwargs)
def __init__(self, layer=None, **kwargs): super(VolumeLayerState, self).__init__(layer=layer) if self.layer is not None: self.color = self.layer.style.color self.alpha = self.layer.style.alpha self.att_helper = ComponentIDComboHelper(self, 'attribute') self.lim_helper = StateAttributeLimitsHelper(self, attribute='attribute', lower='vmin', upper='vmax', cache=self.limits_cache) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() if isinstance(self.layer, Subset): self.vmin = 0 self.lim_helper.lower_frozen = True self.update_from_dict(kwargs)
def __init__(self, **kwargs): super(ScatterViewerState, self).__init__() self.limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper( self, attribute='x_att', lower='x_min', upper='x_max', log='x_log', limits_cache=self.limits_cache) self.y_lim_helper = StateAttributeLimitsHelper( self, attribute='y_att', lower='y_min', upper='y_max', log='y_log', limits_cache=self.limits_cache) self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att') self.y_att_helper = ComponentIDComboHelper(self, 'y_att') self.update_from_dict(kwargs)
def test_component(app, dataxz, dataxyz): # setup state = DummyState() helper = ComponentIDComboHelper(state, 'x_att', app.data_collection) helper.append_data(dataxz) state.helper = helper # main object we test dropdown = LinkedDropdown(state, 'x_att', 'x test attribute') # simple sanity tests assert dropdown.description == 'x test attribute' assert [item[0] for item in dropdown.options] == ['x', 'z'] # initial state assert state.x_att is dataxz.id['x'] assert dropdown.value is dataxz.id['x'] # glue state -> ui state.x_att = dataxz.id['z'] assert dropdown.value is dataxz.id['z'] # ui -> glue state dropdown.value = dataxz.id['x'] assert state.x_att is dataxz.id['x'] # same, but now be ok with strings state.x_att = 'z' assert dropdown.value is dataxz.id['z'] state.x_att = 'x' assert dropdown.value is dataxz.id['x']
def __init__(self, **kwargs): super(ProfileViewerState, self).__init__() self.ref_data_helper = ManualDataComboHelper(self, 'reference_data') self.x_lim_helper = StateAttributeLimitsHelper(self, 'x_att', lower='x_min', upper='x_max') self.add_callback('layers', self._layers_changed) self.add_callback('reference_data', self._reference_data_changed) self.add_callback('normalize', self._reset_y_limits) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', numeric=False, categorical=False, world_coord=True, pixel_coord=True) ProfileViewerState.function.set_choices(self, list(FUNCTIONS)) ProfileViewerState.function.set_display_func(self, FUNCTIONS.get) self.update_from_dict(kwargs)
def _enable_viewer_combo(self, viewer, data, index, selection_label): connect_combo_selection(self, selection_label, viewer.combo) helper = ComponentIDComboHelper(self, selection_label) helper.set_multiple_data([data]) viewer.combo.setEnabled(True) viewer.combo.currentIndexChanged.connect( self._get_change_viewer_combo_func(viewer.combo, index)) self._viewer_combo_helpers.append(helper)
def __init__(self, layer=None, viewer_state=None, **kwargs): self.uuid = str(uuid.uuid4()) super(ImageLayerState, self).__init__(layer=layer, viewer_state=viewer_state) self.attribute_lim_helper = StateAttributeLimitsHelper( self, attribute='attribute', percentile='percentile', lower='v_min', upper='v_max') self.attribute_att_helper = ComponentIDComboHelper(self, 'attribute', numeric=True, categorical=False) percentile_display = { 100: 'Min/Max', 99.5: '99.5%', 99: '99%', 95: '95%', 90: '90%', 'Custom': 'Custom' } ImageLayerState.percentile.set_choices( self, [100, 99.5, 99, 95, 90, 'Custom']) ImageLayerState.percentile.set_display_func(self, percentile_display.get) stretch_display = { 'linear': 'Linear', 'sqrt': 'Square Root', 'arcsinh': 'Arcsinh', 'log': 'Logarithmic' } ImageLayerState.stretch.set_choices( self, ['linear', 'sqrt', 'arcsinh', 'log']) ImageLayerState.stretch.set_display_func(self, stretch_display.get) self.add_callback('global_sync', self._update_syncing) self.add_callback('layer', self._update_attribute) self._update_syncing() if layer is not None: self._update_attribute() self.update_from_dict(kwargs) if self.cmap is None: self.cmap = self.layer.style.preferred_cmap or colormaps.members[ 0][1]
def __init__(self, *args, **kwargs): super(TutorialViewerState, self).__init__(*args, **kwargs) self._x_att_helper = ComponentIDComboHelper(self, 'x_att') self._y_att_helper = ComponentIDComboHelper(self, 'y_att') self.add_callback('layers', self._on_layers_change) self.add_callback('x_att', self._on_attribute_change) self.add_callback('y_att', self._on_attribute_change) TutorialViewerState.orientation.set_choices( self, ['bottom-up', 'left-right', 'top-down', 'right-left']) self.add_callback('orientation', self._on_attribute_change)
def __init__(self, **kwargs): super(ImageViewerState, self).__init__() self.limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper( self, attribute='x_att', lower='x_min', upper='x_max', limits_cache=self.limits_cache) self.y_lim_helper = StateAttributeLimitsHelper( self, attribute='y_att', lower='y_min', upper='y_max', limits_cache=self.limits_cache) self.ref_data_helper = ManualDataComboHelper(self, 'reference_data') self.xw_att_helper = ComponentIDComboHelper(self, 'x_att_world', numeric=False, categorical=False, visible=False, world_coord=True) self.yw_att_helper = ComponentIDComboHelper(self, 'y_att_world', numeric=False, categorical=False, visible=False, world_coord=True) self.add_callback('reference_data', self._reference_data_changed, priority=1000) self.add_callback('layers', self._layers_changed, priority=1000) self.add_callback('x_att', self._on_xatt_change, priority=500) self.add_callback('y_att', self._on_yatt_change, priority=500) self.add_callback('x_att_world', self._update_att, priority=500) self.add_callback('y_att_world', self._update_att, priority=500) self.add_callback('x_att_world', self._on_xatt_world_change, priority=1000) self.add_callback('y_att_world', self._on_yatt_world_change, priority=1000) self.update_from_dict(kwargs)
def __init__(self, **kwargs): super(Vispy3DScatterViewerState, self).__init__() self.x_att_helper = ComponentIDComboHelper(self, 'x_att', categorical=False) self.y_att_helper = ComponentIDComboHelper(self, 'y_att', categorical=False) self.z_att_helper = ComponentIDComboHelper(self, 'z_att', categorical=False) self.add_callback('layers', self._on_layers_change) self.update_from_dict(kwargs)
def __init__(self, data_collection): super(SubsetFacetState, self).__init__() self.data_helper = DataCollectionComboHelper(self, 'data', data_collection) self.att_helper = ComponentIDComboHelper(self, 'att') self.lim_helper = StateAttributeLimitsHelper(self, attribute='att', lower='v_min', upper='v_max', log='log') self.add_callback('data', self._on_data_change) self._on_data_change()
def __init__(self, parent=None, data_viewer=None): super(OptionsWidget, self).__init__(parent=parent) self.ui = load_ui('viewer_options.ui', self, directory=os.path.dirname(__file__)) self.file_helper = ComponentIDComboHelper( self, 'file_att', data_collection=data_viewer._data) connect_combo_selection(self, 'file_att', self.ui.combo_file_attribute)
def __init__(self, data_collection=None): super(SaveDataState, self).__init__() self.data_helper = DataCollectionComboHelper(self, 'data', data_collection) self.component_helper = ComponentIDComboHelper(self, 'component', data_collection=data_collection) self.add_callback('data', self._on_data_change) self._on_data_change() self._sync_data_exporters()
def __init__(self, **kwargs): super(ScatterViewerState, self).__init__() self.limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper( self, attribute='x_att', lower='x_min', upper='x_max', log='x_log', margin=0.04, limits_cache=self.limits_cache) self.y_lim_helper = StateAttributeLimitsHelper( self, attribute='y_att', lower='y_min', upper='y_max', log='y_log', margin=0.04, limits_cache=self.limits_cache) self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', pixel_coord=True, world_coord=True) self.y_att_helper = ComponentIDComboHelper(self, 'y_att', pixel_coord=True, world_coord=True) self.plot_mode_helper = ComboHelper(self, 'plot_mode') self.plot_mode_helper.choices = [ proj for proj in get_projection_names() if proj not in ['3d', 'scatter_density'] ] self.plot_mode_helper.selection = 'rectilinear' self.angle_unit_helper = ComboHelper(self, 'angle_unit') self.angle_unit_helper.choices = ['radians', 'degrees'] self.angle_unit_helper.selection = 'radians' self.update_from_dict(kwargs) self.add_callback('x_log', self._reset_x_limits) self.add_callback('y_log', self._reset_y_limits) if self.using_polar: self.full_circle()
def __init__(self, *args, **kwargs): # provides dropdown, parent, height etc. for plot options super(TutorialViewerState, self).__init__(*args, **kwargs) self._x_att_helper = ComponentIDComboHelper(self, 'x_att') self._y_att_helper = ComponentIDComboHelper(self, 'y_att') self.add_callback('layers', self._on_layers_change) self.add_callback('x_att', self._on_attribute_change) self.add_callback('y_att', self._on_attribute_change) TutorialViewerState.orientation.set_choices( self, ['bottom-up', 'left-right', 'top-down', 'right-left']) self.add_callback('orientation', self._on_attribute_change) TutorialViewerState.sort_by.set_choices(self, ['parent', 'height']) self.add_callback('sort_by', self._on_attribute_change)
def __init__(self, **kwargs): super(OpenSpaceViewerState, self).__init__() OpenSpaceViewerState.mode.set_choices(self, MODES) OpenSpaceViewerState.frame.set_choices(self, CELESTIAL_FRAMES) OpenSpaceViewerState.alt_unit.set_choices( self, [str(x) for x in ALTERNATIVE_UNITS]) OpenSpaceViewerState.alt_type.set_choices(self, ALTERNATIVE_TYPES) self.lon_att_helper = ComponentIDComboHelper(self, 'lon_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False) self.lat_att_helper = ComponentIDComboHelper(self, 'lat_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False) self.lum_att_helper = ComponentIDComboHelper(self, 'lum_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False) self.vel_att_helper = ComponentIDComboHelper(self, 'vel_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False) self.alt_att_helper = ComponentIDComboHelper(self, 'alt_att', numeric=True, categorical=False, world_coord=True, pixel_coord=False) self.add_callback('layers', self._on_layers_changed) self._on_layers_changed() self.update_from_dict(kwargs)
def __init__(self, viewer_state=None, layer=None, **kwargs): super(ScatterLayerState, self).__init__(viewer_state=viewer_state, layer=layer) self.limits_cache = {} self.cmap_lim_helper = StateAttributeLimitsHelper(self, attribute='cmap_att', lower='cmap_vmin', upper='cmap_vmax', limits_cache=self.limits_cache) self.size_lim_helper = StateAttributeLimitsHelper(self, attribute='size_att', lower='size_vmin', upper='size_vmax', limits_cache=self.limits_cache) self.cmap_att_helper = ComponentIDComboHelper(self, 'cmap_att', numeric=True, categorical=False) self.size_att_helper = ComponentIDComboHelper(self, 'size_att', numeric=True, categorical=False) self.xerr_att_helper = ComponentIDComboHelper(self, 'xerr_att', numeric=True, categorical=False) self.yerr_att_helper = ComponentIDComboHelper(self, 'yerr_att', numeric=True, categorical=False) ScatterLayerState.style.set_choices(self, ['Scatter', 'Line']) ScatterLayerState.cmap_mode.set_choices(self, ['Fixed', 'Linear']) ScatterLayerState.size_mode.set_choices(self, ['Fixed', 'Linear']) linestyle_display = {'solid': '–––––––', 'dashed': '– – – – –', 'dotted': '· · · · · · · ·', 'dashdot': '– · – · – ·'} ScatterLayerState.linestyle.set_choices(self, ['solid', 'dashed', 'dotted', 'dashdot']) ScatterLayerState.linestyle.set_display_func(self, linestyle_display.get) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() self.cmap = colormaps.members[0][1] self.size = self.layer.style.markersize self._sync_size = keep_in_sync(self, 'size', self.layer.style, 'markersize') self.update_from_dict(kwargs)
class VolumeLayerState(VispyLayerState): """ A state object for volume layers """ attribute = SelectionCallbackProperty() vmin = CallbackProperty() vmax = CallbackProperty() subset_mode = CallbackProperty('data') limits_cache = CallbackProperty({}) def __init__(self, layer=None, **kwargs): super(VolumeLayerState, self).__init__(layer=layer) if self.layer is not None: self.color = self.layer.style.color self.alpha = self.layer.style.alpha self.att_helper = ComponentIDComboHelper(self, 'attribute') self.lim_helper = StateAttributeLimitsHelper(self, attribute='attribute', lower='vmin', upper='vmax', cache=self.limits_cache) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() if isinstance(self.layer, Subset): self.vmin = 0 self.lim_helper.lower_frozen = True self.update_from_dict(kwargs) def _on_layer_change(self, layer=None): with delay_callback(self, 'vmin', 'vmin'): if self.layer is None: self.att_helper.set_multiple_data([]) else: self.att_helper.set_multiple_data([self.layer]) def update_priority(self, name): return 0 if name.endswith(('vmin', 'vmax')) else 1
class SaveDataState(State): data = SelectionCallbackProperty() subset = SelectionCallbackProperty() component = SelectionCallbackProperty() exporter = SelectionCallbackProperty() def __init__(self, data_collection=None): super(SaveDataState, self).__init__() self.data_helper = DataCollectionComboHelper(self, 'data', data_collection) self.component_helper = ComponentIDComboHelper(self, 'component', data_collection=data_collection) self.add_callback('data', self._on_data_change) self._on_data_change() self._sync_data_exporters() def _sync_data_exporters(self): exporters = list(config.data_exporter) def display_func(exporter): if exporter.extension == '': return "{0} (*)".format(exporter.label) else: return "{0} ({1})".format(exporter.label, ' '.join('*.' + ext for ext in exporter.extension)) SaveDataState.exporter.set_choices(self, exporters) SaveDataState.exporter.set_display_func(self, display_func) def _on_data_change(self, event=None): self.component_helper.set_multiple_data([self.data]) self._sync_subsets() def _sync_subsets(self): def display_func(subset): if subset is None: return "All data (no subsets applied)" else: return subset.label subsets = [None] + list(self.data.subsets) SaveDataState.subset.set_choices(self, subsets) SaveDataState.subset.set_display_func(self, display_func)
class Scatter3dLayerState(ScatterLayerState): vz_att = DDSCProperty(docstring="The attribute to use for the z vector arrow") def __init__(self, viewer_state=None, layer=None, **kwargs): self.vz_att_helper = ComponentIDComboHelper(self, 'vz_att', numeric=True, categorical=False) super(Scatter3dLayerState, self).__init__(viewer_state=viewer_state, layer=layer) # self.update_from_dict(kwargs) def _on_layer_change(self, layer=None): super(Scatter3dLayerState, self)._on_layer_change(layer=layer) if self.layer is None: self.vz_att_helper.set_multiple_data([]) else: self.vz_att_helper.set_multiple_data([self.layer])
class TutorialViewerState(ViewerState): x_att = SelectionCallbackProperty(docstring='The attribute to use on the x-axis') y_att = SelectionCallbackProperty(docstring='The attribute to use on the y-axis') def __init__(self, *args, **kwargs): super(TutorialViewerState, self).__init__(*args, **kwargs) self._x_att_helper = ComponentIDComboHelper(self, 'x_att') self._y_att_helper = ComponentIDComboHelper(self, 'y_att') self.add_callback('layers', self._on_layers_change) def _on_layers_change(self, value): # self.layers_data is a shortcut for # [layer_state.layer for layer_state in self.layers] self._x_att_helper.set_multiple_data(self.layers_data) self._y_att_helper.set_multiple_data(self.layers_data)
def __init__(self, viewer_state=None, **kwargs): super(SpecvizLayerState, self).__init__(viewer_state=viewer_state, **kwargs) self.color = self.layer.style.color self.alpha = self.layer.style.alpha self._sync_color = keep_in_sync(self, 'color', self.layer.style, 'color') self._sync_alpha = keep_in_sync(self, 'alpha', self.layer.style, 'alpha') self._att_helper = ComponentIDComboHelper(self, 'attribute') self.add_callback('layer', self._on_layer_change) self._on_layer_change() SpecvizLayerState.statistic.set_choices(self, list(FUNCTIONS)) SpecvizLayerState.statistic.set_display_func(self, FUNCTIONS.get)
def __init__(self, layer=None, **kwargs): super(WWTImageLayerState, self).__init__(layer=layer) self._sync_color = keep_in_sync(self, 'color', self.layer.style, 'color') self._sync_alpha = keep_in_sync(self, 'alpha', self.layer.style, 'alpha') self.color = self.layer.style.color self.img_data_att_helper = ComponentIDComboHelper(self, 'img_data_att', numeric=True, categorical=False) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() self.update_from_dict(kwargs)
def __init__(self, layer=None, **kwargs): self._sync_markersize = None super(WWTLayerState, self).__init__(layer=layer) self._sync_color = keep_in_sync(self, 'color', self.layer.style, 'color') self._sync_alpha = keep_in_sync(self, 'alpha', self.layer.style, 'alpha') self._sync_size = keep_in_sync(self, 'size', self.layer.style, 'markersize') self.color = self.layer.style.color self.size = self.layer.style.markersize self.alpha = self.layer.style.alpha self.size_att_helper = ComponentIDComboHelper(self, 'size_att', numeric=True, categorical=False) self.cmap_att_helper = ComponentIDComboHelper(self, 'cmap_att', numeric=True, categorical=False) self.size_lim_helper = StateAttributeLimitsHelper(self, attribute='size_att', lower='size_vmin', upper='size_vmax', cache=self.size_limits_cache) self.cmap_lim_helper = StateAttributeLimitsHelper(self, attribute='cmap_att', lower='cmap_vmin', upper='cmap_vmax', cache=self.cmap_limits_cache) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() self.cmap = colormaps.members[0][1] # Color and size encoding depending on attributes is only available # in PyWWT 0.6 or later. if PYWWT_LT_06: modes = ['Fixed'] else: modes = ['Fixed', 'Linear'] WWTLayerState.color_mode.set_choices(self, modes) WWTLayerState.size_mode.set_choices(self, modes) self.update_from_dict(kwargs)
def __init__(self, **kwargs): super(DendrogramViewerState, self).__init__() self.add_callback('layers', self._layers_changed) self.height_att_helper = ComponentIDComboHelper(self, 'height_att') self.parent_att_helper = ComponentIDComboHelper(self, 'parent_att') self.order_att_helper = ComponentIDComboHelper(self, 'order_att') self.add_callback('height_att', self._update_layout) self.add_callback('parent_att', self._update_layout) self.add_callback('order_att', self._update_layout) self.add_callback('reference_data', self._on_reference_data_change) self.update_from_dict(kwargs)
def __init__(self, *args, **kwargs): super(CustomMatplotlibViewerState, self).__init__(*args) self._cid_helpers = [] for name, property in self.iter_callback_properties(): if isinstance(property, DynamicComponentIDProperty): self._cid_helpers.append(ComponentIDComboHelper(self, name)) self.add_callback('layers', self._on_layer_change) self.update_from_dict(kwargs)
def test_component_default_index(app, dataxz, dataxyz): # Regression test for a bug that caused the incorrect element to be selected # when default_index is involved. # setup state = DummyState() helper = ComponentIDComboHelper(state, 'y_att', app.data_collection) state.helper = helper dropdown = LinkedDropdown(state, 'y_att', 'y test attribute') assert [item[0] for item in dropdown.options] == [] helper.append_data(dataxz) assert [item[0] for item in dropdown.options] == ['x', 'z'] assert state.y_att is dataxz.id['z'] assert dropdown.value is dataxz.id['z']
def __init__(self, **kwargs): super(ImageViewerState, self).__init__() self.limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper(self, attribute='x_att', lower='x_min', upper='x_max', limits_cache=self.limits_cache) self.y_lim_helper = StateAttributeLimitsHelper(self, attribute='y_att', lower='y_min', upper='y_max', limits_cache=self.limits_cache) self.ref_data_helper = ManualDataComboHelper(self, 'reference_data') self.xw_att_helper = ComponentIDComboHelper(self, 'x_att_world', numeric=False, categorical=False, world_coord=True) self.yw_att_helper = ComponentIDComboHelper(self, 'y_att_world', numeric=False, categorical=False, world_coord=True) self.add_callback('reference_data', self._reference_data_changed, priority=1000) self.add_callback('layers', self._layers_changed, priority=1000) self.add_callback('x_att', self._on_xatt_change, priority=500) self.add_callback('y_att', self._on_yatt_change, priority=500) self.add_callback('x_att_world', self._update_att, priority=500) self.add_callback('y_att_world', self._update_att, priority=500) self.add_callback('x_att_world', self._on_xatt_world_change, priority=1000) self.add_callback('y_att_world', self._on_yatt_world_change, priority=1000) self.update_from_dict(kwargs)
def __init__(self, **kwargs): super(HistogramViewerState, self).__init__() self.hist_helper = StateAttributeHistogramHelper(self, 'x_att', lower='hist_x_min', upper='hist_x_max', n_bin='hist_n_bin', common_n_bin='common_n_bin') self.x_lim_helper = StateAttributeLimitsHelper(self, 'x_att', lower='x_min', upper='x_max', log='x_log') self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att') self.update_from_dict(kwargs)
def __init__(self, **kwargs): super(ProfileViewerState, self).__init__() self.ref_data_helper = ManualDataComboHelper(self, 'reference_data') self.add_callback('layers', self._layers_changed) self.add_callback('reference_data', self._reference_data_changed) self.add_callback('x_att', self._update_att) self.add_callback('normalize', self._reset_y_limits) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', numeric=False, categorical=False, pixel_coord=True) ProfileViewerState.function.set_choices(self, list(FUNCTIONS)) ProfileViewerState.function.set_display_func(self, FUNCTIONS.get) self.update_from_dict(kwargs)
def __init__(self, layer=None, viewer_state=None, **kwargs): self.uuid = str(uuid.uuid4()) super(ImageLayerState, self).__init__(layer=layer, viewer_state=viewer_state) self.attribute_lim_helper = StateAttributeLimitsHelper(self, attribute='attribute', percentile='percentile', lower='v_min', upper='v_max') self.attribute_att_helper = ComponentIDComboHelper(self, 'attribute', numeric=True, categorical=False) percentile_display = {100: 'Min/Max', 99.5: '99.5%', 99: '99%', 95: '95%', 90: '90%', 'Custom': 'Custom'} ImageLayerState.percentile.set_choices(self, [100, 99.5, 99, 95, 90, 'Custom']) ImageLayerState.percentile.set_display_func(self, percentile_display.get) stretch_display = {'linear': 'Linear', 'sqrt': 'Square Root', 'arcsinh': 'Arcsinh', 'log': 'Logarithmic'} ImageLayerState.stretch.set_choices(self, ['linear', 'sqrt', 'arcsinh', 'log']) ImageLayerState.stretch.set_display_func(self, stretch_display.get) self.add_callback('global_sync', self._update_syncing) self.add_callback('layer', self._update_attribute) self._update_syncing() if layer is not None: self._update_attribute() self.update_from_dict(kwargs) if self.cmap is None: self.cmap = colormaps.members[0][1]
def __init__(self, layer=None, viewer_state=None, **kwargs): super(ProfileLayerState, self).__init__(layer=layer, viewer_state=viewer_state) self.attribute_att_helper = ComponentIDComboHelper(self, 'attribute', numeric=True, categorical=False) percentile_display = {100: 'Min/Max', 99.5: '99.5%', 99: '99%', 95: '95%', 90: '90%', 'Custom': 'Custom'} ProfileLayerState.percentile.set_choices(self, [100, 99.5, 99, 95, 90, 'Custom']) ProfileLayerState.percentile.set_display_func(self, percentile_display.get) self.add_callback('layer', self._update_attribute, priority=1000) if layer is not None: self._update_attribute() self.update_from_dict(kwargs)
def __init__(self, **kwargs): super(HistogramViewerState, self).__init__() self.hist_helper = StateAttributeHistogramHelper(self, 'x_att', lower='hist_x_min', upper='hist_x_max', n_bin='hist_n_bin', common_n_bin='common_n_bin') self.x_lim_helper = StateAttributeLimitsHelper(self, 'x_att', lower='x_min', upper='x_max', log='x_log') self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', pixel_coord=True, world_coord=True) self.update_from_dict(kwargs) # This should be added after update_from_dict since we don't want to # influence the restoring of sessions. self.add_callback('hist_x_min', self.update_view_to_bins) self.add_callback('hist_x_max', self.update_view_to_bins) self.add_callback('x_log', self._reset_x_limits, priority=1000)
class Vispy3DScatterViewerState(Vispy3DViewerState): def __init__(self, **kwargs): super(Vispy3DScatterViewerState, self).__init__() self.x_att_helper = ComponentIDComboHelper(self, 'x_att', categorical=False) self.y_att_helper = ComponentIDComboHelper(self, 'y_att', categorical=False) self.z_att_helper = ComponentIDComboHelper(self, 'z_att', categorical=False) self.add_callback('layers', self._on_layers_change) self.update_from_dict(kwargs) def _on_layers_change(self, *args): layers_data = [layer_state.layer for layer_state in self.layers] self.x_att_helper.set_multiple_data(layers_data) self.y_att_helper.set_multiple_data(layers_data) self.z_att_helper.set_multiple_data(layers_data)
class DendrogramViewerState(MatplotlibDataViewerState): """ A state class that includes all the attributes for a dendrogram viewer. """ height_att = DDSCProperty() parent_att = DDSCProperty() order_att = DDSCProperty() y_log = DDCProperty(False) select_substruct = DDCProperty(True) reference_data = DDCProperty() _layout = DDCProperty() def __init__(self, **kwargs): super(DendrogramViewerState, self).__init__() self.add_callback('layers', self._layers_changed) self.height_att_helper = ComponentIDComboHelper(self, 'height_att') self.parent_att_helper = ComponentIDComboHelper(self, 'parent_att') self.order_att_helper = ComponentIDComboHelper(self, 'order_att') self.add_callback('height_att', self._update_layout) self.add_callback('parent_att', self._update_layout) self.add_callback('order_att', self._update_layout) self.add_callback('reference_data', self._on_reference_data_change) self.update_from_dict(kwargs) def _on_reference_data_change(self, data): if self.reference_data is None: return self.height_att = self.reference_data.find_component_id('height') self.parent_att = self.reference_data.find_component_id('parent') self.order_att = self.height_att def _update_layout(self, att): if self.height_att is None or self.parent_att is None or self.order_att is None or self.reference_data is None: self._layout = None else: height = self.reference_data[self.height_att].ravel() parent = self.reference_data[self.parent_att].astype(int).ravel() order = self.reference_data[self.order_att].ravel() x, y = dendrogram_layout(parent, height, order) self._layout = Layout(x, y) def _layers_changed(self, *args): layers_data = self.layers_data layers_data_cache = getattr(self, '_layers_data_cache', []) if layers_data == layers_data_cache: return self.height_att_helper.set_multiple_data(layers_data) self.parent_att_helper.set_multiple_data(layers_data) self.order_att_helper.set_multiple_data(layers_data) for layer in layers_data: if isinstance(layer, Data): self.reference_data = layer break self._layers_data_cache = layers_data
def __init__(self, viewer_state=None, layer=None, **kwargs): super(ScatterLayerState, self).__init__(viewer_state=viewer_state, layer=layer) self.limits_cache = {} self.cmap_lim_helper = StateAttributeLimitsHelper(self, attribute='cmap_att', lower='cmap_vmin', upper='cmap_vmax', limits_cache=self.limits_cache) self.size_lim_helper = StateAttributeLimitsHelper(self, attribute='size_att', lower='size_vmin', upper='size_vmax', limits_cache=self.limits_cache) self.cmap_att_helper = ComponentIDComboHelper(self, 'cmap_att', numeric=True, categorical=False) self.size_att_helper = ComponentIDComboHelper(self, 'size_att', numeric=True, categorical=False) self.xerr_att_helper = ComponentIDComboHelper(self, 'xerr_att', numeric=True, categorical=False) self.yerr_att_helper = ComponentIDComboHelper(self, 'yerr_att', numeric=True, categorical=False) self.vx_att_helper = ComponentIDComboHelper(self, 'vx_att', numeric=True, categorical=False) self.vy_att_helper = ComponentIDComboHelper(self, 'vy_att', numeric=True, categorical=False) points_mode_display = {'auto': 'Density map or markers (auto)', 'markers': 'Markers', 'density': 'Density map'} ScatterLayerState.points_mode.set_choices(self, ['auto', 'markers', 'density']) ScatterLayerState.points_mode.set_display_func(self, points_mode_display.get) self.add_callback('points_mode', self._update_density_map_mode) ScatterLayerState.cmap_mode.set_choices(self, ['Fixed', 'Linear']) ScatterLayerState.size_mode.set_choices(self, ['Fixed', 'Linear']) linestyle_display = {'solid': '–––––––', 'dashed': '– – – – –', 'dotted': '· · · · · · · ·', 'dashdot': '– · – · – ·'} ScatterLayerState.linestyle.set_choices(self, ['solid', 'dashed', 'dotted', 'dashdot']) ScatterLayerState.linestyle.set_display_func(self, linestyle_display.get) ScatterLayerState.vector_mode.set_choices(self, ['Cartesian', 'Polar']) vector_origin_display = {'tail': 'Tail of vector', 'middle': 'Middle of vector', 'tip': 'Tip of vector'} ScatterLayerState.vector_origin.set_choices(self, ['tail', 'middle', 'tip']) ScatterLayerState.vector_origin.set_display_func(self, vector_origin_display.get) stretch_display = {'linear': 'Linear', 'sqrt': 'Square Root', 'arcsinh': 'Arcsinh', 'log': 'Logarithmic'} ScatterLayerState.stretch.set_choices(self, ['linear', 'sqrt', 'arcsinh', 'log']) ScatterLayerState.stretch.set_display_func(self, stretch_display.get) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() self.cmap = colormaps.members[0][1] self.size = self.layer.style.markersize self._sync_size = keep_in_sync(self, 'size', self.layer.style, 'markersize') self.update_from_dict(kwargs)
class ScatterViewerState(MatplotlibDataViewerState): """ A state class that includes all the attributes for a scatter viewer. """ x_att = DDSCProperty(docstring='The attribute to show on the x-axis', default_index=0) y_att = DDSCProperty(docstring='The attribute to show on the y-axis', default_index=1) dpi = DDCProperty(72, docstring='The resolution (in dots per inch) of density maps, if present') def __init__(self, **kwargs): super(ScatterViewerState, self).__init__() self.limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper(self, attribute='x_att', lower='x_min', upper='x_max', log='x_log', margin=0.05, limits_cache=self.limits_cache) self.y_lim_helper = StateAttributeLimitsHelper(self, attribute='y_att', lower='y_min', upper='y_max', log='y_log', margin=0.05, limits_cache=self.limits_cache) self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', pixel_coord=True, world_coord=True) self.y_att_helper = ComponentIDComboHelper(self, 'y_att', pixel_coord=True, world_coord=True) self.update_from_dict(kwargs) self.add_callback('x_log', self._reset_x_limits) self.add_callback('y_log', self._reset_y_limits) def _reset_x_limits(self, *args): self.x_lim_helper.percentile = 100 self.x_lim_helper.update_values(force=True) def _reset_y_limits(self, *args): self.y_lim_helper.percentile = 100 self.y_lim_helper.update_values(force=True) def reset_limits(self): self._reset_x_limits() self._reset_y_limits() def flip_x(self): """ Flip the x_min/x_max limits. """ self.x_lim_helper.flip_limits() def flip_y(self): """ Flip the y_min/y_max limits. """ self.y_lim_helper.flip_limits() @property def x_categories(self): return self._categories(self.x_att) @property def y_categories(self): return self._categories(self.y_att) def _categories(self, cid): categories = [] for layer_state in self.layers: if isinstance(layer_state.layer, BaseData): layer = layer_state.layer else: layer = layer_state.layer.data try: if layer.data.get_kind(cid) == 'categorical': categories.append(layer.data.get_data(cid).categories) except IncompatibleAttribute: pass if len(categories) == 0: return None else: return np.unique(np.hstack(categories)) @property def x_kinds(self): return self._component_kinds(self.x_att) @property def y_kinds(self): return self._component_kinds(self.y_att) def _component_kinds(self, cid): # Construct list of component kinds over all layers kinds = set() for layer_state in self.layers: if isinstance(layer_state.layer, BaseData): layer = layer_state.layer else: layer = layer_state.layer.data try: kinds.add(layer.data.get_kind(cid)) except IncompatibleAttribute: pass return kinds def _layers_changed(self, *args): layers_data = self.layers_data layers_data_cache = getattr(self, '_layers_data_cache', []) if layers_data == layers_data_cache: return self.x_att_helper.set_multiple_data(self.layers_data) self.y_att_helper.set_multiple_data(self.layers_data) self._layers_data_cache = layers_data
class ScatterLayerState(MatplotlibLayerState): """ A state class that includes all the attributes for layers in a scatter plot. """ # General properties style = DDSCProperty(docstring="The layer style") size = DDCProperty(docstring="The size of the markers") # Scatter layer cmap_mode = DDSCProperty(docstring="Whether to use color to encode an attribute") cmap_att = DDSCProperty(docstring="The attribute to use for the color") cmap_vmin = DDCProperty(docstring="The lower level for the colormap") cmap_vmax = DDCProperty(docstring="The upper level for the colormap") cmap = DDCProperty(docstring="The colormap to use (when in colormap mode)") size_mode = DDSCProperty(docstring="Whether to use size to encode an attribute") size_att = DDSCProperty(docstring="The attribute to use for the size") size_vmin = DDCProperty(docstring="The lower level for the size mapping") size_vmax = DDCProperty(docstring="The upper level for the size mapping") size_scaling = DDCProperty(1, docstring="Relative scaling of the size") xerr_visible = DDCProperty(False, docstring="Whether to show x error bars") yerr_visible = DDCProperty(False, docstring="Whether to show y error bars") xerr_att = DDSCProperty(docstring="The attribute to use for the x error bars") yerr_att = DDSCProperty(docstring="The attribute to use for the y error bars") # Line plot layer linewidth = DDCProperty(1, docstring="The line width") linestyle = DDSCProperty(docstring="The line style") def __init__(self, viewer_state=None, layer=None, **kwargs): super(ScatterLayerState, self).__init__(viewer_state=viewer_state, layer=layer) self.limits_cache = {} self.cmap_lim_helper = StateAttributeLimitsHelper(self, attribute='cmap_att', lower='cmap_vmin', upper='cmap_vmax', limits_cache=self.limits_cache) self.size_lim_helper = StateAttributeLimitsHelper(self, attribute='size_att', lower='size_vmin', upper='size_vmax', limits_cache=self.limits_cache) self.cmap_att_helper = ComponentIDComboHelper(self, 'cmap_att', numeric=True, categorical=False) self.size_att_helper = ComponentIDComboHelper(self, 'size_att', numeric=True, categorical=False) self.xerr_att_helper = ComponentIDComboHelper(self, 'xerr_att', numeric=True, categorical=False) self.yerr_att_helper = ComponentIDComboHelper(self, 'yerr_att', numeric=True, categorical=False) ScatterLayerState.style.set_choices(self, ['Scatter', 'Line']) ScatterLayerState.cmap_mode.set_choices(self, ['Fixed', 'Linear']) ScatterLayerState.size_mode.set_choices(self, ['Fixed', 'Linear']) linestyle_display = {'solid': '–––––––', 'dashed': '– – – – –', 'dotted': '· · · · · · · ·', 'dashdot': '– · – · – ·'} ScatterLayerState.linestyle.set_choices(self, ['solid', 'dashed', 'dotted', 'dashdot']) ScatterLayerState.linestyle.set_display_func(self, linestyle_display.get) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() self.cmap = colormaps.members[0][1] self.size = self.layer.style.markersize self._sync_size = keep_in_sync(self, 'size', self.layer.style, 'markersize') self.update_from_dict(kwargs) def _on_layer_change(self, layer=None): with delay_callback(self, 'cmap_vmin', 'cmap_vmax', 'size_vmin', 'size_vmax'): if self.layer is None: self.cmap_att_helper.set_multiple_data([]) self.size_att_helper.set_multiple_data([]) else: self.cmap_att_helper.set_multiple_data([self.layer]) self.size_att_helper.set_multiple_data([self.layer]) if self.layer is None: self.xerr_att_helper.set_multiple_data([]) self.yerr_att_helper.set_multiple_data([]) else: self.xerr_att_helper.set_multiple_data([self.layer]) self.yerr_att_helper.set_multiple_data([self.layer]) def flip_cmap(self): """ Flip the cmap_vmin/cmap_vmax limits. """ self.cmap_lim_helper.flip_limits() def flip_size(self): """ Flip the size_vmin/size_vmax limits. """ self.size_lim_helper.flip_limits()
class ScatterLayerState(MatplotlibLayerState): """ A state class that includes all the attributes for layers in a scatter plot. """ # Color cmap_mode = DDSCProperty(docstring="Whether to use color to encode an attribute") cmap_att = DDSCProperty(docstring="The attribute to use for the color") cmap_vmin = DDCProperty(docstring="The lower level for the colormap") cmap_vmax = DDCProperty(docstring="The upper level for the colormap") cmap = DDCProperty(docstring="The colormap to use (when in colormap mode)") # Points points_mode = DDSCProperty(docstring='Whether to use markers or a density map') # Markers markers_visible = DDCProperty(True, docstring="Whether to show markers") size = DDCProperty(docstring="The size of the markers") size_mode = DDSCProperty(docstring="Whether to use size to encode an attribute") size_att = DDSCProperty(docstring="The attribute to use for the size") size_vmin = DDCProperty(docstring="The lower level for the size mapping") size_vmax = DDCProperty(docstring="The upper level for the size mapping") size_scaling = DDCProperty(1, docstring="Relative scaling of the size") fill = DDCProperty(True, docstring="Whether to fill the markers") # Density map density_map = DDCProperty(False, docstring="Whether to show the points as a density map") stretch = DDSCProperty(default='log', docstring='The stretch used to render the layer, ' 'which should be one of ``linear``, ' '``sqrt``, ``log``, or ``arcsinh``') density_contrast = DDCProperty(1, docstring="The dynamic range of the density map") # Note that we keep the dpi in the viewer state since we want it to always # be in sync between layers. # Line line_visible = DDCProperty(False, docstring="Whether to show a line connecting all positions") linewidth = DDCProperty(1, docstring="The line width") linestyle = DDSCProperty(docstring="The line style") # Errorbars xerr_visible = DDCProperty(False, docstring="Whether to show x error bars") yerr_visible = DDCProperty(False, docstring="Whether to show y error bars") xerr_att = DDSCProperty(docstring="The attribute to use for the x error bars") yerr_att = DDSCProperty(docstring="The attribute to use for the y error bars") # Vectors vector_visible = DDCProperty(False, docstring="Whether to show vector plot") vx_att = DDSCProperty(docstring="The attribute to use for the x vector arrow") vy_att = DDSCProperty(docstring="The attribute to use for the y vector arrow") vector_arrowhead = DDCProperty(False, docstring="Whether to show vector arrow") vector_mode = DDSCProperty(default_index=0, docstring="Whether to plot the vectors in cartesian or polar mode") vector_origin = DDSCProperty(default_index=1, docstring="Whether to place the vector so that the origin is at the tail, middle, or tip") vector_scaling = DDCProperty(1, docstring="The relative scaling of the arrow length") def __init__(self, viewer_state=None, layer=None, **kwargs): super(ScatterLayerState, self).__init__(viewer_state=viewer_state, layer=layer) self.limits_cache = {} self.cmap_lim_helper = StateAttributeLimitsHelper(self, attribute='cmap_att', lower='cmap_vmin', upper='cmap_vmax', limits_cache=self.limits_cache) self.size_lim_helper = StateAttributeLimitsHelper(self, attribute='size_att', lower='size_vmin', upper='size_vmax', limits_cache=self.limits_cache) self.cmap_att_helper = ComponentIDComboHelper(self, 'cmap_att', numeric=True, categorical=False) self.size_att_helper = ComponentIDComboHelper(self, 'size_att', numeric=True, categorical=False) self.xerr_att_helper = ComponentIDComboHelper(self, 'xerr_att', numeric=True, categorical=False) self.yerr_att_helper = ComponentIDComboHelper(self, 'yerr_att', numeric=True, categorical=False) self.vx_att_helper = ComponentIDComboHelper(self, 'vx_att', numeric=True, categorical=False) self.vy_att_helper = ComponentIDComboHelper(self, 'vy_att', numeric=True, categorical=False) points_mode_display = {'auto': 'Density map or markers (auto)', 'markers': 'Markers', 'density': 'Density map'} ScatterLayerState.points_mode.set_choices(self, ['auto', 'markers', 'density']) ScatterLayerState.points_mode.set_display_func(self, points_mode_display.get) self.add_callback('points_mode', self._update_density_map_mode) ScatterLayerState.cmap_mode.set_choices(self, ['Fixed', 'Linear']) ScatterLayerState.size_mode.set_choices(self, ['Fixed', 'Linear']) linestyle_display = {'solid': '–––––––', 'dashed': '– – – – –', 'dotted': '· · · · · · · ·', 'dashdot': '– · – · – ·'} ScatterLayerState.linestyle.set_choices(self, ['solid', 'dashed', 'dotted', 'dashdot']) ScatterLayerState.linestyle.set_display_func(self, linestyle_display.get) ScatterLayerState.vector_mode.set_choices(self, ['Cartesian', 'Polar']) vector_origin_display = {'tail': 'Tail of vector', 'middle': 'Middle of vector', 'tip': 'Tip of vector'} ScatterLayerState.vector_origin.set_choices(self, ['tail', 'middle', 'tip']) ScatterLayerState.vector_origin.set_display_func(self, vector_origin_display.get) stretch_display = {'linear': 'Linear', 'sqrt': 'Square Root', 'arcsinh': 'Arcsinh', 'log': 'Logarithmic'} ScatterLayerState.stretch.set_choices(self, ['linear', 'sqrt', 'arcsinh', 'log']) ScatterLayerState.stretch.set_display_func(self, stretch_display.get) self.add_callback('layer', self._on_layer_change) if layer is not None: self._on_layer_change() self.cmap = colormaps.members[0][1] self.size = self.layer.style.markersize self._sync_size = keep_in_sync(self, 'size', self.layer.style, 'markersize') self.update_from_dict(kwargs) def _on_layer_change(self, layer=None): with delay_callback(self, 'cmap_vmin', 'cmap_vmax', 'size_vmin', 'size_vmax', 'density_map'): self._update_density_map_mode() if self.layer is None: self.cmap_att_helper.set_multiple_data([]) self.size_att_helper.set_multiple_data([]) else: self.cmap_att_helper.set_multiple_data([self.layer]) self.size_att_helper.set_multiple_data([self.layer]) if self.layer is None: self.xerr_att_helper.set_multiple_data([]) self.yerr_att_helper.set_multiple_data([]) else: self.xerr_att_helper.set_multiple_data([self.layer]) self.yerr_att_helper.set_multiple_data([self.layer]) if self.layer is None: self.vx_att_helper.set_multiple_data([]) self.vy_att_helper.set_multiple_data([]) else: self.vx_att_helper.set_multiple_data([self.layer]) self.vy_att_helper.set_multiple_data([self.layer]) def _update_density_map_mode(self, *args): if self.points_mode == 'auto': if self.layer.size > 100000: self.density_map = True else: self.density_map = False elif self.points_mode == 'density': self.density_map = True else: self.density_map = False def flip_cmap(self): """ Flip the cmap_vmin/cmap_vmax limits. """ self.cmap_lim_helper.flip_limits() def flip_size(self): """ Flip the size_vmin/size_vmax limits. """ self.size_lim_helper.flip_limits() @property def cmap_name(self): return colormaps.name_from_cmap(self.cmap) @classmethod def __setgluestate__(cls, rec, context): # Patch for glue files produced with glue v0.11 if 'style' in rec['values']: style = context.object(rec['values'].pop('style')) if style == 'Scatter': rec['values']['markers_visible'] = True rec['values']['line_visible'] = False elif style == 'Line': rec['values']['markers_visible'] = False rec['values']['line_visible'] = True return super(ScatterLayerState, cls).__setgluestate__(rec, context)
class ScatterViewerState(MatplotlibDataViewerState): """ A state class that includes all the attributes for a scatter viewer. """ x_att = DDSCProperty(docstring='The attribute to show on the x-axis', default_index=0) y_att = DDSCProperty(docstring='The attribute to show on the y-axis', default_index=1) def __init__(self, **kwargs): super(ScatterViewerState, self).__init__() self.limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper(self, attribute='x_att', lower='x_min', upper='x_max', log='x_log', limits_cache=self.limits_cache) self.y_lim_helper = StateAttributeLimitsHelper(self, attribute='y_att', lower='y_min', upper='y_max', log='y_log', limits_cache=self.limits_cache) self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att') self.y_att_helper = ComponentIDComboHelper(self, 'y_att') self.update_from_dict(kwargs) def _update_priority(self, name): if name == 'layers': return 2 elif name.endswith('_log'): return 0.5 elif name.endswith(('_min', '_max')): return 0 else: return 1 def flip_x(self): """ Flip the x_min/x_max limits. """ self.x_lim_helper.flip_limits() def flip_y(self): """ Flip the y_min/y_max limits. """ self.y_lim_helper.flip_limits() def _get_x_components(self): return self._get_components(self.x_att) def _get_y_components(self): return self._get_components(self.y_att) def _get_components(self, cid): # Construct list of components over all layers components = [] for layer_state in self.layers: if isinstance(layer_state.layer, Data): layer = layer_state.layer else: layer = layer_state.layer.data try: components.append(layer.data.get_component(cid)) except IncompatibleAttribute: pass return components def _layers_changed(self, *args): layers_data = self.layers_data layers_data_cache = getattr(self, '_layers_data_cache', []) if layers_data == layers_data_cache: return self.x_att_helper.set_multiple_data(self.layers_data) self.y_att_helper.set_multiple_data(self.layers_data) self._layers_data_cache = layers_data
class HistogramViewerState(MatplotlibDataViewerState): """ A state class that includes all the attributes for a histogram viewer. """ x_att = DDSCProperty(docstring='The attribute to compute the histograms for') cumulative = DDCProperty(False, docstring='Whether to show the histogram as ' 'a cumulative histogram') normalize = DDCProperty(False, docstring='Whether to normalize the histogram ' '(based on the total sum)') hist_x_min = DDCProperty(docstring='The minimum value used to compute the ' 'histogram') hist_x_max = DDCProperty(docstring='The maxumum value used to compute the ' 'histogram') hist_n_bin = DDCProperty(docstring='The number of bins in the histogram') common_n_bin = DDCProperty(True, docstring='The number of bins to use for ' 'all numerical components') def __init__(self, **kwargs): super(HistogramViewerState, self).__init__() self.hist_helper = StateAttributeHistogramHelper(self, 'x_att', lower='hist_x_min', upper='hist_x_max', n_bin='hist_n_bin', common_n_bin='common_n_bin') self.x_lim_helper = StateAttributeLimitsHelper(self, 'x_att', lower='x_min', upper='x_max', log='x_log') self.add_callback('layers', self._layers_changed) self.x_att_helper = ComponentIDComboHelper(self, 'x_att', pixel_coord=True, world_coord=True) self.update_from_dict(kwargs) # This should be added after update_from_dict since we don't want to # influence the restoring of sessions. self.add_callback('hist_x_min', self.update_view_to_bins) self.add_callback('hist_x_max', self.update_view_to_bins) self.add_callback('x_log', self._reset_x_limits, priority=1000) def _reset_x_limits(self, *args): with delay_callback(self, 'hist_x_min', 'hist_x_max', 'x_min', 'x_max', 'x_log'): self.x_lim_helper.percentile = 100 self.x_lim_helper.update_values(force=True) self.update_bins_to_view() def reset_limits(self): self._reset_x_limits() self.y_min = min(getattr(layer, '_y_min', np.inf) for layer in self.layers) self.y_max = max(getattr(layer, '_y_max', 0) for layer in self.layers) def _update_priority(self, name): if name == 'layers': return 2 elif name.endswith('_log'): return 0.5 elif name.endswith(('_min', '_max', '_bin')): return 0 else: return 1 def flip_x(self): """ Flip the x_min/x_max limits. """ self.x_lim_helper.flip_limits() @avoid_circular def update_bins_to_view(self, *args): """ Update the bins to match the current view. """ with delay_callback(self, 'hist_x_min', 'hist_x_max'): if self.x_max > self.x_min: self.hist_x_min = self.x_min self.hist_x_max = self.x_max else: self.hist_x_min = self.x_max self.hist_x_max = self.x_min @avoid_circular def update_view_to_bins(self, *args): """ Update the view to match the histogram interval """ with delay_callback(self, 'x_min', 'x_max'): self.x_min = self.hist_x_min self.x_max = self.hist_x_max def _get_x_components(self): if self.x_att is None: return [] # Construct list of components over all layers components = [] for layer_state in self.layers: if isinstance(layer_state.layer, Data): layer = layer_state.layer else: layer = layer_state.layer.data try: components.append(layer.get_component(self.x_att)) except IncompatibleAttribute: pass return components @property def bins(self): """ The position of the bins for the histogram based on the current state. """ if self.hist_x_min is None or self.hist_x_max is None or self.hist_n_bin is None: return None if self.x_log: return np.logspace(np.log10(self.hist_x_min), np.log10(self.hist_x_max), self.hist_n_bin + 1) elif isinstance(self.hist_x_min, np.datetime64): x_min = self.hist_x_min.astype(int) x_max = self.hist_x_max.astype(self.hist_x_min.dtype).astype(int) return np.linspace(x_min, x_max, self.hist_n_bin + 1).astype(self.hist_x_min.dtype) else: return np.linspace(self.hist_x_min, self.hist_x_max, self.hist_n_bin + 1) @defer_draw def _layers_changed(self, *args): self.x_att_helper.set_multiple_data(self.layers_data)