changeset 2:c8fc3f34cbff draft

planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
author iuc
date Tue, 04 Aug 2015 11:15:16 -0400
parents 1af4f60294c3
children d99cf938ad41
files imagej2_base_utils.py imagej2_macros.xml imagej2_noise.py imagej2_noise.xml jython_script.py jython_utils.py tool_dependencies.xml
diffstat 7 files changed, 112 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/imagej2_base_utils.py	Fri Jun 26 05:28:29 2015 -0400
+++ b/imagej2_base_utils.py	Tue Aug 04 11:15:16 2015 -0400
@@ -30,17 +30,17 @@
 
 def get_base_command_imagej2( memory_size=None, macro=None, jython_script=None ):
     imagej2_executable = get_imagej2_executable()
-    if imagej2_executable:
-        cmd = '%s --headless -DXincgc' % imagej2_executable
-        if memory_size is not None:
-            memory_size_cmd = ' -DXms=%s -DXmx=%s' % ( memory_size, memory_size )
-            cmd += memory_size_cmd
-        if macro is not None:
-            cmd += ' --macro %s' % os.path.abspath( macro )
-        if jython_script is not None:
-            cmd += ' --jython -u %s' % os.path.abspath( jython_script )
-        return cmd
-    return None
+    if imagej2_executable is None:
+        return None
+    cmd = '%s --ij2 --headless --debug' % imagej2_executable
+    if memory_size is not None:
+        memory_size_cmd = ' -DXms=%s -DXmx=%s' % ( memory_size, memory_size )
+        cmd += memory_size_cmd
+    if macro is not None:
+        cmd += ' --macro %s' % os.path.abspath( macro )
+    if jython_script is not None:
+        cmd += ' --jython %s' % os.path.abspath( jython_script )
+    return cmd
 
 def get_file_extension( image_format ):
     """
@@ -109,7 +109,7 @@
     platform_dict[ 'architecture' ] = machine.lower()
     return platform_dict
 
-def get_stderr_exception( tmp_err, tmp_stderr, tmp_stdout ):
+def get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout, include_stdout=False ):
     tmp_stderr.close()
     """
     Return a stderr string of reasonable size.
@@ -126,8 +126,21 @@
     except OverflowError:
         pass
     tmp_stderr.close()
+    if include_stdout:
+        tmp_stdout = open( tmp_out, 'rb' )
+        stdout_str = ''
+        buffsize = BUFF_SIZE
+        try:
+            while True:
+                stdout_str += tmp_stdout.read( buffsize )
+                if not stdout_str or len( stdout_str ) % buffsize != 0:
+                    break
+        except OverflowError:
+            pass
     tmp_stdout.close()
