diff 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
line wrap: on
line diff
--- a/spyboat_cli.py	Thu Nov 26 16:40:47 2020 +0000
+++ b/spyboat_cli.py	Sat Nov 28 18:50:09 2020 +0000
@@ -1,15 +1,15 @@
 #!/usr/bin/env python
 
-## Gets interfaced by Galaxy or can be used for bash scripting
+# Gets interfaced by Galaxy or can be used for bash scripting
 import argparse
-import sys, os
 import logging
+import os
+import sys
 
-from skimage import io
+import output_report
+import spyboat
 from numpy import float32
-
-import spyboat
-import output_report
+from skimage import io
 
 logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True)
 logger = logging.getLogger('spyboat-cli')
@@ -20,12 +20,11 @@
 
 # I/O
 parser.add_argument('--input_path', help="Input movie location", required=True)
-parser.add_argument('--phase_out', help='Phase output file name', required=True)
-parser.add_argument('--period_out', help='Period output file name', required=True)
-parser.add_argument('--power_out', help='Power output file name', required=True)
-parser.add_argument('--amplitude_out', help='Amplitude output file name', required=True)
-parser.add_argument('--preprocessed_out', help="Preprocessed-input output file name, 'None'", required=False)
-
+parser.add_argument('--phase_out', help='Phase output file name', required=False)
+parser.add_argument('--period_out', help='Period output file name', required=False)
+parser.add_argument('--power_out', help='Power output file name', required=False)
+parser.add_argument('--amplitude_out', help='Amplitude output file name', required=False)
+parser.add_argument('--preprocessed_out', help="Preprocessed-input output file name", required=False)
 
 # (Optional) Multiprocessing
 
@@ -33,10 +32,11 @@
                     required=False, type=int, default=1)
 
 # Optional spatial downsampling
-parser.add_argument('--rescale', help='Rescale the image by a factor given in %%, None means no rescaling',
+parser.add_argument('--rescale_factor', help='Rescale the image by a factor given in %%, None means no rescaling',
                     required=False, type=int)
 # Optional Gaussian smoothing
-parser.add_argument('--gauss_sigma', help='Gaussian smoothing parameter, None means no smoothing', required=False, type=float)
+parser.add_argument('--gauss_sigma', help='Gaussian smoothing parameter, None means no smoothing', required=False,
+                    type=float)
 
 # Wavelet Analysis Parameters
 parser.add_argument('--dt', help='Sampling interval', required=True, type=float)
@@ -49,14 +49,15 @@
                     required=False, type=float)
 
 # Optional masking
-parser.add_argument('--masking', help="Set to either 'dynamic', 'static' or 'None' which is the default", default='None', required=False, type=str)
+parser.add_argument('--masking', help="Set to either 'dynamic', 'static' or 'None' which is the default",
+                    default='None', required=False, type=str)
 
 parser.add_argument('--mask_frame',
                     help="The frame of the input movie to create a static mask from, needs masking set to 'static'",
                     required=False, type=int)
 
-
-parser.add_argument('--mask_thresh', help='The threshold of the mask, all pixels with less than this value get masked (if masking enabled).',
+parser.add_argument('--mask_thresh',
+                    help='The threshold of the mask, all pixels with less than this value get masked (if masking enabled).',
                     required=False, type=float,
                     default=0)
 
@@ -64,7 +65,8 @@
 parser.add_argument('--html_fname', help="Name of the html report.",
                     default='OutputReport.html', required=False, type=str)
 
-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)
+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)
 
 parser.add_argument('--version', action='version', version='0.1.0')
 
@@ -85,7 +87,7 @@
     sys.exit(1)
 # -------- Do (optional) spatial downsampling ---------------------------
 
-scale_factor = arguments.rescale
+scale_factor = arguments.rescale_factor
 
 # defaults to None
 if not scale_factor:
@@ -121,13 +123,13 @@
         sys.exit(1)
 
     else:
-        logger.info(f'Creating static mask from frame {arguments.mask_frame} with threshold {arguments.mask_thresh}')  
+        logger.info(f'Creating static mask from frame {arguments.mask_frame} with threshold {arguments.mask_thresh}')
         mask = spyboat.create_static_mask(movie, arguments.mask_frame,
-                                         arguments.mask_thresh)
+                                          arguments.mask_thresh)
 elif arguments.masking == 'dynamic':
     logger.info(f'Creating dynamic mask with threshold {arguments.mask_thresh}')
     mask = spyboat.create_dynamic_mask(movie, arguments.mask_thresh)
