comparison spyboat_cli.py @ 5:d5a4180410c4 draft default tip

"planemo upload commit 7bc843096b70fe1c8fc149e69d8f87fceac4eb3b"
author gregor.m
date Sat, 28 Nov 2020 18:50:09 +0000
parents a4c6fcf2c456
children
comparison
equal deleted inserted replaced
4:a4c6fcf2c456 5:d5a4180410c4
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 ## Gets interfaced by Galaxy or can be used for bash scripting 3 # Gets interfaced by Galaxy or can be used for bash scripting
4 import argparse 4 import argparse
5 import sys, os
6 import logging 5 import logging
7 6 import os
7 import sys
8
9 import output_report
10 import spyboat
11 from numpy import float32
8 from skimage import io 12 from skimage import io
9 from numpy import float32
10
11 import spyboat
12 import output_report
13 13
14 logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True) 14 logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True)
15 logger = logging.getLogger('spyboat-cli') 15 logger = logging.getLogger('spyboat-cli')
16 16
17 # ----------command line parameters --------------- 17 # ----------command line parameters ---------------
18 18
19 parser = argparse.ArgumentParser(description='Process some arguments.') 19 parser = argparse.ArgumentParser(description='Process some arguments.')
20 20
21 # I/O 21 # I/O
22 parser.add_argument('--input_path', help="Input movie location", required=True) 22 parser.add_argument('--input_path', help="Input movie location", required=True)
23 parser.add_argument('--phase_out', help='Phase output file name', required=True) 23 parser.add_argument('--phase_out', help='Phase output file name', required=False)
24 parser.add_argument('--period_out', help='Period output file name', required=True) 24 parser.add_argument('--period_out', help='Period output file name', required=False)
25 parser.add_argument('--power_out', help='Power output file name', required=True) 25 parser.add_argument('--power_out', help='Power output file name', required=False)
26 parser.add_argument('--amplitude_out', help='Amplitude output file name', required=True) 26 parser.add_argument('--amplitude_out', help='Amplitude output file name', required=False)
27 parser.add_argument('--preprocessed_out', help="Preprocessed-input output file name, 'None'", required=False) 27 parser.add_argument('--preprocessed_out', help="Preprocessed-input output file name", required=False)
28
29 28
30 # (Optional) Multiprocessing 29 # (Optional) Multiprocessing
31 30
32 parser.add_argument('--ncpu', help='Number of processors to use', 31 parser.add_argument('--ncpu', help='Number of processors to use',
33 required=False, type=int, default=1) 32 required=False, type=int, default=1)
34 33
35 # Optional spatial downsampling 34 # Optional spatial downsampling
36 parser.add_argument('--rescale', help='Rescale the image by a factor given in %%, None means no rescaling', 35 parser.add_argument('--rescale_factor', help='Rescale the image by a factor given in %%, None means no rescaling',
37 required=False, type=int) 36 required=False, type=int)
38 # Optional Gaussian smoothing 37 # Optional Gaussian smoothing
39 parser.add_argument('--gauss_sigma', help='Gaussian smoothing parameter, None means no smoothing', required=False, type=float) 38 parser.add_argument('--gauss_sigma', help='Gaussian smoothing parameter, None means no smoothing', required=False,
39 type=float)
40 40
41 # Wavelet Analysis Parameters 41 # Wavelet Analysis Parameters
42 parser.add_argument('--dt', help='Sampling interval', required=True, type=float) 42 parser.add_argument('--dt', help='Sampling interval', required=True, type=float)
43 parser.add_argument('--Tmin', help='Smallest period', required=True, type=float) 43 parser.add_argument('--Tmin', help='Smallest period', required=True, type=float)
44 parser.add_argument('--Tmax', help='Biggest period', required=True, type=float) 44 parser.add_argument('--Tmax', help='Biggest period', required=True, type=float)
47 parser.add_argument('--Tcutoff', help='Sinc cut-off period, disables detrending if not set', required=False, type=float) 47 parser.add_argument('--Tcutoff', help='Sinc cut-off period, disables detrending if not set', required=False, type=float)
48 parser.add_argument('--win_size', help='Sliding window size for amplitude normalization, None means no normalization', 48 parser.add_argument('--win_size', help='Sliding window size for amplitude normalization, None means no normalization',
49 required=False, type=float) 49 required=False, type=float)
50 50
51 # Optional masking 51 # Optional masking
52 parser.add_argument('--masking', help="Set to either 'dynamic', 'static' or 'None' which is the default", default='None', required=False, type=str) 52 parser.add_argument('--masking', help="Set to either 'dynamic', 'static' or 'None' which is the default",
53 default='None', required=False, type=str)
53 54
54 parser.add_argument('--mask_frame', 55 parser.add_argument('--mask_frame',
55 help="The frame of the input movie to create a static mask from, needs masking set to 'static'", 56 help="The frame of the input movie to create a static mask from, needs masking set to 'static'",
56 required=False, type=int) 57 required=False, type=int)
57 58
58 59 parser.add_argument('--mask_thresh',
59 parser.add_argument('--mask_thresh', help='The threshold of the mask, all pixels with less than this value get masked (if masking enabled).', 60 help='The threshold of the mask, all pixels with less than this value get masked (if masking enabled).',
60 required=False, type=float, 61 required=False, type=float,
61 default=0) 62 default=0)
62 63
63 # output html report/snapshots 64 # output html report/snapshots
64 parser.add_argument('--html_fname', help="Name of the html report.", 65 parser.add_argument('--html_fname', help="Name of the html report.",
65 default='OutputReport.html', required=False, type=str) 66 default='OutputReport.html', required=False, type=str)
66 67
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) 68 parser.add_argument('--report_img_path', help="For the html report, to be set in Galaxy. Without galaxy leave at cwd!",
69 default='.', required=False, type=str)
68 70
69 parser.add_argument('--version', action='version', version='0.1.0') 71 parser.add_argument('--version', action='version', version='0.1.0')
70 72
71 arguments = parser.parse_args() 73 arguments = parser.parse_args()
72 74
83 # problems get logged in 'open_tif' 85 # problems get logged in 'open_tif'
84 if movie is None: 86 if movie is None:
85 sys.exit(1) 87 sys.exit(1)
86 # -------- Do (optional) spatial downsampling --------------------------- 88 # -------- Do (optional) spatial downsampling ---------------------------
87 89
88 scale_factor = arguments.rescale 90 scale_factor = arguments.rescale_factor
89 91
90 # defaults to None 92 # defaults to None
91 if not scale_factor: 93 if not scale_factor:
92 logger.info('No downsampling requested..') 94 logger.info('No downsampling requested..')
93 95
119 if (arguments.mask_frame > movie.shape[0]) or (arguments.mask_frame < 0): 121 if (arguments.mask_frame > movie.shape[0]) or (arguments.mask_frame < 0):
120 logger.critical(f'Requested frame does not exist, input only has {movie.shape[0]} frames.. exiting') 122 logger.critical(f'Requested frame does not exist, input only has {movie.shape[0]} frames.. exiting')
121 sys.exit(1) 123 sys.exit(1)
122 124
123 else: 125 else:
124 logger.info(f'Creating static mask from frame {arguments.mask_frame} with threshold {arguments.mask_thresh}') 126 logger.info(f'Creating static mask from frame {arguments.mask_frame} with threshold {arguments.mask_thresh}')
125 mask = spyboat.create_static_mask(movie, arguments.mask_frame, 127 mask = spyboat.create_static_mask(movie, arguments.mask_frame,
126 arguments.mask_thresh) 128 arguments.mask_thresh)
127 elif arguments.masking == 'dynamic': 129 elif arguments.masking == 'dynamic':
128 logger.info(f'Creating dynamic mask with threshold {arguments.mask_thresh}') 130 logger.info(f'Creating dynamic mask with threshold {arguments.mask_thresh}')
129 mask = spyboat.create_dynamic_mask(movie, arguments.mask_thresh) 131 mask = spyboat.create_dynamic_mask(movie, arguments.mask_thresh)
130 132
131 else: 133 else:
132 logger.info('No masking requested..') 134 logger.info('No masking requested..')
133 135
134 # ------ Retrieve wavelet parameters --------------------------- 136 # ------ Retrieve wavelet parameters ---------------------------
135 137
136 Wkwargs = {'dt': arguments.dt, 138 Wkwargs = {'dt': arguments.dt,
137 'Tmin': arguments.Tmin, 139 'Tmin': arguments.Tmin,
138 'Tmax': arguments.Tmax, 140 'Tmax': arguments.Tmax,
139 'nT': arguments.nT, 141 'nT': arguments.nT,
140 'T_c' : arguments.Tcutoff, # defaults to None 142 'T_c': arguments.Tcutoff, # defaults to None
141 'win_size' : arguments.win_size # defaults to None 143 'win_size': arguments.win_size # defaults to None
142 } 144 }
143 145
144 # --- start parallel processing --- 146 # --- start parallel processing ---
145 147
146 results = spyboat.run_parallel(movie, arguments.ncpu, **Wkwargs) 148 results = spyboat.run_parallel(movie, arguments.ncpu, **Wkwargs)
147 149
159 # galaxy then magically renders the html from that directory 161 # galaxy then magically renders the html from that directory
160 try: 162 try:
161 163
162 if arguments.report_img_path != '.': 164 if arguments.report_img_path != '.':
163 logger.info(f'Creating report directory {arguments.report_img_path}') 165 logger.info(f'Creating report directory {arguments.report_img_path}')
164 os.mkdir(arguments.report_img_path) 166 os.mkdir(arguments.report_img_path)
165 167
166 # 4 snapshots each 168 # 4 snapshots each
167 Nsnap = 7 169 Nsnap = 7
168 NFrames = movie.shape[0] 170 NFrames = movie.shape[0]
169 # show only frames at least one Tmin 171 # show only frames at least one Tmin
170 # away from the edge (-effects) 172 # away from the edge (-effects)
171 start_frame = int(Wkwargs['Tmin']/Wkwargs['dt']) 173 start_frame = int(Wkwargs['Tmin'] / Wkwargs['dt'])
172 174
173 if (start_frame > NFrames//2): 175 if (start_frame > NFrames // 2):
174 logger.warning("Smallest period already is larger than half the observation time!") 176 logger.warning("Smallest period already is larger than half the observation time!")
175 # set to 0 in this case 177 # set to 0 in this case
176 start_frame = 0 178 start_frame = 0
177 179
178 frame_increment = int( (NFrames - 2*start_frame) / Nsnap) 180 frame_increment = int((NFrames - 2 * start_frame) / Nsnap)
179 snapshot_frames = range(start_frame, NFrames - start_frame, frame_increment) 181 snapshot_frames = range(start_frame, NFrames - start_frame, frame_increment)
180 182
181 for snapshot_frame in snapshot_frames: 183 for snapshot_frame in snapshot_frames:
182 output_report.produce_snapshots(movie, results, snapshot_frame, Wkwargs, img_path=arguments.report_img_path) 184 output_report.produce_snapshots(movie, results, snapshot_frame, Wkwargs, img_path=arguments.report_img_path)
183 185
184 output_report.produce_distr_plots(results, Wkwargs, img_path=arguments.report_img_path) 186 output_report.produce_distr_plots(results, Wkwargs, img_path=arguments.report_img_path)
185 187
186 output_report.create_html(snapshot_frames, arguments.html_fname) 188 output_report.create_html(snapshot_frames, arguments.html_fname)
187 189
188 except FileExistsError as e: 190 except FileExistsError as e:
189 logger.critical(f"Could not create html report directory: {repr(e)}") 191 logger.critical(f"Could not create html report directory: {repr(e)}")
190 192
191
192 # --- save out result movies --- 193 # --- save out result movies ---
193 194
194 # save phase movie 195 # None means output is filtered from galaxy settings
195 io.imsave(arguments.phase_out, results['phase'], plugin="tifffile") 196 if arguments.phase_out is not None:
196 logger.info(f'Written {arguments.phase_out}') 197 # save phase movie
197 # save period movie 198 io.imsave(arguments.phase_out, results['phase'], plugin="tifffile")
198 io.imsave(arguments.period_out, results['period'], plugin="tifffile") 199 logger.info(f'Written phase to {arguments.phase_out}')
199 logger.info(f'Written {arguments.period_out}') 200 if arguments.period_out is not None:
200 # save power movie 201 # save period movie
201 io.imsave(arguments.power_out, results['power'], plugin="tifffile") 202 io.imsave(arguments.period_out, results['period'], plugin="tifffile")
202 logger.info(f'Written {arguments.power_out}') 203 logger.info(f'Written period to {arguments.period_out}')
203 # save amplitude movie 204 if arguments.power_out is not None:
204 io.imsave(arguments.amplitude_out, results['amplitude'], plugin="tifffile") 205 # save power movie
205 logger.info(f'Written {arguments.amplitude_out}') 206 io.imsave(arguments.power_out, results['power'], plugin="tifffile")
207 logger.info(f'Written power to {arguments.power_out}')
208 if arguments.amplitude_out is not None:
209 # save amplitude movie
210 io.imsave(arguments.amplitude_out, results['amplitude'], plugin="tifffile")
211 logger.info(f'Written amplitude to {arguments.amplitude_out}')
206 212
207 # save out the probably pre-processed (scaled and blurred) input movie for 213 # save out the probably pre-processed (scaled and blurred) input movie for
208 # direct comparison to results and coordinate mapping etc. 214 # direct comparison to results and coordinate mapping etc.
209 if arguments.preprocessed_out: 215 if arguments.preprocessed_out is not None:
210 io.imsave(arguments.preprocessed_out, movie, plugin='tifffile') 216 io.imsave(arguments.preprocessed_out, movie.astype(float32), plugin='tifffile')
211 logger.info(f'Written {arguments.preprocessed_out}') 217 logger.info(f'Written preprocessed to {arguments.preprocessed_out}')