Python API

Provides a system for patient-specific cardiovascular modeling and simulation.
User avatar
Peter Patalano
Posts: 25
Joined: Thu Jul 26, 2018 9:11 am

Python API

Post by Peter Patalano » Sat Mar 28, 2020 9:59 am

Can anyone direct me to the Python API. I read about it in previous posts but am not aware of its location.

Specifically, I would like to use the python API to perform mesh generation on surfaces generated in VMTK. I would prefer to explore its functionality prior to embarking on the mission of converting VMTK generated surface and mesh data into data which will be compatible for SV.

Any advice is welcomed and appreciated. :)

-Peter

User avatar
Peter Patalano
Posts: 25
Joined: Thu Jul 26, 2018 9:11 am

Re: Python API

Post by Peter Patalano » Sun Mar 29, 2020 1:26 pm

Nevermind, found it.

Found the packages in /SimVascular/Python/site-packages

Saw some talk that they would be moved to another directory.

User avatar
David Parker
Posts: 1790
Joined: Tue Aug 23, 2005 2:43 pm

Re: Python API

Post by David Parker » Sun Mar 29, 2020 2:35 pm

Hi Peter,

I am in the process of re-writing the SV Python API. The current API is very fragile, has a confusing interface, does no error checking and does not work like a typical Python package (i.e. does not work with Python objects). You can find some examples in https://github.com/SimVascular/SimVascu ... thon_demos.

I don't think that you need the Python API to do what you want to do. VMTK writes out the surface mesh to a .vtp file. You can read a .vtp file directly into the SV Models tool.

Cheers,
Dave

User avatar
Peter Patalano
Posts: 25
Joined: Thu Jul 26, 2018 9:11 am

Re: Python API

Post by Peter Patalano » Mon Mar 30, 2020 11:26 am

Hi Dave,

Thanks for the response. I want to have the mesh totally ready to be used by svPre without having to use the GUI

To give you an idea, I want to be able to go from mesh to the output of svPost without having to interact with the GUI. I have code written to process the svPost output using VTK, which does a series of things (manipulate geometry, replaces flow extensions, smooths, etc.) and want to send back to SV for solving again.

Workflow:
DICOM -> segmentation and surface generation w/ VMTK and VTK -> SV for svSolver +/- mesh generation -> VTK and VMTK for data manipulation -> SV for svSolver +/- mesh generation -> repeat VMTK/VTK, SV stuff until criteria met

Where I am running into trouble is figuring out how to make my VMTK output compatible with the svPre which I assumed was my natural point of entry. The issue is the structure of the DataArrays from the VMTK output which has a slightly different format than the output of the SV model/mesh utilities. My strategy was going to be to either use MMG to create the mesh and go from there until I found out about the SV Python API which I hoped would enable me to access the modelling and meshing capabilities from a python interface.

Is there some SV functionality that I am missing which would enable me to do this? :D

Thanks,
Peter

User avatar
Peter Patalano
Posts: 25
Joined: Thu Jul 26, 2018 9:11 am

Re: Python API

Post by Peter Patalano » Mon Mar 30, 2020 12:01 pm

Dave,
Forgot to say thanks for the link. I will check out those examples now.

-Peter

User avatar
David Parker
Posts: 1790
Joined: Tue Aug 23, 2005 2:43 pm

Re: Python API

Post by David Parker » Mon Mar 30, 2020 9:01 pm

Hi Peter,

I think you can use the SV Python API to do what you want.

1) Create a solid model from a .vtp file
2) Generate a mesh
3) Modify mesh using vmtk
4) Generate mesh-complete files
5) Generate .svpre file

The mesh .vtp surface files contains a ModelFaceID cell data that identifies which cells are associated with which surface faces.

If you modify mesh files using vmtk then you need to maintain consistent region, node and element IDs data arrays.

The Python API is not documented so let me put together a script that demonstrates these steps. I'm that familiar with the API myself so this will take me a day or so to figure out. There are also more examples in https://github.com/ktbolt/cardiovascula ... on-scripts.

Cheers,
Dave

User avatar
Peter Patalano
Posts: 25
Joined: Thu Jul 26, 2018 9:11 am

Re: Python API

Post by Peter Patalano » Tue Mar 31, 2020 8:36 am

Thanks for your help Dave. In the meantime, I'll continue to explore and experiment with the API.

User avatar
David Parker
Posts: 1790
Joined: Tue Aug 23, 2005 2:43 pm

Re: Python API

Post by David Parker » Wed Apr 01, 2020 12:48 pm

Hi Peter,

I've written a Python script demonstrating how to use the SV Python API to generate the mesh files and .svpre file needed to run a simulation. See https://github.com/ktbolt/cardiovascula ... ing-to-sim.

Have a look!

Cheers,
Dave

User avatar
Peter Patalano
Posts: 25
Joined: Thu Jul 26, 2018 9:11 am

Re: Python API

Post by Peter Patalano » Wed Apr 01, 2020 1:12 pm

I'll check it out right now. Thanks.

User avatar
Peter Patalano
Posts: 25
Joined: Thu Jul 26, 2018 9:11 am

Re: Python API

Post by Peter Patalano » Wed Apr 01, 2020 1:50 pm

Dave,

First off. You're a wizard! Thanks for your help. It works for the cylinder but on the bifurcated demo.vtp

Code: Select all

SimVascular Python Shell
Copyright (c) Stanford University, The Regents of the University
              of California, and others.  All Rights Reserved.

New gRepository created from cv_solid_init
  TetGen:      1.5.1
PyModule_Create called
               splinePolygonContour Enabled
               levelSetContour Enabled
               polygonContour Enabled
               circleContour Enabled
               splinePolygonContour Enabled
  OpenCASCADE: 7.3.0
  Itk:         4.13.2
               TetGen Adaption Enabled
---------- Modeling ----------
pySolid Model tp_init called
Array name 'ModelFaceID' does not exist. Regions must be identified and named 'ModelFaceID' prior to this function call
Traceback (most recent call last):
  File "../modeling-to-sim/model-to-sim.py", line 61, in <module>
    solid_face_ids = [int(id) for id in solid.GetFaceIds()]
TypeError: 'NoneType' object is not iterable
Although it looks like the ModelFaceID array is present in the demo.vtp data

Code: Select all

<?xml version="1.0"?>
<VTKFile type="PolyData" version="0.1" byte_order="LittleEndian" compressor="vtkZLibDataCompressor">
  <PolyData>
    <Piece NumberOfPoints="44581"                NumberOfVerts="0"                    NumberOfLines="0"                    NumberOfStrips="0"                    NumberOfPolys="89158"
       >
      <PointData Scalars="GlobalBoundaryPoints">
        <DataArray type="Int32" Name="GlobalBoundaryPoints" format="appended" RangeMin="0"                    RangeMax="1"                    offset="0"                   />
      </PointData>
      <CellData Scalars="ModelFaceID" Normals="Normals">
        <DataArray type="Int32" Name="GlobalBoundaryCells" format="appended" RangeMin="0"                    RangeMax="1"                    offset="632"                 />
        <DataArray type="Int32" Name="CapID" format="appended" RangeMin="-1"                   RangeMax="2"                    offset="1828"                />
I'll snoop around.

POST REPLY