Skip to content

Commit 954f010

Browse files
new updates
1 parent e9223cf commit 954f010

File tree

5 files changed

+195
-55
lines changed

5 files changed

+195
-55
lines changed

Libs/Particles/ParticleShapeStatistics.cpp

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,29 @@ void ParticleShapeStatistics::SetNumberOfParticlesAr(std::vector<int> num_partic
225225
int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<double>> points, unsigned int dps)
226226
{
227227
// debug
228-
// std::cout << "importing points now for MCA dps value " << dps <<" "<< m_domainsPerShape << std::endl;
228+
std::cout << "importing points now for MCA dps value " << dps <<" "<< m_domainsPerShape << std::endl;
229229
unsigned int num_points = (int)(points[0].size() / (3 * dps));
230230
this->m_dps = dps;
231231
this->m_numPoints = num_points;
232232
this->m_N = points.size();
233-
// std::cout << "num_points = " << num_points << "num_samples m_N = " << m_N << std::endl;
233+
std::cout << "num_points = " << num_points << "num_samples m_N = " << m_N << std::endl;
234+
std::cout << "points[0].size() = " << points[0].size() << std::endl;
235+
234236

235237
unsigned int n = m_N * VDimension;
236238
// std::cout << " n " << n << std::endl;
237239
// unsigned int m = num_points * dps;
238240
unsigned int m = 0;
239-
for(unsigned int idx = 0; idx < dps; idx++) { m += (this->m_num_particles_ar[idx] * VDimension); }
241+
for(unsigned int idx = 0; idx < dps; idx++) { m += (this->m_num_particles_ar[idx]); }
240242
std::cout << "m " << m << std::endl;
241243

242244
m_super_matrix.set_size(m, n);
243245
m_shapes_mca.clear();
244-
// std::cout << "cleared" <<std::endl;
246+
std::cout << "cleared" <<std::endl;
245247

246248
for(unsigned int i = 0; i < points.size(); i++){
247-
unsigned int p = num_points * m_dps; // or = m_super_matrix.rows()
249+
// unsigned int p = num_points * m_dps; // or = m_super_matrix.rows()
250+
unsigned int p = m_super_matrix.rows();
248251
// std::cout << "While building shape matrix, p = " << p << " and m_super_matrix.rows() = "<< m_super_matrix.rows() << std::endl;
249252
for(unsigned int j= 0; j < p; j++){
250253
m_super_matrix(j, i * VDimension) = points[i][j * VDimension];
@@ -253,7 +256,7 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
253256
}
254257
}
255258

256-
// std::cout << " Super matrix constructed " << std::endl;
259+
std::cout << " Super matrix constructed " << std::endl;
257260

258261
// Step 2. Compute within covariance matrix
259262

@@ -265,16 +268,19 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
265268

266269
for(unsigned int k = 0; k < m_dps; k++){
267270
// extract shape matrix of kth domain
271+
std::cout << "start k = " << k << std::endl;
268272
vnl_matrix<double> z_k;
269273
// z_k.set_size(num_points, n);
270274
// m_super_matrix.extract(z_k, k * num_points, 0);
271275

272276
z_k.set_size(this->m_num_particles_ar[k], n);
273277
unsigned int row = 0;
274-
for(unsigned int idx = 0; idx < k; idx++){ row += this->m_num_particles_ar[k]; }
278+
for(unsigned int idx = 0; idx < k; idx++){ row += this->m_num_particles_ar[idx]; }
279+
std::cout << "row = " << row << std::endl;
275280
m_super_matrix.extract(z_k, row, 0);
281+
std::cout << "extract done for k = " << k << std::endl;
276282

277-
m_shapes_mca.push_back(z_k); // keep track of shape matriz of each domain
283+
// m_shapes_mca.push_back(z_k); // keep track of shape matriz of each domain
278284
// compute column-wise mean(of each sample)
279285
vnl_matrix<double> mean_k;
280286
mean_k.set_size(n, 1);
@@ -293,6 +299,7 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
293299

294300
// z_within_centred.update(z_k_centred, k * num_points, 0);
295301
z_within_centred.update(z_k_centred, row, 0);
302+
std::cout << "update done " << std::endl;
296303

297304
}
298305

@@ -319,7 +326,7 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
319326
}
320327
}
321328

