def test_custom_template_fuzz(): for i in range(10): integers = np.arange(1, 15) center_y = np.random.choice(integers) center_x = np.random.choice(integers) size_y = np.random.choice(integers) size_x = np.random.choice(integers) radius = np.random.choice(integers) search = np.random.choice(integers) mask_y = np.random.choice(integers) mask_x = np.random.choice(integers) print("center_y:", center_y) print("center_x:", center_x) print("size_y:", size_y) print("size_x:", size_x) print("radius:", radius) print("search:", search) print("mask_y:", mask_y) print("mask_x:", mask_x) template = m.radial_gradient(centerX=center_x, centerY=center_y, imageSizeX=size_x, imageSizeY=size_y, radius=radius) custom = blobfinder.UserTemplate(template=template, search=search) mask = custom.get_mask((mask_y, mask_x)) # noqa
def get_mask(self, sig_shape): return radial_gradient( centerY=sig_shape[0] // 2, centerX=sig_shape[1] // 2, imageSizeY=sig_shape[0], imageSizeX=sig_shape[1], radius=self.radius, )
def get_mask(self, sig_shape): return masks.radial_gradient( centerY=sig_shape[0] // 2, centerX=sig_shape[1] // 2, imageSizeY=sig_shape[0], imageSizeX=sig_shape[1], radius=self.radius, antialiased=True, )
def test_correlation_methods(lt_ctx, cls, dtype, kwargs): shape = np.array([128, 128]) zero = shape / 2 + np.random.uniform(-1, 1, size=2) a = np.array([27.17, 0.]) + np.random.uniform(-1, 1, size=2) b = np.array([0., 29.19]) + np.random.uniform(-1, 1, size=2) indices = np.mgrid[-2:3, -2:3] indices = np.concatenate(indices.T) radius = 8 data, indices, peaks = cbed_frame(*shape, zero, a, b, indices, radius) dataset = MemoryDataSet(data=data, tileshape=(1, *shape), num_partitions=1, sig_dims=2) template = m.radial_gradient( centerX=radius+1, centerY=radius+1, imageSizeX=2*radius+2, imageSizeY=2*radius+2, radius=radius ) match_patterns = [ common.patterns.RadialGradient(radius=radius), common.patterns.Circular(radius=radius), common.patterns.BackgroundSubtraction(radius=radius), common.patterns.RadialGradientBackgroundSubtraction(radius=radius), common.patterns.UserTemplate(template=template) ] print("zero: ", zero) print("a: ", a) print("b: ", b) for match_pattern in match_patterns: print("refining using template %s" % type(match_pattern)) if cls is udf.correlation.SparseCorrelationUDF and kwargs.get('zero_shift'): with pytest.raises(ValueError): m_udf = cls(match_pattern=match_pattern, peaks=peaks.astype(dtype), **kwargs) else: m_udf = cls(match_pattern=match_pattern, peaks=peaks.astype(dtype), **kwargs) res = lt_ctx.run_udf(dataset=dataset, udf=m_udf) print(peaks) print(res['refineds'].data[0]) print(peaks - res['refineds'].data[0]) print(res['peak_values'].data[0]) print(res['peak_elevations'].data[0]) # import matplotlib.pyplot as plt # fig, ax = plt.subplots() # plt.imshow(data[0]) # for p in np.flip(res['refineds'].data[0], axis=-1): # ax.add_artist(plt.Circle(p, radius, fill=False, color='y')) # plt.show() assert np.allclose(res['refineds'].data[0], peaks, atol=0.5)
def test_run_refine_fastmatch(lt_ctx, progress): shape = np.array([128, 128]) zero = shape / 2 + np.random.uniform(-1, 1, size=2) a = np.array([27.17, 0.]) + np.random.uniform(-1, 1, size=2) b = np.array([0., 29.19]) + np.random.uniform(-1, 1, size=2) indices = np.mgrid[-2:3, -2:3] indices = np.concatenate(indices.T) drop = np.random.choice([True, False], size=len(indices), p=[0.9, 0.1]) indices = indices[drop] radius = 10 data, indices, peaks = cbed_frame(*shape, zero, a, b, indices, radius) dataset = MemoryDataSet(data=data, tileshape=(1, *shape), num_partitions=1, sig_dims=2) matcher = grm.Matcher() template = m.radial_gradient(centerX=radius + 1, centerY=radius + 1, imageSizeX=2 * radius + 2, imageSizeY=2 * radius + 2, radius=radius) match_patterns = [ common.patterns.RadialGradient(radius=radius), common.patterns.Circular(radius=radius), common.patterns.BackgroundSubtraction(radius=radius), common.patterns.RadialGradientBackgroundSubtraction(radius=radius), common.patterns.UserTemplate(template=template) ] print("zero: ", zero) print("a: ", a) print("b: ", b) for match_pattern in match_patterns: print("refining using template %s" % type(match_pattern)) (res, real_indices) = udf.refinement.run_refine( ctx=lt_ctx, dataset=dataset, zero=zero + np.random.uniform(-1, 1, size=2), a=a + np.random.uniform(-1, 1, size=2), b=b + np.random.uniform(-1, 1, size=2), matcher=matcher, match_pattern=match_pattern, progress=progress) print(peaks - grm.calc_coords(res['zero'].data[0], res['a'].data[0], res['b'].data[0], indices)) assert np.allclose(res['zero'].data[0], zero, atol=0.5) assert np.allclose(res['a'].data[0], a, atol=0.2) assert np.allclose(res['b'].data[0], b, atol=0.2)
def test_correlation_method_fullframe(lt_ctx, cls, dtype, kwargs): shape = np.array([128, 128]) zero = shape / 2 + np.random.uniform(-1, 1, size=2) a = np.array([34.3, 0.]) + np.random.uniform(-1, 1, size=2) b = np.array([0., 42.19]) + np.random.uniform(-1, 1, size=2) indices = np.mgrid[-2:3, -2:3] indices = np.concatenate(indices.T) radius = 8 data, indices, peaks = cbed_frame(*shape, zero, a, b, indices, radius) dataset = MemoryDataSet(data=data, tileshape=(1, *shape), num_partitions=1, sig_dims=2) template = m.radial_gradient(centerX=radius + 1, centerY=radius + 1, imageSizeX=2 * radius + 2, imageSizeY=2 * radius + 2, radius=radius) match_patterns = [ blobfinder.RadialGradient(radius=radius, search=radius * 1.5), blobfinder.BackgroundSubtraction(radius=radius, radius_outer=radius * 1.5, search=radius * 1.8), blobfinder.RadialGradientBackgroundSubtraction(radius=radius, radius_outer=radius * 1.5, search=radius * 1.8), blobfinder.UserTemplate(template=template, search=radius * 1.5) ] print("zero: ", zero) print("a: ", a) print("b: ", b) for match_pattern in match_patterns: print("refining using template %s" % type(match_pattern)) udf = cls(match_pattern=match_pattern, peaks=peaks.astype(dtype), **kwargs) res = lt_ctx.run_udf(dataset=dataset, udf=udf) print(peaks - res['refineds'].data[0]) # import matplotlib.pyplot as plt # fig, ax = plt.subplots() # plt.imshow(data[0]) # for p in np.flip(res['refineds'].data[0], axis=-1): # ax.add_artist(plt.Circle(p, radius, fill=False, color='y')) # plt.show() assert np.allclose(res['refineds'].data[0], peaks, atol=0.5)
def test_run_refine_fastmatch(lt_ctx): shape = np.array([256, 256]) zero = shape / 2 + np.random.uniform(-1, 1, size=2) a = np.array([27.17, 0.]) + np.random.uniform(-1, 1, size=2) b = np.array([0., 29.19]) + np.random.uniform(-1, 1, size=2) indices = np.mgrid[-3:4, -3:4] indices = np.concatenate(indices.T) radius = 10 data, indices, peaks = cbed_frame(*shape, zero, a, b, indices, radius) dataset = MemoryDataSet(data=data, tileshape=(1, *shape), num_partitions=1, sig_dims=2) matcher = grm.Matcher() template = m.radial_gradient(centerX=radius + 1, centerY=radius + 1, imageSizeX=2 * radius + 2, imageSizeY=2 * radius + 2, radius=radius) match_patterns = [ blobfinder.RadialGradient(radius=radius), blobfinder.BackgroundSubtraction(radius=radius), blobfinder.RadialGradientBackgroundSubtraction(radius=radius), blobfinder.UserTemplate(template=template) ] print("zero: ", zero) print("a: ", a) print("b: ", b) for match_pattern in match_patterns: print("refining using template %s" % type(match_pattern)) (res, real_indices) = blobfinder.run_refine( ctx=lt_ctx, dataset=dataset, zero=zero + np.random.uniform(-1, 1, size=2), a=a + np.random.uniform(-1, 1, size=2), b=b + np.random.uniform(-1, 1, size=2), matcher=matcher, match_pattern=match_pattern) print(peaks - grm.calc_coords(res['zero'].data[0], res['a'].data[0], res['b'].data[0], indices)) assert np.allclose(res['zero'].data[0], zero, atol=0.5) assert np.allclose(res['a'].data[0], a, atol=0.2) assert np.allclose(res['b'].data[0], b, atol=0.2)
def test_custom_template(): template = m.radial_gradient(centerX=10, centerY=10, imageSizeX=21, imageSizeY=23, radius=7) custom = blobfinder.UserTemplate(template=template, search=18) assert custom.get_crop_size() == 18 same = custom.get_mask((23, 21)) larger = custom.get_mask((25, 23)) smaller = custom.get_mask((10, 10)) assert np.allclose(same, template) assert np.allclose(larger[1:-1, 1:-1], template) assert np.allclose(template[6:-7, 5:-6], smaller)
def test_standalone_full(): shape = np.array([128, 128]) zero = shape / 2 + np.random.uniform(-1, 1, size=2) a = np.array([34.3, 0.]) + np.random.uniform(-1, 1, size=2) b = np.array([0., 42.19]) + np.random.uniform(-1, 1, size=2) indices = np.mgrid[-2:3, -2:3] indices = np.concatenate(indices.T) radius = 8 data, indices, peaks = cbed_frame(*shape, zero, a, b, indices, radius) template = m.radial_gradient(centerX=radius + 1, centerY=radius + 1, imageSizeX=2 * radius + 2, imageSizeY=2 * radius + 2, radius=radius) match_patterns = [ common.patterns.RadialGradient(radius=radius, search=radius * 1.5), common.patterns.BackgroundSubtraction(radius=radius, radius_outer=radius * 1.5, search=radius * 1.8), common.patterns.RadialGradientBackgroundSubtraction( radius=radius, radius_outer=radius * 1.5, search=radius * 1.8), common.patterns.UserTemplate(template=template, search=radius * 1.5) ] print("zero: ", zero) print("a: ", a) print("b: ", b) for match_pattern in match_patterns: print("refining using template %s" % type(match_pattern)) (centers, refineds, heights, elevations) = common.correlation.process_frames_full( pattern=match_pattern, frames=data, peaks=peaks.astype(np.int32), ) print(peaks - refineds) assert np.allclose(refineds[0], peaks, atol=0.5)
def test_radial_gradient(antialiased): template = m.radial_gradient(centerX=30, centerY=33, imageSizeX=65, imageSizeY=66, radius=17, antialiased=antialiased) assert template.shape == (66, 65) for i in range(17): assert np.allclose(template[33, 30 + i], i / 17) assert np.allclose(template[33, 30 - i], i / 17) assert np.allclose(template[33 + i, 30], i / 17) assert np.allclose(template[33 - i, 30], i / 17) for i in range(18, 30): assert np.allclose(template[33, 30 + i], 0) assert np.allclose(template[33, 30 - i], 0) assert np.allclose(template[33 + i, 30], 0) assert np.allclose(template[33 - i, 30], 0)