.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\preprocessor\preprocess-fullheart.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_preprocessor_preprocess-fullheart.py: Create a full-heart model ------------------------- This example shows how to process a case from Rodero et al. (2021) into a simulation-ready heart model. .. GENERATED FROM PYTHON SOURCE LINES 32-36 Perform the required imports ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Import the required modules and set relevant paths, including that of the working directory and generated model. .. GENERATED FROM PYTHON SOURCE LINES 36-64 .. code-block:: Python import json import os from pathlib import Path import ansys.health.heart.models as models from ansys.health.heart.pre.database_utils import get_compatible_input from ansys.health.heart.utils.download import download_case_from_zenodo, unpack_case # specify a download directory download_folder = Path.home() / "pyansys-heart" / "downloads" # Download a compatible case from the Zenodo database. tar_file = download_case_from_zenodo("Rodero2021", 1, download_folder, overwrite=False) # Unpack the case to get the input CASE or VTK file. case_file = unpack_case(tar_file) # Specify the working directory. This code uses the directory of the CASE file. workdir = os.path.join(os.path.dirname(case_file), "FullHeart") if not os.path.isdir(workdir): os.makedirs(workdir) # Specify paths to the model, input, and part definitions. path_to_model = os.path.join(workdir, "heart_model.vtu") path_to_input = os.path.join(workdir, "input_model.vtp") path_to_part_definitions = os.path.join(workdir, "part_definitions.json") .. GENERATED FROM PYTHON SOURCE LINES 65-75 .. note:: You can also manually download the CASE or VTK files from the Strocchi 2020 and Rodero 2021 databases. For more information, see: - `A Publicly Available Virtual Cohort of Four-chamber Heart Meshes for Cardiac Electro-mechanics Simulations `_ - `Virtual cohort of adult healthy four-chamber heart meshes from CT images `_ Alternatively, you can simply click one of the buttons at the bottom of this page to download a CASE file for the Rodero 2021 database in an IPYNB, PY, or ZIP format. .. GENERATED FROM PYTHON SOURCE LINES 77-79 Convert the VTK file to a compatible input format ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 79-89 .. code-block:: Python input_geom, part_definitions = get_compatible_input( case_file, model_type="FullHeart", database="Rodero2021" ) # Note that the input model and part definitions can be saved for later use. # Save input geometry and part definitions. input_geom.save(path_to_input) with open(path_to_part_definitions, "w") as f: json.dump(part_definitions, f, indent=True) .. GENERATED FROM PYTHON SOURCE LINES 90-93 Create a heart model ~~~~~~~~~~~~~~~~~~~~ Create a full-heart model. .. GENERATED FROM PYTHON SOURCE LINES 93-114 .. code-block:: Python model = models.FullHeart(working_directory=workdir) # Load input model generated in an earlier step. model.load_input(input_geom, part_definitions, "surface-id") # Mesh the volume of all structural parts. model.mesh_volume(use_wrapper=True, global_mesh_size=2.0, _global_wrap_size=2.0) # Update the model and extract the required anatomical features. model.update() # Optionally save the simulation mesh as a VTK object for "offline" inspection. model.mesh.save(os.path.join(model.workdir, "simulation-mesh.vtu")) model.save_model(os.path.join(model.workdir, "heart_model.vtu")) # Print some information about the processed model. print(model) # Print part names. print(model.part_names) .. rst-class:: sphx-glr-script-out .. code-block:: none GENERAL: total_num_tets: 332383 total_num_nodes: 72347 PARTS: Left ventricle: num_tets: 141000 SURFACES: Left ventricle endocardium: num_faces: 7421 Left ventricle epicardium: num_faces: 8123 Left ventricle septum: num_faces: 0 CAPS: mitral-valve: num_nodes: 67 aortic-valve: num_nodes: 44 Right ventricle: num_tets: 67893 SURFACES: Right ventricle endocardium: num_faces: 8875 Right ventricle epicardium: num_faces: 9324 Right ventricle septum: num_faces: 2758 CAPS: tricuspid-valve: num_nodes: 93 pulmonary-valve: num_nodes: 58 Septum: num_tets: 42734 SURFACES: {} CAPS: {} Left atrium: num_tets: 31372 SURFACES: Left atrium endocardium: num_faces: 5740 Left atrium epicardium: num_faces: 4369 CAPS: mitral-valve-atrium: num_nodes: 45 right-superior-pulmonary-vein: num_nodes: 37 left-superior-pulmonary-vein: num_nodes: 35 right-inferior-pulmonary-vein: num_nodes: 31 left-atrium-appendage: num_nodes: 28 left-inferior-pulmonary-vein: num_nodes: 26 Right atrium: num_tets: 32288 SURFACES: Right atrium endocardium: num_faces: 7168 Right atrium epicardium: num_faces: 5564 CAPS: tricuspid-valve-atrium: num_nodes: 73 inferior-vena-cava: num_nodes: 35 superior-vena-cava: num_nodes: 34 Aorta: num_tets: 12780 SURFACES: Aorta wall: num_faces: 4290 CAPS: {} Pulmonary artery: num_tets: 4316 SURFACES: Pulmonary artery wall: num_faces: 1348 CAPS: {} CAVITIES: Left ventricle cavity: volume: 120446.96049660804 Right ventricle cavity: volume: 185648.59541736593 Left atrium cavity: volume: 68001.90309648328 Right atrium cavity: volume: 110948.18865153291 ['Left ventricle', 'Right ventricle', 'Septum', 'Left atrium', 'Right atrium', 'Aorta', 'Pulmonary artery'] .. GENERATED FROM PYTHON SOURCE LINES 115-119 Visualize results ~~~~~~~~~~~~~~~~~ Visualize and inspect the components of the model by accessing various properties or attributes and invoking methods. .. GENERATED FROM PYTHON SOURCE LINES 119-135 .. code-block:: Python print(f"Volume of LV cavity: {model.left_ventricle.cavity.volume} mm^3") print(f"Volume of LV cavity: {model.left_atrium.cavity.volume} mm^3") # Plot the remeshed model. model.plot_mesh(show_edges=False) # Plot the endocardial surface of the left ventricle. model.left_ventricle.endocardium.plot(show_edges=True, color="r") # Loop over all cavities and plot them in a single window with PyVista. import pyvista as pv cavities = pv.PolyData() for c in model.cavities: cavities += c.surface cavities.plot(show_edges=True) .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/preprocessor/images/sphx_glr_preprocess-fullheart_001.png :alt: preprocess fullheart :srcset: /examples/preprocessor/images/sphx_glr_preprocess-fullheart_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: C:\Users\ansys\actions-runner\_work\pyansys-heart\pyansys-heart\doc\source\examples\preprocessor\images\sphx_glr_preprocess-fullheart_001.vtksz .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/preprocessor/images/sphx_glr_preprocess-fullheart_002.png :alt: preprocess fullheart :srcset: /examples/preprocessor/images/sphx_glr_preprocess-fullheart_002.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: C:\Users\ansys\actions-runner\_work\pyansys-heart\pyansys-heart\doc\source\examples\preprocessor\images\sphx_glr_preprocess-fullheart_002.vtksz .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/preprocessor/images/sphx_glr_preprocess-fullheart_003.png :alt: preprocess fullheart :srcset: /examples/preprocessor/images/sphx_glr_preprocess-fullheart_003.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: C:\Users\ansys\actions-runner\_work\pyansys-heart\pyansys-heart\doc\source\examples\preprocessor\images\sphx_glr_preprocess-fullheart_003.vtksz .. rst-class:: sphx-glr-script-out .. code-block:: none Volume of LV cavity: 120446.96049660804 mm^3 Volume of LV cavity: 68001.90309648328 mm^3 .. rst-class:: sphx-glr-timing **Total running time of the script:** (8 minutes 9.526 seconds) .. _sphx_glr_download_examples_preprocessor_preprocess-fullheart.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: preprocess-fullheart.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: preprocess-fullheart.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: preprocess-fullheart.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_