annotate projective_transformation_points.py @ 2:2749345d62fa draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
author imgteam
date Fri, 08 May 2020 09:21:29 +0000
parents ae600b1c6c48
children b9121ff67280
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 from skimage.transform import ProjectiveTransform
2
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
2 from scipy.ndimage import map_coordinates
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import numpy as np
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4 import pandas as pd
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import argparse
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7
2
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
8 def _stackcopy(a, b):
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
9 if a.ndim == 3:
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
10 a[:] = b[:, :, np.newaxis]
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
11 else:
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
12 a[:] = b
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
13
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
14
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
15 def warp_img_coords_batch(coord_map, shape, dtype=np.float64, batch_size=1000000):
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
16 rows, cols = shape[0], shape[1]
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
17 coords_shape = [len(shape), rows, cols]
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
18 if len(shape) == 3:
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
19 coords_shape.append(shape[2])
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
20 coords = np.empty(coords_shape, dtype=dtype)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
21
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
22 tf_coords = np.indices((cols, rows), dtype=dtype).reshape(2, -1).T
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
23
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
24 for i in range(0, (tf_coords.shape[0]//batch_size+1)):
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
25 tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)])
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
26 tf_coords = tf_coords.T.reshape((-1, cols, rows)).swapaxes(1, 2)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
27
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
28 _stackcopy(coords[1, ...], tf_coords[0, ...])
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
29 _stackcopy(coords[0, ...], tf_coords[1, ...])
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
30 if len(shape) == 3:
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
31 coords[2, ...] = range(shape[2])
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
32
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
33 return coords
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
34
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
35
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 def warp_coords_batch(coord_map, coords, dtype=np.float64, batch_size=1000000):
1
ae600b1c6c48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit 201f4231192bc368b14e72bb1314b9a97b8db8d0
imgteam
parents: 0
diff changeset
37 tf_coords = coords.astype(np.float32)[:, ::-1]
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
38
1
ae600b1c6c48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit 201f4231192bc368b14e72bb1314b9a97b8db8d0
imgteam
parents: 0
diff changeset
39 for i in range(0, (tf_coords.shape[0]//batch_size)+1):
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40 tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)])
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41
2
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
42 return tf_coords[:, ::-1]
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
43
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
44
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
45 def transform(coords, warp_matrix, out):
2
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
46 roi_coords = np.array(pd.read_csv(coords, delimiter="\t"))
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
47 trans_matrix = np.array(pd.read_csv(warp_matrix, delimiter="\t", header=None))
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
48
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
49 tol = 10
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
50 moving = np.zeros(np.max(roi_coords,axis=0)+tol, dtype=np.int8)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
51 idx_roi_coords = (roi_coords[:,0]-1) * moving.shape[1] + roi_coords[:,1] - 1
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
52 moving.flat[idx_roi_coords] = 1
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
53
2
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
54 transP = ProjectiveTransform(matrix=trans_matrix)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
55 roi_coords_warped_direct = warp_coords_batch(transP, roi_coords)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
56 shape_fixed = np.round(np.max(roi_coords_warped_direct,axis=0)).astype(roi_coords.dtype)+tol
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
57
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
58 transI = ProjectiveTransform(matrix=np.linalg.inv(trans_matrix))
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
59 img_coords_warped = warp_img_coords_batch(transI, shape_fixed)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
60
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
61 moving_warped = map_coordinates(moving, img_coords_warped, mode='constant', cval=0)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
62 idx_roi_coords_warped = np.where(moving_warped==1)
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
63
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
64 df = pd.DataFrame()
2
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
65 df['x'] = idx_roi_coords_warped[0] + 1
2749345d62fa "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit f1298dca5a7f5be3acbb5e3d80c98b1cd6d2795b"
imgteam
parents: 1
diff changeset
66 df['y'] = idx_roi_coords_warped[1] + 1
0
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
67 df.to_csv(out, index = False, sep="\t")
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
68
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
69
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
70 if __name__ == "__main__":
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
71 parser = argparse.ArgumentParser(description="Transform coordinates")
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
72 parser.add_argument("coords", help="Paste path to .csv with coordinates to transform (tab separated)")
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
73 parser.add_argument("warp_matrix", help="Paste path to .csv that should be used for transformation (, separated)")
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
74 parser.add_argument("out", help="Paste path to file in which transformed coords should be saved (tab separated)")
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
75 args = parser.parse_args()
dd792352f2a7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
76 transform(args.coords, args.warp_matrix, args.out)