Skip to content

Python PCA API#2434

Merged
akenmorris merged 3 commits intomasterfrom
python_pca
Sep 29, 2025
Merged

Python PCA API#2434
akenmorris merged 3 commits intomasterfrom
python_pca

Conversation

@akenmorris
Copy link
Contributor

@akenmorris akenmorris commented Sep 29, 2025

This PR adds additional functionality to support using ShapeWorks PCA from Python.

Example script:

import shapeworks as sw
import glob
import os
import sys
import numpy as np


def read_data():
    # create ParticleSystem
    script_dir = os.path.dirname(os.path.abspath(__file__))
    particleFilesDir = script_dir + "/data/la_particles/"
    particleFilesList = glob.glob(particleFilesDir + "*world.particles")
    print(particleFilesList)
    ps = sw.ParticleSystem(particleFilesList)
    return ps


# now load data/la_particles/CARMA0046_groomed_world.particles using numpy
new_sample = np.loadtxt(os.path.dirname(os.path.abspath(__file__)) +
                        "/data/la_particles/CARMA0046_groomed_world.particles")
# flatten new_sample to 1D array
new_sample = new_sample.flatten()

# To use raw particle files
# ps = read_data()
# pca = sw.ParticleShapeStatistics()
# pca.PCA(ps)

# To use ShapeWorks Projects
project = sw.Project()
os.chdir("data")
project.load("la.xlsx")
pca = sw.ParticleShapeStatistics()
pca.PCA(project)

# print the mean shape particles
mean_shape = pca.getMean()
print("Mean shape: ", mean_shape)

eigenvalues = pca.eigenValues()
print("Eigenvalues: ", eigenvalues)

eigenvectors = pca.eigenVectors()
print("Eigenvectors: ", eigenvectors)

scores = pca.projectNewSample(new_sample)
print("Scores: ", scores)

@akenmorris akenmorris merged commit 012940e into master Sep 29, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant