annotate jython_script.py @ 1:dd9041dc3c27 draft

planemo upload commit 378c3ecfbaed0330faf197811d379431b0f9e92c-dirty
author iuc
date Sun, 11 Oct 2015 13:29:46 -0400
parents d065ec177dcd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
1 import jython_utils
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
2 import sys
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
3 from ij import IJ
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
4 from ij.plugin.filter import Analyzer
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
5
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
6 # Fiji Jython interpreter implements Python 2.5 which does not
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
7 # provide support for argparse.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
8 error_log = sys.argv[ -14 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
9 input = sys.argv[ -13 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
10 black_background = jython_utils.asbool( sys.argv[ -12 ] )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
11 size = sys.argv[ -11 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
12 circularity_min = float( sys.argv[ -10 ] )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
13 circularity_max = float( sys.argv[ -9 ] )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
14 show = sys.argv[ -8 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
15 display_results = jython_utils.asbool( sys.argv[ -7 ] )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
16 all_results = jython_utils.asbool( sys.argv[ -6 ] )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
17 exclude_edges = jython_utils.asbool( sys.argv[ -5 ] )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
18 include_holes = jython_utils.asbool( sys.argv[ -4 ] )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
19 tmp_output_path = sys.argv[ -3 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
20 output_datatype = sys.argv[ -2 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
21 results_path = sys.argv[ -1 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
22
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
23 # Open the input image file.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
24 input_image_plus = IJ.openImage( input )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
25
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
26 # Create a copy of the image.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
27 input_image_plus_copy = input_image_plus.duplicate()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
28 image_processor_copy = input_image_plus_copy.getProcessor()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
29 analyzer = Analyzer( input_image_plus_copy )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
30
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
31 try:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
32 # Set binary options.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
33 options = jython_utils.get_binary_options( black_background=black_background )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
34 IJ.run( input_image_plus_copy, "Options...", options )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
35
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
36 # Convert image to binary if necessary.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
37 if not image_processor_copy.isBinary():
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
38 # Convert the image to binary grayscale.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
39 IJ.run( input_image_plus_copy, "Make Binary", "" )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
40
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
41 # Set the options.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
42 options = [ 'size=%s' % size ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
43 circularity_str = '%.3f-%.3f' % ( circularity_min, circularity_max )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
44 options.append( 'circularity=%s' % circularity_str )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
45 if show.find( '_' ) >= 0:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
46 show_str = '[%s]' % show.replace( '_', ' ' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
47 else:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
48 show_str = show
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
49 options.append( 'show=%s' % show_str )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
50 if display_results:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
51 options.append( 'display' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
52 if not all_results:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
53 options.append( 'summarize' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
54 if exclude_edges:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
55 options.append( 'exclude' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
56 if include_holes:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
57 options.append( 'include' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
58 # Always run "in_situ".
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
59 options.append( 'in_situ' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
60
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
61 # Run the command.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
62 IJ.run( input_image_plus_copy, "Analyze Particles...", " ".join( options ) )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
63
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
64 # Save outputs.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
65 if tmp_output_path not in [ None, 'None' ]:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
66 # Save the ImagePlus object as a new image.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
67 IJ.saveAs( input_image_plus_copy, output_datatype, tmp_output_path )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
68 if display_results and results_path not in [ None, 'None' ]:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
69 results_table = analyzer.getResultsTable()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
70 results_table.saveAs( results_path )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
71 except Exception, e:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
72 jython_utils.handle_error( error_log, str( e ) )