annotate landmark_registration_ls.py @ 0:2f36165c49fb draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
author imgteam
date Tue, 29 Dec 2020 12:10:53 +0000
parents
children 69db8c7d4244
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
1 from scipy.linalg import lstsq
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
2 import pandas as pd
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
3 import numpy as np
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
4 import argparse
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
5
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
6 def landmark_registration_ls(pts_f1, pts_f2, out_f, delimiter="\t"):
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
7
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
8 points1 = pd.read_csv(pts_f1, delimiter=delimiter)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
9 points2 = pd.read_csv(pts_f2, delimiter=delimiter)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
10
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
11 src = np.concatenate([np.array(points1['x']).reshape([-1,1]),
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
12 np.array(points1['y']).reshape([-1,1])],
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
13 axis=-1)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
14 dst = np.concatenate([np.array(points2['x']).reshape([-1,1]),
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
15 np.array(points2['y']).reshape([-1,1])],
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
16 axis=-1)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
17
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
18 A = np.zeros((src.size,6))
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
19 A[0:src.shape[0],[0,1]] = src
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
20 A[0:src.shape[0],2] = 1
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
21 A[src.shape[0]:,[3,4]] = src
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
22 A[src.shape[0]:,5] = 1
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
23 b = dst.T.flatten().astype('float64')
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
24 x = lstsq(A,b)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
25
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
26 tmat = np.eye(3)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
27 tmat[0,:] = x[0].take([0,1,2])
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
28 tmat[1,:] = x[0].take([3,4,5])
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
29 pd.DataFrame(tmat).to_csv(out_f, header=None, index=False, sep="\t")
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
30
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
31
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
32 if __name__ == "__main__":
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
33
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
34 parser = argparse.ArgumentParser(description="Estimate transformation from points using least squares")
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
35
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
36 parser.add_argument("fn_pts1", help="File name src points")
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
37 parser.add_argument("fn_pts2", help="File name dst points")
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
38 parser.add_argument("fn_tmat", help="File name transformation matrix")
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
39 args = parser.parse_args()
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
40
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
41 landmark_registration_ls(args.fn_pts1, args.fn_pts2, args.fn_tmat)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
42
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
43