comparison imagej2_utils.py @ 0:312e8ce708b1 draft

planemo upload commit 4bf97847c35c4dcf9638008b9b4b6c4e10015f19
author iuc
date Tue, 09 Jun 2015 12:21:40 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:312e8ce708b1
1 import bioformats
2 import javabridge
3 import os
4 import shutil
5
6 FIJI_JAR_DIR = os.environ.get( 'FIJI_JAR_DIR', None )
7 FIJI_OSX_JAVA3D_DIR = os.environ.get( 'FIJI_OSX_JAVA3D_DIR', None )
8 FIJI_PLUGIN_DIR = os.environ.get( 'FIJI_PLUGIN_DIR', None )
9 FIJI_ROOT_DIR = os.environ.get( 'FIJI_ROOT_DIR', None )
10
11 def get_java_class_path():
12 """
13 Return the Java class path for use when starting the JVM via Javabridge.
14 This function expects the following environment variable settings:
15 FIJI_JAR_DIR, FIJI_PLUGIN_DIR, FIJI_OSX_JAVA3D_DIR.
16 """
17 class_path_list = []
18 # Handle javabridge.JARS setting.
19 for jar_file in javabridge.JARS:
20 class_path_list.append( jar_file )
21 if FIJI_JAR_DIR is not None and os.path.isdir( FIJI_JAR_DIR ):
22 for filename in os.listdir( FIJI_JAR_DIR ):
23 if filename.endswith( '.jar' ):
24 class_path_list.append( os.path.join( FIJI_JAR_DIR, filename ) )
25 fiji_bioformats_jar_dir = os.path.join( FIJI_JAR_DIR, 'bio-formats' )
26 if os.path.isdir( fiji_bioformats_jar_dir ):
27 for filename in os.listdir( fiji_bioformats_jar_dir ):
28 if filename.endswith( '.jar' ):
29 class_path_list.append( os.path.join( fiji_bioformats_jar_dir, filename ) )
30 if FIJI_PLUGIN_DIR is not None and os.path.isdir( FIJI_PLUGIN_DIR ):
31 for filename in os.listdir( FIJI_PLUGIN_DIR ):
32 if filename.endswith( '.jar' ):
33 class_path_list.append( os.path.join( FIJI_PLUGIN_DIR, filename ) )
34 if FIJI_OSX_JAVA3D_DIR is not None and os.path.isdir( FIJI_OSX_JAVA3D_DIR ):
35 for filename in os.listdir( FIJI_OSX_JAVA3D_DIR ):
36 if filename.endswith( '.jar' ):
37 class_path_list.append( os.path.join( FIJI_OSX_JAVA3D_DIR, filename ) )
38 return class_path_list
39
40 def kill_vm():
41 javabridge.detach()
42 javabridge.kill_vm()
43
44 def load_image( image_path, c=None, z=0, t=0, series=None, index=None,
45 rescale=True, wants_max_intensity=False, channel_names=None ):
46 """
47 Load in image from a file using bioformats, always returning a tuple of
48 (image, scale) where the image is a numpy.ndarray object, and the scale
49 is an integer or None.
50 """
51 result = bioformats.load_image( path=image_path,
52 c=c,
53 z=z,
54 t=t,
55 series=series,
56 index=index,
57 rescale=rescale,
58 wants_max_intensity=wants_max_intensity,
59 channel_names=channel_names )
60 if wants_max_intensity:
61 # The value of result is a tuple: ( image, scale )
62 image, scale = result
63 else:
64 image = result
65 scale = None
66 # The image is a numpy.ndarray object, and the scale is an integer or None.
67 return image, scale
68
69 def start_vm( args=None, class_path=None, max_heap_size=None, run_headless=False ):
70 """
71 Start the JVM via Javabridge.
72 """
73 if class_path is None:
74 class_path = get_java_class_path()
75 # Start the JVM.
76 javabridge.start_vm( args=args, class_path=class_path, max_heap_size=max_heap_size, run_headless=run_headless )
77 # Suppress Java logging to stdout.
78 java_stack = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" )
79 java_stack_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_stack )
80 javabridge.static_call( 'Ljava/lang/System;', "setErr", '(Ljava/io/PrintStream;)V', java_stack_ps )
81 java_out = javabridge.make_instance( 'java/io/ByteArrayOutputStream', "()V" )
82 java_out_ps = javabridge.make_instance( 'java/io/PrintStream', "(Ljava/io/OutputStream;)V", java_out )
83 javabridge.static_call( 'Ljava/lang/System;', "setOut", '(Ljava/io/PrintStream;)V', java_out_ps )
84 javabridge.attach()
85
86 def write_image( image_path, pixels, pixel_type, c=0, z=0, t=0,
87 size_c=1, size_z=1, size_t=1, channel_names=None,
88 move_to=None ):
89 """
90 Write an image file using bioformats. Bioformats uses file extensions
91 (e.g., .job, .gif, etc) when reading and writing image files, so the
92 Galaxy dataset naming convention of setting all file extensions as .dat
93 is handled by setting the move_to parameter to the value of the Galaxy
94 dataset path.
95 """
96 bioformats.write_image( pathname=image_path,
97 pixels=pixels,
98 pixel_type=pixel_type,
99 c=c,
100 z=z,
101 t=t,
102 size_c=size_c,
103 size_z=size_z,
104 size_t=size_t,
105 channel_names=channel_names )
106 if move_to is not None and os.path.exists( move_to ):
107 shutil.move( image_path, os.path.abspath( move_to ) )