def new_subset(self, subset=None, color=None, label=None, **kwargs): """ Create a new subset, and attach to self. .. note:: The preferred way for creating subsets is via :meth:`~glue.core.data_collection.DataCollection.new_subset_group`. Manually-instantiated subsets will **not** be represented properly by the UI :param subset: optional, reference subset or subset state. If provided, the new subset will copy the logic of this subset. :returns: The new subset object """ nsub = len(self.subsets) color = color or settings.SUBSET_COLORS[nsub % len(settings.SUBSET_COLORS)] label = label or "%s.%i" % (self.label, nsub + 1) new_subset = Subset(self, color=color, label=label, **kwargs) if subset is not None: new_subset.subset_state = subset.subset_state.copy() self.add_subset(new_subset) return new_subset
def update(self, view=None, transpose=False): self._check_subset_state_changed() if self._changed: # erase and make a new artist if not self._recalc(): # no need to update style return self._changed = False has_emph = False if self.emphasis is not None: try: s = Subset(self.layer.data) s.subset_state = self.emphasis if hasattr(self.layer, 'subset_state'): s.subset_state &= self.layer.subset_state x = s[self.xatt].ravel() y = s[self.yatt].ravel() self.artists.extend(self._axes.plot(x, y)) has_emph = True except IncompatibleAttribute: pass self._sync_style() if has_emph: self.artists[-1].set_mec('green') self.artists[-1].set_mew(2) self.artists[-1].set_alpha(1)
def add_subset(self, subset): """Assign a pre-existing subset to this data object. :param subset: A :class:`~glue.core.subset.Subset` or :class:`~glue.core.subset.SubsetState` object If input is a :class:`~glue.core.subset.SubsetState`, it will be wrapped in a new Subset automatically .. note:: The preferred way for creating subsets is via :meth:`~glue.core.data_collection.DataCollection.new_subset_group`. Manually-instantiated subsets will **not** be represented properly by the UI """ if subset in self.subsets: return # prevents infinite recursion if isinstance(subset, SubsetState): # auto-wrap state in subset state = subset subset = Subset(None) subset.subset_state = state self._subsets.append(subset) if subset.data is not self: subset.do_broadcast(False) subset.data = self subset.label = subset.label # hacky. disambiguates name if needed if self.hub is not None: msg = SubsetCreateMessage(subset) self.hub.broadcast(msg) subset.do_broadcast(True)
def _load_subset(rec, context): result = Subset(None) result.style = context.object(rec['style']) result.subset_state = context.object(rec['state']) result.label = rec['label'] return result
def _load_subset(rec, context): result = Subset(None) result.style = context.object(rec["style"]) result.subset_state = context.object(rec["state"]) result.label = rec["label"] return result
def to_object(self, subset): """ Convert a glue Subset object to a astropy regions Region object. Parameters ---------- subset : `glue.core.subset.Subset` The subset to convert to a Region object """ data = subset.data if data.pixel_component_ids[0].axis == 0: x_pix_att = data.pixel_component_ids[1] y_pix_att = data.pixel_component_ids[0] else: x_pix_att = data.pixel_component_ids[0] y_pix_att = data.pixel_component_ids[1] subset_state = subset.subset_state if isinstance(subset_state, RoiSubsetState): roi = subset_state.roi if isinstance(roi, RectangularROI): xcen = 0.5 * (roi.xmin + roi.xmax) ycen = 0.5 * (roi.ymin + roi.ymax) width = roi.xmax - roi.xmin height = roi.ymax - roi.ymin return RectanglePixelRegion(PixCoord(xcen, ycen), width, height) elif isinstance(roi, PolygonalROI): return PolygonPixelRegion(PixCoord(roi.vx, roi.vy)) elif isinstance(roi, CircularROI): return CirclePixelRegion(PixCoord(*roi.get_center()), roi.get_radius()) elif isinstance(roi, EllipticalROI): return EllipsePixelRegion(PixCoord(roi.xc, roi.yc), roi.radius_x, roi.radius_y) elif isinstance(roi, PointROI): return PointPixelRegion(PixCoord(*roi.center())) elif isinstance(roi, RangeROI): return range_to_rect(data, roi.ori, roi.min, roi.max) elif isinstance(roi, AbstractMplRoi): temp_sub = Subset(data) temp_sub.subset_state = RoiSubsetState(x_pix_att, y_pix_att, roi.roi()) try: return self.to_object(temp_sub) except NotImplementedError: raise NotImplementedError( "ROIs of type {0} are not yet supported".format( roi.__class__.__name__)) else: raise NotImplementedError( "ROIs of type {0} are not yet supported".format( roi.__class__.__name__)) elif isinstance(subset_state, RangeSubsetState): if subset_state.att == x_pix_att: return range_to_rect(data, 'x', subset_state.lo, subset_state.hi) elif subset_state.att == y_pix_att: return range_to_rect(data, 'y', subset_state.lo, subset_state.hi) else: raise ValueError( 'Range subset state att should be either x or y pixel coordinate' ) elif isinstance(subset_state, MultiRangeSubsetState): if subset_state.att == x_pix_att: ori = 'x' elif subset_state.att == y_pix_att: ori = 'y' else: message = 'Multirange subset state att should be either x or y pixel coordinate' raise ValueError(message) if len(subset_state.pairs) == 0: message = 'Multirange subset state should contain at least one range' raise ValueError(message) region = range_to_rect(data, ori, subset_state.pairs[0][0], subset_state.pairs[0][1]) for pair in subset_state.pairs[1:]: region = region | range_to_rect(data, ori, pair[0], pair[1]) return region elif isinstance(subset_state, PixelSubsetState): return PointPixelRegion(PixCoord(*subset_state.get_xy(data, 1, 0))) elif isinstance(subset_state, AndState): temp_sub1 = Subset(data=data) temp_sub1.subset_state = subset_state.state1 temp_sub2 = Subset(data=data) temp_sub2.subset_state = subset_state.state2 return self.to_object(temp_sub1) & self.to_object(temp_sub2) elif isinstance(subset_state, OrState): temp_sub1 = Subset(data=data) temp_sub1.subset_state = subset_state.state1 temp_sub2 = Subset(data=data) temp_sub2.subset_state = subset_state.state2 return self.to_object(temp_sub1) | self.to_object(temp_sub2) elif isinstance(subset_state, XorState): temp_sub1 = Subset(data=data) temp_sub1.subset_state = subset_state.state1 temp_sub2 = Subset(data=data) temp_sub2.subset_state = subset_state.state2 return self.to_object(temp_sub1) ^ self.to_object(temp_sub2) elif isinstance(subset_state, MultiOrState): temp_sub = Subset(data=data) temp_sub.subset_state = subset_state.states[0] region = self.to_object(temp_sub) for state in subset_state.states[1:]: temp_sub.subset_state = state region = region | self.to_object(temp_sub) return region else: raise NotImplementedError( "Subset states of type {0} are not supported".format( subset_state.__class__.__name__))