Mercurial > repos > imgteam > imagej2_bunwarpj_compose_raw_elastic
comparison imagej2_bunwarpj_adapt_transform.py @ 0:1bf7e70e67f9 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:02 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1bf7e70e67f9 |
---|---|
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( '--input_elastic_transformation', dest='input_elastic_transformation', help='Input elastic transformation matrix' ) | |
14 parser.add_argument( '--image_size_factor', dest='image_size_factor', type=float, help='Image size factor' ) | |
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 input_elastic_transformation_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input_elastic_transformation, 'txt' ) | |
23 | |
24 # Define command response buffers. | |
25 tmp_out = tempfile.NamedTemporaryFile().name | |
26 tmp_stdout = open( tmp_out, 'wb' ) | |
27 tmp_err = tempfile.NamedTemporaryFile().name | |
28 tmp_stderr = open( tmp_err, 'wb' ) | |
29 | |
30 def is_power2( val ): | |
31 if val < 0: | |
32 return False | |
33 if val < 1: | |
34 val = 1.0 / val | |
35 val = int( val ) | |
36 return ( ( val & ( val - 1 ) ) == 0 ) | |
37 | |
38 # Build the command line to adapt the transformation. | |
39 cmd = imagej2_base_utils.get_base_cmd_bunwarpj( None ) | |
40 if cmd is None: | |
41 imagej2_base_utils.stop_err( "bUnwarpJ not found!" ) | |
42 cmd += ' -adapt_transform' | |
43 | |
44 # Make sure the value of image_size_factor is a power of 2 (positive or negative). | |
45 if is_power2( args.image_size_factor ): | |
46 image_size_factor = args.image_size_factor | |
47 else: | |
48 msg = "Image size factor must be a positive or negative power of 2 (0.25, 0.5, 2, 4, 8, etc)." | |
49 imagej2_base_utils.stop_err( msg ) | |
50 | |
51 # Target is sent before source. | |
52 cmd += ' %s' % target_image_path | |
53 cmd += ' %s' % source_image_path | |
54 cmd += ' %s' % input_elastic_transformation_path | |
55 cmd += ' %s' % args.output | |
56 cmd += ' %2.f' % image_size_factor | |
57 | |
58 # Adapt the transformation based on the image size factor using bUnwarpJ. | |
59 proc = subprocess.Popen( args=cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True ) | |
60 rc = proc.wait() | |
61 if rc != 0: | |
62 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout ) | |
63 imagej2_base_utils.stop_err( error_message ) | |
64 | |
65 imagej2_base_utils.cleanup_before_exit( tmp_dir ) |