Mercurial > repos > imgteam > imagej2_sharpen
comparison imagej2_bunwarpj_elastic_transform.py @ 0:855fa4bb8a44 draft default tip
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
| author | imgteam |
|---|---|
| date | Tue, 17 Sep 2019 17:03:40 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:855fa4bb8a44 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 import argparse | |
| 3 import shutil | |
| 4 import subprocess | |
| 5 import tempfile | |
| 6 import imagej2_base_utils | |
| 7 | |
| 8 # Parse Command Line. | |
| 9 parser = argparse.ArgumentParser() | |
| 10 parser.add_argument( '--source_image', dest='source_image', help='Source image' ) | |
| 11 parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' ) | |
| 12 parser.add_argument( '--target_image', dest='target_image', help='Target image' ) | |
| 13 parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' ) | |
| 14 parser.add_argument( '--elastic_transformation', dest='elastic_transformation', help='Elastic transformation as saved by bUnwarpJ in elastic format' ) | |
| 15 parser.add_argument( '--source_out', help='Output source image' ) | |
| 16 parser.add_argument( '--source_out_datatype', help='Output registered source image format' ) | |
| 17 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' ) | |
| 18 | |
| 19 args = parser.parse_args() | |
| 20 | |
| 21 tmp_dir = imagej2_base_utils.get_temp_dir() | |
| 22 source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format ) | |
| 23 tmp_source_out_tiff_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'tiff' ) | |
| 24 tmp_source_out_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.source_out_datatype ) | |
| 25 target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format ) | |
| 26 elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.elastic_transformation, 'txt' ) | |
| 27 | |
| 28 # Define command response buffers. | |
| 29 tmp_out = tempfile.NamedTemporaryFile().name | |
| 30 tmp_stdout = open( tmp_out, 'wb' ) | |
| 31 tmp_err = tempfile.NamedTemporaryFile().name | |
| 32 tmp_stderr = open( tmp_err, 'wb' ) | |
| 33 | |
| 34 # Build the command line to apply the transformation. | |
| 35 cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) | |
| 36 if cmd is None: | |
| 37 imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) | |
| 38 cmd += ' -elastic_transform' | |
| 39 # Target is sent before source. | |
| 40 cmd += ' %s' % target_image_path | |
| 41 cmd += ' %s' % source_image_path | |
| 42 cmd += ' %s' % elastic_transformation_path | |
| 43 cmd += ' %s' % tmp_source_out_tiff_path | |
| 44 | |
| 45 # Apply the elastic transformation using bUnwarpJ. | |
| 46 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) | |
| 47 rc = proc.wait() | |
| 48 if rc != 0: | |
| 49 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) | |
| 50 imagej2_base_utils.stop_err( error_message ) | |
| 51 | |
| 52 # Convert the registered image to the specified output format. | |
| 53 tmp_out = tempfile.NamedTemporaryFile().name | |
| 54 tmp_stdout = open( tmp_out, 'wb' ) | |
| 55 tmp_err = tempfile.NamedTemporaryFile().name | |
| 56 tmp_stderr = open( tmp_err, 'wb' ) | |
| 57 | |
| 58 cmd = imagej2_base_utils.get_base_command_imagej2( None, jython_script=args.jython_script ) | |
| 59 if cmd is None: | |
| 60 imagej2_base_utils.stop_err( "ImageJ not found!" ) | |
| 61 cmd += ' %s %s %s' % ( tmp_source_out_tiff_path, | |
| 62 args.source_out_datatype, | |
| 63 tmp_source_out_path ) | |
| 64 | |
| 65 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True ) | |
| 66 rc = proc.wait() | |
| 67 if rc != 0: | |
| 68 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) | |
| 69 imagej2_base_utils.stop_err( error_message ) | |
| 70 | |
| 71 # Save the Registered Source Image to the defined output. | |
| 72 shutil.move( tmp_source_out_path, args.source_out ) | |
| 73 imagej2_base_utils.cleanup_before_exit( tmp_dir ) |
