21 #include "itkImageFileReader.h" 22 #include "itkPSMEntropyMixedEffectsModelFilter.h" 23 #include "itkPSMProjectReader.h" 24 #include "itkCommand.h" 33 typedef Command Superclass;
34 typedef SmartPointer< Self > Pointer;
35 typedef SmartPointer< const Self > ConstPointer;
37 typedef Image<float, 3> ImageType;
46 virtual void Execute(Object *caller,
const EventObject &)
55 std::cout <<
" Eigenmode variances: ";
60 std::cout << std::endl;
61 std::cout <<
" Regularization = " << o->GetRegularizationConstant() << std::endl;
63 virtual void Execute(
const Object *,
const EventObject &)
65 std::cout <<
"SHOULDN'T BE HERE" << std::endl;
73 void operator=(
const Self &);
79 int itkPSMEntropyMixedEffectsModelFilterTest(
int argc,
char* argv[] )
82 std::string errstring =
"";
83 std::string output_path =
"";
84 std::string input_path_prefix =
"";
89 std::cout <<
"Wrong number of arguments. \nUse: " 90 <<
"itkPSMEntropyMixedEffectsModelFilterTest parameter_file [output_path] [input_path]\n" 91 <<
"See itk::PSMParameterFileReader for documentation on the parameter file format." 98 output_path = std::string(argv[2]);
103 input_path_prefix = std::string(argv[3]);
106 typedef itk::Image<float, 3> ImageType;
111 itk::PSMProjectReader::Pointer xmlreader =
112 itk::PSMProjectReader::New();
113 xmlreader->SetFileName(argv[1]);
116 itk::PSMProject::Pointer project = xmlreader->GetOutput();
119 itk::PSMEntropyMixedEffectsModelFilter<ImageType>::Pointer P
124 itk::MyMixedEffectsIterationCommand::Pointer mycommand = itk::MyMixedEffectsIterationCommand::New();
125 P->AddObserver(itk::IterationEvent(), mycommand);
128 const std::vector<std::string> &dt_files = project->GetDistanceTransforms();
129 std::cout <<
"Reading distance transforms ..." << std::endl;
130 for (
unsigned int i = 0; i < dt_files.size(); i++)
132 itk::ImageFileReader<ImageType>::Pointer reader =
133 itk::ImageFileReader<ImageType>::New();
134 reader->SetFileName(input_path_prefix + dt_files[i]);
137 std::cout <<
" " << (input_path_prefix + dt_files[i]) << std::endl;
141 std::cout <<
"Done!" << std::endl;
185 std::vector<double> expl;
186 if (project->HasVariables(
"explanatory_variables"))
188 expl = project->GetVariables(
"explanatory_variables");
190 if (expl.size() < dt_files.size())
192 errstring +=
"There are fewer explanatory variables than input files";
197 std::cout <<
"Setting variables" << std::endl;
203 errstring +=
"No explanatory variables were present for the regression.";
236 unsigned int number_of_scales = 6;
238 std::vector<double> regularization_initial(number_of_scales);
239 std::vector<double> regularization_final(number_of_scales);
240 std::vector<double> regularization_decayspan(number_of_scales);
241 std::vector<double> tolerance(number_of_scales);
242 std::vector<unsigned int> maximum_iterations(number_of_scales);
243 unsigned int num_individuals = 4;
245 std::cout <<
"Setting default optimization parameters for scales..." << std::endl;
246 for (
unsigned int i = 0; i < number_of_scales; i++)
248 std::cout <<
"Scale #" << i <<
" :" << std::endl;
249 regularization_initial[i] = 10.0;
250 std::cout <<
" regularization_initial = " << regularization_initial[i] << std::endl;
251 regularization_final[i] = 1.0;
252 std::cout <<
" regularization_final = " << regularization_final[i] << std::endl;
253 regularization_decayspan[i] = 1000;
254 std::cout <<
" regularization_decayspan = " << regularization_decayspan[i] << std::endl;
256 std::cout <<
" tolerance = " << tolerance[i] << std::endl;
257 maximum_iterations[i] = 20;
258 std::cout <<
" maximum_iterations = " << maximum_iterations[i] << std::endl;
272 P->SetRegularizationFinal(regularization_final);
273 P->SetRegularizationDecaySpan(regularization_decayspan);
277 std::cout <<
" num_individuals = " << P->GetNumIndividuals() << std::endl;
280 if (project->HasVariables(
"timepts_per_individual"))
282 std::vector<double> tp = project->GetVariables(
"timepts_per_individual");
283 vnl_vector<int> timepts; timepts.set_size(tp.size());
284 for(
int i = 0; i < timepts.size(); i++)
285 timepts[i] = static_cast<int>(tp[i]);
287 if (timepts.size() != num_individuals)
289 errstring +=
"Number of individuals and timepts associated dont match!";
294 std::cout <<
"Setting variables" << std::endl;
300 errstring +=
"No timepts variables were present.";
314 const std::vector<std::string> &out_files = project->GetModel(std::string(
"optimized"));
315 if (out_files.size() != P->GetParticleSystem()->GetNumberOfDomains())
317 errstring +=
"Number of output files does not match the number of particle domains (inputs).";
323 for (
unsigned int d = 0; d < P->GetParticleSystem()->GetNumberOfDomains(); d++)
326 std::string fname = output_path + out_files[d];
327 std::ofstream out( fname.c_str() );
330 errstring +=
"Could not open point file for output: ";
334 for (
unsigned int j = 0; j < P->GetParticleSystem()->GetNumberOfParticles(d); j++)
336 for (
unsigned int i = 0; i < 3; i++)
338 out << P->GetParticleSystem()->GetPosition(j,d)[i] <<
" ";
346 std::vector<double>::iterator it_expl;
347 vnl_vector<double> output_intercept_slope;
348 vnl_vector<double>::iterator output_it;
349 double min_expl = *std::min_element(expl.begin(), expl.end());
350 double max_expl = *std::max_element(expl.begin(), expl.end());
354 for(
int i = 0; i < step; i++)
356 double value = min_expl + i*((max_expl - min_expl)/(step - 1));
357 output_intercept_slope = P->GetShapeMatrix()->ComputeMean(value);
358 std::ostringstream ss;
360 std::string fname = output_path +
"Intercept+TimexSlope" +
"_" + ss.str() +
".lpts";
361 std::ofstream out(fname.c_str());
364 errstring +=
"Could not open point file for output: ";
369 for(output_it = output_intercept_slope.begin(); output_it != output_intercept_slope.end(); ++output_it)
371 out << *output_it <<
' ';
383 catch(itk::ExceptionObject &e)
385 errstring =
"ITK exception with description: " + std::string(e.GetDescription())
386 + std::string(
"\n at location:") + std::string(e.GetLocation())
387 + std::string(
"\n in file:") + std::string(e.GetFile());
392 errstring =
"Unknown exception thrown";
398 std::cout <<
"All tests passed" << std::endl;
403 std::cout <<
"Test failed with the following error:" << std::endl;
404 std::cout << errstring << std::endl;
void SetNumIndividuals(int n)
void SetMaximumNumberOfIterations(const std::vector< unsigned int > &n)
void SetVariables(const std::vector< double > &v)
itkTypeMacro(MyMixedEffectsIterationCommand, Command)
void SetNumberOfScales(unsigned int n)
unsigned int GetNumberOfElapsedIterations() const
void SetTimePointsPerIndividual(const vnl_vector< int > &v)
This class decorates the base PSMEntropyModelFilter class with some additional methods for setting ex...
const std::vector< double > & GetShapePCAVariances() const
void SetRegularizationInitial(const std::vector< double > &v)
void SetInput(const std::string &s, itk::DataObject *o)
virtual void Execute(Object *caller, const EventObject &)
MyMixedEffectsIterationCommand Self
void SetTolerance(const std::vector< double > &v)