comparison imagej2_bunwarpj_compare_elastic_raw.py @ 0:baa308e2533a draft

"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:07:31 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:baa308e2533a
1 #!/usr/bin/env python
2 import argparse
3 import subprocess
4 import tempfile
5 import imagej2_base_utils
6
7 # Parse Command Line.
8 parser = argparse.ArgumentParser()
9 parser.add_argument( '--source_image', dest='source_image', help='Source image' )
10 parser.add_argument( '--source_image_format', dest='source_image_format', help='Source image format' )
11 parser.add_argument( '--target_image', dest='target_image', help='Target image' )
12 parser.add_argument( '--target_image_format', dest='target_image_format', help='Target image format' )
13 parser.add_argument( '--target_elastic_transformation', dest='target_elastic_transformation', help='Target elastic transformation matrix' )
14 parser.add_argument( '--source_raw_transformation', dest='source_raw_transformation', help='Source raw transformation matrix' )
15 parser.add_argument( '--output', dest='output', help='Warping index' )
16
17 args = parser.parse_args()
18
19 tmp_dir = imagej2_base_utils.get_temp_dir()
20 source_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_image, args.source_image_format )
21 target_image_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_image, args.target_image_format )
22 target_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_elastic_transformation, 'txt' )
23 source_raw_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_raw_transformation, 'txt' )
24 # bUnwarpJ produces several lines of output that we need to discard, so
25 # we'll use a temporary output file from which we'll read only the last line.
26 tmp_output_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.output, '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 calculate the warping index.
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 += ' -compare_elastic_raw'
39 cmd += ' %s' % target_image_path
40 cmd += ' %s' % source_image_path
41 cmd += ' %s' % target_elastic_transformation_path
42 cmd += ' %s' % source_raw_transformation_path
43 cmd += ' > %s' % tmp_output_path
44
45 # Calculate the warping index of elastic and raw transformations using bUnwarpJ.
46 proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, 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 # Example contents of tmp_output_path:
53 # ['Target image : ~/tmpHdt9Cs.jpg\n',
54 # 'Source image : ~/tmpu6kyfc.gif\n',
55 # 'Elastic Transformation file : ~/tmp4vZurG.txt\n',
56 # 'Raw Transformation file : ~/tmp2PNQcT.txt\n',
57 # ' Warping index = 25.007467512204983\n']
58 results = open( tmp_output_path, 'r' ).readlines()
59 warp_index = results[ -1 ].split( ' ' )[ -1 ]
60 outf = open( args.output, 'wb' )
61 outf.write( '%s' % warp_index )
62 outf.close()
63
64 imagej2_base_utils.cleanup_before_exit( tmp_dir )