carto_mesh module¶
This file contains the class CartoMesh with all its reconstruction-specific methods.
This class contains methods for initialising from a .mesh file, plotting, reconstructing and applying conduction velocities.
This file imports mesh_tools for all general mesh functions that are not specific to carto meshes.
mesh_tools also concerns itself with all python module imports.
- class carto_mesh.CartoMesh(name: str = '')[source]¶
Bases:
objectA class containing functions for initialising from file, plotting, reconstructing and applying conduction velocities.
- __cartoToCsv(verbose: bool = True) None¶
Reads in a carto .mesh file and generates .csv files per header in real-time in the same directory
- Args:
verbose: Use verbose output. Default=True
- Returns:
None: Nothing. Writes out .csv files.
- __getNeighboringFaces(index: int) pandas.core.frame.DataFrame¶
Finds all mesh facets containing some point.
- Args:
index: the index referring to the mesh point.
- Returns:
DataFrame: Pandas DataFrame containing the triangles that have the point at index. The DataFrame contains the following columns: ‘Vertex0’, ‘Vertex1’, ‘Vertex2’, ‘NormalX’, ‘NormalY’, ‘NormalZ’, ‘GroupID’
- __initialiseFromFile(name: str = '') None¶
Initialise the class from either a .mesh file or a .vtk file.
- Args:
name: The filename (.mesh or .vtk), or the directory containing the .mesh file.
- Returns:
None: Nothing
- __readSettings() None¶
Reads the settings.ini file and updates the CartoMesh accordingly.
- Returns:
None: Nothing. Updates the mesh settings attribute.
- __readVertTri() None¶
Reads VerticesSection.csv and TrianglesSection.csv files and initializes class
- Returns:
None: Nothing. Updates mesh.
- __update(mesh_: Union[pyvista.core.pointset.PolyData, pyvista.core.pointset.UnstructuredGrid]) None¶
Updates the CartoMesh to match the given mesh.
- Args:
mesh_: A PyVista mesh of the type PolyData or UnstructuredGrid .
- Returns:
None: Nothing. Updates the mesh.
- applyCV(speed_file='speed.csv', write_csv=False, write_VTK_file=False, write_txt=True, write_dat=False, write_xyz=False, outdir='scale_factors/', region_dir='', ncv=None, speed_col='speed')[source]¶
Applies conduction velocities on a reconstructed tetrahedralised carto mesh.
- Args:
speed_file: Name of the file containing the coordinates and conduction velocity values to interpolate.
write_csv: Write out the coordinates and speeds to a .csv file
write_VTK_file: Write out the resulting mesh to .vtk
write_txt: Write out the conduction velocities as scale factors (readable by OpenCARP) to <outdir>
write_dat: Write out the regions of the mesh with CV=0 to .dat file, to visualize in Meshalyzer write_xyz: Write point cloud of the conduction velocites, as interpolated on the mesh outdir: Name of the directory to contain the scale factors aka conduction velocities region_dir: Name of the directory containing .csv files with indices of mesh points whose conduction velocity will be set to 0.
- Returns:
None: Nothing. Writes out one or more of the following files in <outdir>: .csv, .vtk, .txt, .dat or a pointcloud .csv file if <write_xyz> == True.
- cleanMesh(tol: float, max_iter: int = 10, print_si: bool = True) pyvista.core.pointset.PolyData[source]¶
Calls upon PyVista’s built-in clean method to clean the mesh. Afterwards, calls upon PyMesh’s built-in methods remove_degenerated_triangles() and detect_self_intersection() to remove duplicated faces and triangles, and resolve self-intersections.
- Args:
tol: tolerance passed to PyVista’s built-in clean() method (absolute tolerance)
max_iter: max amount of iterations PyMesh is allowed to try and fix self-intersections, duplicate faces and duplicate vertices.
print_si: print the progress of fixing self-intersections
- Returns:
PyVista PolyData: The cleaned mesh
- extractMyoAndNonMyo() Tuple[pyvista.core.pointset.PolyData, pyvista.core.pointset.PolyData][source]¶
Checks scalar data of the mesh in its current state (clinical tags) and extracts the part of the mesh with tags that correspond to myocardium (tag == 0).
- Returns:
Tuple[pv.PolyData, pv.PolyData]: A tuple containing two PyVista PolyData meshes: a pointcloud of points corresponding to the mesh myocardium and a pointcloud of the rest of the mesh.
- getEdgeLengths(mesh: Optional[Union[pymesh.Mesh.Mesh, bool]] = None) numpy.ndarray[source]¶
Gets all edge lengths. If a mesh is given, the mesh must be a PyMesh Mesh object. If no mesh is given (default), the CartoMesh’s mesh attribute is used (PyVista PolyData or UnstructuredGrid)
- Args:
mesh: A PyMesh Mesh object. Default: None
- Returns:
np.ndarray: An array containing the edge lengths of te mesh.
- homogenizeMesh(nsteps=10, boxplot=False, return_dist=False, edge_range=(600.0, 1000.0)) pyvista.core.pointset.PolyData[source]¶
Iteratively splits long edges and collapses short edges until they all have a length between min_edge and max_edge
- Args:
nsteps: Amount of steps to take to iteratively adapt the mesh edge lengths
boxplot: Make a boxplot of the mesh edge lengths for each iteration step after the refinement procedure.
return_dist: Return a 2xnsteps array of the mesh edge lengths
edge_range: Minimum and maximum allowed edge lengths
- Returns:
Union(PolyData, Tuple[PolyData, List]): Either the refined surface mesh in PyVista’s PolyData format, or both the refined mesh and a list containing all the edge lengths for each iteration step.
- plot() None[source]¶
Plots the mesh in its current state using PyVista’s Plotter() method.
- Returns:
None: Nothing. Opens a VtkRenderer window to show the plot.
- reconstruct() None[source]¶
Reads in .mesh file and writes out a refined tetrahedron mesh in .vtk format and carp format. If ‘speed.csv’ exists in the cwd, also interpolates these speeds on the mesh. speed.csv should be a csv file with columns ‘x’, ‘y’, ‘z’ and ‘speed’, where the xyz coordinates refer to point coordinates. Can be calculated with DGM.
Args:
- Returns:
Nothing. Writes out a .vtk file of the tetrahedron mesh. Writes out the same mesh in Carp text format. Updates the mesh to the tetrahedron mesh.
- setNonCondByIndexFiles(region_dir='Regions', write_dat=False, index_col='meshID') None[source]¶
Opens directory region_dir and reads in .csv files there. These .csv files should contain the indices of points whose conduction velocity should be set to zero. Writes out a .dat file for each .csv file if wanted
- Args:
region_dir: Name of directory containing the regions to be set to a conduction velocity of 0. These regions should be .csv files containing the indices of points to be set to CV=0
index_col: Name of the column in the .csv files that contain the point indices. Default=”meshID” for easy workflow in correspondence with
mesh_tools.ptsToParaview()- Returns:
None: Nothing
- splitLayer(thickness: float = 0.5, ratio: float = 0.8) None[source]¶
Calls upon
writeEndoEpi()to write ‘endo.txt’ and ‘epi.txt’: two files containing the point coordinates of the bounding mesh layers. These are read in again and used to construct two surface meshes. These two surface meshes are added together as one mesh. The class properties are updated to these points and faces.- Args:
thickness: the distance between the inner (endo) and outer (epi) layer
ratio: the ratio of distances from both layers to the original middle layer. A ratio of .8 will add an outer layer .8*thickness from the middle and an inner layer .2*thickness to the inside. Ratios > .5 are recommended to avoid self intersections.
- Returns:
Nothing
- tetrahedralise(switches, n_col_retry=3) None[source]¶
Runs TetGen as a bash command.
- Args:
switches: Switches to use with the TetGen command. See https://www.wias-berlin.de/software/tetgen/switches.html
n_col_retry: Amount of times to attempt to fix collinearities during the TetGen process.
- Returns:
None: Nothing. Writes out a .vtk file.
- writeAdjustNa2(tol: float = 1e-05, g_Na: float = 7.8, write_dat: bool = True) None[source]¶
Writes adjustment file to close off Na2+ channels in cells where CV ~ 0
- Args:
tol: tolerance for when a point is considered to have a conduction velocity of 0. Points whose conduction velocity < tol are considered to be zero.
g_Na: value for (maximum) Sodium channel conductance. Open Na-channels are set to this conductance. Default: 7.8 pS
write_dat: write a .dat file for visual inspection in e.g. meshalyzer.
- Returns:
None: Nothing. Writes out a .txt file and, if wanted, a .dat file
- writeEndoEpi(thickness: float, ratio: float) None[source]¶
Reads in .csv files from carto2csv.py, calculates normals of mesh facets and adds two layers of points along these normals: the epi- and endocardium. Writes out these two layers as .txt files.
- Args:
thickness: the distance between the inner (endo) and outer (epi) layer
ratio: the ratio of distances from both layers to the original middle layer. A ratio of .8 will add an outer layer .8*thickness from the middle and an inner layer .2*thickness to the inside. Ratios > .5 are recommended to avoid self intersections.
- Returns:
Nothing. Writes out two files: endo.txt and epi.txt
