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