Skip to content

Commit 114e6b0

Browse files
committed
Add ParticleShapeStatistics::project_new_sample and python API for it.
1 parent 47345be commit 114e6b0

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

Libs/Particles/ParticleShapeStatistics.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ ParticleShapeStatistics::ParticleShapeStatistics(std::shared_ptr<Project> projec
497497

498498
//---------------------------------------------------------------------------
499499
int ParticleShapeStatistics::do_pca(std::vector<std::vector<Point>> global_pts, int domainsPerShape) {
500-
this->domains_per_shape_ = domainsPerShape;
500+
domains_per_shape_ = domainsPerShape;
501501

502502
// Assumes all the same size.
503503
num_samples_ = global_pts.size() / domains_per_shape_;
@@ -508,11 +508,6 @@ int ParticleShapeStatistics::do_pca(std::vector<std::vector<Point>> global_pts,
508508
mean_.resize(num_dimensions_);
509509
mean_.fill(0);
510510

511-
std::cout << "VDimension = " << values_per_particle_ << "-------------\n";
512-
std::cout << "m_numSamples = " << num_samples_ << "-------------\n";
513-
std::cout << "m_domainsPerShape = " << domains_per_shape_ << "-------------\n";
514-
std::cout << "global_pts.size() = " << global_pts.size() << "-------------\n";
515-
516511
// Compile the "meta shapes"
517512
for (unsigned int i = 0; i < num_samples_; i++) {
518513
for (unsigned int k = 0; k < domains_per_shape_; k++) {
@@ -638,6 +633,13 @@ int ParticleShapeStatistics::principal_component_projections() {
638633
return 0;
639634
}
640635

636+
//---------------------------------------------------------------------------
637+
Eigen::VectorXd ParticleShapeStatistics::project_new_sample(const Eigen::VectorXd& new_sample) {
638+
// Subtract mean and project onto principal components
639+
Eigen::VectorXd centered_sample = new_sample - mean_;
640+
return eigenvectors_.transpose() * centered_sample;
641+
}
642+
641643
//---------------------------------------------------------------------------
642644
int ParticleShapeStatistics::write_csv_file(const std::string& s) {
643645
// Write csv file

Libs/Particles/ParticleShapeStatistics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class ParticleShapeStatistics {
5757
//! principal componenent axes for each of the samples. ComputeModes must be called first.
5858
int principal_component_projections();
5959

60+
//! Projects a new sample into the PCA space defined by the original samples.
61+
Eigen::VectorXd project_new_sample(const Eigen::VectorXd& new_sample);
62+
6063
//! Returns the sample size
6164
int get_num_samples() const { return num_samples_; }
6265
int get_group1_num_samples() const { return num_samples_group1_; }

Libs/Python/ShapeworksPython.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,11 @@ PYBIND11_MODULE(shapeworks_py, m) {
13021302
components are constructed")
13031303

13041304
.def("percentVarByMode", &ParticleShapeStatistics::get_percent_variance_by_mode,
1305-
"return the variance accounted for by the principal components");
1305+
"return the variance accounted for by the principal components")
1306+
1307+
.def("projectNewSample", &ParticleShapeStatistics::project_new_sample,
1308+
"project a new sample into the PCA space", "newSample"_a)
1309+
;
13061310

13071311
define_python_analyze(m);
13081312
define_python_groom(m);

0 commit comments

Comments
 (0)