annotate landmark_registration_ls.py @ 1:69db8c7d4244 draft default tip

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
author imgteam
date Sun, 20 Feb 2022 15:47:16 +0000
parents 2f36165c49fb
children
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 import argparse
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
2
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
3 import numpy as np
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
4 import pandas as pd
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
5 from scipy.linalg import lstsq
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
6
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
7
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
8 def landmark_registration_ls(pts_f1, pts_f2, out_f, delimiter="\t"):
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
9
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
10 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
11 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
12
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
13 src = np.concatenate([np.array(points1['x']).reshape([-1, 1]),
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
14 np.array(points1['y']).reshape([-1, 1])],
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
15 axis=-1)
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
16 dst = np.concatenate([np.array(points2['x']).reshape([-1, 1]),
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
17 np.array(points2['y']).reshape([-1, 1])],
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
18 axis=-1)
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
19
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
20 A = np.zeros((src.size, 6))
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
21 A[0:src.shape[0], [0, 1]] = src
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
22 A[0:src.shape[0], 2] = 1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
23 A[src.shape[0]:, [3, 4]] = src
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
24 A[src.shape[0]:, 5] = 1
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
25 b = dst.T.flatten().astype('float64')
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
26 x = lstsq(A, b)
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
27
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
28 tmat = np.eye(3)
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
29 tmat[0, :] = x[0].take([0, 1, 2])
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
30 tmat[1, :] = x[0].take([3, 4, 5])
0
2f36165c49fb "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit abd8b0b42f6a700d61c7b042dbf5e5a291b148a3"
imgteam
parents:
diff changeset
31 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
32
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 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
35 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
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()
1
69db8c7d4244 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/landmark_registration_ls/ commit 927b78d47c31714776ccdf3d16f26c3779298abb"
imgteam
parents: 0
diff changeset
40
0
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)