annotate imagej2_noise.py @ 2:c8fc3f34cbff draft

planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
author iuc
date Tue, 04 Aug 2015 11:15:16 -0400
parents da9a450c85b7
children d99cf938ad41
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
1 #!/usr/bin/env python
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
2 import argparse
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
3 import os
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
4 import shutil
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
5 import subprocess
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
6 import tempfile
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
7 import imagej2_base_utils
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
8
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
9 if __name__=="__main__":
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
10 # Parse Command Line.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
11 parser = argparse.ArgumentParser()
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
12 parser.add_argument( '--input', dest='input', help='Path to the input file' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
13 parser.add_argument( '--input_datatype', dest='input_datatype', help='Datatype of the input image' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
14 parser.add_argument( '--noise', dest='noise', help='Specified noise to add to or remove from the image' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
15 parser.add_argument( '--standard_deviation', dest='standard_deviation', type=float, default=None, help='Standard deviation' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
16 parser.add_argument( '--radius', dest='radius', type=float, default=None, help='Radius' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
17 parser.add_argument( '--threshold', dest='threshold', type=float, default=None, help='Threshold' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
18 parser.add_argument( '--which_outliers', dest='which_outliers', default=None, help='Which outliers' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
19 parser.add_argument( '--randomj', dest='randomj', default=None, help='RandomJ' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
20 parser.add_argument( '--trials', dest='trials', type=float, default=None, help='Trials' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
21 parser.add_argument( '--probability', dest='probability', type=float, default=None, help='Probability' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
22 parser.add_argument( '--lammbda', dest='lammbda', type=float, default=None, help='Lambda' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
23 parser.add_argument( '--order', dest='order', type=int, default=None, help='Order' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
24 parser.add_argument( '--mean', dest='mean', type=float, default=None, help='Mean' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
25 parser.add_argument( '--sigma', dest='sigma', type=float, default=None, help='Sigma' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
26 parser.add_argument( '--min', dest='min', type=float, default=None, help='Min' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
27 parser.add_argument( '--max', dest='max', type=float, default=None, help='Max' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
28 parser.add_argument( '--insertion', dest='insertion', default=None, help='Insertion' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
29 parser.add_argument( '--jython_script', dest='jython_script', help='Path to the Jython script' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
30 parser.add_argument( '--max_heap_size_type', dest='max_heap_size_type', help='Type (default or megabytes) of max_heap_size value' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
31 parser.add_argument( '--max_heap_size', dest='max_heap_size', help='Maximum size of the memory allocation pool used by the JVM.' )
2
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
32 parser.add_argument( '--output', dest='output', help='Path to the output file' )
0
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
33 args = parser.parse_args()
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
34
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
35 tmp_dir = imagej2_base_utils.get_temp_dir()
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
36 # ImageJ expects valid image file extensions, so the Galaxy .dat extension does not
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
37 # work for some features. The following creates a symlink with an appropriate file
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
38 # extension that points to the Galaxy dataset. This symlink is used by ImageJ.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
39 tmp_input_path = imagej2_base_utils.get_input_image_path( tmp_dir, args.input, args.input_datatype )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
40 tmp_output_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, args.input_datatype )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
41 # Set the size of the memory allocation pool used by the JVM.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
42 memory_size = imagej2_base_utils.get_max_heap_size_value( args.max_heap_size_type, args.max_heap_size )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
43
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
44 # Define command response buffers.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
45 tmp_out = tempfile.NamedTemporaryFile().name
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
46 tmp_stdout = open( tmp_out, 'wb' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
47 tmp_err = tempfile.NamedTemporaryFile().name
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
48 tmp_stderr = open( tmp_err, 'wb' )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
49 # Java writes a lot of stuff to stderr, so we'll specify a file for handling actual errors.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
50 error_log = tempfile.NamedTemporaryFile( delete=False ).name
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
51 # Build the command line.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
52 cmd = imagej2_base_utils.get_base_command_imagej2( memory_size, jython_script=args.jython_script )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
53 if cmd is None:
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
54 imagej2_base_utils.stop_err( "ImageJ not found!" )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
55 cmd += ' %s' % error_log
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
56 cmd += ' %s' % tmp_input_path
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
57 cmd += ' %s' % args.input_datatype
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
58 cmd += ' %s ' % args.noise
2
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
59 cmd += imagej2_base_utils.handle_none_type( args.standard_deviation )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
60 cmd += imagej2_base_utils.handle_none_type( args.radius )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
61 cmd += imagej2_base_utils.handle_none_type( args.threshold )
0
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
62 cmd += ' %s' % args.which_outliers
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
63 cmd += ' %s' % args.randomj
2
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
64 cmd += imagej2_base_utils.handle_none_type( args.trials )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
65 cmd += imagej2_base_utils.handle_none_type( args.probability )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
66 cmd += imagej2_base_utils.handle_none_type( args.lammbda )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
67 cmd += imagej2_base_utils.handle_none_type( args.order, val_type='int' )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
68 cmd += imagej2_base_utils.handle_none_type( args.mean )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
69 cmd += imagej2_base_utils.handle_none_type( args.sigma )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
70 cmd += imagej2_base_utils.handle_none_type( args.min )
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
71 cmd += imagej2_base_utils.handle_none_type( args.max )
0
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
72 cmd += ' %s' % args.insertion
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
73 cmd += ' %s' % tmp_output_path
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
74
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
75 proc = subprocess.Popen( args=cmd, stderr=tmp_stderr, stdout=tmp_stdout, shell=True )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
76 rc = proc.wait()
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
77
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
78 # Handle execution errors.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
79 if rc != 0:
2
c8fc3f34cbff planemo upload commit 8f8692e83217a072a3ed928469621f1f606ab28a-dirty
iuc
parents: 0
diff changeset
80 error_message = imagej2_base_utils.get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout )
0
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
81 imagej2_base_utils.stop_err( error_message )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
82 # Handle processing errors.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
83 if os.path.getsize( error_log ) > 0:
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
84 error_message = open( error_log, 'r' ).read()
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
85 imagej2_base_utils.stop_err( error_message )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
86 # Save the output image.
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
87 shutil.move( tmp_output_path, args.output )
da9a450c85b7 planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
88 imagej2_base_utils.cleanup_before_exit( tmp_dir )