-    return str( stderr_str )
+    if include_stdout:
+        return 'STDOUT\n%s\n\nSTDERR\n%s\n' % ( stdout_str, stderr_str )
+    return stderr_str
 
 def get_temp_dir( prefix='tmp-imagej-', dir=None ):
     """
@@ -151,6 +164,15 @@
     file_extension = get_file_extension( image_format )
     return get_tempfilename( tmp_dir, file_extension )
 
+def handle_none_type( val, val_type='float' ):
+    if val is None:
+        return ' None'
+    else:
+        if val_type == 'float':
+            return ' %.1f' % val
+        elif val_type == 'int':
+            return ' %d' % val
+
 def stop_err( msg ):
     sys.stderr.write( msg )
     sys.exit( 1 )
--- a/imagej2_macros.xml	Fri Jun 26 05:28:29 2015 -0400
+++ b/imagej2_macros.xml	Tue Aug 04 11:15:16 2015 -0400
@@ -1,11 +1,11 @@
 <?xml version='1.0' encoding='UTF-8'?>
 <macros>
-    <xml name="fiji_headless_requirements">
+    <xml name="fiji_20141125_requirements">
         <requirements>
             <requirement type="package" version="20141125">fiji</requirement>
         </requirements>
     </xml>
-    <xml name="python_bioformats_requirements">
+    <xml name="python_bioformats_104_requirements">
         <requirements>
             <requirement type="package" version="20141125">fiji</requirement>
             <requirement type="package" version="1.0.11">javabridge</requirement>
@@ -41,6 +41,24 @@
             <option value="RGB_ramp">RGB ramp</option>
         </param>
     </xml>
+    <xml name="make_binary_params">
+        <param name="iterations" type="integer" value="1" min="1" max="100" label="Iterations" help="The number of times (1-100) erosion, dilation, opening, and closing are performed."/>
+        <param name="count" type="integer" value="1" min="1" max="8" label="Count" help="The number of adjacent background pixels necessary (1-8) for erosion or dilation."/>
+        <param name="black_background" type="select" label="Black background" help="If Yes, the background is black and the foreground is white (no implies the opposite).">
+            <option value="no" selected="True">No</option>
+            <option value="yes">Yes</option>
+        </param>
+        <param name="pad_edges_when_eroding" type="select" label="Pad edges when eroding" help="If Yes, eroding does not erode from the edges of the image.">
+            <option value="no" selected="True">No</option>
+            <option value="yes">Yes</option>
+        </param>
+    </xml>
+    <token name="@make_binary_args@">
+        --iterations $iterations
+        --count $count
+        --black_background $black_background
+        --pad_edges_when_eroding $pad_edges_when_eroding
+    </token>
     <xml name="max_heap_size_type_conditional">
         <conditional name="set_max_heap_size">
             <param name="max_heap_size_type" type="select" label="Maximum size of the memory allocation pool used by the JVM" help="This value must be a multiple of 1024 or it will be ignored and the system default will be used.">
@@ -48,8 +66,8 @@
                 <option value="megabytes">Set in megabytes</option>
                 <option value="gigabytes">Set in gigabytes</option>
             </param>
-            <when value="default">
-                <param name="max_heap_size" type="integer" value="0" label="Do not set" help="Use system default"/>
+            <when value="default" >
+                <param name="max_heap_size" type="integer" value="0" hidden="true" label="Do not set" help="Use system default"/>
             </when>
             <when value="megabytes">
                 <param name="max_heap_size" type="integer" value="512" min="256" label="Maximum size, in megabytes, of the memory allocation pool" help="Examples: 256, 512, etc."/>
--- a/imagej2_noise.py	Fri Jun 26 05:28:29 2015 -0400
+++ b/imagej2_noise.py	Tue Aug 04 11:15:16 2015 -0400
@@ -6,15 +6,6 @@
 import tempfile
 import imagej2_base_utils
 
-def handle_none_type( val, val_type='float' ):
-    if val is None:
-        return ' None'
-    else:
-        if val_type == 'float':
-            return ' %.1f' % val
-        elif val_type == 'int':
-            return ' %d' % val
-
 if __name__=="__main__":
     # Parse Command Line.
     parser = argparse.ArgumentParser()
@@ -38,7 +29,7 @@
     parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
     parser.add_argument( '--max_heap_size_type', dest='max_heap_size_type', help='Type (default or megabytes) of max_heap_size value' )
     parser.add_argument( '--max_heap_size', dest='max_heap_size', help='Maximum size of the memory allocation pool used by the JVM.' )
-    parser.add_argument( '--output', help='Path to the output file' )
+    parser.add_argument( '--output', dest='output', help='Path to the output file' )
     args = parser.parse_args()
 
     tmp_dir = imagej2_base_utils.get_temp_dir()
@@ -65,19 +56,19 @@
     cmd += ' %s' % tmp_input_path
     cmd += ' %s' % args.input_datatype
     cmd += ' %s ' % args.noise
-    cmd += handle_none_type( args.standard_deviation )
-    cmd += handle_none_type( args.radius )
-    cmd += handle_none_type( args.threshold )
+    cmd += imagej2_base_utils.handle_none_type( args.standard_deviation )
+    cmd += imagej2_base_utils.handle_none_type( args.radius )
+    cmd += imagej2_base_utils.handle_none_type( args.threshold )
     cmd += ' %s' % args.which_outliers
     cmd += ' %s' % args.randomj
-    cmd += handle_none_type( args.trials )
-    cmd += handle_none_type( args.probability )
-    cmd += handle_none_type( args.lammbda )
-    cmd += handle_none_type( args.order, val_type='int' )
-    cmd += handle_none_type( args.mean )
-    cmd += handle_none_type( args.sigma )
-    cmd += handle_none_type( args.min )
-    cmd += handle_none_type( args.max )
+    cmd += imagej2_base_utils.handle_none_type( args.trials )
+    cmd += imagej2_base_utils.handle_none_type( args.probability )
+    cmd += imagej2_base_utils.handle_none_type( args.lammbda )
+    cmd += imagej2_base_utils.handle_none_type( args.order, val_type='int' )
+    cmd += imagej2_base_utils.handle_none_type( args.mean )
+    cmd += imagej2_base_utils.handle_none_type( args.sigma )
+    cmd += imagej2_base_utils.handle_none_type( args.min )
+    cmd += imagej2_base_utils.handle_none_type( args.max )
     cmd += ' %s' % args.insertion
     cmd += ' %s' % tmp_output_path
 
@@ -86,7 +77,7 @@
 
     # Handle execution errors.
     if rc != 0:
-        error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_stdout )
+        error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
         imagej2_base_utils.stop_err( error_message )
     # Handle processing errors.
     if os.path.getsize( error_log ) > 0:
--- a/imagej2_noise.xml	Fri Jun 26 05:28:29 2015 -0400
+++ b/imagej2_noise.xml	Tue Aug 04 11:15:16 2015 -0400
@@ -10,7 +10,7 @@
             </param>
         </xml>
     </macros>
-    <expand macro="fiji_headless_requirements" />
+    <expand macro="fiji_20141125_requirements" />
     <command>
 <![CDATA[
     python $__tool_directory__/imagej2_noise.py
--- a/jython_script.py	Fri Jun 26 05:28:29 2015 -0400
+++ b/jython_script.py	Tue Aug 04 11:15:16 2015 -0400
@@ -1,11 +1,7 @@
 import sys
 from ij import IJ
 from ij import ImagePlus
-
-def handle_error( error_log, msg ):
-    elh = open( error_log, 'wb' )
-    elh.write( msg )
-    elh.close()
+import jython_utils
 
 # Fiji Jython interpreter implements Python 2.5 which does not
 # provide support for argparse.
@@ -62,14 +58,14 @@
         # we'll be able to write tool validators rather than having to stop the job in
         # an error state.
         msg = "Remove NaNs requires a 32-bit image, the selected image is %d-bit" % bit_depth
-        handle_error( error_log, msg )
+        jython_utils.handle_error( error_log, msg )
         error = True
 elif noise == 'rof_denoise':
     if image_type == ImagePlus.GRAY32:
         IJ.run( image_plus_copy, "ROF Denoise", "" )
     else:
         msg = "ROF Denoise requires an image of type 32-bit grayscale, the selected image is %d-bit" % ( bit_depth )
-        handle_error( error_log, msg )
+        jython_utils.handle_error( error_log, msg )
         error = True
 elif noise == 'randomj':
     if randomj == 'randomj_binomial':
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jython_utils.py	Tue Aug 04 11:15:16 2015 -0400
@@ -0,0 +1,37 @@
+import imagej2_base_utils
+from ij import IJ
+
+IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES = { '0':'GRAY8', '1':'GRAY16', '2':'GRAY32',
+                                       '3':'COLOR_256', '4':'COLOR_RGB' }
+
+def convert_before_saving_as_tiff( image_plus ):
+    # The bUnwarpJ plug-in produces TIFF image stacks consisting of 3
+    # slices which can be viewed in ImageJ.  The 3 slices are: 1) the
+    # registered image, 2) the target image and 3) the black/white warp
+    # image.  When running bUnwarpJ from the command line (as these
+    # Galaxy wrappers do) the initial call to IJ.openImage() (to open the
+    # registered source and target images produced by bUnwarpJ) in the
+    # tool's jython_script.py returns an ImagePlus object with a single
+    # slice which is the "generally undesired" slice 3 discussed above.
+    # However, a call to IJ.saveAs() will convert the single-slice TIFF
+    # into a 3-slice TIFF image stack (as described above) if the selected
+    # format for saving is TIFF.  Galaxy supports only single-layered
+    # images, so to work around this behavior, we have to convert the
+    # image to something other than TIFF so that slices are eliminated.
+    # We can then convert back to TIFF for saving.  There might be a way
+    # to do this without converting twice, but I spent a lot of time looking
+    # and I have yet to discover it.
+    tmp_dir = imagej2_base_utils.get_temp_dir()
+    tmp_out_png_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'png' )
+    IJ.saveAs( image_plus, 'png', tmp_out_png_path )
+    return IJ.openImage( tmp_out_png_path )
+
+def get_display_image_type( image_type ):
+    return IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES.get( str( image_type ), None )
+
+def handle_error( error_log, msg ):
+    # Java writes a lot of stuff to stderr, so the received error_log 
+    # will log actual errors.
+    elh = open( error_log, 'wb' )
+    elh.write( msg )
+    elh.close()
--- a/tool_dependencies.xml	Fri Jun 26 05:28:29 2015 -0400
+++ b/tool_dependencies.xml	Tue Aug 04 11:15:16 2015 -0400
@@ -1,6 +1,6 @@
 <?xml version="1.0"?>
 <tool_dependency>
     <package name="fiji" version="20141125">
-        <repository changeset_revision="7121a1ea4839" name="package_fiji_20141125" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu" />
+        <repository changeset_revision="9dbe89105a25" name="package_fiji_20141125" owner="iuc" toolshed="https://testtoolshed.g2.bx.psu.edu" />
     </package>
 </tool_dependency>