annotate jython_utils.py @ 0:bb84f130a4e1 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 17:04:59 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
1 import imagej2_base_utils
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
2 from ij import IJ
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
3
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
4 IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES = { '0':'GRAY8', '1':'GRAY16', '2':'GRAY32',
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
5 '3':'COLOR_256', '4':'COLOR_RGB' }
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
6
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
7 def asbool( val ):
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
8 return str( val ).lower() in [ 'yes', 'true' ]
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
9
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
10 def convert_before_saving_as_tiff( image_plus ):
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
11 # The bUnwarpJ plug-in produces TIFF image stacks consisting of 3
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
12 # slices which can be viewed in ImageJ. The 3 slices are: 1) the
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
13 # registered image, 2) the target image and 3) the black/white warp
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
14 # image. When running bUnwarpJ from the command line (as these
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
15 # Galaxy wrappers do) the initial call to IJ.openImage() (to open the
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
16 # registered source and target images produced by bUnwarpJ) in the
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
17 # tool's jython_script.py returns an ImagePlus object with a single
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
18 # slice which is the "generally undesired" slice 3 discussed above.
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
19 # However, a call to IJ.saveAs() will convert the single-slice TIFF
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
20 # into a 3-slice TIFF image stack (as described above) if the selected
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
21 # format for saving is TIFF. Galaxy supports only single-layered
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
22 # images, so to work around this behavior, we have to convert the
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
23 # image to something other than TIFF so that slices are eliminated.
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
24 # We can then convert back to TIFF for saving. There might be a way
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
25 # to do this without converting twice, but I spent a lot of time looking
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
26 # and I have yet to discover it.
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
27 tmp_dir = imagej2_base_utils.get_temp_dir()
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
28 tmp_out_png_path = imagej2_base_utils.get_temporary_image_path( tmp_dir, 'png' )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
29 IJ.saveAs( image_plus, 'png', tmp_out_png_path )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
30 return IJ.openImage( tmp_out_png_path )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
31
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
32 def get_binary_options( black_background, iterations=1, count=1, pad_edges_when_eroding='no' ):
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
33 options = [ 'edm=Overwrite', 'iterations=%d' % iterations, 'count=%d' % count ]
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
34 if asbool( pad_edges_when_eroding ):
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
35 options.append( 'pad' )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
36 if asbool( black_background ):
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
37 options.append( "black" )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
38 return " ".join( options )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
39
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
40 def get_display_image_type( image_type ):
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
41 return IMAGE_PLUS_IMAGE_TYPE_FIELD_VALUES.get( str( image_type ), None )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
42
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
43 def handle_error( error_log, msg ):
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
44 # Java writes a lot of stuff to stderr, so the received error_log
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
45 # will log actual errors.
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
46 elh = open( error_log, 'wb' )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
47 elh.write( msg )
bb84f130a4e1 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/image_processing/imagej2 commit b08f0e6d1546caaf627b21f8c94044285d5d5b9c-dirty"
imgteam
parents:
diff changeset
48 elh.close()