OTB  9.0.0
Orfeo Toolbox
otbTrainVectorBase.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef otbTrainVectorBase_h
21 #define otbTrainVectorBase_h
22 
24 #include "otbWrapperApplication.h"
26 
28 #include "otbOGRFeatureWrapper.h"
30 
31 #include "itkVariableLengthVector.h"
33 
34 #include "itkListSample.h"
36 
37 #include <algorithm>
38 #include <locale>
39 #include <string>
40 
41 namespace otb
42 {
43 namespace Wrapper
44 {
45 
46 template <class TInputValue, class TOutputValue>
47 class TrainVectorBase : public LearningApplicationBase<TInputValue, TOutputValue>
48 {
49 public:
53  typedef itk::SmartPointer<Self> Pointer;
54  typedef itk::SmartPointer<const Self> ConstPointer;
55 
57  itkTypeMacro(Self, Superclass);
58 
59  typedef typename Superclass::SampleType SampleType;
62 
63  typedef double ValueType;
64  typedef itk::VariableLengthVector<ValueType> MeasurementType;
65 
67 
69 
70 protected:
73  {
74  public:
77  };
78 
81  {
82  public:
83  typename ListSampleType::Pointer listSample;
84  typename TargetListSampleType::Pointer labeledListSample;
86  {
87  listSample = ListSampleType::New();
88  labeledListSample = TargetListSampleType::New();
89  }
90  };
92 
98  {
99  public:
100 
102  std::vector<int> m_SelectedIdx;
103 
105  std::vector<int> m_SelectedCFieldIdx;
106 
108  std::string m_SelectedCFieldName;
109 
111  std::vector<std::string> m_SelectedNames;
112  unsigned int m_NbFeatures;
113 
114  void SetFieldNames(std::vector<std::string> fieldNames, std::vector<int> selectedIdx)
115  {
116  m_SelectedIdx = selectedIdx;
117  m_NbFeatures = static_cast<unsigned int>(selectedIdx.size());
118  m_SelectedNames = std::vector<std::string>(m_NbFeatures);
119  for (unsigned int i = 0; i < m_NbFeatures; ++i)
120  {
121  m_SelectedNames[i] = fieldNames[selectedIdx[i]];
122  }
123  }
124  void SetClassFieldNames(std::vector<std::string> cFieldNames, std::vector<int> selectedCFieldIdx)
125  {
126  m_SelectedCFieldIdx = selectedCFieldIdx;
127  // Handle only one class field name, if several are provided only the first one is used.
128  if (selectedCFieldIdx.empty())
129  m_SelectedCFieldName.clear();
130  else
131  m_SelectedCFieldName = cFieldNames[selectedCFieldIdx.front()];
132  }
133  };
134 
135 
136 protected:
142  virtual void ExtractAllSamples(const ShiftScaleParameters& measurement);
143 
150  virtual SamplesWithLabel ExtractTrainingSamplesWithLabel(const ShiftScaleParameters& measurement);
151 
158  virtual SamplesWithLabel ExtractClassificationSamplesWithLabel(const ShiftScaleParameters& measurement);
159 
160 
169  SamplesWithLabel ExtractSamplesWithLabel(std::string parameterName, std::string parameterLayer, const ShiftScaleParameters& measurement);
170 
171 
177  ShiftScaleParameters GetStatistics(unsigned int nbFeatures);
178 
181  typename TargetListSampleType::Pointer m_PredictedList;
183 
184  void DoInit() override;
185  void DoUpdateParameters() override;
186  void DoExecute() override;
187 };
188 }
189 }
190 
191 #ifndef OTB_MANUAL_INSTANTIATION
192 #include "otbTrainVectorBase.hxx"
193 #endif
194 
195 #endif
otb::Wrapper::Application
This class represent an application TODO.
Definition: otbWrapperApplication.h:76
otb::Wrapper::TrainVectorBase::SampleType
Superclass::SampleType SampleType
Definition: otbTrainVectorBase.h:57
otb::Wrapper::LearningApplicationBase::ListSampleType
ModelType::InputListSampleType ListSampleType
Definition: otbLearningApplicationBase.h:99
otbStatisticsXMLFileReader.h
otb::Wrapper::LearningApplicationBase
LearningApplicationBase is the base class for application that use machine learning model.
Definition: otbLearningApplicationBase.h:75
otb::Wrapper::TrainVectorBase::m_TrainingSamplesWithLabel
SamplesWithLabel m_TrainingSamplesWithLabel
Definition: otbTrainVectorBase.h:179
otb::Wrapper::TrainVectorBase::ShiftScaleParameters::stddevMeasurementVector
MeasurementType stddevMeasurementVector
Definition: otbTrainVectorBase.h:76
otb::Wrapper::TrainVectorBase::FeaturesInfo::m_SelectedIdx
std::vector< int > m_SelectedIdx
Definition: otbTrainVectorBase.h:102
otbWrapperApplication.h
otb::Wrapper::TrainVectorBase::FeaturesInfo::m_SelectedCFieldName
std::string m_SelectedCFieldName
Definition: otbTrainVectorBase.h:108
otb::Wrapper::TrainVectorBase::DoUpdateParameters
void DoUpdateParameters() override
Definition: otbTrainVectorBase.hxx:101
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbLearningApplicationBase.h
otb::Wrapper::TrainVectorBase::FeaturesInfo::SetClassFieldNames
void SetClassFieldNames(std::vector< std::string > cFieldNames, std::vector< int > selectedCFieldIdx)
Definition: otbTrainVectorBase.h:124
otb::Wrapper::TrainVectorBase::FeaturesInfo::SetFieldNames
void SetFieldNames(std::vector< std::string > fieldNames, std::vector< int > selectedIdx)
Definition: otbTrainVectorBase.h:114
otb::Wrapper::TrainVectorBase::m_FeaturesInfo
FeaturesInfo m_FeaturesInfo
Definition: otbTrainVectorBase.h:182
otbWrapperApplicationFactory.h
otb::Wrapper::TrainVectorBase::ExtractSamplesWithLabel
SamplesWithLabel ExtractSamplesWithLabel(std::string parameterName, std::string parameterLayer, const ShiftScaleParameters &measurement)
Definition: otbTrainVectorBase.hxx:225
otb::Wrapper::TrainVectorBase::SamplesWithLabel::SamplesWithLabel
SamplesWithLabel()
Definition: otbTrainVectorBase.h:85
otb::Wrapper::TrainVectorBase::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbTrainVectorBase.h:54
otb::Wrapper::TrainVectorBase::SamplesWithLabel::listSample
ListSampleType::Pointer listSample
Definition: otbTrainVectorBase.h:83
otb::Wrapper::TrainVectorBase::ShiftScaleParameters
Definition: otbTrainVectorBase.h:72
otb::Wrapper::TrainVectorBase::m_PredictedList
TargetListSampleType::Pointer m_PredictedList
Definition: otbTrainVectorBase.h:181
otb::Wrapper::TrainVectorBase::ValueType
double ValueType
Definition: otbTrainVectorBase.h:63
otb::Wrapper::TrainVectorBase::ListSampleType
Superclass::ListSampleType ListSampleType
Definition: otbTrainVectorBase.h:60
otb::Wrapper::TrainVectorBase::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbTrainVectorBase.h:53
otb::Wrapper::TrainVectorBase::ExtractAllSamples
virtual void ExtractAllSamples(const ShiftScaleParameters &measurement)
Definition: otbTrainVectorBase.hxx:159
otb::Wrapper::LearningApplicationBase::TargetListSampleType
ModelType::TargetListSampleType TargetListSampleType
Definition: otbLearningApplicationBase.h:102
otb::Wrapper::TrainVectorBase::Superclass
LearningApplicationBase< TInputValue, TOutputValue > Superclass
Definition: otbTrainVectorBase.h:52
otb::Wrapper::TrainVectorBase::SamplesWithLabel
Definition: otbTrainVectorBase.h:80
otb::Wrapper::TrainVectorBase::FeaturesInfo::m_SelectedNames
std::vector< std::string > m_SelectedNames
Definition: otbTrainVectorBase.h:111
otb::Wrapper::TrainVectorBase::GetStatistics
ShiftScaleParameters GetStatistics(unsigned int nbFeatures)
Definition: otbTrainVectorBase.hxx:202
otb::Wrapper::TrainVectorBase::DoExecute
void DoExecute() override
Definition: otbTrainVectorBase.hxx:140
otb::Statistics::ShiftScaleSampleListFilter
This class generate a shifted and scaled version of the input sample list.
Definition: otbShiftScaleSampleListFilter.h:51
otb::Wrapper::TrainVectorBase::m_ClassificationSamplesWithLabel
SamplesWithLabel m_ClassificationSamplesWithLabel
Definition: otbTrainVectorBase.h:180
otb::Wrapper::TrainVectorBase::StatisticsReader
otb::StatisticsXMLFileReader< SampleType > StatisticsReader
Definition: otbTrainVectorBase.h:66
otb::Wrapper::TrainVectorBase::ExtractClassificationSamplesWithLabel
virtual SamplesWithLabel ExtractClassificationSamplesWithLabel(const ShiftScaleParameters &measurement)
Definition: otbTrainVectorBase.hxx:174
otb::Wrapper::TrainVectorBase::DoInit
void DoInit() override
Definition: otbTrainVectorBase.hxx:31
otb::Wrapper::LearningApplicationBase::SampleType
ModelType::InputSampleType SampleType
Definition: otbLearningApplicationBase.h:98
otb::Wrapper::TrainVectorBase::ShiftScaleFilterType
otb::Statistics::ShiftScaleSampleListFilter< ListSampleType, ListSampleType > ShiftScaleFilterType
Definition: otbTrainVectorBase.h:68
otb::StatisticsXMLFileReader
Read a xml file where are stored several statistics.
Definition: otbStatisticsXMLFileReader.h:42
otb::Wrapper::TrainVectorBase::FeaturesInfo::m_NbFeatures
unsigned int m_NbFeatures
Definition: otbTrainVectorBase.h:112
otb::Wrapper::TrainVectorBase
Definition: otbTrainVectorBase.h:47
otbTrainVectorBase.hxx
otb::Wrapper::Application::Superclass
itk::Object Superclass
Definition: otbWrapperApplication.h:81
otb::Wrapper::TrainVectorBase::FeaturesInfo
Definition: otbTrainVectorBase.h:97
otb::Wrapper::TrainVectorBase::TargetListSampleType
Superclass::TargetListSampleType TargetListSampleType
Definition: otbTrainVectorBase.h:61
otbStatisticsXMLFileWriter.h
otb::Wrapper::TrainVectorBase::ExtractTrainingSamplesWithLabel
virtual SamplesWithLabel ExtractTrainingSamplesWithLabel(const ShiftScaleParameters &measurement)
Definition: otbTrainVectorBase.hxx:167
otb::Wrapper::TrainVectorBase::Self
TrainVectorBase Self
Definition: otbTrainVectorBase.h:51
otb::Wrapper::TrainVectorBase::FeaturesInfo::m_SelectedCFieldIdx
std::vector< int > m_SelectedCFieldIdx
Definition: otbTrainVectorBase.h:105
otb::Wrapper::TrainVectorBase::ShiftScaleParameters::meanMeasurementVector
MeasurementType meanMeasurementVector
Definition: otbTrainVectorBase.h:75
otbOGRFeatureWrapper.h
otbOGRDataSourceWrapper.h
otb::Wrapper::TrainVectorBase::MeasurementType
itk::VariableLengthVector< ValueType > MeasurementType
Definition: otbTrainVectorBase.h:64
otbShiftScaleSampleListFilter.h
otb::Wrapper::TrainVectorBase::SamplesWithLabel::labeledListSample
TargetListSampleType::Pointer labeledListSample
Definition: otbTrainVectorBase.h:84