322-
// std::cout << "Within objective computed " << std::endl;
329+
std::cout << "Within objective computed " << std::endl;
323330

324331
// 2..b compute within covariance matrix
325332
m_pointsMinusMean_for_within.set_size(M, m_N);
@@ -335,7 +342,7 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
335342
}
336343
}
337344

338-
// std::cout << "Within Covariance Matrix computed " << std::endl;
345+
std::cout << "Within Covariance Matrix computed " << std::endl;
339346

340347
// Step 3.Compute Between Covariance matrix
341348

@@ -349,6 +356,7 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
349356
z_between_centred(j, i) = m_super_matrix(j, i) - col_mean;
350357
}
351358
}
359+
std::cout << "between means done before covariance" << std::endl;
352360

353361
vnl_matrix<double> z_between;
354362
z_between.set_size(m_dps, n);
@@ -358,12 +366,17 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
358366
z_between_k_mean.set_size(1, n);;
359367
for(unsigned int i = 0; i < n; i++){
360368
vnl_matrix<double> mean_temp_vec;
361-
mean_temp_vec.set_size(num_points, 1);
362-
z_between_centred.extract(mean_temp_vec, k * num_points, i);
369+
// mean_temp_vec.set_size(num_points, 1);
370+
mean_temp_vec.set_size(this->m_num_particles_ar[k], 1);
371+
// z_between_centred.extract(mean_temp_vec, k * num_points, i);
372+
unsigned int row = 0;
373+
for(unsigned int idx = 0; idx < k; idx++){ row += this->m_num_particles_ar[idx]; }
374+
z_between_centred.extract(mean_temp_vec, row, i);
363375
z_between_k_mean(0, i) = mean_temp_vec.mean();
364376
}
365377
z_between.update(z_between_k_mean, k, 0);
366378
}
379+
std::cout << "z_between formed" << std::endl;
367380
vnl_matrix<double> z_between_objective;
368381
z_between_objective.set_size(m_dps * VDimension, m_N);
369382
this->m_MatrixBetween.resize(m_dps * VDimension, m_N);
@@ -379,6 +392,7 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
379392
this->m_MatrixBetween(k * VDimension + 2, i) = z_between(k, i * VDimension + 2);
380393
}
381394
}
395+
std::cout << "z_between objective done" << std::endl;
382396

383397

384398
//3.b between covariance matrix
@@ -396,8 +410,8 @@ int ParticleShapeStatistics::ImportPointsAndComputeMlpca(std::vector<vnl_vector<
396410
}
397411
}
398412

399-
// std::cout << "Between Covariance Matrix computed " << std::endl;
400-
// std::cout << "MLPCA base part done" << std::endl;
413+
std::cout << "Between Covariance Matrix computed " << std::endl;
414+
std::cout << "MLPCA base part done" << std::endl;
401415

402416
}
403417

@@ -804,7 +818,49 @@ int ParticleShapeStatistics::PrincipalComponentProjections()
804818

805819
}
806820
}
821+
auto fn = this->m_results_dir + "PCA_projections.txt";
822+
std::cout << "Writing Projections to file " << fn << std::endl;
823+
824+
std::ofstream outfile;
825+
outfile.open(fn.c_str());
826+
for(int s = 0; s < m_numSamples; s++){
827+
for (int n = 0; n < m_numSamples; n++){
828+
outfile << m_principals(s, n) << ((n == m_numSamples-1) ? "\n" : " ");
829+
}
830+
}
831+
outfile.close();
832+
return 0;
833+
}
834+
835+
836+
int ParticleShapeStatistics::MultiLevelPrincipalComponentProjections()
837+
{
838+
// Now print the projection of each shape
839+
m_mulit_level_combined_principals.resize(m_numSamples, m_numSamples);
840+
841+
for (unsigned int n = 0; n < m_numSamples; n++) {
842+
for (unsigned int s = 0; s < m_numSamples; s++) {
843+
double within = dot_product<double>(m_withinEigenvectors.get_column((m_numSamples - 1) - n),
844+
m_pointsMinusMean_for_within.get_column(s));
845+
double between = dot_product<double>(m_betweenEigenvectors.get_column((m_numSamples - 1) - n),
846+
m_pointsMinusMean_for_between.get_column(s));
847+
848+
m_mulit_level_combined_principals(s, n) = (within + between); // each row is a sample, columns index PC
849+
850+
}
851+
}
852+
853+
auto fn = this->m_results_dir + "MLPCA_projections.txt";
854+
std::cout << "Writing Projections to file " << fn << std::endl;
807855

856+
std::ofstream outfile;
857+
outfile.open(fn.c_str());
858+
for(int s = 0; s < m_numSamples; s++){
859+
for (int n = 0; n < m_numSamples; n++){
860+
outfile << m_mulit_level_combined_principals(s, n) << ((n == m_numSamples-1) ? "\n" : " ");
861+
}
862+
}
863+
outfile.close();
808864
return 0;
809865
}
810866

