Mercurial > repos > iuc > imagej2_bunwarpj_raw_transform
diff imagej2_bunwarpj_raw_transform.py @ 0:64aacdf3171f draft
planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
| author | iuc |
|---|---|
| date | Fri, 19 Jun 2015 16:59:58 -0400 |
| parents | |
| children | 0ff301860507 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imagej2_bunwarpj_raw_transform.py Fri Jun 19 16:59:58 2015 -0400 @@ -0,0 +1,79 @@ +#!/usr/bin/env python +import argparse +import os +import shutil +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( '--raw_transformation', dest='raw_transformation', default=None, help='Raw transformation as saved by bUnwarpJ' ) +parser.add_argument( '--source_out', help='Output source image' ) +parser.add_argument( '--source_out_datatype', help='Output registered source image format' ) +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.' ) + +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 ) +tmp_source_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' ) +tmp_source_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.source_out_datatype ) +target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) +raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.raw_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' ) + +# Build the command line to apply the raw transformation. +cmd = imagej2_base_utils.get_base_cmd_bunwarpj( memory_size ) +if cmd is None: + imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) +cmd += ' -raw_transform' +# Target is sent before source. +cmd += ' %s' % target_image_path +cmd += ' %s' % source_image_path +cmd += ' %s' % raw_transformation_path +cmd += ' %s' % tmp_source_out_tiff_path + +# Apply the raw transformation using bUnwarpJ. +proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, 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 ) + +# Convert the registered image to the specified output format. +tmp_out = tempfile.NamedTemporaryFile().name +tmp_stdout = open( tmp_out, 'wb' ) +tmp_err = tempfile.NamedTemporaryFile().name +tmp_stderr = open( tmp_err, 'wb' ) + +cmd = imagej2_base_utils.get_base_command_imagej2( memory_size, jython_script=args.jython_script ) +if cmd is None: + imagej2_base_utils.stop_err( "ImageJ not found!" ) +cmd += ' %s %s %s' % ( tmp_source_out_tiff_path, + args.source_out_datatype, + tmp_source_out_path ) + +proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, 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 ) + +# Save the Registered Source Image to the defined output. +shutil.move( tmp_source_out_path, args.source_out ) +imagej2_base_utils.cleanup_before_exit( tmp_dir )
