comparison imagej2_bunwarpj_compare_elastic.py @ 0:aeb9bb864b8c 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 16:56:37 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:aeb9bb864b8c
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( '--source_transformation', dest='source_transformation', help='Direct source transformation matrix' )
14 parser.add_argument( '--target_transformation', dest='target_transformation', help='Inverse target 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 source_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.source_transformation, 'txt' )
23 target_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.target_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'
39 # Target is sent before source.
40 cmd += ' %s' % target_image_path
41 cmd += ' %s' % source_image_path
42 cmd += ' %s' % target_transformation_path
43 cmd += ' %s' % source_transformation_path
44 cmd += ' > %s' % tmp_output_path
45
46 # Calculate the warping index of two elastic transformations using bUnwarpJ.
47 proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True )
48 rc = proc.wait()
49 if rc != 0:
50 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
51 imagej2_base_utils.stop_err( error_message )
52
53 # Example contents of tmp_output_path:
54 # ['Target image : ~/tmpKAYF1P.jpg\n',
55 # 'Source image : ~/tmpgQX0dy.gif\n',
56 # 'Target Transformation file : ~/tmpZJC_4B.txt\n',
57 # 'Source Transformation file : ~/tmphsSojl.txt\n',
58 # ' Warping index = 14.87777347388348\n']
59 results = open( tmp_output_path, 'r' ).readlines()
60 warp_index = results[ -1 ].split( ' ' )[ -1 ]
61 outf = open( args.output, 'wb' )
62 outf.write( '%s' % warp_index )
63 outf.close()
64
65 imagej2_base_utils.cleanup_before_exit( tmp_dir )