def apply_roi(self, roi): if not isinstance(roi, PointROI): raise NotImplementedError("Only PointROI supported") if self._layout is None or self.display_data is None: return x, y = roi.x, roi.y if not roi.defined(): return xs, ys = self._layout[:, ::3] parent_ys = self._layout[1, 1::3] delt = np.abs(x - xs) delt[y > ys] = np.nan delt[y < parent_ys] = np.nan if np.isfinite(delt).any(): select = np.nanargmin(delt) if self.select_substruct: select = self._substructures(select) select = np.asarray(select, dtype=np.int) else: select = np.array([], dtype=np.int) state = CategorySubsetState(self.display_data.pixel_component_ids[0], select) EditSubsetMode().update(self.collect, state, focus_data=self.display_data)
def update_selection(selection, viewer): """Called after an ImageJ selection keypress. Assumes first dataset contains the 'x' and 'y' components. Builds a Glue SubsetState and applies it using Glue's current settings. Selection consists of (xmin, xmax, ymin, ymax) """ xmin, xmax, ymin, ymax = selection roi = RectangularROI(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) xatt = lasagna.config.app.data_collection[0].data.find_component_id('x') yatt = lasagna.config.app.data_collection[0].data.find_component_id('y') xy_state = RoiSubsetState(xatt=xatt, yatt=yatt, roi=roi) file_state = CategorySubsetState(viewer.source_id, viewer.source_val) # selection only applies to data in displayed file subset_state = AndState(xy_state, file_state) data_collection = lasagna.config.app.data_collection # if no subset groups selected, make a new one layers = (lasagna.config.app._layer_widget.selected_layers()) subset_groups = [s for s in layers if isinstance(s, SubsetGroup)] if len(subset_groups) == 0: global fiji_label new_label = 'Fiji %d' % fiji_label fiji_label += 1 data_collection.new_subset_group(label=new_label, subset_state=subset_state) else: edit_mode = EditSubsetMode() edit_mode.update(data_collection, subset_state)
def apply_roi(self, roi, override_mode=None): # Force redraw to get rid of ROI. We do this because applying the # subset state below might end up not having an effect on the viewer, # for example there may not be any layers, or the active subset may not # be one of the layers. So we just explicitly redraw here to make sure # a redraw will happen after this method is called. self.redraw() # TODO Does subset get applied to all data or just visible data? if self.state._layout is None: return if not roi.defined(): return if len(self.layers) == 0: return if isinstance(roi, PointROI): x, y = roi.x, roi.y xs, ys = self.state._layout.xy parent_ys = ys[1::3] xs, ys = xs[::3], ys[::3] delt = np.abs(x - xs) delt[y > ys] = np.nan delt[y < parent_ys] = np.nan if np.isfinite(delt).any(): select = np.nanargmin(delt) if self.state.select_substruct: parent = self.state.reference_data[self.state.parent_att] select = _substructures(parent, select) select = np.asarray(select, dtype=np.int) else: select = np.array([], dtype=np.int) subset_state = CategorySubsetState( self.state.reference_data.pixel_component_ids[0], select) self.apply_subset_state(subset_state) else: raise TypeError("Only PointROI selections are supported")
def apply_roi(self, roi, use_current=False): # TODO Does subset get applied to all data or just visible data? if self.state._layout is None: return if not roi.defined(): return if len(self.layers) == 0: # Force redraw to get rid of ROI return self.redraw() if isinstance(roi, PointROI): x, y = roi.x, roi.y xs, ys = self.state._layout.xy parent_ys = ys[1::3] xs, ys = xs[::3], ys[::3] delt = np.abs(x - xs) delt[y > ys] = np.nan delt[y < parent_ys] = np.nan if np.isfinite(delt).any(): select = np.nanargmin(delt) if self.state.select_substruct: parent = self.state.reference_data[self.state.parent_att] select = _substructures(parent, select) select = np.asarray(select, dtype=np.int) else: select = np.array([], dtype=np.int) subset_state = CategorySubsetState( self.state.reference_data.pixel_component_ids[0], select) self.apply_subset_state(subset_state) else: raise TypeError("Only PointROI selections are supported")
def apply_roi(self, roi, override_mode=None): # Force redraw to get rid of ROI. We do this because applying the # subset state below might end up not having an effect on the viewer, # for example there may not be any layers, or the active subset may not # be one of the layers. So we just explicitly redraw here to make sure # a redraw will happen after this method is called. self.redraw() # TODO Does subset get applied to all data or just visible data? # if self.state._layout is None: # return if self.state.x_att is None or self.state.y_att is None: return if not roi.defined(): return if len(self.layers) == 0: return if isinstance(roi, PointROI): x, y = roi.x, roi.y # calculate everything parent = self.state.layers_data[0][self.state.x_att] ys = self.state.layers_data[0][self.state.y_att] leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = ys[parent] parent_ys[0] = ys[0] # sort everything sort_by_array = self.state.layers_data[0][self.state.sort_by] parent, ys, iter_array_updated = sort1Darrays( parent, ys, sort_by_array) leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = parent_ys[np.asarray(iter_array_updated, dtype=np.int)] orientation = self.state.orientation if orientation in ['bottom-up', 'top-down']: delt = np.abs(x - xs) delt[y > ys] = np.nan delt[y < parent_ys] = np.nan elif orientation in ['left-right', 'right-left']: delt = np.abs(y - xs) delt[x > ys] = np.nan delt[x < parent_ys] = np.nan if np.isfinite(delt).any(): select = np.nanargmin(delt) if self.state.select_substruct: #leafness = calculate_leafness(parent) subtree = calculate_subtree(parent, leafness) select = np.concatenate([[int(select)], np.asarray(subtree[select], dtype=np.int)]) select = np.asarray(select, dtype=np.int) else: select = np.array([], dtype=np.int) select = iter_array_updated[select] subset_state = CategorySubsetState( self.state.layers_data[0].components[0], select) self.apply_subset_state(subset_state) elif isinstance(roi, RectangularROI): xmin, xmax = roi.xmin, roi.xmax ymin, ymax = roi.ymin, roi.ymax # calculate everything parent = self.state.layers_data[0][self.state.x_att] ys = self.state.layers_data[0][self.state.y_att] leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = ys[parent] parent_ys[0] = ys[0] # sort everything sort_by_array = self.state.layers_data[0][self.state.sort_by] parent, ys, iter_array_updated = sort1Darrays( parent, ys, sort_by_array) leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = parent_ys[np.asarray(iter_array_updated, dtype=np.int)] orientation = self.state.orientation if orientation in ['bottom-up', 'top-down']: delt = np.arange(len(xs), dtype=np.float) delt[xs < xmin] = np.nan delt[xs > xmax] = np.nan delt[ymin > ys] = np.nan delt[ymax < parent_ys] = np.nan elif orientation in ['left-right', 'right-left']: delt = np.arange(len(xs), dtype=np.float) delt[xs < ymin] = np.nan delt[xs > ymax] = np.nan delt[xmin > ys] = np.nan delt[xmax < parent_ys] = np.nan if np.isfinite(delt).any(): select = np.where(np.isfinite(delt))[0] if self.state.select_substruct: subtree = calculate_subtree(parent, leafness) for sl in select: select = np.concatenate( [select, np.asarray(subtree[sl], dtype=np.int)]) select = np.asarray(select, dtype=np.int) else: select = np.array([], dtype=np.int) select = iter_array_updated[select] subset_state = CategorySubsetState( self.state.layers_data[0].components[0], select) self.apply_subset_state(subset_state) elif isinstance(roi, XRangeROI): xmin, xmax = roi.min, roi.max # calculate everything parent = self.state.layers_data[0][self.state.x_att] ys = self.state.layers_data[0][self.state.y_att] leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = ys[parent] parent_ys[0] = ys[0] # sort everything sort_by_array = self.state.layers_data[0][self.state.sort_by] parent, ys, iter_array_updated = sort1Darrays( parent, ys, sort_by_array) leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = parent_ys[np.asarray(iter_array_updated, dtype=np.int)] orientation = self.state.orientation if orientation in ['bottom-up', 'top-down']: delt = np.arange(len(xs), dtype=np.float) delt[xs < xmin] = np.nan delt[xs > xmax] = np.nan elif orientation in ['left-right', 'right-left']: delt = np.arange(len(xs), dtype=np.float) delt[xmin > ys] = np.nan delt[xmax < parent_ys] = np.nan if np.isfinite(delt).any(): select = np.where(np.isfinite(delt))[0] if self.state.select_substruct: subtree = calculate_subtree(parent, leafness) for sl in select: select = np.concatenate( [select, np.asarray(subtree[sl], dtype=np.int)]) select = np.asarray(select, dtype=np.int) else: select = np.array([], dtype=np.int) select = iter_array_updated[select] subset_state = CategorySubsetState( self.state.layers_data[0].components[0], select) self.apply_subset_state(subset_state) elif isinstance(roi, YRangeROI): ymin, ymax = roi.min, roi.max # calculate everything parent = self.state.layers_data[0][self.state.x_att] ys = self.state.layers_data[0][self.state.y_att] leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = ys[parent] parent_ys[0] = ys[0] # sort everything sort_by_array = self.state.layers_data[0][self.state.sort_by] parent, ys, iter_array_updated = sort1Darrays( parent, ys, sort_by_array) leafness = calculate_leafness(parent) children = calculate_children(parent, leafness) xs = calculate_xpos(parent, leafness, children) parent_ys = parent_ys[np.asarray(iter_array_updated, dtype=np.int)] orientation = self.state.orientation if orientation in ['bottom-up', 'top-down']: delt = np.arange(len(xs), dtype=np.float) delt[ymin > ys] = np.nan delt[ymax < parent_ys] = np.nan elif orientation in ['left-right', 'right-left']: delt = np.arange(len(xs), dtype=np.float) delt[xs < ymin] = np.nan delt[xs > ymax] = np.nan if np.isfinite(delt).any(): select = np.where(np.isfinite(delt))[0] if self.state.select_substruct: subtree = calculate_subtree(parent, leafness) for sl in select: select = np.concatenate( [select, np.asarray(subtree[sl], dtype=np.int)]) select = np.asarray(select, dtype=np.int) else: select = np.array([], dtype=np.int) select = iter_array_updated[select] subset_state = CategorySubsetState( self.state.layers_data[0].components[0], select) self.apply_subset_state(subset_state) else: raise TypeError("Only PointROI selections are supported")