OTB  9.0.0
Orfeo Toolbox
otbDecisionTree.hxx
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 
21 #ifndef otbDecisionTree_hxx
22 #define otbDecisionTree_hxx
23 
24 #include "otbDecisionTree.h"
25 #include "otbMacro.h"
26 #include "itkNumericTraits.h"
27 
28 namespace otb
29 {
33 template <class AttributeValueType, class LabelType>
35 {
36  m_TreeMap = new TreeMapType;
37  m_IsFinal = true;
38  m_Label = static_cast<LabelType>(0);
39 }
40 
44 template <class AttributeValueType, class LabelType>
46 {
47  delete m_TreeMap;
48 }
49 
50 template <class AttributeValueType, class LabelType>
52 {
53  m_IsFinal = false;
54  KeyType key = KeyType(attr, testType);
55  (*m_TreeMap)[key] = branch;
56 }
57 
58 template <class AttributeValueType, class LabelType>
60 {
61  m_IsFinal = false;
62  KeyType key = KeyType(attr, EQ);
63  (*m_TreeMap)[key] = branch;
64 }
65 
66 template <class AttributeValueType, class LabelType>
67 void DecisionTree<AttributeValueType, LabelType>::AddBranch(AttributeValueType attr, DecisionTreeTestType testType, LabelType label)
68 {
69  m_IsFinal = true;
70  KeyType key = KeyType(attr, testType);
71  m_LabelMap[key] = label;
72 }
73 
74 template <class AttributeValueType, class LabelType>
75 void DecisionTree<AttributeValueType, LabelType>::AddBranch(AttributeValueType attr, LabelType label)
76 {
77  m_IsFinal = true;
78  KeyType key = KeyType(attr, EQ);
79  m_LabelMap[key] = label;
80 }
81 
82 template <class AttributeValueType, class LabelType>
84 {
85  AttributeValueType attrValue = example[m_Attribute];
86 
87  otbMsgDevMacro(<< "Trying to match attribute " << m_Attribute << " with value " << attrValue);
88 
89  bool found = false;
90  if (m_IsFinal)
91  {
92  typename LabelMapType::const_iterator lmIt = m_LabelMap.begin();
93  while (lmIt != m_LabelMap.end())
94  {
95  KeyType theKey = lmIt->first;
96  DecisionTreeTestType theTest = theKey.second;
97  AttributeValueType theValue = theKey.first;
98  switch (theTest)
99  {
100  case LT:
101  if (attrValue < theValue)
102  return lmIt->second;
103  break;
104  case LE:
105  if (attrValue <= theValue)
106  return lmIt->second;
107  break;
108  case EQ:
109  if (attrValue == theValue)
110  return lmIt->second;
111  break;
112  case GE:
113  if (attrValue >= theValue)
114  return lmIt->second;
115  break;
116  case GT:
117  if (attrValue > theValue)
118  return lmIt->second;
119  break;
120  }
121  ++lmIt;
122  }
123 
124  // if we get here it means that a verified test was not found
125  itkGenericExceptionMacro(<< "Example could not be handled by decision tree.");
126  }
127  else
128  {
129  found = false;
130 
131  // Look for branches matching the test on the attribute
132  std::vector<KeyType> candidateKeys;
133 
134  typename TreeMapType::const_iterator tmIt = m_TreeMap->begin();
135  while (tmIt != m_TreeMap->end())
136  {
137  KeyType theKey = tmIt->first;
138  DecisionTreeTestType theTest = theKey.second;
139  AttributeValueType theValue = theKey.first;
140  switch (theTest)
141  {
142  case LT:
143  if (attrValue < theValue)
144  {
145  candidateKeys.push_back(theKey);
146  found = true;
147  }
148  break;
149  case LE:
150  if (attrValue <= theValue)
151  {
152  candidateKeys.push_back(theKey);
153  found = true;
154  }
155  break;
156  case EQ:
157  if (attrValue == theValue)
158  {
159  candidateKeys.push_back(theKey);
160  found = true;
161  }
162  break;
163  case GE:
164  if (attrValue >= theValue)
165  {
166  candidateKeys.push_back(theKey);
167  found = true;
168  }
169  break;
170  case GT:
171  if (attrValue > theValue)
172  {
173  candidateKeys.push_back(theKey);
174  found = true;
175  }
176  break;
177  }
178  ++tmIt;
179  }
180 
181  if (!found) // attribute
182  // not found
183  // in the map
184  {
185  itkGenericExceptionMacro(<< "Example could not be handled by decision tree.");
186  }
187  else
188  {
189  // If we found one or several matching tests
190  typename std::vector<KeyType>::const_iterator ckIt = candidateKeys.begin();
191  while (ckIt != candidateKeys.end())
192  {
193  otbMsgDevMacro(<< (*ckIt).first << " " << (*ckIt).second);
194  Pointer child = (*m_TreeMap)[(*ckIt)];
195  return child->Decide(example);
196  }
197  }
198  }
199  // added return statement to suppress warning, but if/then/else
200  // conditions make sure it is never reached
201  return static_cast<LabelType>(0);
202 }
203 
204 
208 template <class AttributeValueType, class LabelType>
209 void DecisionTree<AttributeValueType, LabelType>::PrintSelf(std::ostream& os, itk::Indent indent) const
210 {
211  Superclass::PrintSelf(os, indent);
212 }
213 
214 } // end namespace otb
215 
216 #endif
otbDecisionTree.h
otb::DecisionTree::AddBranch
void AddBranch(AttributeValueType attr, DecisionTreeTestType testType, Pointer branch)
Definition: otbDecisionTree.hxx:51
otb::MetaDataKey::KeyType
KeyType
Definition: otbMetaDataKey.h:85
otb::DecisionTree::TreeMapType
std::map< KeyType, Pointer > TreeMapType
Definition: otbDecisionTree.h:87
otb::DecisionTree::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbDecisionTree.h:77
otb::DecisionTree::DecisionTree
DecisionTree()
Definition: otbDecisionTree.hxx:34
otb::DecisionTree::DecisionTreeTestType
DecisionTreeTestType
Definition: otbDecisionTree.h:64
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::DecisionTree::Decide
LabelType Decide(const ExampleType &example)
Definition: otbDecisionTree.hxx:83
otb::DecisionTree::KeyType
std::pair< AttributeValueType, DecisionTreeTestType > KeyType
Definition: otbDecisionTree.h:84
otbMacro.h
otb::DecisionTree::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbDecisionTree.hxx:209
otbMsgDevMacro
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64
otb::DecisionTree::ExampleType
std::vector< AttributeValueType > ExampleType
Definition: otbDecisionTree.h:89
otb::DecisionTree::~DecisionTree
~DecisionTree() override
Definition: otbDecisionTree.hxx:45