changeset 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
files landmark_registration_ls.py landmark_registration_ls.xml test-data/points1.tsv test-data/points2.tsv test-data/tmat.tsv
diffstat 5 files changed, 87 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/landmark_registration_ls.py	Tue Dec 29 12:10:53 2020 +0000
@@ -0,0 +1,43 @@
+from scipy.linalg import lstsq
+import pandas as pd
+import numpy as np
+import argparse
+
+def landmark_registration_ls(pts_f1, pts_f2, out_f, delimiter="\t"):
+    
+    points1 = pd.read_csv(pts_f1, delimiter=delimiter)
+    points2 = pd.read_csv(pts_f2, delimiter=delimiter)
+
+    src = np.concatenate([np.array(points1['x']).reshape([-1,1]), 
+                          np.array(points1['y']).reshape([-1,1])], 
+                         axis=-1)
+    dst = np.concatenate([np.array(points2['x']).reshape([-1,1]), 
+                          np.array(points2['y']).reshape([-1,1])], 
+                         axis=-1)
+
+    A = np.zeros((src.size,6))
+    A[0:src.shape[0],[0,1]] = src
+    A[0:src.shape[0],2] = 1
+    A[src.shape[0]:,[3,4]] = src
+    A[src.shape[0]:,5] = 1
+    b = dst.T.flatten().astype('float64')
+    x = lstsq(A,b)
+    
+    tmat = np.eye(3)
+    tmat[0,:] = x[0].take([0,1,2])
+    tmat[1,:] = x[0].take([3,4,5])
+    pd.DataFrame(tmat).to_csv(out_f, header=None, index=False, sep="\t")
+
+
+if __name__ == "__main__":
+
+    parser = argparse.ArgumentParser(description="Estimate transformation from points using least squares")
+    
+    parser.add_argument("fn_pts1", help="File name src points")
+    parser.add_argument("fn_pts2", help="File name dst points")
+    parser.add_argument("fn_tmat", help="File name transformation matrix")
+    args = parser.parse_args()
+    
+    landmark_registration_ls(args.fn_pts1, args.fn_pts2, args.fn_tmat)
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/landmark_registration_ls.xml	Tue Dec 29 12:10:53 2020 +0000
@@ -0,0 +1,33 @@
+<tool id="ip_landmark_registration_ls" name="Landmark Registration" version="0.0.1">
+    <description>using least squares</description>
+    <requirements>
+        <requirement type="package" version="1.2.1">scipy</requirement>
+        <requirement type="package" version="0.23.4">pandas</requirement>
+        <requirement type="package" version="1.15.2">numpy</requirement>
+    </requirements>
+    <command><![CDATA[ 
+         python '$__tool_directory__/landmark_registration_ls.py'
+         '$fn_pts1'
+         '$fn_pts2'
+         '$fn_tmat'
+]]></command>
+    <inputs>
+          <param name="fn_pts1" type="data" format="tabular" label="Path to tab-separated file with src points" />
+          <param name="fn_pts2" type="data" format="tabular" label="Path to tab-separated file with dst points" />
+    </inputs>
+    <outputs>
+       <data format="tabular" name="fn_tmat" />
+    </outputs>
+    <tests>
+        <test>
+            <param name="fn_pts1" value="points1.tsv"/>
+            <param name="fn_pts2" value="points2.tsv"/>
+            <output name="fn_tmat" value="tmat.tsv" ftype="tabular" compare="diff" lines_diff="6"/>
+        </test>
+    </tests>
+    <help>
+    **What it does**
+
+    This tool estimates the (affine) transformation matrix using least squares given two sets of 2d coordinates of landmarks.
+    </help>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/points1.tsv	Tue Dec 29 12:10:53 2020 +0000
@@ -0,0 +1,4 @@
+	x	y
+0	1	6
+1	5	2
+2	13	15
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/points2.tsv	Tue Dec 29 12:10:53 2020 +0000
@@ -0,0 +1,4 @@
+	x	y
+0	3	5
+1	7	1
+2	15	14
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/tmat.tsv	Tue Dec 29 12:10:53 2020 +0000
@@ -0,0 +1,3 @@
+1.0000000000000002	-7.211110073938364e-17	1.9999999999999973
+7.211110073938364e-17	0.9999999999999999	-0.9999999999999973
+0.0	0.0	1.0