comparison points_association_nn.py @ 2:b30aa285ac0a draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points_association_nn/ commit bf6e2870c94ed4ceb8bce4003813fe11724f5ca2"
author imgteam
date Sun, 25 Jul 2021 20:10:46 +0000
parents 04e692ee53a8
children
comparison
equal deleted inserted replaced
1:fd4293bee0dc 2:b30aa285ac0a
8 """ 8 """
9 9
10 import argparse 10 import argparse
11 11
12 import numpy as np 12 import numpy as np
13 import openpyxl # noqa: F401
14 import pandas as pd 13 import pandas as pd
15 import skimage.util 14 import skimage.util
16 15
17 16
18 def disk_mask(imsz, ir, ic, nbpx): 17 def disk_mask(imsz, ir, ic, nbpx):
67 stack_r = np.zeros((stack_h, stack_w, nSlices), dtype='float64') 66 stack_r = np.zeros((stack_h, stack_w, nSlices), dtype='float64')
68 67
69 for i in range(all_data.shape[0]): 68 for i in range(all_data.shape[0]):
70 iyxz = tuple(coords[i, ::-1] - 1) 69 iyxz = tuple(coords[i, ::-1] - 1)
71 stack[iyxz] = 1 70 stack[iyxz] = 1
72 stack_r[iyxz] = all_data[i, -1] 71 if all_data.shape[1] == 4:
72 stack_r[iyxz] = all_data[i, -1]
73 else:
74 stack_r[iyxz] = 1
73 75
74 tracks_all = np.array([], dtype=float).reshape(0, nSlices, 4) 76 tracks_all = np.array([], dtype=float).reshape(0, nSlices, 4)
75 maxv = np.max(stack_r) 77 maxv = np.max(stack_r)
76 br_max = maxv 78 br_max = maxv
77 idx_max = np.argmax(stack_r) 79 idx_max = np.argmax(stack_r)
135 if not np.isnan(track[iz, 0]): 137 if not np.isnan(track[iz, 0]):
136 stack[track[iz, 2].astype(int) - 1, track[iz, 1].astype(int) - 1, iz] = 0 138 stack[track[iz, 2].astype(int) - 1, track[iz, 1].astype(int) - 1, iz] = 0
137 stack_r[track[iz, 2].astype(int) - 1, track[iz, 1].astype(int) - 1, iz] = 0 139 stack_r[track[iz, 2].astype(int) - 1, track[iz, 1].astype(int) - 1, iz] = 0
138 140
139 # discard short trajectories 141 # discard short trajectories
140 if np.count_nonzero(~np.isnan(spot_br)) > minlen * (frame_end - frame_1st) / 100: 142 if np.count_nonzero(~np.isnan(spot_br)) > np.max((1, minlen * (frame_end - frame_1st) / 100)):
141 tmp = np.concatenate((track, spot_br), axis=1) 143 tmp = np.concatenate((track, spot_br), axis=1)
142 tracks_all = np.concatenate((tracks_all, tmp.reshape(1, -1, 4)), axis=0) 144 tracks_all = np.concatenate((tracks_all, tmp.reshape(1, -1, 4)), axis=0)
143 145
144 maxv = np.max(stack_r) 146 maxv = np.max(stack_r)
145 idx_max = np.argmax(stack_r) 147 idx_max = np.argmax(stack_r)
146 if maxv < th * br_max / 100: 148 if maxv < th * br_max / 100 or maxv == 0:
147 break 149 break
148 150
149 with pd.ExcelWriter(fn_out, engine="openpyxl") as writer: 151 with pd.ExcelWriter(fn_out) as writer:
150 for i in range(tracks_all.shape[0]): 152 if tracks_all.shape[0] == 0:
151 df = pd.DataFrame() 153 df = pd.DataFrame()
152 df['FRAME'] = tracks_all[i, :, 0] 154 df['No tracks found'] = np.NaN
153 df['POS_X'] = tracks_all[i, :, 1] 155 df.to_excel(writer, index=False, float_format='%.2f')
154 df['POS_Y'] = tracks_all[i, :, 2] 156 else:
155 df['INTENSITY'] = tracks_all[i, :, 3] 157 for i in range(tracks_all.shape[0]):
156 df.to_excel(writer, sheet_name='spot%s' % (i + 1), index=False, float_format='%.2f') 158 df = pd.DataFrame()
159 df['FRAME'] = tracks_all[i, :, 0]
160 df['POS_X'] = tracks_all[i, :, 1]
161 df['POS_Y'] = tracks_all[i, :, 2]
162 if all_data.shape[1] == 4:
163 df['INTENSITY'] = tracks_all[i, :, 3]
164 df.to_excel(writer, sheet_name='spot%s' % (i + 1), index=False, float_format='%.2f')
157 writer.save() 165 writer.save()
158 166
159 167
160 if __name__ == "__main__": 168 if __name__ == "__main__":
161 parser = argparse.ArgumentParser(description="Association of points in consecutive frames using the nearest neighbor algorithm") 169 parser = argparse.ArgumentParser(description="Association of points in consecutive frames using the nearest neighbor algorithm")
162 parser.add_argument("fn_in", help="Name of input file (tsv tabular)") 170 parser.add_argument("fn_in", help="Coordinates (and intensities) of input points (tsv tabular)")
163 parser.add_argument("fn_out", help="Name of output file (xlsx)") 171 parser.add_argument("fn_out", help="Name of output file (xlsx)")
164 parser.add_argument("nbpx", type=int, help="Neighborhood size in pixel") 172 parser.add_argument("nbpx", type=int, help="Neighborhood size (in pixel) for associating points")
165 parser.add_argument("thres", type=float, help="Percentage of the global maximal intensity for thresholding some event") 173 parser.add_argument("thres", type=float, help="Tracks with all intensities lower than certain percentage (%) of the global maximal intensity will be discarded")
166 parser.add_argument("minlen", type=float, help="Minimum length of tracks (percentage of senquence length)") 174 parser.add_argument("minlen", type=float, help="Minimum length of tracks (percentage of senquence length)")
167 args = parser.parse_args() 175 args = parser.parse_args()
168 points_linking(args.fn_in, args.fn_out, args.nbpx, args.thres, args.minlen) 176 points_linking(args.fn_in, args.fn_out, args.nbpx, args.thres, args.minlen)