@@ -1015,15 +1071,15 @@ Eigen::VectorXd ParticleShapeStatistics::get_specificity(std::function<void(floa
10151071
Eigen::VectorXd ParticleShapeStatistics::get_generalization(std::function<void(float)> progress_callback)
10161072
{
10171073
auto ps = shapeworks::ParticleSystem(this->m_Matrix);
1018-
if (m_dps > 1)
1019-
{
1020-
std::cout << "Computing Generalization for Multi-level Modeling" << std::endl;
1021-
Eigen::VectorXd generalization = shapeworks::ShapeEvaluation::ComputeFullGeneralizationMultiLevel(ps, this->m_num_particles_ar, progress_callback);
1022-
// std::string fn = "/home/sci/nawazish.khan/Desktop/gener.csv";
1023-
// this->WriteEvaluationResults(generalization, fn);
1024-
return generalization;
1025-
}
1026-
else
1074+
// if (m_dps > 1)
1075+
// {
1076+
// std::cout << "Computing Generalization for Multi-level Modeling" << std::endl;
1077+
// Eigen::VectorXd generalization = shapeworks::ShapeEvaluation::ComputeFullGeneralizationMultiLevel(ps, this->m_num_particles_ar, progress_callback);
1078+
// // std::string fn = this->m_results_dir + "generalization_multi_level.csv";
1079+
// // this->WriteEvaluationResults(generalization, fn);
1080+
// return generalization;
1081+
// }
1082+
// else
10271083
return shapeworks::ShapeEvaluation::ComputeFullGeneralization(ps, progress_callback);
10281084
}
10291085

Libs/Particles/ParticleShapeStatistics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ParticleShapeStatistics
4646
int ImportPoints(std::vector<vnl_vector<double>> points, std::vector<int> group_ids);
4747
int ComputeWithinModesForMca();
4848
int ComputeBetweenModesForMca();
49+
int MultiLevelPrincipalComponentProjections();
4950
int ImportPointsAndComputeMlpca(std::vector<vnl_vector<double>> points, unsigned int dps);
5051
void SetNumberOfParticlesAr(std::vector<int> num_particles_ar);
5152
/** Loads a set of point files and pre-computes some statistics. */
@@ -82,6 +83,9 @@ class ParticleShapeStatistics
8283
const int NumberOfDimensions() { return m_numDimensions; }
8384
const int DomainsNumber() { return m_dps; }
8485
const int NumberOfPoints() { return m_numPoints; }
86+
const std::vector<int> NumberOfPointsArray() { return m_num_particles_ar; }
87+
88+
void SetResultsDir(std::string dir_name) { m_results_dir = dir_name; }
8589

8690
/** Returns the group ids */
8791
const int GroupID(unsigned int i) { return m_groupIDs[i]; }
@@ -163,6 +167,7 @@ class ParticleShapeStatistics
163167
unsigned int m_N;
164168
unsigned int m_numPoints;
165169
std::vector<int> m_num_particles_ar;
170+
std::string m_results_dir;
166171

167172

168173
unsigned int m_numDimensions;
@@ -197,6 +202,7 @@ class ParticleShapeStatistics
197202
std::vector<double> m_percentVarByMode;
198203
vnl_vector<double> m_fishersLD;
199204
Eigen::MatrixXd m_principals;
205+
Eigen::MatrixXd m_mulit_level_combined_principals;
200206

201207
vnl_vector<double> m_groupdiff;
202208
vnl_vector<double> m_groupdiffnorm;

0 commit comments

Comments
 (0)