def test_run_refine_fastmatch_zeroshift(lt_ctx): 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 # Exactly between peaks, worst case shift = (a + b) / 2 data_0, indices_0, peaks_0 = cbed_frame(*shape, zero, a, b, indices, radius) data_1, indices_1, peaks_1 = cbed_frame(*shape, zero + shift, a, b, indices, radius) data = np.concatenate((data_0, data_1), axis=0) dataset = MemoryDataSet(data=data, tileshape=(1, *shape), num_partitions=1, sig_dims=2) matcher = grm.Matcher() match_patterns = [ # Least reliable pattern common.patterns.Circular(radius=radius), ] print("zero: ", zero) print("a: ", a) print("b: ", b) for match_pattern in match_patterns: print("refining using template %s" % type(match_pattern)) zero_shift = np.array([(0., 0.), shift]).astype(np.float32) (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, zero_shift=UDF.aux_data(zero_shift, kind='nav', extra_shape=(2, ))) print(peaks_0 - grm.calc_coords(res['zero'].data[0], res['a'].data[0], res['b'].data[0], indices_0)) print(peaks_1 - grm.calc_coords(res['zero'].data[1], res['a'].data[1], res['b'].data[1], indices_1)) assert np.allclose(res['zero'].data[0], zero, atol=0.5) assert np.allclose(res['zero'].data[1], zero + shift, atol=0.5) assert np.allclose(res['a'].data, a, atol=0.2) assert np.allclose(res['b'].data, b, atol=0.2)
def test_fastmatch(zero, a, b): grid = _fullgrid(zero, a, b, 5) matcher = grm.Matcher() match = matcher.fastmatch(centers=grid, zero=zero, a=a, b=b) assert (np.allclose(zero, match.zero)) assert (np.allclose(a, match.a)) assert (np.allclose(b, match.b)) assert (len(match) == len(grid)) assert (np.allclose(match.calculated_refineds, grid))
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_affinematch(zero, a, b): grid = _fullgrid(zero, a, b, 5) indices = grm.get_indices(grid, zero, a, b) matcher = grm.Matcher() match = matcher.affinematch(centers=grid, indices=indices) assert (np.allclose(zero, match.zero)) assert (np.allclose(a, match.a)) assert (np.allclose(b, match.b)) assert (len(match) == len(grid)) assert (np.allclose(match.calculated_refineds, grid))
def test_run_refine_affinematch(lt_ctx): for i in range(1): try: 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 = 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() match_pattern = common.patterns.RadialGradient(radius=radius) affine_indices = peaks - zero for j in range(5): zzero = zero + np.random.uniform(-1, 1, size=2) aa = np.array([1, 0]) + np.random.uniform(-0.05, 0.05, size=2) bb = np.array([0, 1]) + np.random.uniform(-0.05, 0.05, size=2) (res, real_indices) = udf.refinement.run_refine( ctx=lt_ctx, dataset=dataset, zero=zzero, a=aa, b=bb, indices=affine_indices, matcher=matcher, match_pattern=match_pattern, match='affine') assert np.allclose(res['zero'].data[0], zero, atol=0.5) assert np.allclose(res['a'].data[0], [1, 0], atol=0.05) assert np.allclose(res['b'].data[0], [0, 1], atol=0.05) except Exception: print("zero = np.array([%s, %s])" % tuple(zero)) print("a = np.array([%s, %s])" % tuple(a)) print("b = np.array([%s, %s])" % tuple(b)) print("zzero = np.array([%s, %s])" % tuple(zzero)) print("aa = np.array([%s, %s])" % tuple(aa)) print("bb = np.array([%s, %s])" % tuple(bb)) raise
def test_visualize_smoke(navshape, lt_ctx): 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 = 10 data, indices, peaks = cbed_frame(*shape, zero, a, b, indices, radius) data = data.reshape((*navshape, *shape)) dataset = MemoryDataSet(data=data, tileshape=(1, *shape), num_partitions=1, sig_dims=2) matcher = grm.Matcher() match_pattern = common.patterns.RadialGradientBackgroundSubtraction( radius=radius) print("zero: ", zero) print("a: ", a) print("b: ", b) (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) fig, axes = plt.subplots() if len(navshape) == 1: y = None elif len(navshape) == 2: y = 0 else: raise ValueError( f"Nav shape too long, supported are 1D and 2D: {navshape}") udf.utils.visualize_frame(ctx=lt_ctx, ds=dataset, result=res, indices=real_indices, r=radius, y=y, x=0, axes=axes)
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_run_refine_sparse(lt_ctx): 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 = 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() match_pattern = common.patterns.RadialGradient(radius=radius) print("zero: ", zero) print("a: ", a) print("b: ", b) (res, real_indices) = udf.refinement.run_refine( ctx=lt_ctx, dataset=dataset, zero=zero + np.random.uniform(-0.5, 0.5, size=2), a=a + np.random.uniform(-0.5, 0.5, size=2), b=b + np.random.uniform(-0.5, 0.5, size=2), matcher=matcher, match_pattern=match_pattern, correlation='sparse', steps=3 ) 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_run_refine_affinematch(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() match_pattern = blobfinder.RadialGradient(radius=radius) affine_indices = peaks - zero print("zero: ", zero) print("a: ", a) print("b: ", b) (res, real_indices) = blobfinder.run_refine( ctx=lt_ctx, dataset=dataset, zero=zero + np.random.uniform(-1, 1, size=2), a=np.array([1, 0]) + np.random.uniform(-0.05, 0.05, size=2), b=np.array([0, 1]) + np.random.uniform(-0.05, 0.05, size=2), indices=affine_indices, matcher=matcher, match_pattern=match_pattern, match='affine') assert np.allclose(res['zero'].data[0], zero, atol=0.5) assert np.allclose(res['a'].data[0], [1, 0], atol=0.05) assert np.allclose(res['b'].data[0], [0, 1], atol=0.05)