Statistics via Python API

Hello ShapeWorks!

Is it possible to use the Python API to compute a mean shape, median shape, and other statistics?

For example, something like the follow would be extremely useful:

model = shapeworks.load_model("myssm.xlsx")
mean_particles = sw.stats.mean(model)
median_particles = sw.stats.median(model)

(A related question: Is the Python API fully documented somewhere?)

thanks!

The python documentation is here:

There is an Analyze module available that accomplishes some of this:

import shapeworks as sw
project = sw.Project()
project.load("d2_pre_only.xlsx")
analyze = sw.Analyze(project)
mean_particles = analyze.get_mean_shape_points()

But it looks like there isn’t yet a method to return the median shape particles. It should be pretty easy to add if desired.

Thanks Alan! I’m not sure how I didn’t find the documentation, but now it is clear.

Hey, Can you please tell how to get the shape meshes from these particles using python?

Mesh reconstruction from particles in ShapeWorks is typically done using mesh warping. A reference template mesh is used to warp based on the particles.

For example:

from shapeworks import *
reference_mesh = Mesh("template.vtk")
particles = ParticleSystem("template_particles.particles"]).ShapeAsPointSet(0)

warper = MeshWarper()
warper.generateWarp(reference_mesh, particles)

target_particles = ParticleSystem("target_particles.particles"]).ShapeAsPointSet(0)
warped_mesh = warper.buildMesh(target_particles)

Thank you. But with the below code,
from shapeworks import *

shape_path = ‘/mean_geo/par_512.stl’
shapeMesh = sw.Mesh(shape_path)
shapeMesh_vtk = sw.sw2vtkMesh(shapeMesh)
mean_particles = '
/particles/mean_particles.particles’

reference_mesh =shapeMesh_vtk # Mesh(shapeMesh_vtk)
particles = ParticleSystem([mean_particles]).ShapeAsPointSet(0)

warper = MeshWarper()
warper.generateWarp(reference_mesh, particles)
it is showing me error as ’ generateWarp(): incompatible function arguments. The following argument types are supported:
1. (self: shapeworks_py.MeshWarper, reference_mesh: shapeworks_py.Mesh, reference_particles: numpy.ndarray[numpy.float64[m, n]]) → bool
2. (self: shapeworks_py.MeshWarper, reference_mesh: shapeworks_py.Mesh, reference_particles: numpy.ndarray[numpy.float64[m, n]], landmarks: numpy.ndarray[numpy.float64[m, n]]) → bool

Invoked with: <shapeworks_py.MeshWarper object at 0x000002132F3BDE30>, PolyData (0x2132f485040)
N Cells: 17996
N Points: 9000
X Bounds: -5.189e+01, 3.424e+01
Y Bounds: -3.262e+01, 3.035e+01
Z Bounds: -1.396e+02, 9.510e+01
N Arrays: 0
, array([[ -2.94732, 5.13163, -75.2647 ],
[ -5.07009, -23.7317 , 26.3011 ],
[ 21.2718 , -5.59686, -30.3606 ],
…,
[ 9.99318, -25.7292 , 76.1548 ],
[ -29.259 , 13.1121 , 92.0959 ],
[ 21.045 , 15.0181 , -136.303 ]])’

Please let me know how to move forward with this. It difficult to debug it.
Thanks in advance
I

The generateWarp call takes a ShapeWorks Mesh class, instead of a vtkPolyData. No need to use sw.sw2vtkMesh

Try:

from shapeworks import *

shape_path = '/mean_geo/par_512.stl'
refrence_mesh = sw.Mesh(shape_path)
mean_particles = '/particles/mean_particles.particles'

particles = ParticleSystem([mean_particles]).ShapeAsPointSet(0)

warper = MeshWarper()
warper.generateWarp(reference_mesh, particles)

Thank you this was helpful.