def test_1px_1(): """shift by one pixel on axis 1""" resultsSC = np.zeros(im0.shape + (basis.shape[1], ), np.float32) for z, r in enumerate(cb.generator(im0)): resultsSC[z] = mSC(r) A0 = qAbc.A(resultsSC) b0 = qAbc.b(resultsSC) c0 = qAbc.c(resultsSC) #shift by one pixel on axis 1 im1 = np.zeros((64, 64, 64)) im1[29:32, 31:34, 32:35] = 1 for z, r in enumerate(cb.generator(im1)): resultsSC[z] = mSC(r) A1 = qAbc.A(resultsSC) np.testing.assert_array_almost_equal(A1[:, 1:], A0[:, :-1]) b1 = qAbc.b(resultsSC) np.testing.assert_array_almost_equal(b1[:, 1:], b0[:, :-1]) c1 = qAbc.c(resultsSC) np.testing.assert_array_almost_equal(c1[:, 1:], c0[:, :-1]) # no initial guess of the displacement A, Delta_b = memory_efficient.prepare_displacement_matrices_homogeneous( A0, b0, A1, b1) assert np.all(Delta_b == -0.5 * (b1 - b0)) assert np.all(A == 0.5 * (A0 + A1)) #A must be symmetric for i in range(A.shape[-2]): for j in range(A.shape[-1]): assert np.all(A[..., i, j] == A[..., j, i]) M = memory_efficient.A_Deltab2G_h(A, Delta_b) Gh = np.empty_like(M) for z, m in enumerate(cb2.generator(M)): Gh[z] = mSNC2(m, z, zlen=len(M), n_fields=N * (N + 3) // 2)[..., 0] displ = memory_efficient.Gh2displ(Gh[..., :-N], Gh[..., -N:]) np.testing.assert_almost_equal(displ[30, 31, 33, 0], 0) np.testing.assert_almost_equal(displ[30, 31, 33, 1], 1, 1) np.testing.assert_almost_equal(displ[30, 31, 33, 2], 0) #initial guess of the displacement d0 = np.array([0, 1, 0]) A, Delta_b = memory_efficient.prepare_displacement_matrices_homogeneous( A0, b0, A1, b1, d0) assert np.all(A == A0) #np.testing.assert_almost_equal(Delta_b, A @ d0, 0) M = memory_efficient.A_Deltab2G_h(A, Delta_b) Gh = np.empty_like(M) for z, m in enumerate(cb2.generator(M)): Gh[z] = mSNC2(m, z, zlen=len(M), n_fields=N * (N + 3) // 2)[..., 0] displ = memory_efficient.Gh2displ(Gh[..., :-N], Gh[..., -N:]) np.testing.assert_almost_equal(displ[30, 31, 33, 0], 0) np.testing.assert_almost_equal(displ[30, 31, 33, 1], 1, 1) np.testing.assert_almost_equal(displ[30, 31, 33, 2], 0)
def test_1px_0(): """shift by one pixel on axis 0""" resultsSC = np.zeros(im0.shape + (basis.shape[1], ), np.float32) for z, r in enumerate(cb.generator(im0)): resultsSC[z] = mSC(r) A0 = qAbc.A(resultsSC) b0 = qAbc.b(resultsSC) c0 = qAbc.c(resultsSC) #shift by one pixel on axis 0 im1 = np.zeros((64, 64)) im1[31:34, 32:35] = 1 for z, r in enumerate(cb.generator(im1)): resultsSC[z] = mSC(r) A1 = qAbc.A(resultsSC) assert np.all(A1[1:] == A0[:-1]) b1 = qAbc.b(resultsSC) assert np.all(b1[1:] == b0[:-1]) c1 = qAbc.c(resultsSC) assert np.all(c1[1:] == c0[:-1]) # no initial guess of the displacement A, Delta_b = memory_efficient.prepare_displacement_matrices_homogeneous( A0, b0, A1, b1) assert np.all(Delta_b == -0.5 * (b1 - b0)) assert np.all(A == 0.5 * (A0 + A1)) M = memory_efficient.A_Deltab2G_h(A, Delta_b) Gh = np.empty_like(M) for z, m in enumerate(cb2.generator(M)): Gh[z] = mSNC2(m, z, zlen=len(M), n_fields=N * (N + 3) // 2)[..., 0] displ = memory_efficient.Gh2displ(Gh[..., :-N], Gh[..., -N:]) assert int(displ[31, 33, 0]) == 1 assert int(displ[31, 33, 1]) == 0 #initial guess of the displacement d0 = np.array([1, 0]) A, Delta_b = memory_efficient.prepare_displacement_matrices_homogeneous( A0, b0, A1, b1, d0) assert np.all(A == A0) #np.testing.assert_almost_equal(Delta_b, A @ d0, 0) M = memory_efficient.A_Deltab2G_h(A, Delta_b) Gh = np.empty_like(M) for z, m in enumerate(cb2.generator(M)): Gh[z] = mSNC2(m, z, zlen=len(M), n_fields=N * (N + 3) // 2)[..., 0] displ = memory_efficient.Gh2displ(Gh[..., :-N], Gh[..., -N:]) assert int(displ[31, 33, 0]) == 1 assert int(displ[31, 33, 1]) == 0
def test_no_displacement(): """identical images should not detect displacement""" resultsSC = np.zeros(im0.shape + (basis.shape[1], ), np.float32) for z, r in enumerate(cb.generator(im0)): resultsSC[z] = mSC(r) A0 = qAbc.A(resultsSC) b0 = qAbc.b(resultsSC) #identical images should not detect displacement A, Delta_b = memory_efficient.prepare_displacement_matrices_homogeneous( A0, b0, A0, b0) assert np.all(A == A0) assert np.all(Delta_b == 0) M = memory_efficient.A_Deltab2G_h(A, Delta_b) Gh = np.empty_like(M) for z, m in enumerate(cb2.generator(M)): Gh[z] = mSNC2(m, z, zlen=len(M), n_fields=N * (N + 3) // 2)[..., 0] displ = memory_efficient.Gh2displ(Gh[..., :-N], Gh[..., -N:]) assert np.all(displ == 0)