def test_3d_backprop_real(): """ Check if the real reconstruction matches the real part of the complex reconstruction. """ sino, angles = create_test_sino_3d(Nx=10, Ny=10) parameters = get_test_parameter_set(2) # complex r = list() for p in parameters: f = odtbrain.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, onlyreal=False, **p) r.append(f) # real r2 = list() for p in parameters: f = odtbrain.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, onlyreal=True, **p) r2.append(f) assert np.allclose(np.array(r).real, np.array(r2))
def test_angle_offset(): """ Tests if things are still correct when there is a 2PI offset in the angles. """ sino, angles = create_test_sino_2d() parameters = get_test_parameter_set(2) # reference r1 = [] for p in parameters: f1 = odtbrain.backpropagate_2d(sino, angles, weight_angles=False, **p) r1.append(f1) # with offset angles[::2] += 2*np.pi*np.arange(angles[::2].shape[0]) r2 = [] for p in parameters: f2 = odtbrain.backpropagate_2d(sino, angles, weight_angles=False, **p) r2.append(f2) # with offset and weights r3 = [] for p in parameters: f3 = odtbrain.backpropagate_2d(sino, angles, weight_angles=True, **p) r3.append(f3) assert np.allclose(np.array(r1).flatten().view(float), np.array(r2).flatten().view(float)) assert np.allclose(np.array(r2).flatten().view(float), np.array(r3).flatten().view(float))
def test_angle_swap(): """ Test if everything still works, when angles are swapped. """ myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_2d() # remove elements so that we can see that weighting works angles = angles[:-2] sino = sino[:-2,:] parameters = get_test_parameter_set(2) # reference r1 = [] for p in parameters: f1 = odtbrain._Back_2D.backpropagate_2d(sino, angles, weight_angles=True, **p) r1.append(f1) # change order of angles order = np.argsort(angles % .5) angles = angles[order] sino = sino[order,:] r2 = [] for p in parameters: f2 = odtbrain._Back_2D.backpropagate_2d(sino, angles, weight_angles=True, **p) r2.append(f2) assert np.allclose(np.array(r1).flatten().view(float), np.array(r2).flatten().view(float))
def test_back3d_tilted(): sino, angles = create_test_sino_3d(Nx=10, Ny=10) parameters = get_test_parameter_set(2) # complex r = list() for p in parameters: f = odtbrain.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, save_memory=False, **p) r.append(f) # real r2 = list() for p in parameters: f = odtbrain.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, save_memory=True, **p) r2.append(f) assert np.allclose(np.array(r), np.array(r2))
def test_3d_backprop_real(): """ Check if the real reconstruction matches the real part of the complex reconstruction. """ myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d(Nx=10, Ny=10) parameters = get_test_parameter_set(2) # complex r = list() for p in parameters: f = odtbrain._Back_3D.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, onlyreal=False, **p) r.append(f) # real r2 = list() for p in parameters: f = odtbrain._Back_3D.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, onlyreal=True, **p) r2.append(f) assert np.allclose(np.array(r).real, np.array(r2))
def test_correct_reproduce(): myframe = sys._getframe() sino, angles = create_test_sino_3d(Nx=10, Ny=10) p = get_test_parameter_set(1)[0] sryt = odtbrain.sinogram_as_rytov(uSin=sino, u0=1, align=False) f = odtbrain.backpropagate_3d(sryt, angles, padval=0, dtype=np.float64, copy=False, **p) fc = odtbrain.apple.correct(f=f, res=p["res"], nm=p["nm"], enforce_envelope=.95, max_iter=100, min_diff=0.01) fo = cutout(fc) fo = np.array(fo, dtype=np.complex128) if WRITE_RES: write_results(myframe, fo) data = fo.flatten().view(float) assert np.allclose(data, get_results(myframe))
def test_3d_backprop_plane_alignment_along_axes(): """ Tests whether the reconstruction is always aligned with the rotational axis (and not antiparallel). """ parameters = get_test_parameter_set(1) p = parameters[0] results = [] # These are specially selected angles that don't give high results. # Probably due to phase-wrapping, errors >2 may appear. Hence, we # call it a soft test. tilts = [0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi] for angz in tilts: sino, angles = create_test_sino_3d_tilted(tilt_plane=angz, A=21) rotmat = np.array([ [np.cos(angz), -np.sin(angz), 0], [np.sin(angz), np.cos(angz), 0], [0, 0, 1], ]) # rotate `tilted_axis` onto the y-z plane. tilted_axis = np.dot(rotmat, [0, 1, 0]) fref = odtbrain.backpropagate_3d_tilted(sino, angles, padval="edge", tilted_axis=tilted_axis, padding=(False, False), dtype=np.float64, onlyreal=True, **p) results.append(fref) for ii in np.arange(len(results)): assert np.allclose(results[ii], results[ii-1], atol=.2, rtol=.2)
def test_angle_offset(): """ Tests if things are still correct when there is a 2PI offset in the angles. """ myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_2d() parameters = get_test_parameter_set(2) # reference r1 = [] for p in parameters: f1 = odtbrain._Back_2D.backpropagate_2d(sino, angles, weight_angles=False, **p) r1.append(f1) # with offset angles[::2] += 2*np.pi*np.arange(angles[::2].shape[0]) r2 = [] for p in parameters: f2 = odtbrain._Back_2D.backpropagate_2d(sino, angles, weight_angles=False, **p) r2.append(f2) # with offset and weights r3 = [] for p in parameters: f3 = odtbrain._Back_2D.backpropagate_2d(sino, angles, weight_angles=True, **p) r3.append(f3) assert np.allclose(np.array(r1).flatten().view(float), np.array(r2).flatten().view(float)) assert np.allclose(np.array(r2).flatten().view(float), np.array(r3).flatten().view(float))
def test_2d_backprop_full(): myframe = sys._getframe() sino, angles = create_test_sino_2d(ampl_range=(0.9, 1.1)) parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain.backpropagate_2d(sino, angles, **p) r.append(cutout(f)) if WRITE_RES: write_results(myframe, r) assert np.allclose(np.array(r).flatten().view(float), get_results(myframe))
def test_fmp_2d(): sino, angles = create_test_sino_2d(N=10) p = get_test_parameter_set(1)[0] # complex jmc = mp.Value("i", 0) jmm = mp.Value("i", 0) odtbrain.fourier_map_2d(sino, angles, count=jmc, max_count=jmm, **p) assert jmc.value == jmm.value assert jmc.value != 0
def test_2d_fmap(): myframe = sys._getframe() sino, angles = create_test_sino_2d() parameters = get_test_parameter_set(1) r = [] for p in parameters: f = odtbrain.fourier_map_2d(sino, angles, **p) r.append(cutout(f)) if WRITE_RES: write_results(myframe, r) assert np.allclose(np.array(r).flatten().view(float), get_results(myframe))
def test_3d_backprop_weights_even(): """ even weights """ platform.system = lambda: "Windows" sino, angles = create_test_sino_3d() p = get_test_parameter_set(1)[0] f1 = odtbrain.backpropagate_3d(sino, angles, weight_angles=False, **p) f2 = odtbrain.backpropagate_3d(sino, angles, weight_angles=True, **p) data1 = np.array(f1).flatten().view(float) data2 = np.array(f2).flatten().view(float) assert np.allclose(data1, data2)
def test_2d_backprop_full(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_2d(ampl_range=(0.9,1.1)) parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain._Back_2D.backpropagate_2d(sino, angles, **p) r.append(cutout(f)) if WRITE_RES: write_results(myframe, r) assert np.allclose(np.array(r).flatten().view(float), get_results(myframe))
def test_back3d_tilted(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d(Nx=10, Ny=10) p = get_test_parameter_set(1)[0] f1 = odtbrain._Back_3D_tilted.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, copy=False, **p) f2 = odtbrain._Back_3D_tilted.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, copy=True, **p) assert np.allclose(f1, f2)
def test_3d_backprop_phase32(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain._Back_3D.backpropagate_3d(sino, angles, dtype=np.float32, padval=0, **p) r.append(cutout(f)) data32 = np.array(r).flatten().view(np.float32) data64 = test_3d_backprop_phase() assert np.allclose(data32, data64, atol=6e-7, rtol=0)
def test_3d_backprop_phase32(): sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain.backpropagate_3d(sino, angles, dtype=np.float32, padval=0, **p) r.append(cutout(f)) data32 = np.array(r).flatten().view(np.float32) data64 = test_3d_backprop_phase() assert np.allclose(data32, data64, atol=6e-7, rtol=0)
def test_bpp_2d(): sino, angles = create_test_sino_2d(N=10) p = get_test_parameter_set(1)[0] # complex jmc = mp.Value("i", 0) jmm = mp.Value("i", 0) odtbrain.backpropagate_2d(sino, angles, padval=0, count=jmc, max_count=jmm, **p) assert jmc.value == jmm.value assert jmc.value != 0
def test_back3d_tilted(): sino, angles = create_test_sino_3d(Nx=10, Ny=10) p = get_test_parameter_set(1)[0] # complex jmc = mp.Value("i", 0) jmm = mp.Value("i", 0) odtbrain.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, count=jmc, max_count=jmm, **p) assert jmc.value == jmm.value assert jmc.value != 0
def test_back3d(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d(Nx=10, Ny=10) p = get_test_parameter_set(1)[0] # complex jmc = mp.Value("i", 0) jmm = mp.Value("i", 0) f = odtbrain._Back_3D.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, jmc=jmc, jmm=jmm, **p) assert jmc.value == jmm.value assert jmc.value != 0
def test_3d_backprop_phase(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain._Back_3D.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, **p) r.append(cutout(f)) if WRITE_RES: write_results(myframe, r) data = np.array(r).flatten().view(float) assert np.allclose(data, get_results(myframe)) return data
def test_back3d_tilted(): sino, angles = create_test_sino_3d(Nx=10, Ny=10) p = get_test_parameter_set(1)[0] f1 = odtbrain.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, copy=False, **p) f2 = odtbrain.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, copy=True, **p) assert np.allclose(f1, f2)
def test_3d_backprop_phase(): myframe = sys._getframe() sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, **p) r.append(cutout(f)) if WRITE_RES: write_results(myframe, r) data = np.array(r).flatten().view(float) assert np.allclose(data, get_results(myframe)) return data
def test_fmap2d(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_2d(N=10) p = get_test_parameter_set(1)[0] # complex jmc = mp.Value("i", 0) jmm = mp.Value("i", 0) f = odtbrain._Back_2D.fourier_map_2d(sino, angles, jmc=jmc, jmm=jmm, **p) assert jmc.value == jmm.value assert jmc.value != 0
def test_3d_backprop_phase_real(): sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) # reference rref = list() for p in parameters: fref = odtbrain.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, onlyreal=True, **p) rref.append(cutout(fref)) dataref = np.array(rref).flatten().view(float) r = list() for p in parameters: f = odtbrain.backpropagate_3d_tilted(sino, angles, padval=0, dtype=np.float64, onlyreal=True, **p) r.append(cutout(f)) data = np.array(r).flatten().view(float) assert np.allclose(data, dataref)
def test_correct_values(): sino, angles = create_test_sino_3d(Nx=10, Ny=10) p = get_test_parameter_set(1)[0] f = odtbrain.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, copy=False, **p) try: odtbrain.apple.correct( f=f, res=p["res"], nm=p["nm"], enforce_envelope=1.05, ) except ValueError: pass else: assert False, "`enforce_envelope` must be in [0, 1]"
def test_3d_backprop_nopadreal(): """ - no padding - only real result """ platform.system = lambda:"Windows" myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain._Back_3D.backpropagate_3d(sino, angles, padding=(False,False), dtype=np.float64, onlyreal=True, **p) r.append(cutout(f)) if WRITE_RES: write_results(myframe, r) data = np.array(r).flatten().view(float) assert np.allclose(data, get_results(myframe))
def test_3d_backprop_plane_rotation(): """ A very soft test to check if planar rotation works fine in the reconstruction with tilted angles. """ myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) parameters = get_test_parameter_set(1) results = [] # These are specially selected angles that don't give high results. # Probably due to phase-wrapping, errors >2 may appear. Hence, we # call it a soft test. tilts = [1.1, 0.0, 0.234, 2.80922, -0.29, 9.87] for angz in tilts: sino, angles = create_test_sino_3d_tilted(tilt_plane=angz, A=21) rotmat = np.array([[np.cos(angz), -np.sin(angz), 0], [np.sin(angz), np.cos(angz), 0], [0, 0, 1]]) # rotate `tilted_axis` onto the y-z plane. tilted_axis = np.dot(rotmat, [0, 1, 0]) rref = list() for p in parameters: fref = odtbrain._Back_3D_tilted.backpropagate_3d_tilted( sino, angles, padval=None, tilted_axis=tilted_axis, padding=(False, False), dtype=np.float64, onlyreal=False, **p ) rref.append(cutout(fref)) data = np.array(rref).flatten().view(float) results.append(data) for ii in np.arange(len(results)): assert np.allclose(results[ii], results[ii - 1], atol=0.2, rtol=0.2)
def test_3d_backprop_phase_real(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) # reference rref = list() for p in parameters: fref = odtbrain._Back_3D.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, onlyreal=True, **p) rref.append(cutout(fref)) dataref = np.array(rref).flatten().view(float) r = list() for p in parameters: f = odtbrain._Back_3D_tilted.backpropagate_3d_tilted( sino, angles, padval=0, dtype=np.float64, onlyreal=True, **p ) r.append(cutout(f)) data = np.array(r).flatten().view(float) assert np.allclose(data, dataref)
def test_back3d(): myframe = sys._getframe() myname = myframe.f_code.co_name print("running ", myname) sino, angles = create_test_sino_3d(Nx=10, Ny=10) parameters = get_test_parameter_set(2) # complex r = list() for p in parameters: f = odtbrain._Back_3D.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, save_memory=False, **p) r.append(f) # real r2 = list() for p in parameters: f = odtbrain._Back_3D.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, save_memory=True, **p) r2.append(f) assert np.allclose(np.array(r), np.array(r2))
def test_correct_counter(): count = mp.Value("I", lock=True) max_count = mp.Value("I", lock=True) sino, angles = create_test_sino_3d(Nx=10, Ny=10) p = get_test_parameter_set(1)[0] f = odtbrain.backpropagate_3d(sino, angles, padval=0, dtype=np.float64, copy=False, **p) odtbrain.apple.correct(f=f, res=p["res"], nm=p["nm"], enforce_envelope=.95, max_iter=100, min_diff=0.01, count=count, max_count=max_count) assert count.value == max_count.value
def test_3d_backprop_nopadreal(): """ - no padding - only real result """ platform.system = lambda: "Windows" myframe = sys._getframe() sino, angles = create_test_sino_3d() parameters = get_test_parameter_set(2) r = list() for p in parameters: f = odtbrain.backpropagate_3d(sino, angles, padding=(False, False), dtype=np.float64, onlyreal=True, **p) r.append(cutout(f)) if WRITE_RES: write_results(myframe, r) data = np.array(r).flatten().view(float) assert np.allclose(data, get_results(myframe))
def test_3d_backprop_plane_rotation(): """ A very soft test to check if planar rotation works fine in the reconstruction with tilted angles. """ parameters = get_test_parameter_set(1) results = [] # These are specially selected angles that don't give high results. # Probably due to phase-wrapping, errors >2 may appear. Hence, we # call it a soft test. tilts = [1.1, 0.0, 0.234, 2.80922, -.29, 9.87] for angz in tilts: sino, angles = create_test_sino_3d_tilted(tilt_plane=angz, A=21) rotmat = np.array([ [np.cos(angz), -np.sin(angz), 0], [np.sin(angz), np.cos(angz), 0], [0, 0, 1], ]) # rotate `tilted_axis` onto the y-z plane. tilted_axis = np.dot(rotmat, [0, 1, 0]) rref = list() for p in parameters: fref = odtbrain.backpropagate_3d_tilted(sino, angles, padval="edge", tilted_axis=tilted_axis, padding=(False, False), dtype=np.float64, onlyreal=False, **p) rref.append(cutout(fref)) data = np.array(rref).flatten().view(float) results.append(data) for ii in np.arange(len(results)): assert np.allclose(results[ii], results[ii-1], atol=.2, rtol=.2)
def test_angle_swap(): """ Test if everything still works, when angles are swapped. """ sino, angles = create_test_sino_2d() # remove elements so that we can see that weighting works angles = angles[:-2] sino = sino[:-2, :] parameters = get_test_parameter_set(2) # reference r1 = [] for p in parameters: f1 = odtbrain.backpropagate_2d(sino, angles, weight_angles=True, **p) r1.append(f1) # change order of angles order = np.argsort(angles % .5) angles = angles[order] sino = sino[order, :] r2 = [] for p in parameters: f2 = odtbrain.backpropagate_2d(sino, angles, weight_angles=True, **p) r2.append(f2) assert np.allclose(np.array(r1).flatten().view(float), np.array(r2).flatten().view(float))