def test_circle_spec(self): s = self.s_s s.data = np.ones_like(s.data) r = CircleROI(20, 25, 20) r_ann = CircleROI(20, 25, 20, 15) sr = r(s) sr_ann = r_ann(s) scale = s.axes_manager[0].scale n = int(round(40 / scale)) assert sr.axes_manager.navigation_shape == (n, n) assert sr_ann.axes_manager.navigation_shape == (n, n) # Check that mask is same for all images: for i in range(n): for j in range(n): assert (np.all(sr.data.mask[j, i, :] == True) or np.all(sr.data.mask[j, i, :] == False)) assert (np.all(sr_ann.data.mask[j, i, :] == True) or np.all(sr_ann.data.mask[j, i, :] == False)) # Check that the correct elements has been masked out: mask = sr.data.mask[:, :, 0] print(mask) # To help debugging, this shows the shape of the mask np.testing.assert_array_equal( np.where(mask.flatten())[0], [0, 1, 6, 7, 8, 15, 48, 55, 56, 57, 62, 63]) mask_ann = sr_ann.data.mask[:, :, 0] print(mask_ann) # To help debugging, this shows the shape of the mask np.testing.assert_array_equal( np.where(mask_ann.flatten())[0], [0, 1, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22, 25, 26, 27, 28, 29, 30, 33, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 46, 48, 50, 51, 52, 53, 55, 56, 57, 62, 63]) # Check that mask works for sum assert np.sum(sr.data) == (n**2 - 3 * 4) * 4 assert np.sum(sr_ann.data) == 4 * 5 * 4
def plot_corrected_diffraction_pattern(self, reference_circle=True): """Plot the distortion corrected diffraction pattern with an optional reference circle. Parameters ---------- reference_circle : bool If True a CircleROI widget is added to the plot for reference. """ # Check all required parameters are defined as attributes if self.calibration_data.au_x_grating_dp is None: raise ValueError("This method requires an Au X-grating diffraction " "pattern to be provided. Please update the " "CalibrationDataLibrary.") if self.affine_matrix is None: raise ValueError("This method requires a distortion matrix to have " "been determined. Use get_elliptical_distortion " "to determine this matrix.") # Set name for experimental data pattern dpeg = self.calibration_data.au_x_grating_dp # Apply distortion corrections to experimental data size = dpeg.data.shape[0] dpegs = stack_method([dpeg, dpeg, dpeg, dpeg]) dpegs = ElectronDiffraction2D(dpegs.data.reshape((2, 2, size, size))) dpegs.apply_affine_transformation(self.affine_matrix, preserve_range=True, inplace=True) dpegm = dpegs.mean((0, 1)) # Plot distortion corrected data dpegm.plot(cmap='magma', vmax=0.1) # add reference circle if specified if reference_circle is True: circ = CircleROI(cx=128, cy=128, r=53.5, r_inner=0) circ.add_widget(dpegm)
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_interactive_special_case(self): s = self.s.inav[0] r = CircleROI(3, 5, 2) sr = r.interactive(s, None, color="blue") np.testing.assert_array_equal( np.where(sr.data.mask.flatten())[0], [0, 3, 12, 15]) r.r_inner = 1 r.cy = 16 sr2 = r(s) np.testing.assert_array_equal( np.where(sr.data.mask.flatten())[0], [0, 3, 5, 6, 9, 10, 12, 15]) np.testing.assert_array_equal(sr2.data, sr.data)
def test_interactive_special_case(self): s = self.s.inav[0] r = CircleROI(3, 5, 2) sr = r.interactive(s, None) np.testing.assert_array_equal(np.where(sr.data.mask.flatten())[0], [0, 3, 12, 15]) r.r_inner = 1 r.cy = 16 sr2 = r(s) np.testing.assert_array_equal(np.where(sr.data.mask.flatten())[0], [0, 3, 5, 6, 9, 10, 12, 15]) np.testing.assert_array_equal(sr2.data, sr.data)
def test_out_special_case(self): s = self.s.inav[0] r = CircleROI(3, 5, 2) sr = r(s) np.testing.assert_array_equal( np.where(sr.data.mask.flatten())[0], [0, 3, 12, 15]) r.r_inner = 1 r.cy = 16 sr2 = r(s) r(s, out=sr) np.testing.assert_array_equal( np.where(sr.data.mask.flatten())[0], [0, 3, 5, 6, 9, 10, 12, 15]) np.testing.assert_array_equal(sr2.data, sr.data)
def test_circle_spec(self): s = self.s_s s.data = np.ones_like(s.data) r = CircleROI(20, 25, 20) r_ann = CircleROI(20, 25, 20, 15) sr = r(s) sr_ann = r_ann(s) scale = s.axes_manager[0].scale n = int(round(40 / scale)) assert sr.axes_manager.navigation_shape == (n, n) assert sr_ann.axes_manager.navigation_shape == (n, n) # Check that mask is same for all images: for i in range(n): for j in range(n): assert (np.all(sr.data.mask[j, i, :] == True) or np.all(sr.data.mask[j, i, :] == False)) assert (np.all(sr_ann.data.mask[j, i, :] == True) or np.all(sr_ann.data.mask[j, i, :] == False)) # Check that the correct elements has been masked out: mask = sr.data.mask[:, :, 0] print(mask) # To help debugging, this shows the shape of the mask np.testing.assert_array_equal( np.where(mask.flatten())[0], [0, 1, 6, 7, 8, 15, 48, 55, 56, 57, 62, 63]) mask_ann = sr_ann.data.mask[:, :, 0] print(mask_ann) # To help debugging, this shows the shape of the mask np.testing.assert_array_equal( np.where(mask_ann.flatten())[0], [0, 1, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22, 25, 26, 27, 28, 29, 30, 33, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 46, 48, 50, 51, 52, 53, 55, 56, 57, 62, 63]) # Check that mask works for sum assert np.sum(sr.data) == (n**2 - 3 * 4) * 4 assert np.sum(sr_ann.data) == 4 * 5 * 4 s.plot() r_signal = r.interactive(signal=s) r_ann_signal = r_ann.interactive(signal=s) assert np.sum(r_signal.sum().data) == (n**2 - 3 * 4) * 4 assert np.sum(r_ann_signal.sum().data) == 4 * 5 * 4
def test_annulus_getitem(self): r_ann = CircleROI(20, 25, 20, 15) assert tuple(r_ann) == (20, 25, 20, 15)
def test_circle_getitem(self): r = CircleROI(20, 25, 20) assert tuple(r) == (20, 25, 20)
def test_get_virtual_image(self, diffraction_pattern): roi = CircleROI(3, 3, 5) diffraction_pattern.get_virtual_image(roi)
def test_plot_interactive_virtual_image(self, diffraction_pattern): roi = CircleROI(3, 3, 5) diffraction_pattern.plot_interactive_virtual_image(roi)