annotate spyboat_cli.py @ 4:a4c6fcf2c456 draft

"planemo upload commit 0584e8878deaf91614c6bb3c0122975300132c25"
author gregor.m
date Thu, 26 Nov 2020 16:40:47 +0000
parents 4d7f30a7e2f6
children d5a4180410c4
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)
4
a4c6fcf2c456 "planemo upload commit 0584e8878deaf91614c6bb3c0122975300132c25"
gregor.m
parents: 3
diff changeset
15 logger = logging.getLogger('spyboat-cli')
3
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 sys.exit(1)
4
a4c6fcf2c456 "planemo upload commit 0584e8878deaf91614c6bb3c0122975300132c25"
gregor.m
parents: 3
diff changeset
83 # problems get logged in 'open_tif'
a4c6fcf2c456 "planemo upload commit 0584e8878deaf91614c6bb3c0122975300132c25"
gregor.m
parents: 3
diff changeset
84 if movie is None:
a4c6fcf2c456 "planemo upload commit 0584e8878deaf91614c6bb3c0122975300132c25"
gregor.m
parents: 3
diff changeset
85 sys.exit(1)
3
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
86 # -------- Do (optional) spatial downsampling ---------------------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
87
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
88 scale_factor = arguments.rescale
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
89
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
90 # defaults to None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
91 if not scale_factor:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
92 logger.info('No downsampling requested..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
93
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
94 elif 0 < scale_factor < 100:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
95 logger.info(f'Downsampling the movie to {scale_factor:d}% of its original size..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
96 movie = spyboat.down_sample(movie, scale_factor / 100)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
97 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
98 raise ValueError('Scale factor must be between 0 and 100!')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
99
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
100 # -------- Do (optional) pre-smoothing -------------------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
101 # note that downsampling already is a smoothing operation..
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
102
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
103 # check if pre-smoothing requested
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
104 if not arguments.gauss_sigma:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
105 logger.info('No pre-smoothing requested..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
106 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
107 logger.info(f'Pre-smoothing the movie with Gaussians, sigma = {arguments.gauss_sigma:.2f}..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
108
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
109 movie = spyboat.gaussian_blur(movie, arguments.gauss_sigma)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
110
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
111 # ----- Set up Masking before processing ----
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
112
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
113 mask = None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
114 if arguments.masking == 'static':
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
115 if not arguments.mask_frame:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
116 logger.critical("Frame number for static masking is missing!")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
117 sys.exit(1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
118
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
119 if (arguments.mask_frame > movie.shape[0]) or (arguments.mask_frame < 0):
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
120 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
121 sys.exit(1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
122
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
123 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
124 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
125 mask = spyboat.create_static_mask(movie, arguments.mask_frame,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
126 arguments.mask_thresh)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
127 elif arguments.masking == 'dynamic':
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
128 logger.info(f'Creating dynamic mask with threshold {arguments.mask_thresh}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
129 mask = spyboat.create_dynamic_mask(movie, arguments.mask_thresh)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
130
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
131 else:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
132 logger.info('No masking requested..')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
133
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
134 # ------ Retrieve wavelet parameters ---------------------------
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
135
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
136 Wkwargs = {'dt': arguments.dt,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
137 'Tmin': arguments.Tmin,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
138 'Tmax': arguments.Tmax,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
139 'nT': arguments.nT,
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
140 'T_c' : arguments.Tcutoff, # defaults to None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
141 'win_size' : arguments.win_size # defaults to None
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
142 }
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
143
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
144 # --- start parallel processing ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
145
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
146 results = spyboat.run_parallel(movie, arguments.ncpu, **Wkwargs)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
147
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
148 # --- masking? ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
149
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
150 if mask is not None:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
151 # mask all output movies (in place!)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
152 for key in results:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
153 logger.info(f'Masking {key}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
154 spyboat.apply_mask(results[key], mask, fill_value=-1)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
155
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
156 # --- Produce Output HTML Report Figures/png's ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
157
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
158 # create the directory, yes we have to do that ourselves :)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
159 # galaxy then magically renders the html from that directory
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
160 try:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
161
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
162 if arguments.report_img_path != '.':
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
163 logger.info(f'Creating report directory {arguments.report_img_path}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
164 os.mkdir(arguments.report_img_path)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
165
4
a4c6fcf2c456 "planemo upload commit 0584e8878deaf91614c6bb3c0122975300132c25"
gregor.m
parents: 3
diff changeset
166 # 4 snapshots each
a4c6fcf2c456 "planemo upload commit 0584e8878deaf91614c6bb3c0122975300132c25"
gregor.m
parents: 3
diff changeset
167 Nsnap = 7
3
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
168 NFrames = movie.shape[0]
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
169 # show only frames at least one Tmin
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
170 # away from the edge (-effects)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
171 start_frame = int(Wkwargs['Tmin']/Wkwargs['dt'])
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
172
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
173 if (start_frame > NFrames//2):
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
174 logger.warning("Smallest period already is larger than half the observation time!")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
175 # set to 0 in this case
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
176 start_frame = 0
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
177
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
178 frame_increment = int( (NFrames - 2*start_frame) / Nsnap)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
179 snapshot_frames = range(start_frame, NFrames - start_frame, frame_increment)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
180
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
181 for snapshot_frame in snapshot_frames:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
182 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
183
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
184 output_report.produce_distr_plots(results, Wkwargs, img_path=arguments.report_img_path)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
185
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
186 output_report.create_html(snapshot_frames, arguments.html_fname)
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
187
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
188 except FileExistsError as e:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
189 logger.critical(f"Could not create html report directory: {repr(e)}")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
190
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
191
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
192 # --- save out result movies ---
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
193
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
194 # save phase movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
195 io.imsave(arguments.phase_out, results['phase'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
196 logger.info(f'Written {arguments.phase_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
197 # save period movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
198 io.imsave(arguments.period_out, results['period'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
199 logger.info(f'Written {arguments.period_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
200 # save power movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
201 io.imsave(arguments.power_out, results['power'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
202 logger.info(f'Written {arguments.power_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
203 # save amplitude movie
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
204 io.imsave(arguments.amplitude_out, results['amplitude'], plugin="tifffile")
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
205 logger.info(f'Written {arguments.amplitude_out}')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
206
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
207 # save out the probably pre-processed (scaled and blurred) input movie for
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
208 # direct comparison to results and coordinate mapping etc.
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
209 if arguments.preprocessed_out:
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
210 io.imsave(arguments.preprocessed_out, movie, plugin='tifffile')
4d7f30a7e2f6 "planemo upload commit d070f1ba04a5141a65487f45b29c22767639e44b"
gregor.m
parents:
diff changeset
211 logger.info(f'Written {arguments.preprocessed_out}')