25 #include "itkListSample.h"
35 template <
typename TInput,
typename TTarget>
36 bool ReadDataFile(
const std::string& infname, itk::SmartPointer<itk::Statistics::ListSample<TInput>> samples,
37 itk::SmartPointer<itk::Statistics::ListSample<TTarget>> labels)
39 typedef typename itk::Statistics::ListSample<TInput>::MeasurementType IValueType;
40 typedef typename itk::Statistics::ListSample<TTarget>::MeasurementType TValueType;
48 std::cout <<
"Could not read file " << infname << std::endl;
52 labels->SetMeasurementVectorSize(1);
53 unsigned int nbfeatures = 0;
55 for (std::string line; std::getline(ifs, line) ;)
57 boost::algorithm::trim(line);
61 nbfeatures = std::count(line.begin(), line.end(),
' ');
62 samples->SetMeasurementVectorSize(nbfeatures);
69 itk::NumericTraits<TInput>::SetLength(sample, nbfeatures);
72 auto it_parts = parts.begin();
73 auto const end_parts = parts.end();
75 if (it_parts == end_parts) {
76 throw std::runtime_error(
"Line is not correctly formatted.");
81 itk::NumericTraits<TTarget>::SetLength(label, 1);
82 label[0] = to<TValueType>(*it_parts,
"extracting label");
87 for (++it_parts ; it_parts != end_parts ; ++it_parts)
89 auto semicolon_pos =
std::find(it_parts->begin(), it_parts->end(),
':');
90 if (semicolon_pos == it_parts->end())
93 value = to<IValueType>(*it_parts,
"extracting field");
97 id = to<unsigned int>(
string_view(it_parts->begin(), semicolon_pos),
"extracting field id");
98 value = to<IValueType>(
string_view(semicolon_pos + 1, it_parts->end()),
"extracting field");
102 throw std::runtime_error(
"id is higher than the number of features.");
104 sample[
id - 1] = value;
106 samples->PushBack(sample);
107 labels->PushBack(label);