annotate jython_utils.py @ 2:6f129c1ec6d1 draft

planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
author iuc
date Tue, 04 Aug 2015 13:21:24 -0400
parents 64aacdf3171f
children 2a8118c2ec03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
1 import imagej2_base_utils
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
2 from ij import IJ
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
3
2
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
4 IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES = { '0':'GRAY8', '1':'GRAY16', '2':'GRAY32',
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
5 '3':'COLOR_256', '4':'COLOR_RGB' }
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
6
0
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
7 def convert_before_saving_as_tiff( image_plus ):
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
8 # The bUnwarpJ plug-in produces TIFF image stacks consisting of 3
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
9 # slices which can be viewed in ImageJ. The 3 slices are: 1) the
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
10 # registered image, 2) the target image and 3) the black/white warp
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
11 # image. When running bUnwarpJ from the command line (as these
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
12 # Galaxy wrappers do) the initial call to IJ.openImage() (to open the
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
13 # registered source and target images produced by bUnwarpJ) in the
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
14 # tool's jython_script.py returns an ImagePlus object with a single
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
15 # slice which is the "generally undesired" slice 3 discussed above.
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
16 # However, a call to IJ.saveAs() will convert the single-slice TIFF
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
17 # into a 3-slice TIFF image stack (as described above) if the selected
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
18 # format for saving is TIFF. Galaxy supports only single-layered
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
19 # images, so to work around this behavior, we have to convert the
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
20 # image to something other than TIFF so that slices are eliminated.
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
21 # We can then convert back to TIFF for saving. There might be a way
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
22 # to do this without converting twice, but I spent a lot of time looking
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
23 # and I have yet to discover it.
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
24 tmp_dir = imagej2_base_utils.get_temp_dir()
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
25 tmp_out_png_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'png' )
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
26 IJ.saveAs( image_plus, 'png', tmp_out_png_path )
64aacdf3171f planemo upload commit 7195716d447effc5bc32154279de4b84369eccdb-dirty
iuc
parents:
diff changeset
27 return IJ.openImage( tmp_out_png_path )
2
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
28
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
29 def get_display_image_type( image_type ):
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
30 return IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES.get( str( image_type ), None )
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
31
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
32 def handle_error( error_log, msg ):
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
33 # Java writes a lot of stuff to stderr, so the received error_log
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
34 # will log actual errors.
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
35 elh = open( error_log, 'wb' )
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
36 elh.write( msg )
6f129c1ec6d1 planemo upload commit 98c34e070343a117019ffd25a3af117808159bf5-dirty
iuc
parents: 0
diff changeset
37 elh.close()