comparison projective_transformation_points.py @ 4:aaac58d83043 draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation_points/ commit 7391bb4256f49ec30cf38d9438f97e11ec25a115"
author imgteam
date Wed, 23 Dec 2020 23:56:29 +0000
parents 0d2707c82d29
children 3a686b6aa7fc
comparison
equal deleted inserted replaced
3:a84822a0060c 4:aaac58d83043
40 tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)]) 40 tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)])
41 41
42 return tf_coords[:, ::-1] 42 return tf_coords[:, ::-1]
43 43
44 44
45 def transform(coords, warp_matrix, out): 45 def transform(fn_roi_coords, fn_warp_matrix, fn_out):
46 roi_coords = np.array(pd.read_csv(coords, delimiter="\t")) 46 data = pd.read_csv(fn_roi_coords, delimiter="\t")
47 trans_matrix = np.array(pd.read_csv(warp_matrix, delimiter="\t", header=None)) 47 all_data = np.array(data)
48
49 nrows = all_data.shape[0]
50 ncols = all_data.shape[1]
51 roi_coords = all_data.take([0,1],axis=1).astype('int64')
48 52
49 tol = 10 53 tol = 10
50 moving = np.zeros(np.max(roi_coords,axis=0)+tol, dtype=np.int8) 54 moving = np.zeros(np.max(roi_coords,axis=0)+tol, dtype=np.uint32)
51 idx_roi_coords = (roi_coords[:,0]-1) * moving.shape[1] + roi_coords[:,1] - 1 55 idx_roi_coords = (roi_coords[:,0]-1) * moving.shape[1] + roi_coords[:,1] - 1
52 moving.flat[idx_roi_coords] = 1 56 moving.flat[idx_roi_coords] = np.transpose(np.arange(nrows)+1)
53 57
58 trans_matrix = np.array(pd.read_csv(fn_warp_matrix, delimiter="\t", header=None))
54 transP = ProjectiveTransform(matrix=trans_matrix) 59 transP = ProjectiveTransform(matrix=trans_matrix)
55 roi_coords_warped_direct = warp_coords_batch(transP, roi_coords) 60 roi_coords_warped_direct = warp_coords_batch(transP, roi_coords)
56 shape_fixed = np.round(np.max(roi_coords_warped_direct,axis=0)).astype(roi_coords.dtype)+tol 61 shape_fixed = np.round(np.max(roi_coords_warped_direct,axis=0)).astype(roi_coords.dtype)+tol
57 62
58 transI = ProjectiveTransform(matrix=np.linalg.inv(trans_matrix)) 63 transI = ProjectiveTransform(matrix=np.linalg.inv(trans_matrix))
59 img_coords_warped = warp_img_coords_batch(transI, shape_fixed) 64 img_coords_warped = warp_img_coords_batch(transI, shape_fixed)
60 65
61 moving_warped = map_coordinates(moving, img_coords_warped, mode='constant', cval=0) 66 moving_warped = map_coordinates(moving, img_coords_warped, order=0, mode='constant', cval=0)
62 idx_roi_coords_warped = np.where(moving_warped==1) 67 idx_roi_coords_warped = np.where(moving_warped>0)
68 roi_annots_warped = moving_warped.compress((moving_warped>0).flat)
63 69
64 df = pd.DataFrame() 70 df = pd.DataFrame()
71 col_names = data.columns.tolist()
65 df['x'] = idx_roi_coords_warped[0] + 1 72 df['x'] = idx_roi_coords_warped[0] + 1
66 df['y'] = idx_roi_coords_warped[1] + 1 73 df['y'] = idx_roi_coords_warped[1] + 1
67 df.to_csv(out, index = False, sep="\t") 74 if ncols>2:
75 for i in range(2,ncols):
76 df[col_names[i]] = all_data[:,i].take(roi_annots_warped)
77 df.to_csv(fn_out, index = False, sep="\t")
68 78
69 79
70 if __name__ == "__main__": 80 if __name__ == "__main__":
71 parser = argparse.ArgumentParser(description="Transform coordinates") 81 parser = argparse.ArgumentParser(description="Transform coordinates")
72 parser.add_argument("coords", help="Paste path to .csv with coordinates to transform (tab separated)") 82 parser.add_argument("coords", help="Paste path to .csv with coordinates (and labels) to transform (tab separated)")
73 parser.add_argument("warp_matrix", help="Paste path to .csv that should be used for transformation (, separated)") 83 parser.add_argument("warp_matrix", help="Paste path to .csv that should be used for transformation (tab separated)")
74 parser.add_argument("out", help="Paste path to file in which transformed coords should be saved (tab separated)") 84 parser.add_argument("out", help="Paste path to file in which transformed coords (and labels) should be saved (tab separated)")
75 args = parser.parse_args() 85 args = parser.parse_args()
76 transform(args.coords, args.warp_matrix, args.out) 86 transform(args.coords, args.warp_matrix, args.out)