-        
+
 else:
     logger.info('No masking requested..')
 
@@ -135,11 +137,11 @@
 
 Wkwargs = {'dt': arguments.dt,
            'Tmin': arguments.Tmin,
-           'Tmax': arguments.Tmax,           
+           'Tmax': arguments.Tmax,
            'nT': arguments.nT,
-           'T_c' : arguments.Tcutoff, # defaults to None
-           'win_size' : arguments.win_size # defaults to None          
-}
+           'T_c': arguments.Tcutoff,  # defaults to None
+           'win_size': arguments.win_size  # defaults to None
+           }
 
 # --- start parallel processing ---
 
@@ -161,51 +163,55 @@
 
     if arguments.report_img_path != '.':
         logger.info(f'Creating report directory {arguments.report_img_path}')
-        os.mkdir(arguments.report_img_path)    
+        os.mkdir(arguments.report_img_path)
 
-    # 4 snapshots each
+        # 4 snapshots each
     Nsnap = 7
     NFrames = movie.shape[0]
     # show only frames at least one Tmin
     # away from the edge (-effects)
-    start_frame = int(Wkwargs['Tmin']/Wkwargs['dt'])        
-    
-    if (start_frame > NFrames//2):
+    start_frame = int(Wkwargs['Tmin'] / Wkwargs['dt'])
+
+    if (start_frame > NFrames // 2):
         logger.warning("Smallest period already is larger than half the observation time!")
         # set to 0 in this case
         start_frame = 0
-        
-    frame_increment = int( (NFrames - 2*start_frame) / Nsnap)
+
+    frame_increment = int((NFrames - 2 * start_frame) / Nsnap)
     snapshot_frames = range(start_frame, NFrames - start_frame, frame_increment)
 
-    for snapshot_frame in snapshot_frames:  
+    for snapshot_frame in snapshot_frames:
         output_report.produce_snapshots(movie, results, snapshot_frame, Wkwargs, img_path=arguments.report_img_path)
-    
+
     output_report.produce_distr_plots(results, Wkwargs, img_path=arguments.report_img_path)
-    
+
     output_report.create_html(snapshot_frames, arguments.html_fname)
-                          
+
 except FileExistsError as e:
     logger.critical(f"Could not create html report directory: {repr(e)}")
 
-
 # --- save out result movies ---
 
-# save phase movie
-io.imsave(arguments.phase_out, results['phase'], plugin="tifffile")
-logger.info(f'Written {arguments.phase_out}')
-# save period movie
-io.imsave(arguments.period_out, results['period'], plugin="tifffile")
-logger.info(f'Written {arguments.period_out}')
-# save power movie
-io.imsave(arguments.power_out, results['power'], plugin="tifffile")
-logger.info(f'Written {arguments.power_out}')
-# save amplitude movie
-io.imsave(arguments.amplitude_out, results['amplitude'], plugin="tifffile")
-logger.info(f'Written {arguments.amplitude_out}')
+# None means output is filtered from galaxy settings
+if arguments.phase_out is not None:
+    # save phase movie
+    io.imsave(arguments.phase_out, results['phase'], plugin="tifffile")
+    logger.info(f'Written phase to {arguments.phase_out}')
+if arguments.period_out is not None:
+    # save period movie
+    io.imsave(arguments.period_out, results['period'], plugin="tifffile")
+    logger.info(f'Written period to {arguments.period_out}')
+if arguments.power_out is not None:
+    # save power movie
+    io.imsave(arguments.power_out, results['power'], plugin="tifffile")
+    logger.info(f'Written power to {arguments.power_out}')
+if arguments.amplitude_out is not None:
+    # save amplitude movie
+    io.imsave(arguments.amplitude_out, results['amplitude'], plugin="tifffile")
+    logger.info(f'Written amplitude to {arguments.amplitude_out}')
 
 # save out the probably pre-processed (scaled and blurred) input movie for
 # direct comparison to results and coordinate mapping etc.
-if arguments.preprocessed_out:
-    io.imsave(arguments.preprocessed_out, movie, plugin='tifffile')
-    logger.info(f'Written {arguments.preprocessed_out}')
+if arguments.preprocessed_out is not None:
+    io.imsave(arguments.preprocessed_out, movie.astype(float32), plugin='tifffile')
+    logger.info(f'Written preprocessed to {arguments.preprocessed_out}')