def test_interactive(self): s = self.s r = RectangularROI(left=3, right=7, top=2, bottom=5) sr = r.interactive(s, None) r.x += 5 sr2 = r(s) np.testing.assert_array_equal(sr.data, sr2.data)
def test_widget_initialisation(self): s = Signal1D(np.arange(2 * 4 * 6).reshape(2, 4, 6)) s.axes_manager[0].scale = 0.5 s.axes_manager[1].scale = 1.0 roi_nav = RectangularROI(0, 0, 1, 0.5) s.plot() roi_nav.add_widget(s)
def roi_from_grid(self, index: Union[Tuple, List[Tuple]]) -> RectangularROI: """Return a rectangular region of interest (ROI) on the EBSD detector from one or multiple generator grid tile indices as row(s) and column(s). Parameters ---------- index Row and column of one or multiple grid tiles as a tuple or a list of tuples. Returns ------- roi : hyperspy.roi.RectangularROI ROI defined by the grid indices. """ rows = self.grid_rows cols = self.grid_cols if isinstance(index, tuple): index = (index, ) index = np.array(index) min_col = cols[min(index[:, 1])] max_col = cols[max(index[:, 1])] + cols[1] min_row = rows[min(index[:, 0])] max_row = rows[max(index[:, 0])] + rows[1] return RectangularROI( left=min_col, top=min_row, right=max_col, bottom=max_row, )
def test_repr_vals(self): repr(Point1DROI(1.1)) repr(Point2DROI(1.1, 2.1)) repr(Line2DROI(0, 0, 1, 1, 0.1)) repr(RectangularROI(0, 0, 1, 1)) repr(SpanROI(3., 5.)) repr(CircleROI(5, 5, 3)) repr(CircleROI(5, 5, 3, 1))
def test_out(self): s = self.s r = RectangularROI(left=3, right=7, top=2, bottom=5) sr = r(s) d = s.data.sum() sr.data += 2 assert d + sr.data.size * 2 == s.data.sum() r.x += 2 sr2 = r(s) r(s, out=sr) np.testing.assert_array_equal(sr2.data, sr.data)
def test_rect_image(self): s = self.s_i s.axes_manager[0].scale = 0.2 s.axes_manager[1].scale = 0.8 r = RectangularROI(left=2.3, top=5.6, right=3.5, bottom=12.2) sr = r(s) scale0 = s.axes_manager[0].scale scale1 = s.axes_manager[1].scale n = ((int(round(2.3 / scale0)), int(round(3.5 / scale0)),), (int(round(5.6 / scale1)), int(round(12.2 / scale1)),)) assert (sr.axes_manager.navigation_shape == (n[0][1] - n[0][0], n[1][1] - n[1][0])) np.testing.assert_equal( sr.data, s.data[n[1][0]:n[1][1], n[0][0]:n[0][1], ...])
def test_get_rgb_alpha_signal(self): s = load(KIKUCHIPY_FILE) vbse_gen = VirtualBSEGenerator(s) vbse_img = s.get_virtual_bse_intensity(roi=RectangularROI(0, 0, 10, 10)) vbse_rgb_img1 = vbse_gen.get_rgb_image( r=(0, 1), g=(0, 2), b=(0, 3), alpha=vbse_img ) vbse_rgb_img2 = vbse_gen.get_rgb_image( r=(0, 1), g=(0, 2), b=(0, 3), alpha=vbse_img.data, ) vbse_rgb_img1.change_dtype(np.uint8) vbse_rgb_img2.change_dtype(np.uint8) assert np.allclose(vbse_rgb_img1.data, vbse_rgb_img2.data)
def _get_rois(self, signal): rois = [] if self.have_selection(signal): if self.ndim(signal) == 1: if self.ranged: roi_template = SpanROI(0, 1) else: roi_template = Point1DROI(0) elif self.ndim(signal) > 1: if self.ranged: roi_template = RectangularROI(0, 0, 1, 1) else: roi_template = Point2DROI(0, 0) for w in self.widgets[signal]: roi = copy.deepcopy(roi_template) # ROI gets coords from widget: roi._on_widget_change(w) rois.append(roi) return rois
def test_out(self): s = self.s r = RectangularROI(left=3, right=7, top=2, bottom=5) sr = r(s) sr_ref = r(s) sr.data = sr.data + 2 sr2 = r(s) r(s, out=sr) if s._lazy: s.compute() sr.compute() sr2.compute() sr_ref.compute() np.testing.assert_array_equal(sr2.data, sr.data) np.testing.assert_array_equal(sr2.data, sr_ref.data)
def accept(self): if self.is_on(): if self.ndim == 1: if self.ranged: roi = SpanROI(0, 1) else: roi = Point1DROI(0) elif self.ndim > 1: if self.ranged: roi = RectangularROI(0, 0, 1, 1) else: roi = Point2DROI(0, 0) else: raise RuntimeWarning("No figure could be found.") return roi._on_widget_change(self.widget) # ROI gets coords from widget self.accepted[BaseInteractiveROI].emit(roi) self.accepted[BaseInteractiveROI, SignalFigureTool].emit(roi, self) if self.cancel_on_accept: self.cancel()
def _get_default_refinement_roi(self): signal_axes = self.signal.axes_manager.signal_axes start = [relative2value(axis, 1 / 4) for axis in signal_axes] end = [relative2value(axis, 3 / 4) for axis in signal_axes] return RectangularROI(*start, *end)
def test_rect_image_boundary_roi(self): s = self.s_i r = RectangularROI(0, 0, 100, 100) # Test adding roi to plot s.plot() w = r.add_widget(s) np.testing.assert_equal(r(s).data, s.data) # width and height should range between 1 and axes shape with pytest.raises(ValueError): w.width = 101 with pytest.raises(ValueError): w.height = 101 s.axes_manager[0].scale = 0.2 s.axes_manager[1].scale = 0.8 r2 = RectangularROI(0, 0, 20, 80) # Test adding roi to plot s.plot() w2 = r2.add_widget(s) np.testing.assert_equal(r2(s).data, s.data) w2.set_bounds(x=-10) # below min x assert w2._pos[0] == 0 w2.set_bounds(width=0.1) # below min width assert w2._size[0] == 0.2 w2.set_bounds(width=30.0) # above max width assert w2._size[0] == 20 w2.set_bounds(y=0) # min y w2.set_bounds(height=0.7) # below min height assert w2._size[1] == 0.8 w2.set_bounds(height=90.0) # about max height assert w2._size[1] == 80.0 # by indices # width and height should range between 1 and axes shape with pytest.raises(ValueError): w2.width = 0 with pytest.raises(ValueError): w2.height = 0 with pytest.raises(ValueError): w2.width = 101 with pytest.raises(ValueError): w2.height = 101 # the combination of the two is not valid w2.set_bounds(x=10, width=20) assert w2._pos[0] == 0.0 assert w2._size[0] == 20.0 # the combination of the two is not valid w2.set_bounds(y=40, height=60) assert w2._pos[1] == 0 assert w2._size[1] == 80 w2.set_bounds(x=10) w2.set_bounds(width=20) assert w2._pos[0] == 0 assert w2._size[0] == 20 w2.set_bounds(y=10) w2.set_bounds(height=79.2) assert w2._pos[1] == 0.0 assert w2._size[1] == 79.2
def test_rectroi_getitem(self): r = RectangularROI(left=2.3, top=5.6, right=3.5, bottom=12.2) assert tuple(r) == (2.3, 3.5, 5.6, 12.2)
def test_interactive_snap(self, snap): kwargs = {} if snap != 'default': kwargs['snap'] = snap else: # default is True snap = True s = self.s r = RectangularROI(left=3, right=7, top=2, bottom=5) s.plot() _ = r.interactive(s, **kwargs) for w in r.widgets: old_position = w.position new_position = (3.25, 2.2) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_all == snap assert w.snap_position == snap assert w.snap_size == snap p1 = Point1DROI(4) _ = p1.interactive(s, **kwargs) for w in p1.widgets: old_position = w.position new_position = (4.2, ) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_position == snap p2 = Point2DROI(4, 5) _ = p2.interactive(s, **kwargs) for w in p2.widgets: old_position = w.position new_position = (4.3, 5.3) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_position == snap span = SpanROI(4.01, 5) _ = span.interactive(s, **kwargs) for w in span.widgets: old_position = w.position new_position = (4.2, ) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_all == snap assert w.snap_position == snap assert w.snap_size == snap # check that changing snap is working fine new_snap = not snap w.snap_all = new_snap old_position = w.position new_position = (4.2, ) w.position = new_position assert w.position == old_position if new_snap else new_position line2d = Line2DROI(4, 5, 6, 6, 1) _ = line2d.interactive(s, **kwargs) for w in line2d.widgets: old_position = w.position new_position = ([4.3, 5.3], [6.0, 6.0]) w.position = new_position assert w.position == old_position if snap else new_position assert w.snap_all == snap assert w.snap_position == snap assert w.snap_size == snap