Mercurial > repos > greg > icqsol_add_surface_field_from_expression
changeset 0:d59a9d960f67 draft
Uploaded
author | greg |
---|---|
date | Sat, 14 Nov 2015 14:14:45 -0500 |
parents | |
children | f239d7513008 |
files | add_surface_field_from_expression.py icqsol_add_surface_field_from_expression.py icqsol_add_surface_field_from_expression.xml icqsol_macros.xml icqsol_utils.py test-data/box.vtkascii test-data/box_with_surface_field.vtkascii tool_dependencies.xml |
diffstat | 8 files changed, 444 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/add_surface_field_from_expression.py Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,53 @@ +#!/usr/bin/env python +import argparse +import shutil + +import icqsol_utils +from icqsol.shapes.icqShapeManager import ShapeManager + +# Parse Command Line. +parser = argparse.ArgumentParser() +parser.add_argument( '--input', dest='input', help='Shape dataset selected from history' ) +parser.add_argument( '--field_name', dest='field_name', help='Surface field name' ) +parser.add_argument( '--expression', dest='expression', help='Expression for applying surface field to shape' ) +parser.add_argument( '--time_point', dest='time_points', type=float, action='append', nargs=1, help='Points in time' ) +parser.add_argument( '--output', dest='output', help='Output dataset' ) +parser.add_argument( '--output_format_and_type', dest='output_format_and_type', help='Output file format and type' ) + +args = parser.parse_args() + +valid_field_name = args.field_name.replace( ' ', '_' ) +tmp_dir = icqsol_utils.get_temp_dir() +shape_mgr = ShapeManager() + +# Get the shape from the input dataset. +shape = shape_mgr.load(args.input) +# Get the points from the shape. +pdata = shape_mgr.shapeToVTKPolyData(shape) +points = pdata.GetPoints() +num_points = points.GetNumberOfPoints() +# Define the data. +data = vtk.vtkDoubleArray() +data.SetName(valid_field_name) +# Handle time points. +time_points = [ '%.1f' % tp for tp in args.time_points ] +num_time_points = len(time_points) +# Update the data. +data.SetNumberOfComponents(num_time_points) +data.SetNumberOfTuples(num_points) +# Add the surface field. +for i in range(num_points): + x, y, z = points.GetPoint(i) + for j in range(num_time_points): + t = time_points[ j ] + field_value = eval(args.expression) + data.SetComponent(i, j, field_value) +pdata.GetPointData().SetScalars(data) + +# Define the output file format and type. +output_format, output_file_type = icqsol_utils.get_format_and_type( args.output_format_and_type ) +tmp_output_path = icqsol_utils.get_temporary_file_path( tmp_dir, output_format ) + +# Save the output. +shape_mgr.save( composite_shape, file_name=tmp_output_path, file_format=output_format, file_type=output_file_type ) +shutil.move( tmp_output_path, args.output )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_add_surface_field_from_expression.py Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,57 @@ +#!/usr/bin/env python +import argparse +import shutil + +import icqsol_utils +from icqsol.shapes.icqShapeManager import ShapeManager + +# Parse Command Line. +parser = argparse.ArgumentParser() +parser.add_argument( '--input', dest='input', help='Shape dataset selected from history' ) +parser.add_argument( '--input_file_format_and_type', dest='input_file_format_and_type', help='Input file format and type' ) +parser.add_argument( '--input_dataset_type', dest='input_dataset_type', help='Input dataset_type' ) +parser.add_argument( '--field_name', dest='field_name', help='Surface field name' ) +parser.add_argument( '--location', dest='location', help='Location of field within cell, either point or cell' ) +parser.add_argument( '--expression', dest='expression', help='Expression for applying surface field to shape' ) +parser.add_argument( '--time_point', dest='time_points', type=float, action='append', nargs=1, help='Points in time' ) +parser.add_argument( '--max_edge_length', dest='max_edge_length', type=float, default='0', help='Maximum edge length' ) +parser.add_argument( '--output', dest='output', help='Output dataset' ) +parser.add_argument( '--output_vtk_type', dest='output_vtk_type', help='Output VTK type' ) + +args = parser.parse_args() + +input_format, input_file_type = icqsol_utils.get_format_and_type( args.input_file_format_and_type ) +time_points = [ tp[0] for tp in args.time_points ] +tmp_dir = icqsol_utils.get_temp_dir() + +# Instantiate a ShapeManager for loading the input. +if input_format == 'vtk': + shape_mgr = ShapeManager( file_format=input_format, vtk_dataset_type=args.input_dataset_type ) +else: + shape_mgr = ShapeManager( file_format=input_format ) + +# Get the shape from the input dataset. +shp = shape_mgr.loadAsShape(args.input) + +max_edge_length = float('inf') +if args.max_edge_length > 0: + max_edge_length = args.max_edge_length + +# Add surface field to shape data. +vtk_poly_data = shape_mgr.addSurfaceFieldFromExpressionToShape(shp, + args.field_name, + args.expression, + time_points, + max_edge_length, + args.location) + +# Define the output file format and type (the outpur_format can only be 'vtk'). +output_format, output_file_type = icqsol_utils.get_format_and_type( args.output_vtk_type ) +tmp_output_path = icqsol_utils.get_temporary_file_path( tmp_dir, output_format ) + +# Make sure the ShapeManager's writer is vtk. +shape_mgr.setWriter( file_format='vtk', vtk_dataset_type=icqsol_utils.POLYDATA ) + +# Save the output. +shape_mgr.saveVtkPolyData( vtk_poly_data=vtk_poly_data, file_name=tmp_output_path, file_type=output_file_type) +shutil.move( tmp_output_path, args.output )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_add_surface_field_from_expression.xml Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,94 @@ +<?xml version='1.0' encoding='UTF-8'?> +<tool id="icqsol_add_surface_field_from_expression" name="Add surface field" version="1.0.0"> + <description>from expression</description> + <macros> + <import>icqsol_macros.xml</import> + </macros> + <expand macro="requirements" /> + <command> +<![CDATA[ + python $__tool_directory__/icqsol_add_surface_field_from_expression.py + --input "$input" + --input_file_format_and_type $input.ext + --input_dataset_type $input.metadata.dataset_type + --field_name "$field_name" + --location $location + --expression "$expression" + #for $tp in $time_points: + --time_point ${tp.time_point} + #end for + #if $refine_shape_cond.refine_shape == "yes": + --max_edge_length $refine_shape_cond.max_edge_length + #end if + --output "$output" + --output_vtk_type $output_vtk_type +]]> + </command> + <inputs> + <param name="input" type="data" format="plyascii,plybinary,vtkascii,vtkbinary" label="Shape"> + <validator type="dataset_ok_validator" /> + </param> + <param name="field_name" type="text" value="surface_field" label="Surface field name" help="Spaces will be converted to underscore character."/> + <param name="location" type="select" label="Location of field" help="Field values vary linearly between nodes when visualizing fields located at points, while each triangle is assigned a single value with fields located at cells."> + <option value="cell" selected="True">Cell</option> + <option value="point">Point</option> + </param> + <param name="expression" type="text" value="(x**2 * sin(y*pi) + z**4) * exp(t)" label="Expression" help="Legal variables are x,y,z (point coordinates) and t (time)."/> + <repeat name="time_points" title="Points in time" min="1" help="Provides snapshots of surface field over time sequence."> + <param name="time_point" type="float" value="0" label="Time point" help="Floating point number"/> + </repeat> + <conditional name="refine_shape_cond"> + <param name="refine_shape" type="select" label="Refine shape?" help="Points will be added to the shape's edges restricting their length to the maximum."> + <option value="no" selected="True">No</option> + <option value="yes">Yes</option> + </param> + <when value="no" /> + <when value="yes"> + <param name="max_edge_length" type="float" value="0" label="Maximum edge length" help="Floating point number, 0 results in no refinement."/> + </when> + </conditional> + <expand macro="output_vtk_type_params" /> + </inputs> + <outputs> + <data name="output" format="vtkascii" label="${tool.name} ${on_string}"> + <actions> + <action type="format"> + <option type="from_param" name="output_vtk_type" /> + </action> + </actions> + </data> + </outputs> + <tests> + <test> + <param name="input" value="box.vtkascii" ftype="vtkascii" /> + <param name="input_file_format_and_type" value="vtkascii" /> + <param name="input_dataset_type" value="POLYDATA" /> + <param name="field_name" value="surface_field" /> + <param name="expression" value="(x**2 * sin(y*pi) + z**4) * exp(t)" /> + <repeat name="time_points"> + <param name="time_point" value="0.1" /> + </repeat> + <output name="output" file="box_with_surface_field.vtkascii" ftype="vtkascii" /> + <param name="output_vtk_type" value="vtkascii" /> + </test> + </tests> + <help> +**What it does** + +<![CDATA[ + +Adds a surface field to a selected shape based on a given mathematical expression consisting of +variables x, y, z (shape point coordinates) and t (time). This tool will generate VTK POLYDATA +files, so input PLY files or VTK files with a different dataset type will automatically be converted +to VTK POLYDATA during tool execution. + +* **Shape** - Shape to which a surface field will be added. +* **Surface field name** - Name of the surface field to be added to the shape. +* **Expression** - Mathematical expression consisting of variables x, y, z (shape point coordinates) and t (time) that defines the surface field. +* **Time point** - A floating point value that defines a time point where multiple time points define a sequence of time snapshots for the surface field. +* **Refine shape** - Select yes to refine the shape by shortening the edge lengths to a defined maximum. +* **Maximum edge length** - Points are added to the shape's edges that are longer than the defined maximum length. +]]> + </help> + <expand macro="citations" /> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_macros.xml Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,115 @@ +<?xml version='1.0' encoding='UTF-8'?> +<macros> + <xml name="requirements"> + <requirements> + <requirement type="package" version="1.0">icqsol</requirement> + </requirements> + </xml> + <xml name="stdio"> + <stdio> + <exit_code range="1:"/> + <exit_code range=":-1"/> + <regex match="Error:"/> + <regex match="Exception:"/> + </stdio> + </xml> + <!-- TODO: place this in colormaps_conf.xml --> + <xml name="color_map_param"> + <param name="color_map" type="select" label="Color map"> + <option value="hot" selected="True">Hot</option> + <option value="cold">Cold</option> + <option value="blackbody">Blackbody</option> + <option value="gnu">Gnu</option> + </param> + </xml> + <xml name="output_vtk_type_params"> + <param name="output_vtk_type" type="select" label="Output file type"> + <option value="vtkascii" selected="True">Vtk-ascii</option> + <option value="vtkbinary">Vtk-binary</option> + </param> + </xml> + <token name="@origin_cmd_args@"> + --origin_x $create_process_cond.shape_cond.origin_x + --origin_y $create_process_cond.shape_cond.origin_y + --origin_z $create_process_cond.shape_cond.origin_z + </token> + <xml name="origin_params"> + <param name="origin_x" type="float" value="0.0" label="X coordinate of origin" help="Floating point number"/> + <param name="origin_y" type="float" value="0.0" label="Y coordinate of origin" help="Floating point number"/> + <param name="origin_z" type="float" value="0.0" label="Z coordinate of origin" help="Floating point number"/> + </xml> + <token name="@lengths_cmd_args@"> + --length_x $create_process_cond.shape_cond.length_x + --length_y $create_process_cond.shape_cond.length_y + --length_z $create_process_cond.shape_cond.length_z + </token> + <xml name="lengths_params"> + <!-- At least one of these lengths must be greater than zero, but we have no validator for this. --> + <param name="length_x" type="float" value="1.0" label="Length in the X direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="false" /> + </param> + <param name="length_y" type="float" value="0.0" label="Length in the Y direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="false" /> + </param> + <param name="length_z" type="float" value="0.0" label="Length in the Z direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="false" /> + </param> + </xml> + <xml name="lengths_exclude_min_params"> + <param name="length_x" type="float" value="1.0" label="Length in the X direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="true" /> + </param> + <param name="length_y" type="float" value="1.0" label="Length in the Y direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="true" /> + </param> + <param name="length_z" type="float" value="1.0" label="Length in the Z direction" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="true" /> + </param> + </xml> + <token name="@radius_cmd_args@"> + --radius $create_process_cond.shape_cond.radius + </token> + <xml name="radius_params"> + <param name="radius" type="float" value="1.0" label="Radius" help="Floating point number"> + <validator type="in_range" min="0" exclude_min="True" /> + </param> + </xml> + <token name="@n_theta_cmd_args@"> + --n_theta $create_process_cond.shape_cond.n_theta + </token> + <xml name="n_theta_params"> + <param name="n_theta" type="integer" value="16" label="Number of slices" help="Controls the tessellation along the longitude direction"> + <validator type="in_range" min="0" exclude_min="False" /> + </param> + </xml> + <token name="@n_phi_cmd_args@"> + --n_phi $create_process_cond.shape_cond.n_phi + </token> + <xml name="n_phi_params"> + <param name="n_phi" type="integer" value="8" label="Number of stacks" help="Controls the tessellation along the latitude direction"> + <validator type="in_range" min="0" exclude_min="False" /> + </param> + </xml> + <xml name="citations"> + <citations> + <citation type="bibtex"> + @unpublished{None, + author = {None}, + title = {None}, + year = {None}, + eprint = {None}, + url = {https://github.com/gregvonkuster/galaxy-csg} + }</citation> + <citation type="bibtex"> + @misc(Schroeder-Martin-Lorensen2006, + author = "Will Schroeder and + Ken Martin and + Bill Lorensen", + year = "2006", + title = "The Visualization Toolkit (4th ed.)", + publisher = "Kitware", + url = "https://en.wikipedia.org/wiki/Special:BookSources/978-1-930934-19-1") + </citation> + </citations> + </xml> +</macros>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icqsol_utils.py Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,59 @@ +import os +import sys +import tempfile + +POLYDATA='POLYDATA' + +def asbool( val ): + return str( val ).lower() in [ 'yes', 'true' ] + +def get_format_and_type( galaxy_ext ): + # Define the output file format and type. + format = None + datatype = None + if galaxy_ext in [ 'vtkascii', 'vtkbinary' ]: + format = 'vtk' + elif galaxy_ext in [ 'plyascii', 'plybinary' ]: + format = 'ply' + if galaxy_ext in [ 'vtkascii', 'plyascii' ]: + datatype = 'ascii' + elif galaxy_ext in [ 'vtkbinary', 'plybinary' ]: + datatype = 'binary' + return format, datatype + +def get_input_file_path( tmp_dir, input_file, format ): + """ + iCqSol uses file extensions (e.g., .ply, .vtk) when reading and + writing files, so the Galaxy dataset naming convention of + setting all file extensions as .dat must be handled. + """ + file_path = get_temporary_file_path( tmp_dir, format ) + # Remove the file so we can create a symlink. + os.remove( file_path ) + os.symlink( input_file, file_path ) + return file_path + +def get_temp_dir( prefix='tmp-vtk-', dir=None ): + """ + Return a temporary directory. + """ + return tempfile.mkdtemp( prefix=prefix, dir=dir ) + +def get_tempfilename( dir=None, suffix=None ): + """ + Return a temporary file name. + """ + fd, name = tempfile.mkstemp( suffix=suffix, dir=dir ) + os.close( fd ) + return name + +def get_temporary_file_path( tmp_dir, file_extension ): + """ + Return the path to a temporary file with a valid VTK format + file extension. + """ + return get_tempfilename( tmp_dir, file_extension ) + +def stop_err( msg ): + sys.stderr.write( "%s\n" % msg ) + sys.exit()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/box.vtkascii Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,16 @@ +# vtk DataFile Version 3.0 +vtk output +ASCII +DATASET POLYDATA +POINTS 8 float +0 0 0 0 0 1 0 1 1 +0 1 0 1 0 0 1 1 0 +1 1 1 1 0 1 +POLYGONS 6 30 +4 0 1 2 3 +4 4 5 6 7 +4 0 4 7 1 +4 3 2 6 5 +4 0 3 5 4 +4 1 7 6 2 +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/box_with_surface_field.vtkascii Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,44 @@ +# vtk DataFile Version 3.0 +vtk output +ASCII +DATASET POLYDATA +POINTS 24 float +0 0 0 0 0 1 0 1 1 +0 1 0 1 0 0 1 1 0 +1 1 1 1 0 1 0 0 0 +0 0 0 0 0 1 0 0 1 +0 1 1 0 1 1 0 1 0 +0 1 0 1 0 0 1 0 0 +1 1 0 1 1 0 1 1 1 +1 1 1 1 0 1 1 0 1 + +POLYGONS 12 48 +3 3 0 1 +3 1 2 3 +3 7 4 5 +3 5 6 7 +3 10 8 16 +3 16 22 10 +3 18 14 12 +3 12 20 18 +3 17 9 15 +3 15 19 17 +3 13 11 23 +3 23 21 13 + +POINT_DATA 24 +SCALARS surface_field double +LOOKUP_TABLE default +0 1.10517 1.10517 0 0 1.35344e-16 1.10517 1.10517 0 +0 1.10517 1.10517 1.10517 1.10517 0 0 0 0 +1.35344e-16 1.35344e-16 1.10517 1.10517 1.10517 1.10517 +NORMALS Normals float +-1 0 0 -1 0 0 -1 0 0 +-1 0 0 1 0 0 1 0 0 +1 0 0 1 0 0 0 -1 0 +0 0 -1 0 -1 0 0 0 1 +0 1 0 0 0 1 0 1 0 +0 0 -1 0 -1 0 0 0 -1 +0 1 0 0 0 -1 0 1 0 +0 0 1 0 -1 0 0 0 1 +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Sat Nov 14 14:14:45 2015 -0500 @@ -0,0 +1,6 @@ +<?xml version="1.0"?> +<tool_dependency> + <package name="icqsol" version="1.0"> + <repository changeset_revision="a357536fb363" name="package_icqsol_1_0" owner="greg" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> +</tool_dependency>