mahautils.multics.VTKFile¶
- class mahautils.multics.VTKFile(path: str | Path | None = None, unit_converter: UnitConverter | None = None, **kwargs)¶
Bases:
BinaryFileAn object for representing VTK files
VTK files are commonly used with the Maha Multics software to store film property distributions and other simulation results. This class provides the ability to read and parse such files, and perform tasks such as extracting particular data, converting the units of data stored in the VTK file, and plotting scalar property distributions.
Attributes
The units in which the \(x\)-, \(y\)-, and \(z\)-coordinates of points in the VTK file are stored
The list of all VTK data identifiers for data stored in the file
The number of points in the VTK grid for which vector, scalar, and/or tensor data are stored
A Pandas DataFrame containing the coordinates of each point in the VTK file and any scalar or vector data for the point
Whether the
VTKFileinstance is capable of performing unit conversions on VTK dataThe unit converter used to perform unit conversions for quantities stored in the VTK file
Methods
__init__([path, unit_converter])Creates an object that can parse data from a VTK file
coordinates(axis[, unit])Returns a NumPy array containing the coordinates of all grid points in the VTK file along a particular coordinate axis
extract_data_series(identifier[, unit])Returns a NumPy array containing a single VTK data field
extract_dataframe(identifiers[, units])Returns a Pandas DataFrame containing one or more VTK data fields
interpolate(identifier, query_points, ...[, ...])Interpolates data from the VTK file
is_scalar(identifier)Whether a given VTK data identifier stores scalar point data
is_vector(identifier)Whether a given VTK data identifier stores vector point data
points([unit])Returns a list of all grid points in the VTK file
read([path, unit_conversion_enabled, ...])Reads a VTK file from the disk
Inherited Attributes
A copy of the dictionary containing any file hashes previously computed for the file specified by the
pathattributePath describing the location of the file on the disk
Inherited Methods
Clears any stored file hashes
compute_file_hashes([hash_functions, store])Computes hashes of the file specified by the
pathattributeReturns whether the file specified by the
pathattribute has changed since the last time file hashes were computedset_read_metadata([path])Configures metadata related to file to be read from disk
store_file_hashes([hash_functions])Computes and stores hashes of the file specified by the
pathattributetrack_new_file(path[, hash_functions])Shortcut for simultaneously modifying the
pathattribute and storing file hashes- __init__(path: str | Path | None = None, unit_converter: UnitConverter | None = None, **kwargs) None¶
Creates an object that can parse data from a VTK file
Creates an instance of the
VTKFileclass and optionally reads and parses a specified VTK file.- Parameters:
path (str or pathlib.Path, optional) – The path and filename of the VTK file to read and parse (default is
None). If set toNone, no VTK file is readunit_converter (pyxx.units.UnitConverter, optional) – A
pyxx.units.UnitConverterinstance which will be used to convert units of quantities stored in the VTK file (default isNone). If set toNone, theMahaMulticsUnitConverterunit converter will be used to perform unit conversions**kwargs – Any valid arguments (other than
path) for theread()method can be passed to this constructor as keyword arguments
- property coordinate_units: str | None¶
The units in which the \(x\)-, \(y\)-, and \(z\)-coordinates of points in the VTK file are stored
- property identifiers: List[str]¶
The list of all VTK data identifiers for data stored in the file
- property num_points: int¶
The number of points in the VTK grid for which vector, scalar, and/or tensor data are stored
- property pointdata_df: DataFrame¶
A Pandas DataFrame containing the coordinates of each point in the VTK file and any scalar or vector data for the point
This DataFrame stores the raw data parsed from the VTK file. The first three columns of the file store the \(x\)-, \(y\)-, and \(z\)-coordinates of point in the VTK grid, and subsequent columns store the raw scalar or vector data for each point. Data are stored in the same units defined in the VTK file.
- property unit_conversion_enabled: bool¶
Whether the
VTKFileinstance is capable of performing unit conversions on VTK dataVTK files don’t inherently store data. However, it can be useful to perform unit conversions and extract data from VTK files in different units than the data were stored. The
VTKFileprovides such unit conversion capability, but in order to do so, the user must appropriately name the data identifiers in the VTK file such that they include the unit in which the data are stored.The naming convention adopted in this package to facilitate unit conversions for VTK data requires that VTK data identifiers (for both scalar and vector data) are formatted in two parts: (1) a descriptive name, (2) the unit in square brackets. There should be no whitespace in any part of the identifier.
For instance, one potential identifier that could denote VTK data storing pressure in units of Pascal might be:
pressure[Pa]. Similarly, an identifier for VTK data storing the velocity of a tractor might betractorVelocity[m/s].
- property unit_converter: UnitConverter¶
The unit converter used to perform unit conversions for quantities stored in the VTK file
This attribute must be an instance or subclass of a
pyxx.units.UnitConverterobject. Unit conversions are only performed ifunit_conversion_enabledisTrue.
- coordinates(axis: str, unit: str | None = None) ndarray¶
Returns a NumPy array containing the coordinates of all grid points in the VTK file along a particular coordinate axis
VTK files store data (scalars, vectors, etc.) at a set of defined grid points in 3D. This method returns a 1D NumPy array containing the coordinates of the coordinates of such points along an axis specified by
axis. Point coordinates are returned in the order in which they were defined in the VTK file.- Parameters:
axis (str) – The coordinate axis for which to retrieve coordinates. Must be exactly one of:
'x','y','z'unit (str, optional) – The unit in which the VTK points should be returned (default is
None). Must be provided ifunit_conversion_enabledisTrueand omitted ifunit_conversion_enabledisFalse
- Returns:
A 1D NumPy array containing the coordinates of all VTK grid points along the axis specified by
axis- Return type:
np.ndarray
- Raises:
FileNotParsedError – If attempting to call this method before calling
read()to read and parse a VTK file
- extract_data_series(identifier: str, unit: str | None = None) ndarray¶
Returns a NumPy array containing a single VTK data field
VTK files store data (scalars, vectors, etc.) at a set of defined grid points in 3D. This method retrieves a single such field of data (i.e., it retrieves one column in
pointdata_df), and returns the resulting values in a NumPy array.- Parameters:
identifier (str) – The identifier specifying the data in the VTK file to return
unit (str, optional) – The units in which the data should be returned (only applicable if
unit_conversion_enabledisTrue; otherwise, must not be specified)
- Returns:
A NumPy array containing the data corresponding to
identifierin the VTK file- Return type:
np.ndarray
- extract_dataframe(identifiers: List[str], units: List[str] | None = None) DataFrame¶
Returns a Pandas DataFrame containing one or more VTK data fields
VTK files store data (scalars, vectors, etc.) at a set of defined grid points in 3D. This method retrieves one or more such fields of data (i.e., it retrieves one or more columns in
pointdata_df), and returns the resulting values in a Pandas DataFrame.- Parameters:
identifiers (list of str) – The (one or more) identifiers specifying the data in the VTK file to return
units (list of str, optional) – The units in which the data should be returned (only applicable if
unit_conversion_enabledisTrue; otherwise, must not be specified). If supplied,unitsshould be a list of strings of equal length asidentifiers
- Returns:
A Pandas DataFrame containing the columns of
pointdata_dfcorresponding toidentifiersin the VTK file- Return type:
pd.DataFrame
- interpolate(identifier: str, query_points: List[List[float] | Tuple[float, ...] | ndarray] | Tuple[List[float] | Tuple[float, ...] | ndarray, ...] | ndarray, interpolator_type: str, output_units: str | None = None, query_point_units: str | None = None, interpolate_axes: List[str] | Tuple[str, ...] | str = ('x', 'y', 'z'), idx_slice: slice | tuple = slice(None, None, None), **kwargs) ndarray¶
Interpolates data from the VTK file
Retrieves the value of a given data field stored in the VTK file, interpolating between VTK grid points if necessary. Interpolation is performed using the
scipy.interpolatepackage (https://docs.scipy.org/doc/scipy/reference/interpolate.html).- Parameters:
identifier (str) – The identifier specifying the data in the VTK file to return
query_points (tuple or list or np.ndarray) – The point(s) at which to return possibly interpolated value(s) of the VTK data corresponding to
identifierinterpolator_type (str) – The SciPy interpolation function to use to perform interpolation. Can be selected from any of the options in the “Notes” section
output_units (str, optional) – The units in which the data should be returned (only applicable if
unit_conversion_enabledisTrue; otherwise, must not be specified)query_point_units (str, optional) – The units of the
query_pointsargument (only applicable ifunit_conversion_enabledisTrue; otherwise, must not be specified)interpolate_axes (list or tuple or set or str, optional) – The coordinate axes on which interpolation should be performed. Must be selected from any combination of
'x','y', and'z'(default is('x', 'y', 'z'))idx_slice (slice or tuple, optional) – Filters the points in the VTK file and only uses a subset of points for interpolation (default is
slice(None)which uses all points in the VTK file). See the “Notes” section for more information**kwargs – Any keyword arguments to be supplied to the SciPy interpolation function specified by
interpolator_type. See the “Notes” section for more information
- Returns:
The interpolated value(s) of the VTK data given by
identifierat the query pointsquery_points- Return type:
np.ndarray
Notes
Interpolation Functions
The following interpolation functions are available (set the
interpolator_typeargument to the given string to use each):'griddata': Thescipy.interpolate.griddatainterpolation function for unstructured, multivariate interpolation (reference)'RBFInterpolator': Thescipy.interpolate.RBFInterpolatorfunction for unstructured, multivariate interpolation using a radial basis function (reference)
Review the SciPy documentation for questions about parameters for the interpolation functions. These interpolation functions require data and interpolation parameters to meet specific mathematical requirements, and errors may be encountered if such requirements are not met. This may require using non-default parameters of the
interpolate()method.For instance, if your VTK file is defined on a 2D
xy-plane and you attempt to perform 3D interpolation (i.e., you setinterpolation_axesto('x', 'y', 'z')) usinggriddatawithmethod='linear', an error will be thrown. In this case, you need to reduce the problem to a 2D interpolation by settinginterpolation_axesto('x', 'y').Point Coordinates
The order in which
interpolate_axesvalues are provided must be in the following sequence:(x, y, z). An error will be thrown if an argument such asinterpolate_axes=('y', 'x', 'z')is provided.Filtering/Slicing Points
In some cases, it may be desirable to perform interpolation using only a subset of points in the VTK file. For instance, if a lubricating film lies on a specific face, it may be desirable to only perform interpolation using that face.
The
idx_sliceargument facilitates this use case. Either a Pythonsliceobject can be passed as input, or a tuple of array indices generated by NumPy’sindex_exp()ornp.s_()methods (more information).Note that if using
index_exp()ornp.s_(), only a 1D index tuple should be generated (e.g.,np.index_exp[0:4]). An easy way to test the index tuple is to apply it to the output ofpoints()(e.g.,vtk_file.points()[np.index_exp[...]]) and observe whether the desired points are extracted (these are the points that would be used for interpolation).
- is_scalar(identifier: str) bool¶
Whether a given VTK data identifier stores scalar point data
- Parameters:
identifier (str) – The VTK data identifier to analyze
- Returns:
Returns
Trueif the VTK data identifier given byidentifierstores scalar data, andFalseotherwise- Return type:
bool
- clear_file_hashes() None¶
Clears any stored file hashes
- compute_file_hashes(hash_functions: tuple | str = ('md5', 'sha256'), store: bool = False) Dict[str, str]¶
Computes hashes of the file specified by the
pathattributeComputes and returns the hashes of the file specified by the
pathattribute, with the option to populate thehashesdictionary with their values.- Parameters:
hash_functions (tuple or str, optional) – Tuple of strings (or individual string) specifying which hash(es) to compute. Any hash functions supported by
hashlibcan be used. Default is('md5', 'sha256')store (bool, optional) – Whether to store the computed hashes in the
hashesdictionary (default isFalse)
- Returns:
A dictionary containing the file hashes specified by
hash_functions- Return type:
dict
See also
pyxx.files.compute_file_hashFunction used to compute file hashes
Notes
Prior to calling this method, the
pathattribute must be defined. To simultaneously set thepathattribute and store file hashes, usetrack_new_file().
- has_changed() bool¶
Returns whether the file specified by the
pathattribute has changed since the last time file hashes were computed- Returns:
Whether file has changed since the last time file hashes were computed
- Return type:
bool
- property hashes: Dict[str, str]¶
A copy of the dictionary containing any file hashes previously computed for the file specified by the
pathattribute
- is_vector(identifier: str) bool¶
Whether a given VTK data identifier stores vector point data
- Parameters:
identifier (str) – The VTK data identifier to analyze
- Returns:
Returns
Trueif the VTK data identifier given byidentifierstores vector data, andFalseotherwise- Return type:
bool
- property path: Path | None¶
Path describing the location of the file on the disk
Assigning a value to this attribute (regardless whether it matches the current value or is a different path) will save the value as a
pathlib.Pathand will automatically clear any saved file hashes.
- set_read_metadata(path: str | Path | None = None) None¶
Configures metadata related to file to be read from disk
This method performs several pre-processing steps to prepare to read a file from the disk:
Sets the
pathattribute. If thepathargument was provided, the attribute is set to this value; otherwise, the existing value stored in thepathattribute is used (or an error is thrown if not defined).Verifies that the file specified by the
pathattribute exists.Stores the hashes for the file.
It is advised that this method be called prior to reading any file.
- Parameters:
path (str or pathlib.Path, optional) – Location of the file in the file system (default is
None)- Raises:
- store_file_hashes(hash_functions: tuple | str = ('md5', 'sha256')) None¶
Computes and stores hashes of the file specified by the
pathattributeComputes given hashes of the file specified by the
pathattribute and populates thehashesdictionary with their values.- Parameters:
hash_functions (tuple or str, optional) – Tuple of strings (or individual string) specifying which hash(es) to compute. Any hash functions supported by
hashlibcan be used. Default is('md5', 'sha256')
See also
pyxx.files.compute_file_hashFunction used to compute file hashes
track_new_fileUse this method if you want to store file hashes but the
pathattribute isn’t yet defined
Notes
Prior to calling this method, the
pathattribute must be defined. To simultaneously set thepathattribute and store file hashes, usetrack_new_file().
- track_new_file(path: str | Path, hash_functions: tuple | str = ('md5', 'sha256')) None¶
Shortcut for simultaneously modifying the
pathattribute and storing file hashesThis method functions as a “shortcut,” both modifying the
pathattribute and storing an optionally user-specified list of file hashes in thehashesattribute. The intention of this method is that if aFileinstance is tracking a given file, and user wants to switch to tracking another file, this provides a convenient way to do so with a single line of code.- Parameters:
file (str or pathlib.Path) – File that the object is to represent
hash_functions (tuple or str, optional) – Tuple of strings (or individual string) specifying which hash(es) to compute. Any hash functions supported by
hashlibcan be used. Default is('md5', 'sha256')
See also
pyxx.files.compute_file_hashFunction used to compute file hashes
- points(unit: str | None = None) ndarray¶
Returns a list of all grid points in the VTK file
VTK files store data (scalars, vectors, etc.) at a set of defined grid points in 3D. This method returns a list containing the coordinates of all such points. Refer to the “Notes” section for details about the format of the returned points.
- Parameters:
unit (str, optional) – The unit in which the VTK points should be returned (default is
None). Must be provided ifunit_conversion_enabledisTrueand omitted ifunit_conversion_enabledisFalse- Returns:
A NumPy array containing a list of all VTK grid points. Refer to the “Notes” section for details about the format of the array
- Return type:
np.ndarray
- Raises:
FileNotParsedError – If attempting to call this method before calling
read()to read and parse a VTK file
Notes
The VTK grid points are returned as a 2D array, where the first index specifies a particular point (out of the
num_pointspoints) and the second index specifies the coordinate axis (\(x\), \(y\), or \(z\)).For example, suppose that the VTK file stored data for five points:
(x1, y1, z1),(x2, y2, z2), …,(x5, y5, z5). In this case, thepoints()method would return:array([[x1, y1, z1], [x2, y2, z2], [x3, y3, z3], [x4, y4, z4], [x5, y5, z5]])
Point coordinates are returned in the order in which they were defined in the VTK file.
- read(path: str | Path | None = None, unit_conversion_enabled: bool = False, coordinate_units: str | None = None, strict: bool = False, fallback_units: Dict[str, str] | None = None) None¶
Reads a VTK file from the disk
This method reads a VTK file from the disk, parsing its content and storing the data as a Pandas DataFrame in the
pointdata_dfattribute.- Parameters:
path (str or pathlib.Path, optional) – The path and filename from which to read the VTK file (default is
None). If not provided orNone, the file will be read from the location specified by thepathattributeunit_conversion_enabled (bool, optional) – Whether to enable unit conversions for the VTK file data (see the
unit_conversion_enabledattribute for additional details) (default isFalse)coordinate_units (str, optional) – The units used by the coordinate system in the VTK file (default is
None). Must be provided ifunit_conversion_enabledisTrueand omitted ifunit_conversion_enabledisFalsestrict (bool, optional) – Whether to throw an exception if the data in the VTK file being read are not formatted in a valid way
fallback_units (dict, optional) – This setting allows VTK files where some data are missing units in the identifier to still be read with unit conversions enabled. Must be a dictionary where keys and values are both strings. If
unit_conversion_enabledisTrueand a VTK data identifier does not include the data units, then if there is a key infallback_unitsmatching the identifier, the corresponding value infallback_unitswill be set as the units
Warning
If there are two point data fields in the VTK file with exactly the same identifier, only one of the fields (the last one with the identifier) will be read.
Setting
stricttoTruemodifies thestderrfile descriptor, including redirectingstderrto a temporary file, so it can cause problems for other code that relies on streams (for instance, it may causeunittesttests to fail in some cases).