annotate icqsol_utils.py @ 0:f63d4da328a3 draft default tip

Uploaded
author iuc
date Tue, 23 Aug 2016 15:02:48 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
1 import os
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
2 import sys
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
3 import tempfile
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
4
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
5 from icqsol.shapes.icqShapeManager import ShapeManager
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
6 from icqsol.bem.icqLaplaceSolver import LaplaceSolver
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
7
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
8 PLY = 'ply'
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
9 POLYDATA = 'POLYDATA'
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
10 VTK = 'vtk'
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
11
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
12
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
13 def asbool(val):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
14 return str(val).lower() in ['yes', 'true']
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
15
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
16
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
17 def get_format_and_type(galaxy_ext):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
18 # Define the output file format and type.
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
19 format = None
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
20 datatype = None
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
21 if galaxy_ext in ['vtkascii', 'vtkbinary']:
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
22 format = VTK
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
23 elif galaxy_ext in ['plyascii', 'plybinary']:
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
24 format = PLY
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
25 if galaxy_ext in ['vtkascii', 'plyascii']:
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
26 datatype = 'ascii'
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
27 elif galaxy_ext in ['vtkbinary', 'plybinary']:
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
28 datatype = 'binary'
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
29 return format, datatype
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
30
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
31
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
32 def get_input_file_path(tmp_dir, input_file, format):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
33 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
34 iCqSol uses file extensions (e.g., .ply, .vtk) when reading and
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
35 writing files, so the Galaxy dataset naming convention of
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
36 setting all file extensions as .dat must be handled.
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
37 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
38 file_path = get_temporary_file_path(tmp_dir, format)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
39 # Remove the file so we can create a symlink.
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
40 os.remove(file_path)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
41 os.symlink(input_file, file_path)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
42 return file_path
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
43
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
44
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
45 def get_laplace_solver(shape_data, max_edge_length=float('inf')):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
46 return LaplaceSolver(shape_data, max_edge_length=max_edge_length)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
47
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
48
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
49 def get_shape_manager(format=None, dataset_type=None):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
50 # Instantiate a ShapeManager.
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
51 return ShapeManager(file_format=format, vtk_dataset_type=dataset_type)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
52
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
53
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
54 def get_temp_dir(prefix='tmp-vtk-', dir=None):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
55 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
56 Return a temporary directory.
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
57 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
58 return tempfile.mkdtemp(prefix=prefix, dir=dir)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
59
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
60
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
61 def get_tempfilename(dir=None, suffix=None):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
62 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
63 Return a temporary file name.
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
64 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
65 if suffix is None:
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
66 s = None
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
67 elif suffix.startswith('.'):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
68 s = suffix
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
69 else:
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
70 s = '.%s' % suffix
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
71 fd, name = tempfile.mkstemp(suffix=s, dir=dir)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
72 os.close(fd)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
73 return name
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
74
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
75
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
76 def get_temporary_file_path(tmp_dir, file_extension):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
77 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
78 Return the path to a temporary file with a valid VTK format
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
79 file extension.
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
80 """
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
81 return get_tempfilename(tmp_dir, file_extension)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
82
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
83
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
84 def stop_err(msg):
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
85 sys.stderr.write("%s\n" % msg)
f63d4da328a3 Uploaded
iuc
parents:
diff changeset
86 sys.exit()