def test_fill_value(self): """ Test that the fill_value parameter of nfold() is working correctly """ im = 1000 * np.random.random(size=(256, 256)) mask = np.random.choice([True, False], size=im.shape) with self.subTest("fill_value = np.nan"): with catch_warnings(): simplefilter("ignore") rot = nfold(im, center=(100, 150), mod=5, mask=mask, fill_value=np.nan) self.assertTrue(np.any(np.isnan(rot))) with self.subTest("fill_value = 0.0"): with catch_warnings(): simplefilter("ignore") rot = nfold(im, center=(100, 150), mod=5, mask=mask, fill_value=0.0) self.assertFalse(np.any(np.isnan(rot)))
def test_no_side_effects(self): """ Test that nfold() does not modify the input image and mask """ im = np.empty((128, 128), dtype=np.float) mask = np.zeros_like(im, dtype=np.bool) im.setflags(write=False) mask.setflags(write=False) rot = nfold(im, center=(67, 93), mod=3, mask=mask)
def test_nfold_no_side_effects(): """ Test that nfold() does not modify the input image and mask """ im = np.empty((128, 128), dtype=float) mask = np.zeros_like(im, dtype=bool) im.setflags(write=False) mask.setflags(write=False) with catch_warnings(): simplefilter("ignore") rot = nfold(im, center=(67, 93), mod=3, mask=mask)
def test_mask(self): """ Test that nfold_symmetry() works correctly with a mask """ im = np.zeros((128, 128), dtype=np.int) mask = np.ones_like(im, dtype=np.bool) with catch_warnings(): simplefilter("ignore") im[0:20] = 1 mask[0:20] = False rot = nfold(im, mod=2, mask=mask) self.assertTrue(np.allclose(rot, np.zeros_like(rot)))
def test_nfold_mask_with_overlapping_symmetry_fill_value(): """ Test that nfold() works correctly with a mask which overlaps with it when rotated, when `fill_value` is not 0. """ im = np.zeros((128, 128), dtype=int) mask = np.ones_like(im, dtype=bool) with catch_warnings(): simplefilter("ignore") im[64 - 4:64 + 4, :] = 1 mask[64 - 5:64 + 5, :] = False # mask slightly larger rot = nfold(im, mod=6, mask=mask, fill_value=500) assert rot[64, 64] == 500
def test_nfold_mask_with_overlapping_symmetry(): """ Test that nfold() works correctly with a mask which overlaps with it when rotated. """ im = np.zeros((128, 128), dtype=int) mask = np.ones_like(im, dtype=bool) with catch_warnings(): simplefilter("ignore") im[64 - 4:64 + 4, :] = 1 mask[64 - 5:64 + 5, :] = False # mask slightly larger rot = nfold(im, mod=6, mask=mask) assert np.allclose(rot, np.zeros_like(rot))
def test_symmetrization_parallel(self): """ Test correctness of symmetrization operation in parallel mode """ before = np.array(self.dataset.diffraction_group["intensity"]) symmetrized = np.array(before, copy=True) for index, _ in enumerate(self.dataset.time_points): symmetrized[:, :, index] = nfold(before[:, :, index], mod=3, center=(63, 65)) self.dataset.symmetrize(mod=3, center=(63, 65), processes=2) after = np.array(self.dataset.diffraction_group["intensity"]) self.assertTrue(np.allclose(symmetrized, after))
def test_output_range(self): """ Test that nfold() does not modify the value range """ im = 1000 * np.random.random(size=(256, 256)) mask = np.random.choice([True, False], size=im.shape) with catch_warnings(): simplefilter("ignore") rot = nfold(im, center=(100, 150), mod=5, mask=mask) self.assertLessEqual(rot.max(), im.max()) # In the case of a mask that overlaps with itself when rotated, # the average will be zero due to nan_to_num self.assertGreaterEqual(rot.min(), min(im.min(), 0))
def test_symmetrization(self): """ Test correctness of symmetrization operation """ before = np.array(self.dataset.diffraction_group["intensity"]) symmetrized = np.array(before, copy=True) for index, _ in enumerate(self.dataset.time_points): symmetrized[:, :, index] = nfold(before[:, :, index], mod=3, center=(63, 65)) self.dataset.symmetrize(mod=3, center=(63, 65)) after = np.array(self.dataset.diffraction_group["intensity"]) self.assertTrue(np.allclose(symmetrized, after)) self.assertEqual( self.dataset.center, (63, 65), "Diffraction center was not properly set after symmetrization", )
def test_mod_1(self): """ Test that nfold(mod = 1) returns an unchanged image, except perhaps for a cast to float """ im = 1000 * np.random.random(size=(256, 256)) rot = nfold(im, mod=1) self.assertTrue(np.allclose(im, rot))
def test_valid_mod(self): """ Test the the N-fold symmetry argument is valid """ im = np.empty((128, 128)) with self.assertRaises(ValueError): nfold(im, mod=1.7)
def test_trivial(self): """ Test nfold_symmetry averaging on trivial array """ im = np.zeros((256, 256)) rot = nfold(im, mod=3) self.assertTrue(np.allclose(rot, im))
def test_nfold_valid_mod(): """ Test the the N-fold symmetry argument is valid """ im = np.empty((128, 128)) with pytest.raises(ValueError): nfold(im, mod=1.7)
def test_nfold_trivial(): """ Test nfold_symmetry averaging on trivial array """ im = np.zeros((256, 256)) rot = nfold(im, mod=3) assert np.allclose(rot, im)
def _symmetrize(im, mod, center, mask, kernel_size): im = nfold(im, mod=mod, center=center, mask=mask) if kernel_size is None: return im return gaussian_filter(im, order=0, sigma=kernel_size, mode="nearest")