Conditions | 7 |
Total Lines | 125 |
Code Lines | 85 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """ |
||
261 | def main(args=None): |
||
262 | """ |
||
263 | CLI for deepreg_vis tool. |
||
264 | |||
265 | Requires ffmpeg wirter to write gif files. |
||
266 | |||
267 | :param args: |
||
268 | """ |
||
269 | parser = argparse.ArgumentParser( |
||
270 | description="deepreg_vis", formatter_class=argparse.RawTextHelpFormatter |
||
271 | ) |
||
272 | |||
273 | parser.add_argument( |
||
274 | "--mode", |
||
275 | "-m", |
||
276 | help="Mode of visualisation \n" |
||
277 | "0 for animtion over image slices, \n" |
||
278 | "1 for warp animation, \n" |
||
279 | "2 for tile plot", |
||
280 | type=int, |
||
281 | required=True, |
||
282 | ) |
||
283 | parser.add_argument( |
||
284 | "--image-paths", |
||
285 | "-i", |
||
286 | help="File path for image file " |
||
287 | "(can specify multiple paths using a comma separated string)", |
||
288 | type=str, |
||
289 | required=True, |
||
290 | ) |
||
291 | parser.add_argument( |
||
292 | "--save-path", |
||
293 | "-s", |
||
294 | help="Path to directory where resulting visualisation is saved", |
||
295 | default="", |
||
296 | ) |
||
297 | |||
298 | parser.add_argument( |
||
299 | "--interval", |
||
300 | help="Interval between frames of animation (in miliseconds)\n" |
||
301 | "Applicable only if --mode 0 or --mode 1 or --mode 3", |
||
302 | type=int, |
||
303 | default=50, |
||
304 | ) |
||
305 | parser.add_argument( |
||
306 | "--ddf-path", |
||
307 | help="Path to ddf used for warping images\n" |
||
308 | "Applicable only and required if --mode 1", |
||
309 | type=str, |
||
310 | default=None, |
||
311 | ) |
||
312 | parser.add_argument( |
||
313 | "--num-interval", |
||
314 | help="Number of intervals to use for warping\n" "Applicable only if --mode 1", |
||
315 | type=int, |
||
316 | default=100, |
||
317 | ) |
||
318 | parser.add_argument( |
||
319 | "--slice-inds", |
||
320 | help="Comma separated string of indexes of slices" |
||
321 | " to be used for the visualisation\n" |
||
322 | "Applicable only if --mode 1 or --mode 2", |
||
323 | type=str, |
||
324 | default=None, |
||
325 | ) |
||
326 | parser.add_argument( |
||
327 | "--fname", |
||
328 | help="File name (with extension like .png, .jpeg, .gif, ...)" |
||
329 | " to save visualisation to\n" |
||
330 | "Applicable only if --mode 2 or --mode 3", |
||
331 | type=str, |
||
332 | default=None, |
||
333 | ) |
||
334 | parser.add_argument( |
||
335 | "--col-titles", |
||
336 | help="Comma separated string of column titles to use " |
||
337 | "(inferred from file names if not provided)\n" |
||
338 | "Applicable only if --mode 2", |
||
339 | default=None, |
||
340 | ) |
||
341 | parser.add_argument( |
||
342 | "--size", |
||
343 | help="Comma separated string of number of columns and rows (e.g. '2,2')\n" |
||
344 | "Applicable only if --mode 3", |
||
345 | default="2,2", |
||
346 | ) |
||
347 | |||
348 | # init arguments |
||
349 | args = parser.parse_args(args) |
||
350 | |||
351 | if args.slice_inds is not None: |
||
352 | args.slice_inds = string_to_list(args.slice_inds) |
||
353 | args.slice_inds = [int(elem) for elem in args.slice_inds] |
||
354 | |||
355 | if args.mode == 0: |
||
356 | gif_slices( |
||
357 | img_paths=args.image_paths, save_path=args.save_path, interval=args.interval |
||
358 | ) |
||
359 | elif args.mode == 1: |
||
360 | if args.ddf_path is None: |
||
361 | raise Exception("--ddf-path is required when using --mode 1") |
||
362 | gif_warp( |
||
363 | img_paths=args.image_paths, |
||
364 | ddf_path=args.ddf_path, |
||
365 | slice_inds=args.slice_inds, |
||
366 | num_interval=args.num_interval, |
||
367 | interval=args.interval, |
||
368 | save_path=args.save_path, |
||
369 | ) |
||
370 | elif args.mode == 2: |
||
371 | tile_slices( |
||
372 | img_paths=args.image_paths, |
||
373 | save_path=args.save_path, |
||
374 | fname=args.fname, |
||
375 | slice_inds=args.slice_inds, |
||
376 | col_titles=args.col_titles, |
||
377 | ) |
||
378 | elif args.mode == 3: |
||
379 | size = tuple([int(elem) for elem in string_to_list(args.size)]) |
||
380 | gif_tile_slices( |
||
381 | img_paths=args.image_paths, |
||
382 | save_path=args.save_path, |
||
383 | fname=args.fname, |
||
384 | interval=args.interval, |
||
385 | size=size, |
||
386 | ) |
||
391 |