def test_rotation_model(): # can't do this rt = renderapi.transform.Polynomial2DTransform() with pytest.raises(AlignerTransformException): t = AlignerRotationModel(transform=rt) # check args rt = renderapi.transform.AffineModel() t = AlignerTransform(name='RotationModel', transform=rt) assert (t.__class__ == AlignerRotationModel) # make block t = AlignerTransform(name='RotationModel', transform=rt, fullsize=True) nmatch = 100 # scale up because rotation filters out things near center-of-mass match = example_match(nmatch, fac=1000) ncol = 1000 icol = 73 ppts, qpts, w = AlignerRotationModel.preprocess( np.array(match['matches']['p']).transpose(), np.array(match['matches']['q']).transpose(), np.array(match['matches']['w'])) assert ppts.shape == qpts.shape assert ppts.shape[0] <= nmatch block, weights, rhs = t.block_from_pts(ppts, w, icol, ncol) assert rhs.shape == ppts.shape assert block.check_format() is None assert weights.size == ppts.shape[0] assert block.shape == (ppts.shape[0] * t.rows_per_ptmatch, ncol) assert block.nnz == ppts.shape[0] # to vec t = AlignerTransform(name='RotationModel') v = t.to_solve_vec() assert np.all(v == np.array([0.0]).reshape(-1, 1)) # from vec ntiles = 6 vec = np.random.randn(ntiles) vec = vec.reshape(-1, 1) index = 0 for i in range(ntiles): t = AlignerTransform(name='RotationModel') index += t.from_solve_vec(vec[index:, :]) msub = t.rotation assert np.isclose(np.mod(np.abs(msub - vec[i][0]), 2.0 * np.pi), 0.0) # reg rdict = {"default_lambda": 1.2345, "translation_factor": 0.1} t = AlignerTransform(name='RotationModel') r = t.regularization(rdict) assert np.all(np.isclose(r, 1.2345))
def test_aliases(): t = AlignerTransform(name='affine') assert (t.__class__ == AlignerAffineModel) assert (not t.fullsize) t = AlignerTransform(name='affine_fullsize') assert (t.__class__ == AlignerAffineModel) assert (t.fullsize) t = AlignerTransform(name='rigid') assert (t.__class__ == AlignerSimilarityModel)
def test_transform(): # must specify something with pytest.raises(AlignerTransformException): t = AlignerTransform() # two ways to load affine t = AlignerTransform(name='AffineModel') assert (t.__class__ == AlignerAffineModel) del t rt = renderapi.transform.AffineModel() t = AlignerTransform(transform=rt) assert (t.__class__ == AlignerAffineModel) # two ways to load similarity t = AlignerTransform(name='SimilarityModel') assert (t.__class__ == AlignerSimilarityModel) del t rt = renderapi.transform.SimilarityModel() t = AlignerTransform(transform=rt) assert (t.__class__ == AlignerSimilarityModel) # two ways to load polynomial t = AlignerTransform(name='Polynomial2DTransform') assert (t.__class__ == AlignerPolynomial2DTransform) del t rt = renderapi.transform.Polynomial2DTransform(identity=True) t = AlignerTransform(transform=rt) assert (t.__class__ == AlignerPolynomial2DTransform) # specifying something not real with pytest.raises(AlignerTransformException): t = AlignerTransform(name='LudicrousModel')
def test_translation_model(): # can't do this rt = renderapi.transform.Polynomial2DTransform() with pytest.raises(AlignerTransformException): t = AlignerTranslationModel(transform=rt) # check args rt = renderapi.transform.AffineModel() t = AlignerTransform(name='TranslationModel', transform=rt) assert (t.__class__ == AlignerTranslationModel) # make block t = AlignerTransform(name='TranslationModel', transform=rt) nmatch = 100 match = example_match(nmatch) ncol = 1000 icol = 73 block, weights, rhs = t.block_from_pts( np.array(match['matches']['p']).transpose(), np.array(match['matches']['w']), icol, ncol) assert rhs.shape == (nmatch, 2) assert block.check_format() is None assert weights.size == nmatch * t.rows_per_ptmatch assert block.shape == (nmatch * t.rows_per_ptmatch, ncol) assert block.nnz == 1 * nmatch # to vec t = AlignerTransform(name='TranslationModel') v = t.to_solve_vec() assert np.all(v == np.array([0.0, 0.0]).reshape(-1, 2)) # from vec ntiles = 6 vec = np.random.randn(ntiles * 2) vec = vec.reshape(-1, 2) index = 0 for i in range(ntiles): t = AlignerTransform(name='TranslationModel') index += t.from_solve_vec(vec[index:, :]) msub = t.translation assert np.all(np.isclose(msub, vec[i])) # reg rdict = {"default_lambda": 1.2345, "translation_factor": 0.1} t = AlignerTransform(name='TranslationModel') r = t.regularization(rdict) assert np.all(np.isclose(r, 0.12345))
def test_similarity_model(): # can't do this rt = renderapi.transform.Polynomial2DTransform() with pytest.raises(AlignerTransformException): t = AlignerSimilarityModel(transform=rt) # check args rt = renderapi.transform.SimilarityModel() t = AlignerTransform(name='SimilarityModel', transform=rt) assert (t.__class__ == AlignerSimilarityModel) # make block t = AlignerTransform(name='SimilarityModel', transform=rt, fullsize=True) nmatch = 100 match = example_match(nmatch) ncol = 1000 icol = 73 block, weights, rhs = t.block_from_pts( np.array(match['matches']['p']).transpose(), np.array(match['matches']['w']), icol, ncol) assert np.all(np.isclose(rhs, 0.0)) assert block.check_format() is None assert weights.size == nmatch * t.rows_per_ptmatch assert block.shape == (nmatch * t.rows_per_ptmatch, ncol) assert block.nnz == 10 * nmatch # to vec t = AlignerTransform(name='SimilarityModel') v = t.to_solve_vec() assert np.all(v == np.array([1.0, 0.0, 0.0, 0.0]).reshape(-1, 1)) # from vec ntiles = 6 vi = [1.0, 0.02, -10.0, 12.1] vec = np.tile(vi, ntiles) vec = vec.reshape(-1, 1) index = 0 for i in range(ntiles): index += t.from_solve_vec(vec[index:, :]) msub = t.M.flatten()[[0, 1, 2, 5]] assert np.all(np.isclose(msub, vi)) # reg rdict = {"default_lambda": 1.0, "translation_factor": 0.1} t = AlignerTransform(name='SimilarityModel') r = t.regularization(rdict) assert np.all(r[[0, 1]] == 1.0) assert np.all(r[[2, 3]] == 0.1)
def test_thinplate_model(computeAffine): # can't do this rt = renderapi.transform.Polynomial2DTransform() with pytest.raises(AlignerTransformException): t = AlignerThinPlateSplineTransform(transform=rt) # or this with pytest.raises(AlignerTransformException): t = AlignerTransform(name='ThinPlateSplineTransform') hw = 5000 x = y = np.linspace(0, hw, 4) xt, yt = np.meshgrid(x, y) src = np.vstack((xt.flatten(), yt.flatten())).transpose() dst = src + np.random.randn(src.shape[0], src.shape[1]) * 50 # check args rt = renderapi.transform.ThinPlateSplineTransform() rt.estimate(src, dst, computeAffine=computeAffine) t = AlignerTransform(name='ThinPlateSplineTransform', transform=rt) assert (t.__class__ == AlignerThinPlateSplineTransform) # make block nmatch = 100 match = example_match(nmatch, fac=hw) ncol = 1000 icol = 73 block, weights, rhs = t.block_from_pts( np.array(match['matches']['p']).transpose(), np.array(match['matches']['w']), icol, ncol) assert rhs.shape == (nmatch, 2) assert block.check_format() is None assert weights.size == nmatch * t.rows_per_ptmatch assert block.shape == (nmatch * t.rows_per_ptmatch, ncol) if computeAffine: assert block.nnz == nmatch * (t.srcPts.shape[1] + 3) else: assert block.nnz == nmatch * t.srcPts.shape[1] # to vec v = t.to_solve_vec() if computeAffine: assert v.shape == (t.srcPts.shape[1] + 3, 2) else: assert v.shape == (t.srcPts.shape[1], 2) # from vec ntiles = 6 vec = np.tile(v, (ntiles, 1)) vec = vec.reshape(-1, 2) index = 0 orig = renderapi.transform.ThinPlateSplineTransform() orig.estimate(src, dst, computeAffine=computeAffine) for i in range(ntiles): index += t.from_solve_vec(vec[index:, :]) assert np.all(np.isclose(orig.dMtxDat, t.dMtxDat)) if computeAffine: assert np.all(np.isclose(orig.aMtx, t.aMtx)) assert np.all(np.isclose(orig.bVec, t.bVec)) # reg rdict = { "default_lambda": 1.2345, "translation_factor": 0.1, "thinplate_factor": 1000 } r = t.regularization(rdict) n0 = 0 if computeAffine: assert np.isclose( r[0], rdict['default_lambda'] * rdict['translation_factor']) assert np.all(np.isclose(r[1:3], rdict['default_lambda'])) n0 += 3 assert np.all( np.isclose(r[n0:], rdict['default_lambda'] * rdict['thinplate_factor'])) # scale assert isinstance(t.scale, tuple) assert len(t.scale) == 2
def test_transform(): # must specify something with pytest.raises(AlignerTransformException): t = AlignerTransform() # two ways to load affine t = AlignerTransform(name='AffineModel') assert (t.__class__ == AlignerAffineModel) del t rt = renderapi.transform.AffineModel() t = AlignerTransform(name='AffineModel', transform=rt) assert (t.__class__ == AlignerAffineModel) # two ways to load similarity t = AlignerTransform(name='SimilarityModel') assert (t.__class__ == AlignerSimilarityModel) del t rt = renderapi.transform.SimilarityModel() t = AlignerTransform(name='SimilarityModel', transform=rt) assert (t.__class__ == AlignerSimilarityModel) # two ways to load rotation t = AlignerTransform(name='RotationModel') assert (t.__class__ == AlignerRotationModel) del t rt = renderapi.transform.AffineModel() t = AlignerTransform(name='RotationModel', transform=rt) assert (t.__class__ == AlignerRotationModel) # two ways to load translation t = AlignerTransform(name='TranslationModel') assert (t.__class__ == AlignerTranslationModel) del t rt = renderapi.transform.AffineModel() t = AlignerTransform(name='TranslationModel', transform=rt) assert (t.__class__ == AlignerTranslationModel) # two ways to load polynomial t = AlignerTransform(name='Polynomial2DTransform') assert (t.__class__ == AlignerPolynomial2DTransform) del t rt = renderapi.transform.Polynomial2DTransform(identity=True) t = AlignerTransform(name='Polynomial2DTransform', transform=rt) assert (t.__class__ == AlignerPolynomial2DTransform) # one way to load thinplatespline x = y = np.linspace(0, 2000, 4) xt, yt = np.meshgrid(x, y) src = np.vstack((xt.flatten(), yt.flatten())).transpose() dst = rt.tform(src) tps = renderapi.transform.ThinPlateSplineTransform() tps.estimate(src, dst) t = AlignerTransform(name='ThinPlateSplineTransform', transform=tps) assert (t.__class__ == AlignerThinPlateSplineTransform) del t # specifying something not real with pytest.raises(AlignerTransformException): AlignerTransform(name='LudicrousModel')
def test_polynomial_model(): # check args for o in range(4): t = AlignerTransform(name="Polynomial2DTransform", order=o) assert (t.__class__ == AlignerPolynomial2DTransform) assert (t.order == o) rt = renderapi.transform.AffineModel() for o in range(4): t = AlignerTransform(name="Polynomial2DTransform", order=o, transform=rt) assert (t.__class__ == AlignerPolynomial2DTransform) assert (t.order == o) # make block for order in range(4): n = int((order + 1) * (order + 2) / 2) params = np.zeros((2, n)) rt = renderapi.transform.Polynomial2DTransform(params=params) t = AlignerTransform(name='Polynomial2DTransform', transform=rt) nmatch = 100 match = example_match(nmatch) ncol = 1000 icol = 73 block, weights, rhs = t.block_from_pts( np.array(match['matches']['p']).transpose(), np.array(match['matches']['w']), icol, ncol) assert np.all(np.isclose(rhs, 0.0)) assert block.check_format() is None assert weights.size == nmatch assert block.shape == (nmatch, ncol) assert block.nnz == n * nmatch # to vec for order in range(4): n = int((order + 1) * (order + 2) / 2) params = np.random.randn(2, n) rt = renderapi.transform.Polynomial2DTransform(params=params) t = AlignerTransform(name='Polynomial2DTransform', transform=rt) v = t.to_solve_vec() assert np.all(np.isclose(v, np.transpose(params))) # from vec for order in range(4): n = int((order + 1) * (order + 2) / 2) v0 = np.random.randn(n, 2) rt0 = renderapi.transform.Polynomial2DTransform(params=np.zeros((2, n))) t = AlignerTransform(name='Polynomial2DTransform', transform=rt0) assert t.order == order vec = np.concatenate((v0, v0, v0, v0)) index = 0 for i in range(4): index += t.from_solve_vec(vec[index:, :]) assert np.all(np.isclose(t.params.transpose(), v0)) # reg for order in range(4): n = int((order + 1) * (order + 2) / 2) vec = np.zeros((n, 2)) rt0 = renderapi.transform.Polynomial2DTransform(params=vec) t = AlignerTransform(name='Polynomial2DTransform', transform=rt0) rdict = { "default_lambda": 1.0, "translation_factor": 0.1, "poly_factors": None } r = t.regularization(rdict) assert np.isclose(r[0], 0.1) assert np.all(np.isclose(r[1:], 1.0)) # reg for order in range(4): n = int((order + 1) * (order + 2) / 2) vec = np.zeros((n, 2)) rt0 = renderapi.transform.Polynomial2DTransform(params=vec) t = AlignerTransform(name='Polynomial2DTransform', transform=rt0) pf = np.random.randn(order + 1) rdict = { "default_lambda": 1.0, "translation_factor": 0.1, "poly_factors": pf.tolist() } r = t.regularization(rdict) ni = 0 for i in range(order + 1): for j in range(i + 1): assert np.all(r[ni::n] == pf[i]) ni += 1
def test_affine_model(): # can't do this rt = renderapi.transform.Polynomial2DTransform() with pytest.raises(AlignerTransformException): t = AlignerAffineModel(transform=rt) # check args rt = renderapi.transform.AffineModel() t = AlignerTransform(name='AffineModel', transform=rt) assert (t.__class__ == AlignerAffineModel) assert (not t.fullsize) t = AlignerTransform(name='AffineModel', transform=rt, fullsize=True) assert (t.__class__ == AlignerAffineModel) assert (t.fullsize) # make block (fullsize) t = AlignerTransform(name='AffineModel', transform=rt, fullsize=True) nmatch = 100 match = example_match(nmatch) ncol = 1000 icol = 73 block, weights, rhs = t.block_from_pts( np.array(match['matches']['p']).transpose(), np.array(match['matches']['w']), icol, ncol) assert np.all(np.isclose(rhs, 0.0)) assert block.check_format() is None assert weights.size == nmatch * t.rows_per_ptmatch assert block.shape == (nmatch * t.rows_per_ptmatch, ncol) assert block.nnz == 2 * nmatch * 3 # make CSR (halfsize) t = AlignerTransform(name='AffineModel', transform=rt, fullsize=False) nmatch = 100 match = example_match(nmatch) ncol = 1000 icol = 73 block, weights, rhs = t.block_from_pts( np.array(match['matches']['p']).transpose(), np.array(match['matches']['w']), icol, ncol) assert np.all(np.isclose(rhs, 0.0)) assert block.check_format() is None assert weights.size == nmatch * t.rows_per_ptmatch assert block.shape == (nmatch * t.rows_per_ptmatch, ncol) assert block.nnz == nmatch * 3 # to vec t = AlignerTransform(name='AffineModel', transform=rt, fullsize=True) v = t.to_solve_vec() assert np.all(v == np.array([1.0, 0.0, 0.0, 0.0, 1.0, 0.0]).reshape(6, 1)) t.fullsize = False v = t.to_solve_vec() assert np.all(v == np.array([[1.0, 0.0], [0.0, 1.0], [0.0, 0.0]])) # from vec t.fullsize = True ntiles = 6 vi = [1.0, 0.2, 0.0, -0.1, 1.0, 0.0] vec = np.tile(vi, ntiles) vec = vec.reshape(-1, 1) index = 0 for i in range(ntiles): index += t.from_solve_vec(vec[index:, :]) assert np.all(np.isclose(t.M[0:2, :].flatten(), vi)) t.fullsize = False vi = np.array([[1.0, 0.2, 0.0], [-0.1, 1.0, 0.0]]).transpose() vec = np.tile(vi, reps=[ntiles, 1]) index = 0 for i in range(ntiles): index += t.from_solve_vec(vec[index:, :]) assert np.all(np.isclose(t.M[0:2, :], vi.transpose())) # reg rdict = {"default_lambda": 1.0, "translation_factor": 0.1} t = AlignerTransform(name='AffineModel', transform=rt, fullsize=True) r = t.regularization(rdict) assert np.all(r[[0, 1, 3, 4]] == 1.0) assert np.all(r[[2, 5]] == 0.1) t = AlignerTransform(name='AffineModel', transform=rt, fullsize=False) r = t.regularization(rdict) assert np.all(r[[0, 1]] == 1.0) assert np.all(r[[2]] == 0.1)
def test_polynomial_model(): # can't do this rt = renderapi.transform.AffineModel() with pytest.raises(AlignerTransformException): t = AlignerPolynomial2DTransform(transform=rt) # check args for o in range(4): t = AlignerTransform(name="Polynomial2DTransform", order=o) assert (t.__class__ == AlignerPolynomial2DTransform) assert (t.order == o) # make CSR for order in range(4): n = int((order + 1) * (order + 2) / 2) params = np.zeros((2, n)) rt = renderapi.transform.Polynomial2DTransform(params=params) t = AlignerTransform(transform=rt) match = example_match(100) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 5, 500, True) indptr = np.insert(indptr, 0, 0) c = csr_matrix((data, indices, indptr)) assert c.check_format() is None assert weights.size == 100 * t.rows_per_ptmatch assert npts == 100 # make CSR zero weights t = AlignerTransform(transform=rt) match = example_match(100) match['matches']['w'] = list(np.zeros(100 * t.rows_per_ptmatch)) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 5, 500, True) assert data is None # minimum size t = AlignerTransform(transform=rt) match = example_match(100) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 200, 500, True) assert data is None # to vec for order in range(4): n = int((order + 1) * (order + 2) / 2) params = np.zeros((2, n)) rt = renderapi.transform.Polynomial2DTransform(params=params) t = AlignerTransform(transform=rt) v = t.to_solve_vec(rt) assert np.all(v == np.transpose(params)) rt = renderapi.transform.AffineModel() v = t.to_solve_vec(rt) it = renderapi.transform.Polynomial2DTransform(identity=True) if order == 0: assert (np.all(v == 0)) else: assert np.all(v[0:3, :] == np.transpose(it.params)) if v.shape[0] > 3: assert np.all(v[3:, :] == 0) rt = renderapi.transform.NonLinearCoordinateTransform() with pytest.raises(AlignerTransformException): v = t.to_solve_vec(rt) # pass low-order input into a higher-order solve tform osolve = 3 n = int((osolve + 1) * (osolve + 2) / 2) params = np.zeros((2, n)) rt = renderapi.transform.Polynomial2DTransform(params=params) t = AlignerTransform(transform=rt) assert t.to_solve_vec(rt).shape == (n, 2) oin = 2 n2 = int((oin + 1) * (oin + 2) / 2) params = np.zeros((2, n2)) rt2 = renderapi.transform.Polynomial2DTransform(params=params) assert t.to_solve_vec(rt2).shape == (n, 2) # try to pass in an uninitialized transform n = int((order + 1) * (order + 2) / 2) params = np.zeros((2, n)) rt = renderapi.transform.Polynomial2DTransform(params=params) t = AlignerTransform(transform=rt) uninit = renderapi.transform.Polynomial2DTransform() with pytest.raises(AlignerTransformException): v = t.to_solve_vec(uninit) # from vec for order in range(4): n = int((order + 1) * (order + 2) / 2) vec = np.zeros((n, 2)) rt0 = renderapi.transform.Polynomial2DTransform(params=vec) t = AlignerTransform(transform=rt0) vec = np.concatenate((vec, vec, vec, vec)) tforms = t.from_solve_vec(vec) assert len(tforms) == 4 for rt in tforms: assert np.all(np.isclose(np.transpose(rt.params), rt0.params)) # reg for order in range(4): n = int((order + 1) * (order + 2) / 2) vec = np.zeros((n, 2)) rt0 = renderapi.transform.Polynomial2DTransform(params=vec) t = AlignerTransform(transform=rt0) rdict = { "default_lambda": 1.0, "translation_factor": 0.1, "poly_factors": None } r = t.create_regularization(n * 17, rdict) assert np.all(r.data[0::n] == 0.1) for j in range(1, n): assert np.all(r.data[j::n] == 1.0) # reg for order in range(4): n = int((order + 1) * (order + 2) / 2) vec = np.zeros((n, 2)) rt0 = renderapi.transform.Polynomial2DTransform(params=vec) t = AlignerTransform(transform=rt0) pf = np.random.randn(order + 1) rdict = { "default_lambda": 1.0, "translation_factor": 0.1, "poly_factors": pf.tolist() } r = t.create_regularization(n * 17, rdict) ni = 0 for i in range(order + 1): for j in range(i + 1): assert np.all(r.data[ni::n] == pf[i]) ni += 1
def test_similarity_model(): # can't do this rt = renderapi.transform.Polynomial2DTransform() with pytest.raises(AlignerTransformException): t = AlignerSimilarityModel(transform=rt) # check args rt = renderapi.transform.SimilarityModel() t = AlignerTransform(transform=rt) assert (t.__class__ == AlignerSimilarityModel) # make CSR t = AlignerTransform(transform=rt) match = example_match(100) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 5, 500, True) indptr = np.insert(indptr, 0, 0) c = csr_matrix((data, indices, indptr)) assert c.check_format() is None assert weights.size == 100 * t.rows_per_ptmatch assert npts == 100 # make CSR zero weights t = AlignerTransform(transform=rt, fullsize=False) match = example_match(100) match['matches']['w'] = list(np.zeros(100 * t.rows_per_ptmatch)) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 5, 500, True) assert data is None t = AlignerTransform(transform=rt, fullsize=True) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 5, 500, True) assert data is None # minimum size t = AlignerTransform(transform=rt, fullsize=False) match = example_match(100) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 200, 500, True) assert data is None t = AlignerTransform(transform=rt, fullsize=True) data, indices, indptr, weights, npts = t.CSR_from_tilepair( match, 1, 2, 200, 500, True) assert data is None # to vec rt = renderapi.transform.SimilarityModel() t = AlignerTransform(transform=rt) v = t.to_solve_vec(rt) assert np.all(v == np.array([1, 0, 0, 0]).reshape(4, 1)) rt = renderapi.transform.Polynomial2DTransform(identity=True) v = t.to_solve_vec(rt) assert np.all(v == np.array([1, 0, 0, 0]).reshape(4, 1)) rt = renderapi.transform.NonLinearCoordinateTransform() with pytest.raises(AlignerTransformException): v = t.to_solve_vec(rt) # from vec vec = np.tile([1.0, 0.0, 0.0, 0.0], 6) tforms = t.from_solve_vec(vec) assert len(tforms) == 6 for rt in tforms: assert np.all(np.isclose(rt.M, renderapi.transform.SimilarityModel().M)) # reg rdict = {"default_lambda": 1.0, "translation_factor": 0.1} r = t.create_regularization(96, rdict) assert np.all(r.data[0::4] == 1.0) assert np.all(r.data[1::4] == 1.0) assert np.all(r.data[2::4] == 0.1) assert np.all(r.data[3::4] == 0.1)