annotate imagej2_base_utils.py @ 0:d065ec177dcd draft

planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
author iuc
date Wed, 26 Aug 2015 14:36:17 -0400
parents
children dd9041dc3c27
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 os
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
2 import shutil
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
3 import sys
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
4 import tempfile
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
5
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
6 FIJI_JAR_DIR = os.environ.get( 'FIJI_JAR_DIR', None )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
7 FIJI_OSX_JAVA3D_DIR = os.environ.get( 'FIJI_OSX_JAVA3D_DIR', None )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
8 FIJI_PLUGIN_DIR = os.environ.get( 'FIJI_PLUGIN_DIR', None )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
9 FIJI_ROOT_DIR = os.environ.get( 'FIJI_ROOT_DIR', None )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
10
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
11 BUFF_SIZE = 1048576
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
12
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
13 def cleanup_before_exit( tmp_dir ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
14 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
15 Remove temporary files and directories prior to tool exit.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
16 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
17 if tmp_dir and os.path.exists( tmp_dir ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
18 shutil.rmtree( tmp_dir )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
19
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
20 def get_base_cmd_bunwarpj( jvm_memory ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
21 if FIJI_JAR_DIR is not None and FIJI_PLUGIN_DIR is not None:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
22 if jvm_memory in [ None, 'None' ]:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
23 jvm_memory_str = ''
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
24 else:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
25 jvm_memory_str = '-Xmx%s' % jvm_memory
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
26 bunwarpj_base_cmd = "java %s -cp %s/ij-1.49k.jar:%s/bUnwarpJ_-2.6.1.jar bunwarpj.bUnwarpJ_" % \
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
27 ( jvm_memory_str, FIJI_JAR_DIR, FIJI_PLUGIN_DIR )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
28 return bunwarpj_base_cmd
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
29 return None
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
30
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
31 def get_base_command_imagej2( memory_size=None, macro=None, jython_script=None ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
32 imagej2_executable = get_imagej2_executable()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
33 if imagej2_executable is None:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
34 return None
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
35 cmd = '%s --ij2 --headless --debug' % imagej2_executable
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
36 if memory_size is not None:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
37 memory_size_cmd = ' -DXms=%s -DXmx=%s' % ( memory_size, memory_size )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
38 cmd += memory_size_cmd
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
39 if macro is not None:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
40 cmd += ' --macro %s' % os.path.abspath( macro )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
41 if jython_script is not None:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
42 cmd += ' --jython %s' % os.path.abspath( jython_script )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
43 return cmd
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
44
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
45 def get_file_extension( image_format ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
46 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
47 Return a valid bioformats file extension based on the received
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
48 value of image_format( e.g., "gif" is returned as ".gif".
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
49 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
50 return '.%s' % image_format
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
51
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
52 def get_file_name_without_extension( file_path ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
53 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
54 Eliminate the .ext from the received file name, assuming that
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
55 the file name consists of only a single '.'.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
56 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
57 if os.path.exists( file_path ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
58 path, name = os.path.split( file_path )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
59 name_items = name.split( '.' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
60 return name_items[ 0 ]
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
61 return None
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
62
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
63 def get_imagej2_executable():
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
64 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
65 Fiji names the ImageJ executable different names for different
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
66 architectures, so figure out which name we need.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
67 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
68 platform_dict = get_platform_info_dict()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
69 if platform_dict.get( 'architecture', None ) in [ 'x86_64' ]:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
70 if platform_dict.get( 'os', None ) in [ 'darwin' ]:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
71 return 'ImageJ-macosx'
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
72 if platform_dict.get( 'os', None ) in [ 'linux' ]:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
73 return 'ImageJ-linux64'
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
74 return None
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
75
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
76 def get_input_image_path( tmp_dir, input_file, image_format ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
77 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
78 Bioformats uses file extensions (e.g., .job, .gif, etc)
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
79 when reading and writing image files, so the Galaxy dataset
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
80 naming convention of setting all file extensions as .dat
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
81 must be handled.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
82 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
83 image_path = get_temporary_image_path( tmp_dir, image_format )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
84 # Remove the file so we can create a symlink.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
85 os.remove( image_path )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
86 os.symlink( input_file, image_path )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
87 return image_path
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
88
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
89 def get_platform_info_dict():
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
90 '''Return a dict with information about the current platform.'''
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
91 platform_dict = {}
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
92 sysname, nodename, release, version, machine = os.uname()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
93 platform_dict[ 'os' ] = sysname.lower()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
94 platform_dict[ 'architecture' ] = machine.lower()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
95 return platform_dict
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
96
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
97 def get_stderr_exception( tmp_err, tmp_stderr, tmp_out, tmp_stdout, include_stdout=False ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
98 tmp_stderr.close()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
99 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
100 Return a stderr string of reasonable size.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
101 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
102 # Get stderr, allowing for case where it's very large.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
103 tmp_stderr = open( tmp_err, 'rb' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
104 stderr_str = ''
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
105 buffsize = BUFF_SIZE
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
106 try:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
107 while True:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
108 stderr_str += tmp_stderr.read( buffsize )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
109 if not stderr_str or len( stderr_str ) % buffsize != 0:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
110 break
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
111 except OverflowError:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
112 pass
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
113 tmp_stderr.close()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
114 if include_stdout:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
115 tmp_stdout = open( tmp_out, 'rb' )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
116 stdout_str = ''
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
117 buffsize = BUFF_SIZE
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
118 try:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
119 while True:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
120 stdout_str += tmp_stdout.read( buffsize )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
121 if not stdout_str or len( stdout_str ) % buffsize != 0:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
122 break
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
123 except OverflowError:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
124 pass
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
125 tmp_stdout.close()
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
126 if include_stdout:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
127 return 'STDOUT\n%s\n\nSTDERR\n%s\n' % ( stdout_str, stderr_str )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
128 return stderr_str
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
129
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
130 def get_temp_dir( prefix='tmp-imagej-', dir=None ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
131 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
132 Return a temporary directory.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
133 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
134 return tempfile.mkdtemp( prefix=prefix, dir=dir )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
135
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
136 def get_tempfilename( dir=None, suffix=None ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
137 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
138 Return a temporary file name.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
139 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
140 fd, name = tempfile.mkstemp( suffix=suffix, dir=dir )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
141 os.close( fd )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
142 return name
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
143
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
144 def get_temporary_image_path( tmp_dir, image_format ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
145 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
146 Return the path to a temporary file with a valid image format
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
147 file extension that can be used with bioformats.
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
148 """
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
149 file_extension = get_file_extension( image_format )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
150 return get_tempfilename( tmp_dir, file_extension )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
151
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
152 def handle_none_type( val, val_type='float' ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
153 if val is None:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
154 return ' None'
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
155 else:
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
156 if val_type == 'float':
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
157 return ' %.1f' % val
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
158 elif val_type == 'int':
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
159 return ' %d' % val
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
160 return ' %s' % val
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
161
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
162 def stop_err( msg ):
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
163 sys.stderr.write( msg )
d065ec177dcd planemo upload commit 18df9e67efd4adafcde4eb9b62cd815e4afe9733-dirty
iuc
parents:
diff changeset
164 sys.exit( 1 )