def test_ensure_spacing_trivial(self): for p in [1, 2, np.inf]: for size in [30, 50, None]: with self.subTest(p=p, size=size): # --- Empty input self.assertEqual(ensure_spacing([], p_norm=p), []) # --- A unique point coord = np.random.randn(1, 2) np.testing.assert_array_equal( coord, ensure_spacing(coord, p_norm=p, min_split_size=size)) # --- Verified spacing coord = np.random.randn(100, 2) # --- 0 spacing np.testing.assert_array_equal( coord, ensure_spacing(coord, spacing=0, p_norm=p, min_split_size=size), ) # Spacing is chosen to be half the minimum distance spacing = pdist(coord, metric=minkowski, p=p).min() * 0.5 out = ensure_spacing(coord, spacing=spacing, p_norm=p, min_split_size=size) np.testing.assert_array_equal(coord, out)
def test_ensure_spacing_batch_processing(self): for p in [1, 2, np.inf]: for size in [50, 100, None]: with self.subTest(p=p, size=size): coord = np.random.randn(100, 2) # --- Consider the average distance between the point as spacing spacing = np.median(pdist(coord, metric=minkowski, p=p)) expected = ensure_spacing(coord, spacing=spacing, p_norm=p) assert np.array_equal( ensure_spacing(coord, spacing=spacing, p_norm=p, min_split_size=size), expected, )
def test_ensure_spacing_nD(self): for ndim in [1, 2, 3, 4, 5]: for size in [2, 10, None]: with self.subTest(ndim=ndim, size=size): coord = np.ones((5, ndim)) expected = np.ones((1, ndim)) assert np.array_equal( ensure_spacing(coord, min_split_size=size), expected)
def test_ensure_spacing_p_norm(self): for p in [1, 2, np.inf]: for size in [30, 50, None]: with self.subTest(p=p, size=size): coord = np.random.randn(100, 2) # --- Consider the average distance between the point as spacing spacing = np.median(pdist(coord, metric=minkowski, p=p)) out = ensure_spacing(coord, spacing=spacing, p_norm=p, min_split_size=size) self.assertGreater( pdist(out, metric=minkowski, p=p).min(), spacing)
def test_multiple(self): """ `find_spot_positions` should find all spot positions in a generated test image. """ n = 100 shape = (256, 256) refractive_index = 1 numerical_aperture = 0.95 wavelength = 0.55 # [µm] magnification = 40 pixel_size = 3.45 # [µm] # Ensure that each time when the test case is run we have the same # 'random' numbers. numpy.random.seed(0) sigma = (magnification / pixel_size) * synthetic.psf_sigma_wffm( refractive_index, numerical_aperture, wavelength) # Generate a test image containing at most 100 randomly distributed # spots with a guaranteed minimal spacing. spacing = int(round(10 * sigma)) border = numpy.asarray((spacing, spacing)) srange = numpy.asarray(shape) - 2 * border - numpy.array((1, 1)) loc = border + srange * numpy.random.random_sample((n, 2)) loc = ensure_spacing(loc, spacing=spacing, p_norm=2) image = synthetic.psf_gaussian(shape, loc, sigma) ji = spot.find_spot_positions(image, sigma) # Map the found spot locations to the known spot locations. tree = scipy.spatial.cKDTree(loc) # NOTE: Starting SciPy v1.6.0 the `n_jobs` argument will be renamed `workers` distances, indices = tree.query(ji, k=1, n_jobs=-1) # Check that all spot positions have been found within the required # accuracy. numpy.testing.assert_array_equal(sorted(indices), range(len(loc))) numpy.testing.assert_array_less(distances, 0.05)