# Statistics via Python API

**URL:** <https://shapeworks.discourse.group/t/statistics-via-python-api/222>\
**Category:** Uncategorized\
**Created:** [April 10, 2024, 5:28pm UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222 "2024-04-10T17:28:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshcates](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@joshcates](https://shapeworks.discourse.group/u/joshcates)\
**Post date:** [April 10, 2024, 5:28pm UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/1 "2024-04-10T17:28:35Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![amorris](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amorris](https://shapeworks.discourse.group/u/amorris)\
**Post date:** [April 10, 2024, 6:32pm UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/2 "2024-04-10T18:32:01Z")

</div>

The python documentation is here:

> **[Python API Reference - ShapeWorks](http://sciinstitute.github.io/ShapeWorks/latest/python/python-api.html)**
>
> The official documentation of ShapeWorks

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![joshcates](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@joshcates](https://shapeworks.discourse.group/u/joshcates)\
**Post date:** [April 11, 2024, 1:17am UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/3 "2024-04-11T01:17:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [July 18, 2024, 9:04am UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/4 "2024-07-18T09:04:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amorris](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amorris](https://shapeworks.discourse.group/u/amorris)\
**Post date:** [July 18, 2024, 8:45pm UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/5 "2024-07-18T20:45:12Z")

</div>

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:

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [July 22, 2024, 9:29am UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/6 "2024-07-22T09:29:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amorris](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amorris](https://shapeworks.discourse.group/u/amorris)\
**Post date:** [July 22, 2024, 3:39pm UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/7 "2024-07-22T15:39:19Z")

</div>

> [@Apoorva](#):
>
> ```python
> 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)
> 
> ```

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

Try:

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Apoorva](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Apoorva](https://shapeworks.discourse.group/u/Apoorva)\
**Post date:** [July 23, 2024, 9:19am UTC](https://shapeworks.discourse.group/t/statistics-via-python-api/222/8 "2024-07-23T09:19:08Z")

</div>

Thank you this was helpful.
