def _data_frame_view(self): """ Return the view used by the editor. """ return View( Item( self._target_name(self.name), id='tabular_editor', show_label=False, editor=TabularEditor( show_titles=self.factory.show_titles, editable=self.factory.editable, adapter=self.adapter, selected=self._target_name(self.factory.selected), selected_row=self._target_name(self.factory.selected_row), selectable=self.factory.selectable, activated=self._target_name(self.factory.selected), activated_row=self._target_name( self.factory.activated_row), # noqa clicked=self._target_name(self.factory.clicked), dclicked=self._target_name(self.factory.dclicked), right_clicked=self._target_name( self.factory.right_clicked), # noqa right_dclicked=self._target_name( self.factory.right_dclicked), # noqa column_clicked=self._target_name( self.factory.column_clicked), # noqa column_right_clicked=self._target_name( self.factory.column_right_clicked), # noqa operations=self.factory.operations, update=self._target_name(self.factory.update), refresh=self._target_name(self.factory.refresh), )), id='data_frame_editor', resizable=True)
class TableViewController(HasTraits): """Separate table view for the data set matrix.""" # table = Array() table = Property() rows = List() title_text = Str(u"Result table", auto_set=True) cp_clip = Button('Copy to clipboard') ad = ArrayAdapter() tab_ed = TabularEditor( adapter=ad, operations=[], editable=False, show_titles=True, # multi_select=True, ) trait_view = View( Group( Item( 'table', editor=tab_ed, show_label=False, style='readonly', ), Group( Item('cp_clip', show_label=False), orientation="horizontal", ), layout="normal", ), width=0.3, height=0.3, resizable=True, buttons=[OKButton], handler=TitleHandler(), ) def _get_table(self): return array(self.rows) def set_col_names(self, names): self.ad.columns = [] for i, name in enumerate(names): self.ad.columns.append((name, i)) def add_row(self, row_data, row_name): self.rows.append(row_data) def _cp_clip_changed(self, info): tf = tempfile.TemporaryFile() savetxt(tf, self.table, fmt='%.2f', delimiter='\t', newline='\n') tf.seek(0) txt_mat = tf.read() clipboard.data = txt_mat
def traits_view(self): nclasses = self.beta.shape[1] w_table = WIDTH_CELL * nclasses h_table = HEIGHT_CELL * nclasses w_view = min(MAX_WIDTH, w_table + W_MARGIN) h_view = min(MAX_HEIGHT, h_table + HEIGHT_CELL + H_MARGIN) view = View( VGroup( Label('Beta parameters (prior over pi):'), UItem('beta', editor=TabularEditor( adapter=Array2DAdapter(ncolumns=nclasses, format='%.4f', show_index=True, count_from_one=False), ), width = w_table, height = HEIGHT_CELL, padding = 10 ), Label('Alpha parameters (prior over theta):'), UItem('alpha', editor=TabularEditor( adapter=Array2DAdapter(ncolumns=nclasses, format='%.4f', show_index=True, count_from_one=False), ), width = w_table, height = h_table, padding = 10 ), ), width = w_view, height = h_view, scrollable = True, resizable = True, buttons = ['OK', 'Cancel'] ) return view
def traits_view(self): ncolumns = len(self.data[0]) w_table = min(WIDTH_CELL * ncolumns, MAX_WIDTH) w_view = min(w_table + W_MARGIN, MAX_WIDTH) return View(Group( Item('data', editor=TabularEditor(adapter=Array2DAdapter( ncolumns=ncolumns, format='%s', show_index=True)), show_label=False, width=w_table, padding=10), ), title='Annotations', width=w_view, height=800, resizable=True, buttons=OKCancelButtons)
def _data_frame_view(self): """ Return the view used by the editor. """ return View( Item( self._target_name(self.name), id="tabular_editor", show_label=False, editor=TabularEditor( show_titles=self.factory.show_titles, editable=self.factory.editable, adapter=self.adapter, selected=self._target_name(self.factory.selected), selected_row=self._target_name(self.factory.selected_row), selectable=self.factory.selectable, multi_select=self.factory.multi_select, activated=self._target_name(self.factory.activated), activated_row=self._target_name( self.factory.activated_row), # noqa clicked=self._target_name(self.factory.clicked), dclicked=self._target_name(self.factory.dclicked), scroll_to_row=self._target_name( self.factory.scroll_to_row), # noqa scroll_to_position_hint=( self.factory.scroll_to_position_hint), scroll_to_column=self._target_name( self.factory.scroll_to_column), # noqa right_clicked=self._target_name( self.factory.right_clicked), # noqa right_dclicked=self._target_name( self.factory.right_dclicked), # noqa column_clicked=self._target_name( self.factory.column_clicked), # noqa column_right_clicked=self._target_name( self.factory.column_right_clicked), # noqa operations=self.factory.operations, update=self._target_name(self.factory.update), refresh=self._target_name(self.factory.refresh), ), ), id="data_frame_editor", resizable=True, )
class ClusterEditor(HasTraits): # Options for track aggregation auto_aggregate = Bool(False) compute_prototypes = Bool(False) # XXX: this should be True for non-interactive use render_tracks = Bool(False) tracks_drawn = Bool(False) render_clusters = Bool(False) clusters_drawn = Bool(False) interactive = Bool(False) clusters = List(Instance(Cluster)) parameters = [] # Data track_sets = List(Instance(TrackDataset)) pre_filter_matrix = Array pre_filter_connections = Array post_filter_matrix = Array post_filter_connections = Array # 3d plotting scene3d = Instance(MlabSceneModel) # Collect labels? label_lookup = List() # static_color from browserbuilder subject_colors = List() # post-processing algorithm, defaults to "None" post_processor = Instance(AcrossSubjectPostproc, ()) filter_operation = DelegatesTo("post_processor") # local ui cluster_editor_group = cluster_editor_group # convenience for saving tracks or screenshots after a search save_name = Str def __init__(self, **traits): """ Creates a panel for editing cluster assignments. """ super(ClusterEditor, self).__init__(**traits) self.cluster_editor_group = cluster_editor_group @on_trait_change('+parameter') def aggregation_param_changed(self, obj, name, old, new): print name, "parameter on aggregator changed" if name in self.parameters and self.auto_aggregate: self.update_clusters() def _auto_aggregate_changed(self): print "+ automatic aggregation changed:", self.auto_aggregate if self.auto_aggregate: self.update_clusters() def set_track_sets(self, tsets): """ The entry point for streamline data to the aggregator. Some of the logic implemented here depends on whether a postprocessor will be used """ # Remove the old track_sets self.clear_tracks() # set the new track_sets self.track_sets = tsets if self.render_tracks: self.draw_tracks() def set_track_source(self, tsource): pass def _render_tracks_changed(self): """ When render_tracks gets changed, the render_tracks property on the items in ``track_source`` gets changed so that future subsets of them will inherit it. Additionally, the visibility of already-existing streamlines will get toggled. """ # Apply the new "render_tracks" to for tds in self.track_sets: tds.render_tracks = self.render_tracks print "+ render_tracks changed to", self.render_tracks #print "\t+ setting track_source's render_tracks attribute" #self.track_source.set_render_tracks(self.render_tracks) self.scene3d.disable_render = True # Tracks are drawn if self.tracks_drawn: print "\t+ toggling glyph visibility on previously drawn tracks" for tds in self.track_sets: tds.set_track_visibility(self.render_tracks) else: if self.render_tracks: self.draw_tracks() self.scene3d.disable_render = False print "+ Done." def _render_clusters_changed(self): print "+ render_clusters changed to", self.render_clusters self.scene3d.disable_render = True for tds in self.track_sets: tds.set_cluster_visibility(self.render_clusters) self.scene3d.disable_render = False #def update_clusters(self): #""" Creates new clusters when a aggregation parameter has been #changed. Will CREATE MayaVi objects if ``self.interactive`` #and ``self.render_clusters`` are true. #""" #print "+ Updating cluster assignments." #_clusters = [] #self.label_lookup = [] ## If interactive, convert the tds to something that can render ##if self.render_tracks: ## print "\t++ rendering tracks" ## self.draw_tracks() ## Update the clusters in each TrackDataset through self.aggregate() ## NOTE: Someday this could be parallelized #for tnum, tds in enumerate(self.track_sets): #print "\t+ cluster %d of %d" % (tnum+1, len(self.track_sets)) #clusts = self.aggregate(tds) #tds.set_clusters(clusts) #_clusters += tds.clusters # collect the colorized version #self.clusters = _clusters ## Take care of the graphics #if self.render_clusters: #print "\t++ rendering tracks" #self.draw_clusters() #print "+ Aggregation Complete" def update_clusters(self): """ Creates new clusters when a aggregation parameter has been changed. Will CREATE MayaVi objects if ``self.interactive`` and ``self.render_clusters`` are true. """ print "+ Updating cluster assignments." _clusters = [] self.label_lookup = [] for tnum, tds in enumerate(self.track_sets): labels = self.aggregate(tds) tds.labels = labels clusts = [] # ----------------------------------------------------- # Convert the labels array to a list of Cluster objects labels, occurrences = np.unique(labels, return_inverse=True) for labelnum, label in enumerate(labels): indices = np.flatnonzero(occurrences == labelnum) if self.compute_prototypes: prototype = self.make_prototype(tds.tracks[indices]) else: prototype = None clusts.append( Cluster(ntracks=len(indices), id_number=label, indices=indices, scan_id=tds.scan_id)) # This grabs the colors from the rendered streamlines tds.set_clusters(clusts) _clusters += tds.clusters # collect the colorized version self.clusters = _clusters # Take care of the graphics if self.render_clusters: print "\t++ rendering tracks" self.draw_clusters() print "+ Aggregation Complete" ## Must be overwritten by a subclass def aggregate(self, track_datasets): raise NotImplementedError() def make_prototype(self, tracks): raise NotImplementedError() def draw_tracks(self): print "+ called draw_tracks" self.scene3d.disable_render = True if not self.render_tracks: return if self.tracks_drawn: print "\t+ Clearing pre-existing tracks" self.clear_tracks() print "\t+ Drawing the tracks" for tds in self.track_sets: tds.draw_tracks() self.tracks_drawn = True self.scene3d.disable_render = False print "++ Done" def add_tracks(self, tds): """ Adds a TrackDataset to the working set """ tds.render_tracks = self.render_tracks self.track_sets.append(tds) if self.render_tracks: self.scene3d.disable_render = True self.track_sets[-1].draw_tracks() self.tracks_drawn = True self.scene3d.disable_render = False if self.render_clusters: self.scene3d.disable_render = True self.track_sets[-1].draw_clusters() self.tracks_drawn = True self.scene3d.disable_render = False def draw_clusters(self): if not self.render_clusters: return print "+ Drawing Clusters" self.scene3d.disable_render = True for tds in self.track_sets: tds.draw_clusters() self.scene3d.disable_render = False def clear_tracks(self): for trk in self.track_sets: trk.remove_glyphs() self.tracks_drawn = False def clear_clusters(self): pass cluster_table = TabularEditor(adapter=ClusterAdapter(), editable=False) cluster_rows = Group(Item(name='clusters', editor=cluster_table, height=400, show_label=False), label="Aggregation Options", show_border=True) algorithm_widgets = Group() def default_traits_view(self): return View(self.cluster_editor_group) browser_view = View( HSplit(Item('scene3d', editor=SceneEditor(scene_class=Scene), height=500, width=500), cluster_editor_group, show_labels=False), )
class AlgorithmParameterHandler(Handler): def setattr(self, info, obj, name, value): print "name:", name print "value:", value cluster_editor_group = \ VGroup( Group( Item("auto_aggregate"), #Item("render_clusters"), Item("render_tracks"), ), Include("algorithm_widgets"), Group(Item("clusters", name='clusters', editor=TabularEditor( adapter=ClusterAdapter(), editable=False), height=400, width=200, show_label=False), label="Clusters/Segments List", show_border=True) ) class ClusterEditor(HasTraits): # Options for track aggregation auto_aggregate = Bool(False) compute_prototypes = Bool(False) # XXX: this should be True for non-interactive use render_tracks = Bool(False) tracks_drawn = Bool(False) render_clusters = Bool(False)
class TableViewController(ModelView): """Separate table view for the data set matrix.""" table = Array() cp_clip = Button('Copy to clipboard') ad = ArrayAdapter() tab_ed = TabularEditor( adapter=ad, operations=[], editable=False, show_titles=True, # multi_select=True, ) trait_view = View( Group( Item( 'table', editor=tab_ed, show_label=False, style='readonly', ), Group( Item('cp_clip', show_label=False), orientation="horizontal", ), layout="normal", ), title='Data set matrix', width=0.3, height=0.3, resizable=True, buttons=[OKButton], ) def init_info(self, info): la = [] self.ad.index = self.model.data.list_data() self.ad.index.sort() for name in self.ad.index: # FIXME: Lot of hack here # needs redesign if name in [ 'ell_full_x', 'ell_full_y', 'ell_half_x', 'ell_half_y', 'pcy1', 'pcy2' ]: continue vec = self.model.data.get_data(name) la.append(vec) self.table = array(la) rows, cols = self.table.shape # self.ad.columns = [('i', 'index')] self.ad.columns = [] for i in range(cols): self.ad.columns.append((str(i), i)) # self.ad.columns=[('en', 0), ('to', 1), ('tre', 2)] def object_cp_clip_changed(self, info): tf = tempfile.TemporaryFile() savetxt(tf, self.table, fmt='%.2f', delimiter='\t', newline='\n') tf.seek(0) txt_mat = tf.read() clipboard.data = txt_mat
class SegmentationEditor(ClusterEditor): # Options for track aggregation render_segments = Bool(False) segments_drawn = Bool(False) segments = List(Instance(Segment)) parameters = [] # Collect labels? segment_lookup = List() # local ui segment_editor_group = segment_editor_group def __init__(self, **traits): """ Creates a panel for editing cluster assignments. """ super(SegmentationEditor, self).__init__(**traits) self.segment_editor_group = segment_editor_group def update_clusters(self): self.update_segments() def _auto_aggregate_changed(self): print "+ automatic aggregation changed:", self.auto_aggregate if self.auto_aggregate: self.update_segments() @on_trait_change('+parameter') def aggregation_param_changed(self, obj, name, old, new): print name, "parameter on aggregator changed" if name in self.parameters and self.auto_aggregate: self.update_segments() def _render_segments_changed(self): print "+ render_segments changed to", self.render_segments self.scene3d.disable_render = True for tds in self.track_sets: tds.set_segment_visibility(self.render_segments) self.scene3d.disable_render = False def update_segments(self): """ Creates new segments when a aggregation parameter has been changed. Will CREATE MayaVi objects if ``self.interactive`` and ``self.render_segments`` are true. """ print "+ Updating segment assignments." _segments = [] for tnum, tds in enumerate(self.track_sets): __segments = [] segments = self.segment(tds) number_of_labeled_segments, indices = summarize_segments(segments) labels = sorted(number_of_labeled_segments.keys()) for labelnum, label in enumerate(labels): __segments.append( Segment(ntracks=len(indices[label]), segment_id=label, indices=np.array(indices[label]), scan_id=tds.scan_id, segments=segments, ncoords=number_of_labeled_segments[label])) # This grabs the colors from the rendered streamlines tds.set_segments(__segments) _segments += tds.segments # collect the colorized version self.segments = _segments # Take care of the graphics if self.render_segments: print "\t++ rendering tracks" self.draw_segments() print "+ Aggregation Complete" ## Must be overwritten by a subclass def segment(self, track_datasets): raise NotImplementedError() def draw_segments(self): if not self.render_segments: return print "+ Drawing Segments" self.scene3d.disable_render = True for tds in self.track_sets: tds.draw_segments() self.scene3d.disable_render = False segment_table = TabularEditor(adapter=SegmentAdapter(), editable=False) segment_rows = Group(Item(name='segments', editor=segment_table, height=400, show_label=False), label="Segmentation Options", show_border=True) algorithm_widgets = Group() def default_traits_view(self): return View(self.segment_editor_group) browser_view = View( HSplit(Item('scene3d', editor=SceneEditor(scene_class=Scene), height=500, width=500), segment_editor_group, show_labels=False), )
return col from ..cluster_ui import ClusterEditor, ClusterAdapter segment_editor_group = \ VGroup( Group( Item("auto_aggregate"), Item("render_segments"), Item("render_tracks"), ), Include("algorithm_widgets"), Group(Item("segments", name='segments', editor=TabularEditor( adapter=SegmentAdapter(), editable=False), height=400, width=200, show_label=False), label="Segmentation Options", show_border=True) ) class SegmentationEditor(ClusterEditor): # Options for track aggregation render_segments = Bool(False) segments_drawn = Bool(False) segments = List(Instance(Segment)) parameters = [] # Collect labels?
def default_traits_view(self): """ The editable traits in the algorithm_widgets will depend on the atlas and therefore we must build the ui on the fly """ if self.track_source is None: raise ValueError("Must have a track_source set to determine traits") self.possible_atlases = self.atlas_parameters.keys() groups = [] # Loop over the available atlases for atlas_name in self.possible_atlases: # define a trait for each editable parameter group_items = [] for editable_param, possible_values in \ self.atlas_parameters[atlas_name].iteritems(): # use a modified version of the param name as a new trait new_trait_name = atlas_name + "_" + editable_param self.parameters.append(new_trait_name) self.add_trait(new_trait_name,Enum(possible_values,parameter=True)) setattr(self,new_trait_name,possible_values[0]) #self.on_trait_change(new_trait_name,self.atlas_parameter_trait_changed) group_items.append( Item(new_trait_name, label=editable_param) ) groups.append( Group( *tuple(group_items), visible_when="atlas_name=='%s'" % atlas_name, show_border=True, label=atlas_name+" parameters") ) # widgets for editing algorithm parameters traits_view = View( # All cluster editors have these Group( Group( Item("auto_aggregate"), Item("render_clusters"), Item("render_tracks"), ), Group( Item(name="min_tracks", editor=RangeEditor(mode="slider", high = 100,low = 0,format = "%i")), # Specific to the dynamic atlas class Item("atlas_name", editor= EnumEditor(name="possible_atlases")), Group(*tuple(groups)), # Actions for altering internals Group( Item("b_plot_connection_vector"), Item("b_query_region_pairs"), Item("b_change_postproc"), show_labels=False,orientation="horizontal"), ), Group( Item("clusters", name='clusters', editor=TabularEditor( adapter=ClusterAdapter(), editable=False), height=400, width=200, show_label=False), label="Aggregation Options", show_border=True) ) ) return traits_view