annotate spyboat_cli.py @ 3:4d7f30a7e2f6 draft

"planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
author gregor.m
date Tue, 24 Nov 2020 13:06:26 +0000
parents
children a4c6fcf2c456
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
1 #!/usr/bin/env python
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
2
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
3 ## Gets interfaced by Galaxy or can be used for bash scripting
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
4 import argparse
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
5 import sys, os
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
6 import logging
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
7
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
8 from skimage import io
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
9 from numpy import float32
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
10
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
11 import spyboat
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
12 import output_report
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
13
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
14 logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
15 logger = logging.getLogger('wrapper')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
16
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
17 # ----------command line parameters ---------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
18
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
19 parser = argparse.ArgumentParser(description='Process some arguments.')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
20
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
21 # I/O
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
22 parser.add_argument('--input_path', help="Input movie location", required=True)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
23 parser.add_argument('--phase_out', help='Phase output file name', required=True)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
24 parser.add_argument('--period_out', help='Period output file name', required=True)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
25 parser.add_argument('--power_out', help='Power output file name', required=True)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
26 parser.add_argument('--amplitude_out', help='Amplitude output file name', required=True)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
27 parser.add_argument('--preprocessed_out', help="Preprocessed-input output file name, 'None'", required=False)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
28
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
29
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
30 # (Optional) Multiprocessing
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
31
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
32 parser.add_argument('--ncpu', help='Number of processors to use',
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
33 required=False, type=int, default=1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
34
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
35 # Optional spatial downsampling
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
36 parser.add_argument('--rescale', help='Rescale the image by a factor given in %%, None means no rescaling',
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
37 required=False, type=int)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
38 # Optional Gaussian smoothing
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
39 parser.add_argument('--gauss_sigma', help='Gaussian smoothing parameter, None means no smoothing', required=False, type=float)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
40
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
41 # Wavelet Analysis Parameters
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
42 parser.add_argument('--dt', help='Sampling interval', required=True, type=float)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
43 parser.add_argument('--Tmin', help='Smallest period', required=True, type=float)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
44 parser.add_argument('--Tmax', help='Biggest period', required=True, type=float)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
45 parser.add_argument('--nT', help='Number of periods to scan for', required=True, type=int)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
46
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
47 parser.add_argument('--Tcutoff', help='Sinc cut-off period, disables detrending if not set', required=False, type=float)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
48 parser.add_argument('--win_size', help='Sliding window size for amplitude normalization, None means no normalization',
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
49 required=False, type=float)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
50
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
51 # Optional masking
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
52 parser.add_argument('--masking', help="Set to either 'dynamic', 'static' or 'None' which is the default", default='None', required=False, type=str)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
53
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
54 parser.add_argument('--mask_frame',
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
55 help="The frame of the input movie to create a static mask from, needs masking set to 'static'",
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
56 required=False, type=int)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
57
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
58
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
59 parser.add_argument('--mask_thresh', help='The threshold of the mask, all pixels with less than this value get masked (if masking enabled).',
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
60 required=False, type=float,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
61 default=0)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
62
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
63 # output html report/snapshots
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
64 parser.add_argument('--html_fname', help="Name of the html report.",
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
65 default='OutputReport.html', required=False, type=str)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
66
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
67 parser.add_argument('--report_img_path', help="For the html report, to be set in Galaxy. Without galaxy leave at cwd!", default='.', required=False, type=str)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
68
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
69 parser.add_argument('--version', action='version', version='0.1.0')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
70
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
71 arguments = parser.parse_args()
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
72
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
73 logger.info("Received following arguments:")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
74 for arg in vars(arguments):
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
75 logger.info(f'{arg} -> {getattr(arguments, arg)}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
76
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
77 # ------------Read the input----------------------------------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
78 try:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
79 movie = spyboat.open_tif(arguments.input_path)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
80 except FileNotFoundError:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
81 logger.critical(f"Couldn't open {arguments.input_path}, check movie storage directory!")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
82
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
83 sys.exit(1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
84
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
85 # -------- Do (optional) spatial downsampling ---------------------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
86
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
87 scale_factor = arguments.rescale
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
88
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
89 # defaults to None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
90 if not scale_factor:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
91 logger.info('No downsampling requested..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
92
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
93 elif 0 < scale_factor < 100:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
94 logger.info(f'Downsampling the movie to {scale_factor:d}% of its original size..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
95 movie = spyboat.down_sample(movie, scale_factor / 100)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
96 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
97 raise ValueError('Scale factor must be between 0 and 100!')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
98
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
99 # -------- Do (optional) pre-smoothing -------------------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
100 # note that downsampling already is a smoothing operation..
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
101
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
102 # check if pre-smoothing requested
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
103 if not arguments.gauss_sigma:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
104 logger.info('No pre-smoothing requested..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
105 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
106 logger.info(f'Pre-smoothing the movie with Gaussians, sigma = {arguments.gauss_sigma:.2f}..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
107
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
108 movie = spyboat.gaussian_blur(movie, arguments.gauss_sigma)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
109
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
110 # ----- Set up Masking before processing ----
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
111
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
112 mask = None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
113 if arguments.masking == 'static':
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
114 if not arguments.mask_frame:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
115 logger.critical("Frame number for static masking is missing!")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
116 sys.exit(1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
117
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
118 if (arguments.mask_frame > movie.shape[0]) or (arguments.mask_frame < 0):
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
119 logger.critical(f'Requested frame does not exist, input only has {movie.shape[0]} frames.. exiting')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
120 sys.exit(1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
121
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
122 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
123 logger.info(f'Creating static mask from frame {arguments.mask_frame} with threshold {arguments.mask_thresh}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
124 mask = spyboat.create_static_mask(movie, arguments.mask_frame,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
125 arguments.mask_thresh)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
126 elif arguments.masking == 'dynamic':
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
127 logger.info(f'Creating dynamic mask with threshold {arguments.mask_thresh}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
128 mask = spyboat.create_dynamic_mask(movie, arguments.mask_thresh)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
129
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
130 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
131 logger.info('No masking requested..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
132
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
133 # ------ Retrieve wavelet parameters ---------------------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
134
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
135 Wkwargs = {'dt': arguments.dt,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
136 'Tmin': arguments.Tmin,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
137 'Tmax': arguments.Tmax,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
138 'nT': arguments.nT,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
139 'T_c' : arguments.Tcutoff, # defaults to None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
140 'win_size' : arguments.win_size # defaults to None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
141 }
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
142
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
143 # --- start parallel processing ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
144
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
145 results = spyboat.run_parallel(movie, arguments.ncpu, **Wkwargs)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
146
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
147 # --- masking? ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
148
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
149 if mask is not None:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
150 # mask all output movies (in place!)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
151 for key in results:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
152 logger.info(f'Masking {key}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
153 spyboat.apply_mask(results[key], mask, fill_value=-1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
154
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
155 # --- Produce Output HTML Report Figures/png's ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
156
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
157 # create the directory, yes we have to do that ourselves :)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
158 # galaxy then magically renders the html from that directory
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
159 try:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
160
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
161 if arguments.report_img_path != '.':
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
162 logger.info(f'Creating report directory {arguments.report_img_path}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
163 os.mkdir(arguments.report_img_path)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
164
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
165 # make 6 times 4 snapshots each
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
166 Nsnap = 6
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
167 NFrames = movie.shape[0]
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
168 # show only frames at least one Tmin
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
169 # away from the edge (-effects)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
170 start_frame = int(Wkwargs['Tmin']/Wkwargs['dt'])
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
171
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
172 if (start_frame > NFrames//2):
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
173 logger.warning("Smallest period already is larger than half the observation time!")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
174 # set to 0 in this case
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
175 start_frame = 0
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
176
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
177 frame_increment = int( (NFrames - 2*start_frame) / Nsnap)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
178 snapshot_frames = range(start_frame, NFrames - start_frame, frame_increment)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
179
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
180 for snapshot_frame in snapshot_frames:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
181 output_report.produce_snapshots(movie, results, snapshot_frame, Wkwargs, img_path=arguments.report_img_path)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
182
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
183 output_report.produce_distr_plots(results, Wkwargs, img_path=arguments.report_img_path)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
184
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
185 output_report.create_html(snapshot_frames, arguments.html_fname)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
186
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
187 except FileExistsError as e:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
188 logger.critical(f"Could not create html report directory: {repr(e)}")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
189
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
190
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
191 # --- save out result movies ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
192
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
193 # save phase movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
194 io.imsave(arguments.phase_out, results['phase'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
195 logger.info(f'Written {arguments.phase_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
196 # save period movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
197 io.imsave(arguments.period_out, results['period'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
198 logger.info(f'Written {arguments.period_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
199 # save power movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
200 io.imsave(arguments.power_out, results['power'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
201 logger.info(f'Written {arguments.power_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
202 # save amplitude movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
203 io.imsave(arguments.amplitude_out, results['amplitude'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
204 logger.info(f'Written {arguments.amplitude_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
205
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
206 # save out the probably pre-processed (scaled and blurred) input movie for
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
207 # direct comparison to results and coordinate mapping etc.
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
208 if arguments.preprocessed_out:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
209 io.imsave(arguments.preprocessed_out, movie, plugin='tifffile')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
210 logger.info(f'Written {arguments.preprocessed_out}')