def make_selector(roi, x, y): state = RoiSubsetState() state.roi = roi state.xatt = x.id state.yatt = y.id return state
def make_selector(self, roi, x, y): state = RoiSubsetState() state.roi = roi state.xatt = x.id state.yatt = y.id return state
def apply_roi(self, roi): subset_state = RoiSubsetState() xroi, yroi = roi.to_polygon() x, y = self._get_plot_attributes() subset_state.xatt = x subset_state.yatt = y subset_state.roi = PolygonalROI(xroi, yroi) mode = EditSubsetMode() mode.update(self.data, subset_state, focus_data=self.display_data)
def make_selector(self, roi, source, y): """Selections coming from ImageJ correspond to a rectangle in x and y. Could propagate more complex selection criteria from image (e.g., magic wand for nearest neighbors on barcode simplex) """ state = RoiSubsetState() state.roi = roi state.xatt = source.id state.yatt = y.id return state
def make_selector(roi, full_path): if full_path: hit = roi.contains(timelines['x'], timelines['y']).any(axis=1) rids = timelines[hit].index.unique() return CategorySubsetState('run_id', rids) state = RoiSubsetState() state.xatt = 'x' state.yatt = 'y' state.roi = roi return state
def test_reordered_pixel_components(self): self.data._pixel_component_ids = self.data._pixel_component_ids[::-1] range_state = RangeSubsetState(105.5, 107.7, self.data.pixel_component_ids[1]) self.dc.new_subset_group(subset_state=range_state, label='reordered_range') rect_state = RoiSubsetState(self.data.pixel_component_ids[0], self.data.pixel_component_ids[1], RectangularROI(1, 3.5, -0.2, 3.3)) self.dc.new_subset_group(subset_state=rect_state, label='reordered_rectangular') range_region = self.data.get_selection_definition( subset_id='reordered_range', format='astropy-regions') rect_region = self.data.get_selection_definition( subset_id='reordered_rectangular', format='astropy-regions') assert isinstance(range_region, RectanglePixelRegion) assert_allclose(range_region.center.x, 128) assert_allclose(range_region.center.y, 106.6) assert_allclose(range_region.width, 256) assert_allclose(range_region.height, 2.2) assert isinstance(rect_region, RectanglePixelRegion) assert_allclose(rect_region.center.x, 2.25) assert_allclose(rect_region.center.y, 1.55) assert_allclose(rect_region.width, 2.5) assert_allclose(rect_region.height, 3.5)
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 test_multior_region(self): rects = [(1, 2, 3, 4), (1.5, 2.5, 3.5, 4.5), (2, 3, 4, 5)] states = [ RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], RectangularROI(*rect)) for rect in rects ] multior_subset_state = MultiOrState(states) self.dc.new_subset_group(subset_state=multior_subset_state, label='multiorstate') reg = self.data.get_selection_definition(format='astropy-regions') assert isinstance(reg, CompoundPixelRegion) assert isinstance(reg.region1, CompoundPixelRegion) assert isinstance(reg.region2, RectanglePixelRegion) assert isinstance(reg.region1.region1, RectanglePixelRegion) assert isinstance(reg.region1.region2, RectanglePixelRegion) assert reg.contains(PixCoord(1.25, 3.25)) assert reg.contains(PixCoord(1.75, 3.75)) assert reg.contains(PixCoord(2.25, 3.75)) assert reg.contains(PixCoord(2.25, 4.25)) assert reg.contains(PixCoord(2.75, 4.75)) assert not reg.contains(PixCoord(5, 7))
def test_region_from_subset_profile(jdaviz_app, spectral_cube_wcs): data = Data(flux=np.ones((256, 128, 128)), label='Test 1D Flux', coords=spectral_cube_wcs) jdaviz_app.data_collection.append(data) subset_state = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0], XRangeROI(1, 3.5)) jdaviz_app.add_data_to_viewer('spectrum-viewer', 'Test 1D Flux') jdaviz_app.data_collection.new_subset_group(subset_state=subset_state, label='rectangular') subsets = jdaviz_app.get_subsets_from_viewer('spectrum-viewer') reg = subsets.get('rectangular') assert len(subsets) == 1 assert isinstance(reg, RectanglePixelRegion) assert_allclose(reg.center.x, 2.25) assert_allclose(reg.center.y, 128) assert_allclose(reg.width, 2.5) assert_allclose(reg.height, 256)
def spectrum(data, attribute, roi, slc, zaxis): # Find the integer index of the x and y axes, which are the axes for # which the image is shown (the ROI is drawn along these attributes) xaxis = slc.index('x') yaxis = slc.index('y') # Get the actual component IDs corresponding to these axes xatt = data.get_pixel_component_id(xaxis) yatt = data.get_pixel_component_id(yaxis) # Set up a view that does not reduce the dimensionality of the array but # extracts 1-element slices along dimensions that are not relevant. view = [] for idim, dim in enumerate(slc): if idim in (xaxis, yaxis, zaxis): view.append(slice(None)) else: view.append(slice(dim, dim + 1)) view = tuple(view) # We now delegate to RoiSubsetState to compute the mask based on the ROI subset_state = RoiSubsetState(xatt=xatt, yatt=yatt, roi=roi) mask = subset_state.to_mask(data, view=view) # We now extract the values that fall inside the ROI. Unfortunately, # this returns a flat 1-d array, so we need to then reshape it to get # an array with shape (n_spec, n_pix), where n_pix is the number of # pixels inside the ROI values = data[attribute, view] if zaxis != 0: values = values.swapaxes(zaxis, 0) mask = mask.swapaxes(zaxis, 0) values = values[mask].reshape(data.shape[zaxis], -1) # We then average along the spatial dimension spectrum = np.nanmean(values, axis=1) # Get the world coordinates of the spectral axis x = Extractor.abcissa(data, zaxis) return x, spectrum
def setup(self): self.data_collection, self.viewer = get_data_collection_and_viewer() load_wcs_data(self.data_collection) self.viewer.add_data(self.data_collection[0]) self.viewer.add_data(self.data_collection[1]) roi = CircularROI(256, 256, 120) py1, px1 = self.data_collection[0].pixel_component_ids self.subset_state = RoiSubsetState(px1, py1, roi) self.viewer.register_to_hub(self.data_collection.hub)
def test_or_region(self): subset_state1 = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], RectangularROI(1, 5, 2, 6)) subset_state2 = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], CircularROI(4.75, 5.75, 0.5)) or_subset_state = OrState(subset_state1, subset_state2) self.dc.new_subset_group(subset_state=or_subset_state, label='orstate') reg = self.data.get_selection_definition(format='astropy-regions') assert isinstance(reg, CompoundPixelRegion) assert isinstance(reg.region1, RectanglePixelRegion) assert isinstance(reg.region2, CirclePixelRegion) assert reg.contains(PixCoord(4.5, 5.5)) assert reg.contains(PixCoord(3, 4)) assert reg.contains(PixCoord(5.1, 6.1)) assert not reg.contains(PixCoord(11, 12))
def setup(self): # Set up main 3-d dataset self.data = Data(values=np.random.random((512, 512, 512))) # Set up subset state which is a polygon defined in pixel space and # only covering a small fraction of the whole cube. self.polygonal_roi = PolygonalROI([440, 460, 450], [400, 455, 500]) self.polygonal_subset_state = RoiSubsetState(xatt=self.data.pixel_component_ids[0], yatt=self.data.pixel_component_ids[2], roi=self.polygonal_roi)
def test_point_roi(self): subset_state = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], PointROI(2.64, 5.4)) self.dc.new_subset_group(subset_state=subset_state, label='point') reg = self.data.get_selection_definition(format='astropy-regions') assert isinstance(reg, PointPixelRegion) assert_equal(reg.center.x, 2.64) assert_equal(reg.center.y, 5.4)
def test_invalid_combos(self): good_subset = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], RectangularROI(1, 5, 2, 6)) bad_subset = RoiSubsetState(self.data.pixel_component_ids[1], self.data.main_components[0], CircularROI(4.75, 5.75, 0.5)) and_sub = AndState(good_subset, bad_subset) or_sub = OrState(good_subset, bad_subset) xor_sub = XorState(good_subset, bad_subset) multior = MultiOrState([good_subset, bad_subset]) self.dc.new_subset_group(subset_state=and_sub, label='and') self.dc.new_subset_group(subset_state=or_sub, label='or') self.dc.new_subset_group(subset_state=xor_sub, label='xor') self.dc.new_subset_group(subset_state=multior, label='multior') expected_error = 'Subset state yatt should be y pixel coordinate' with pytest.raises(ValueError) as exc: self.data.get_selection_definition(subset_id='and', format='astropy-regions') assert exc.value.args[0] == expected_error with pytest.raises(ValueError) as exc: self.data.get_selection_definition(subset_id='or', format='astropy-regions') assert exc.value.args[0] == expected_error with pytest.raises(ValueError) as exc: self.data.get_selection_definition(subset_id='xor', format='astropy-regions') assert exc.value.args[0] == expected_error with pytest.raises(ValueError) as exc: self.data.get_selection_definition(subset_id='multior', format='astropy-regions') assert exc.value.args[0] == expected_error
def test_circular_roi(self): subset_state = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], CircularROI(1, 3.5, 0.75)) self.dc.new_subset_group(subset_state=subset_state, label='circular') reg = self.data.get_selection_definition(format='astropy-regions') assert isinstance(reg, CirclePixelRegion) assert_equal(reg.center.x, 1) assert_equal(reg.center.y, 3.5) assert_equal(reg.radius, 0.75)
def subset_roi(self, attributes, roi): """ Create a subset from a region of interest. Parameters ---------- attributes : iterable The attributes on the x and y axis roi : `~glue.core.roi.Roi` The region of interest to use to create the subset. """ subset_state = RoiSubsetState(attributes[0], attributes[1], roi) cmd = ApplySubsetState(data_collection=self.data_collection, subset_state=subset_state) self._session.command_stack.do(cmd)
def test_xregion_roi(self): subset_state = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], XRangeROI(1, 3.5)) self.dc.new_subset_group(subset_state=subset_state, label='xrangeroi') reg = self.data.get_selection_definition(format='astropy-regions') assert isinstance(reg, RectanglePixelRegion) assert_allclose(reg.center.x, 2.25) assert_allclose(reg.center.y, 64) assert_allclose(reg.width, 2.5) assert_allclose(reg.height, 128)
def test_mpl_roi(self): rect_roi = RectangularROI(1, 3.5, -0.2, 3.3) subset_state = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], AbstractMplRoi(None, rect_roi)) self.dc.new_subset_group(subset_state=subset_state, label='mpl_rectangular') reg = self.data.get_selection_definition(format='astropy-regions') assert isinstance(reg, RectanglePixelRegion) assert_allclose(reg.center.x, 2.25) assert_allclose(reg.center.y, 1.55) assert_allclose(reg.width, 2.5) assert_allclose(reg.height, 3.5)
def test_polygonal_roi(self): xv = [1.3, 2, 3, 1.5, 0.5] yv = [10, 20.20, 30, 25, 17.17] subset_state = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], PolygonalROI(xv, yv)) self.dc.new_subset_group(subset_state=subset_state, label='polygon') reg = self.data.get_selection_definition(format='astropy-regions') assert isinstance(reg, PolygonPixelRegion) assert_array_equal(reg.vertices.x, xv) assert_array_equal(reg.vertices.y, yv)
def layer_to_subset(selected_layers, data_collection): # loop over selected layers for layer in selected_layers: if isinstance(layer, RegionData): for data in data_collection: if hasattr(data, 'coords') and hasattr(data.coords, 'wcs'): list_of_rois = layer.to_subset(data.coords.wcs) roisubstates = [RoiSubsetState(data.coordinate_components[1], data.coordinate_components[0], roi=roi ) for roi in list_of_rois] composite_substate = roisubstates[0] if len(list_of_rois) > 1: composite_substate = MultiOrState(roisubstates) else: composite_substate = roisubstates[0] subset_group = data_collection.new_subset_group(label=layer.label, subset_state=composite_substate)
def test_subset_id(self): subset_state = RoiSubsetState(self.data.pixel_component_ids[1], self.data.pixel_component_ids[0], RectangularROI(1, 3.5, -0.2, 3.3)) self.dc.new_subset_group(subset_state=subset_state, label='rectangular') for subset_id in [None, 0, 'rectangular']: reg = self.data.get_selection_definition(format='astropy-regions', subset_id=subset_id) assert isinstance(reg, RectanglePixelRegion) assert_allclose(reg.center.x, 2.25) assert_allclose(reg.center.y, 1.55) assert_allclose(reg.width, 2.5) assert_allclose(reg.height, 3.5) with pytest.raises(ValueError) as exc: self.data.get_selection_definition(format='astropy-regions', subset_id='circular') assert exc.value.args[0] == "No subset found with the label 'circular'"
def test_region_from_subset_3d(jdaviz_app): data = Data(flux=np.ones((256, 128, 128)), label='Test 3D Flux') jdaviz_app.data_collection.append(data) subset_state = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0], RectangularROI(1, 3.5, -0.2, 3.3)) jdaviz_app.add_data_to_viewer('flux-viewer', 'Test 3D Flux') jdaviz_app.data_collection.new_subset_group(subset_state=subset_state, label='rectangular') subsets = jdaviz_app.get_subsets_from_viewer('flux-viewer') reg = subsets.get('rectangular') assert len(subsets) == 1 assert isinstance(reg, RectanglePixelRegion) assert_allclose(reg.center.x, 2.25) assert_allclose(reg.center.x, 2.25) assert_allclose(reg.center.y, 1.55) assert_allclose(reg.width, 2.5) assert_allclose(reg.height, 3.5)
def _load_roi_subset_state(rec, context): return RoiSubsetState(context.object(rec['xatt']), context.object(rec['yatt']), context.object(rec['roi']))
def subset_roi(self, components, roi, use_current=False): subset_state = RoiSubsetState(components[0], components[1], roi) cmd = ApplySubsetState(data_collection=self.data_collection, subset_state=subset_state, use_current=use_current) self._session.command_stack.do(cmd)
def subset_from_roi(self, att, roi, other_comp=None, other_att=None, coord='x'): """ Create a SubsetState object from an ROI. This encapsulates the logic for creating subset states with Components. See the documentation for CategoricalComponents for caveats involved with mixed-type plots. :param att: attribute name of this Component :param roi: an ROI object :param other_comp: The other Component for 2D ROIs :param other_att: The attribute name of the other Component :param coord: The orientation of this Component :param is_nested: True if this was passed from another Component. :return: A SubsetState (or subclass) object """ if coord not in ('x', 'y'): raise ValueError('coord should be one of x/y') other_coord = 'y' if coord == 'x' else 'x' if isinstance(roi, RangeROI): # The selection is either an x range or a y range if roi.ori == coord: # The selection applies to the current component lo, hi = roi.range() subset_state = RangeSubsetState(lo, hi, att) else: # The selection applies to the other component, so we delegate return other_comp.subset_from_roi(other_att, roi, other_comp=self, other_att=att, coord=other_coord) else: # The selection is polygon-like. Categorical components require # special care, so if the other component is categorical, we need to # delegate to CategoricalComponent.subset_from_roi. if isinstance(other_comp, CategoricalComponent): return other_comp.subset_from_roi(other_att, roi, other_comp=self, other_att=att, is_nested=True, coord=other_coord) else: subset_state = RoiSubsetState() subset_state.xatt = att subset_state.yatt = other_att x, y = roi.to_polygon() subset_state.roi = PolygonalROI(x, y) return subset_state
def _roi_to_subset_state(self, components, roi): return RoiSubsetState(components[0], components[1], roi)
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__))
def _load_roi_subset_state(rec, context): return RoiSubsetState(context.object(rec['xatt']), context.object(rec['yatt']), context.object(rec['roi']), context.object(rec['pretransform'] if 'pretransform' in rec else None))