From 1104f338d13a8e86eac83106e495e55a20416579 Mon Sep 17 00:00:00 2001 From: Abe Levitan Date: Thu, 25 Apr 2019 13:53:35 -0400 Subject: [PATCH] Test the synthesize image functionality --- CDTools/scripts/synthesize.py | 4 +++- CDTools/tools/analysis.py | 10 ++++------ tests/tools/test_analysis.py | 36 +++++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/CDTools/scripts/synthesize.py b/CDTools/scripts/synthesize.py index e876f8b..518807e 100644 --- a/CDTools/scripts/synthesize.py +++ b/CDTools/scripts/synthesize.py @@ -27,7 +27,9 @@ if __name__ == '__main__': synth_probe, synth_obj, aligned_objs = synthesize_reconstructions( dataset['probe'], dataset['obj'], args.use_probe) - + + print(np.max(np.abs(synth_obj))) + print(np.max(np.abs(aligned_objs[0]))) freqs, prtf = calc_consistency_prtf(synth_obj, aligned_objs, dataset['basis']) diff --git a/CDTools/tools/analysis.py b/CDTools/tools/analysis.py index 1e84def..05a002a 100644 --- a/CDTools/tools/analysis.py +++ b/CDTools/tools/analysis.py @@ -203,13 +203,11 @@ def synthesize_reconstructions(probes, objects, use_probe=False, obj_slice=None, (objects[0].shape[1]//8)*3:(objects[0].shape[1]//8)*5] - synth_probe, synth_obj = standardize(probes[0], objects[0], obj_slice=obj_slice,correct_ramp=correct_ramp) + synth_probe, synth_obj = standardize(probes[0].clone(), objects[0].clone(), obj_slice=obj_slice,correct_ramp=correct_ramp) obj_stack = [synth_obj] for i, (probe, obj) in enumerate(zip(probes[1:],objects[1:])): - probe, obj = standardize(probe, obj, obj_slice=obj_slice,correct_ramp=correct_ramp) - - + probe, obj = standardize(probe.clone(), obj.clone(), obj_slice=obj_slice,correct_ramp=correct_ramp) if use_probe: shift = ip.find_shift(synth_probe[0],probe[0], resolution=50) else: @@ -224,8 +222,8 @@ def synthesize_reconstructions(probes, objects, use_probe=False, obj_slice=None, else: probe = ip.sinc_subpixel_shift(probe,tuple(shift)) - synth_probe += probe - synth_obj += obj + synth_probe = synth_probe + probe + synth_obj = synth_obj + obj obj_stack.append(obj) diff --git a/tests/tools/test_analysis.py b/tests/tools/test_analysis.py index a1fb468..cdb085e 100644 --- a/tests/tools/test_analysis.py +++ b/tests/tools/test_analysis.py @@ -115,11 +115,39 @@ def test_standardize(): from matplotlib import pyplot as plt def test_synthesize_reconstructions(): - # Not really sure how to test this to be honest + # I can only really test for a lack of failures, so I think my plan + # will be to create a dataset that just needs to be added and see that + # it successfully doesn't mess it up. - # Perhaps it's just best to test for a lack of failures? - pass + # Start by making a probe and object that should meet the standardization + # conditions + probe = initializers.gaussian((230,240),(20,20),curvature=(0.01,0.01)) + probe = cmath.torch_to_complex(probe) + probe = probe * np.sqrt(len(probe.ravel()) / np.sum(np.abs(probe)**2)) + probe = probe * np.exp(-1j * np.angle(np.sum(probe))) + assert np.isclose(1, np.sum(np.abs(probe)**2)/ len(probe.ravel())) + assert np.isclose(0,np.angle(np.sum(probe))) + + obj = 30 * np.random.rand(230,240) * np.exp(1j * (np.random.rand(230,240) - 0.5)) + obj_slice = np.s_[(obj.shape[0]//8)*3:(obj.shape[0]//8)*5, + (obj.shape[1]//8)*3:(obj.shape[1]//8)*5] + + obj = obj * np.exp(-1j * np.angle(np.sum(obj[obj_slice]))) + assert np.isclose(0,np.angle(np.sum(obj[obj_slice]))) + + # Now I make stacks of identical probes and objects + probes = [probe,probe,probe,probe] + probe = np.copy(probe) + objects = [obj,obj,obj,obj] + obj = np.copy(obj) + + s_probe, s_obj, obj_stack = analysis.synthesize_reconstructions(probes,objects) + assert np.max(s_probe - probe) < 1e-5 + assert np.max(s_obj - obj) < 1e-5 + for t_obj in obj_stack: + assert np.max(t_obj - obj) < 1e-5 + def test_calc_consistency_prtf(): @@ -129,7 +157,7 @@ def test_calc_consistency_prtf(): # synth_obj = np.sqrt(0.7) * obj - obj_stack = [obj]#,obj,obj,obj] + obj_stack = [obj,obj,obj,obj] basis = np.array([[0,2,0], [3,0,0]])