diff imagej2_bunwarpj_adapt_transform.py @ 0:213ace68ff50 draft

planemo upload commit 369e40078146d00608d52205bb8cee66ae735b76-dirty
author iuc
date Fri, 26 Jun 2015 05:29:22 -0400
parents
children 6beb1921064d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/imagej2_bunwarpj_adapt_transform.py	Fri Jun 26 05:29:22 2015 -0400
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+import argparse
+import subprocess
+import tempfile
+import imagej2_base_utils
+
+# Parse Command Line.
+parser = argparse.ArgumentParser()
+parser.add_argument( '--source_image', dest='source_image', help='Source image' )
+parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' )
+parser.add_argument( '--target_image', dest='target_image', help='Target image' )
+parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' )
+parser.add_argument( '--input_elastic_transformation', dest='input_elastic_transformation', help='Input elastic transformation matrix' )
+parser.add_argument( '--image_size_factor', dest='image_size_factor', type=float, help='Image size factor' )
+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', dest='output', help='Warping index' )
+
+args = parser.parse_args()
+
+tmp_dir = imagej2_base_utils.get_temp_dir()
+source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format )
+target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format )
+input_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input_elastic_transformation, 'txt' )
+
+# Set the size of the memory allocation pool used by the JVM.
+memory_size = imagej2_base_utils.get_max_heap_size_value( args.max_heap_size_type, args.max_heap_size )
+
+# Define command response buffers.
+tmp_out = tempfile.NamedTemporaryFile().name
+tmp_stdout = open( tmp_out, 'wb' )
+tmp_err = tempfile.NamedTemporaryFile().name
+tmp_stderr = open( tmp_err, 'wb' )
+
+def is_power2( val ):
+    if val < 0:
+        return False
+    if val < 1:
+        val = 1.0 / val
+    val = int( val )
+    return ( ( val & ( val - 1 ) ) == 0 )
+
+# Build the command line to adapt the transformation.
+cmd = imagej2_base_utils.get_base_cmd_bunwarpj( memory_size )
+if cmd is None:
+    imagej2_base_utils.stop_err( "bUnwarpJ not found!" )
+cmd += ' -adapt_transform'
+
+# Make sure the value of image_size_factor is a power of 2 (positive or negative).
+if is_power2( args.image_size_factor ):
+    image_size_factor = args.image_size_factor
+else:
+    msg = "Image size factor must be a positive or negative power of 2 (0.25, 0.5, 2, 4, 8, etc)."
+    imagej2_base_utils.stop_err( msg )
+
+# Target is sent before source.
+cmd += ' %s' % target_image_path
+cmd += ' %s' % source_image_path
+cmd += ' %s' % input_elastic_transformation_path
+cmd += ' %s' % args.output
+cmd += ' %2.f' % image_size_factor
+
+# Adapt the transformation based on the image size factor using bUnwarpJ.
+proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True )
+rc = proc.wait()
+if rc != 0:
+    error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_stdout )
+    imagej2_base_utils.stop_err( error_message )
+
+imagej2_base_utils.cleanup_before_exit( tmp_dir )