def test_mask(self): numpy_array = np.zeros((10, 10, 30, 30)) numpy_array[:, :, 0, 0] = 1000 numpy_array[:, :, -1, -1] = 1 dask_array = da.from_array(numpy_array, chunks=(5, 5, 5, 5)) centre_x, centre_y = np.ones((2, 100)) * 15 data = pst._radial_average_dask_array( dask_array, return_sig_size=22, centre_x=centre_x, centre_y=centre_y, normalize=False, show_progressbar=False, ) assert data.shape == (10, 10, 22) assert (data != 0.0).any() mask = pst._make_circular_mask(15, 15, 30, 30, 15) data = pst._radial_average_dask_array( dask_array, return_sig_size=22, centre_x=centre_x, centre_y=centre_y, normalize=False, mask_array=mask, show_progressbar=False, ) assert data.shape == (10, 10, 22) assert (data == 0.0).all()
def test_mask_not_all_zeros(self): im = np.zeros((12, 45)) im[7, 31] = 10 im[2, 43] = 5 mask = pst._make_circular_mask(32, 7, 45, 12, 2) im_out = pst._threshold_and_mask_single_frame(im, mask=mask) assert im_out[7, 31] == 10 im_out[7, 31] = 0 assert (im_out == 0).all()
def test_simple(self): numpy_array = np.zeros((11, 10, 40, 50)) numpy_array[:, :, 25, 25] = 1 dask_array = da.from_array(numpy_array, chunks=(5, 5, 5, 5)) mask_array = pst._make_circular_mask(25, 25, 50, 40, 10) data = dt._mask_array(dask_array, mask_array=mask_array) data = data.compute() assert data.shape == (11, 10, 40, 50) assert (data == np.zeros((11, 10, 40, 50))).all()
def test_mask(self): numpy_array = np.zeros((10, 10, 50, 50)) numpy_array[:, :, 25, 25] = 1 numpy_array[:, :, 1, 1] = 100000000 dask_array = da.from_array(numpy_array, chunks=(5, 5, 5, 5)) data0 = dt._center_of_mass_array(dask_array) data0 = data0.compute() np.testing.assert_allclose(data0, np.ones((2, 10, 10)), rtol=1e-05) mask_array = pst._make_circular_mask(25, 25, 50, 50, 10) mask_array = np.invert(mask_array) data1 = dt._center_of_mass_array(dask_array, mask_array=mask_array) data1 = data1.compute() assert (data1 == np.ones((2, 10, 10)) * 25).all()
def get_angle_image_comparison(s0, s1, angleN=12, mask_radius=None): """Compare two images by overlaying one on the other in slices. This function takes two images, extracts different angular slices and combines them into one image. Useful for comparing two diffraction images, to see if the rings have the same radius. Parameters ---------- s0, s1 : HyperSpy 2D Signal Both signals need to have the same shape, and no navigation dimensions. angleN : int, default 12 Number of angular slices. mask_radius : int, optional Mask the centre of the image. The default is not to mask anything. Useful to mask the most intense parts of the diffraction pattern, so the less intense parts can be visualized. Returns ------- comparison_signal : HyperSpy 2D Examples -------- >>> from pyxem.dummy_data import MakeTestData >>> test_data0 = MakeTestData(300, 300) >>> test_data0.add_ring(150, 150, 40) >>> test_data1 = MakeTestData(300, 300) >>> test_data1.add_ring(150, 150, 60) >>> s0 = test_data0.signal >>> s1 = test_data1.signal >>> s0.axes_manager[0].offset, s0.axes_manager[1].offset = -150, -150 >>> s1.axes_manager[0].offset, s1.axes_manager[1].offset = -150, -150 >>> import pyxem.utils.radial_utils as ra >>> s = ra.get_angle_image_comparison(s0, s1) >>> s.plot() Mask the inner parts >>> s = ra.get_angle_image_comparison(s0, s1, mask_radius=10) """ if s0.axes_manager.shape != s1.axes_manager.shape: raise ValueError("s0 and s1 need to have the same shape") s = s0.deepcopy() angle_array = np.ogrid[0:2 * np.pi:(1 + angleN) * 1j] for i in range(len(angle_array[:-1])): if i % 2: angle0, angle1 = angle_array[i:i + 2] bool_array = pst._get_angle_sector_mask(s, angle0, angle1) s.data[bool_array] = s1.data[bool_array] if mask_radius is not None: am = s.axes_manager mask = pst._make_circular_mask( am[0].value2index(0.0), am[1].value2index(0.0), am[0].size, am[1].size, mask_radius, ) mask = np.invert(mask) s.data *= mask return s
def test_mask_all_zeros(self): im = np.zeros((12, 45)) im[2, 10] = 10 mask = pst._make_circular_mask(6, 31, 45, 12, 2) im_out = pst._threshold_and_mask_single_frame(im, mask=mask) assert (im_out == 0).all()
def test_wrong_mask_array_shape(self): dask_array = da.zeros((10, 12, 23, 8), chunks=(2, 2, 2, 2)) mask_array = pst._make_circular_mask(7, 4, 22, 9, 5) with pytest.raises(ValueError): dt._mask_array(dask_array, mask_array=mask_array)