Example #1
0
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))
Example #2
0
def test_sino_radon():
    myframe = sys._getframe()
    sino = get_test_data_set_sino()
    rad = odtbrain.sinogram_as_radon(sino)
    twopi = 2*np.pi
    # When moving from unwrap to skimage, there was an offset introduced.
    # Since this particular array is not flat at the borders, there is no
    # correct way here. We just subtract 2PI.
    # 2019-04-18: It turns out that on Windows, this is not the case.
    # Hence, we only subtract 2PI if the minimum of the array is above
    # 2PI..
    if rad.min() > twopi:
        rad -= twopi
    if WRITE_RES:
        write_results(myframe, rad)
    assert np.allclose(np.array(rad).flatten().view(
        float), get_results(myframe))
    # Check the 3D result with the 2D result. They should be the same except
    # for a multiple of 2PI offset, because odtbrain._align_unwrapped
    # subtracts the background such that the minimum phase change is closest
    # to zero.
    # 2D A
    rad2d = odtbrain.sinogram_as_radon(sino[:, :, 0])
    assert np.allclose(0, negative_modulo_rest(
        rad2d - rad[:, :, 0], twopi), atol=1e-6)
    # 2D B
    rad2d2 = odtbrain.sinogram_as_radon(sino[:, 0, :])
    assert np.allclose(0, negative_modulo_rest(
        rad2d2 - rad[:, 0, :], twopi), atol=1e-6)
Example #3
0
def test_opt_to_ri():
    myframe = sys._getframe()
    f, res, nm = get_test_data_set()
    ri = odtbrain.opt_to_ri(f=f, res=res, nm=nm)
    if WRITE_RES:
        write_results(myframe, ri)
    assert np.allclose(np.array(ri).flatten().view(
        float), get_results(myframe))
    # Also test 2D version
    ri2d = odtbrain.opt_to_ri(f=f[0], res=res, nm=nm)
    assert np.allclose(ri2d, ri[0])
Example #4
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))
Example #5
0
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))
Example #6
0
def test_opt_to_ri():
    myframe = sys._getframe()
    myname = myframe.f_code.co_name
    print("running ", myname)
    f, res, nm = get_test_data_set()
    ri = odtbrain._br.opt_to_ri(f=f, res=res, nm=nm)
    if WRITE_RES:
        write_results(myframe, ri)
    assert np.allclose(np.array(ri).flatten().view(float), get_results(myframe))
    # Also test 2D version
    ri2d = odtbrain._br.opt_to_ri(f=f[0], res=res, nm=nm)
    assert np.allclose(ri2d, ri[0])
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_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
Example #9
0
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
Example #10
0
def test_3d_mprotate():
    myframe = sys._getframe()
    ln = 10
    ln2 = 2 * ln
    initial_array = np.arange(ln2**3).reshape((ln2, ln2, ln2))
    shared_array = mp.RawArray(ctypes.c_double, ln2 * ln2 * ln2)
    arr = np.frombuffer(shared_array).reshape(ln2, ln2, ln2)
    arr[:, :, :] = initial_array
    _alg3d_bpp.mprotate_dict["X"] = shared_array
    _alg3d_bpp.mprotate_dict["X_shape"] = (ln2, ln2, ln2)

    pool = mp.Pool(processes=mp.cpu_count(),
                   initializer=_alg3d_bpp._init_worker,
                   initargs=(shared_array, (ln2, ln2, ln2), np.dtype(float)))
    _alg3d_bpp._mprotate(2, ln, pool, 2)
    if WRITE_RES:
        write_results(myframe, arr)
    assert np.allclose(
        np.array(arr).flatten().view(float), get_results(myframe))
Example #11
0
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))
Example #12
0
def test_sino_radon():
    myframe = sys._getframe()
    myname = myframe.f_code.co_name
    print("running ", myname)
    sino = get_test_data_set_sino()
    rad = odtbrain._br.sinogram_as_radon(sino)
    if WRITE_RES:
        write_results(myframe, rad)
    assert np.allclose(np.array(rad).flatten().view(float), get_results(myframe))
    # Check the 3D result with the 2D result. They should be the same except
    # for a multiple of 2PI offset, because odtbrain._br._align_unwrapped
    # subtracts the background such that the minimum phase change is closest
    # to zero.
    # 2D A
    twopi = 2*np.pi
    rad2d = odtbrain._br.sinogram_as_radon(sino[:,:,0])
    assert np.allclose(0, negative_modulo_rest(rad2d - rad[:,:,0], twopi), atol=1e-6)
    # 2D B
    rad2d2 = odtbrain._br.sinogram_as_radon(sino[:,0,:])
    assert np.allclose(0, negative_modulo_rest(rad2d2 - rad[:,0,:], twopi), atol=1e-6)
Example #13
0
def test_3d_mprotate():
    myframe = sys._getframe()
    myname = myframe.f_code.co_name
    print("running ", myname)
    import ctypes
    import multiprocessing as mp

    ln = 10
    ln2 = 2*ln
    initial_array = np.arange(ln2**3).reshape((ln2,ln2,ln2))
    shared_array_base = mp.Array(ctypes.c_double, ln2 * ln2 * ln2)
    _shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
    _shared_array = _shared_array.reshape(ln2, ln2, ln2)
    _shared_array[:,:,:] = initial_array
    odtbrain._shared_array = _shared_array
    # pool must be created after _shared array
    pool = mp.Pool(processes=mp.cpu_count())
    odtbrain._Back_3D._mprotate(2, ln, pool, 2)
    if WRITE_RES:
        write_results(myframe, _shared_array)
    assert np.allclose(np.array(_shared_array).flatten().view(float), get_results(myframe))
Example #14
0
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))