iRoCS Toolbox  1.1.0
GroupedTrainingData.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: Container for grouped training data for Multiclass SVM's
26 ** $RCSfile$
27 ** $Revision: 4820 $$Name$
28 ** $Date: 2011-11-08 10:57:01 +0100 (Tue, 08 Nov 2011) $
29 ** Copyright: GPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.5 2005/03/29 17:52:50 ronneber
38 ** - added FV_begin() and FV_end()
39 **
40 ** Revision 1.4 2004/09/13 10:02:25 ronneber
41 ** - documentation update
42 **
43 ** Revision 1.3 2004/09/03 07:08:20 ronneber
44 ** - now using Accessor in create() arguments
45 **
46 ** Revision 1.2 2004/09/01 14:43:36 ronneber
47 ** changed IterToPointerTraits stuff to
48 ** DirectAccessor and DereferencingAccessor, to make code more
49 ** intuitive understandable
50 **
51 ** Revision 1.1 2004/08/26 08:36:58 ronneber
52 ** initital import
53 **
54 **
55 **
56 **************************************************************************/
57 
58 #ifndef GROUPEDTRAININGDATA
59 #define GROUPEDTRAININGDATA
60 
61 #ifdef HAVE_CONFIG_H
62 #include <config.hh>
63 #endif
64 
65 #include <algorithm>
66 
67 #include "SVMError.hh"
68 #include "SVM_Problem.hh"
69 
70 // requirements of template parameters
72 
73 namespace svt
74 {
75 
76  /*======================================================================*/
94  /*======================================================================*/
95  template<typename FV>
97  {
99  public:
101  :_labelsStoredInExtraArray( false)
102  {}
103 
104 
106  :_labelsStoredInExtraArray( false)
107  {
108  create( problem);
109  }
110 
111  template< typename ForwardIter, typename Accessor>
112  GroupedTrainingData( ForwardIter FV_begin, const ForwardIter& FV_end,
113  Accessor accessor)
114  :_labelsStoredInExtraArray( false)
115  {
116  create( FV_begin, FV_end, accessor);
117  }
118 
119  void create( const SVM_Problem<FV>& problem);
120 
121  template< typename ForwardIter, typename Accessor>
122  void create( ForwardIter FV_begin, const ForwardIter& FV_end,
123  Accessor accessor);
124 
125  unsigned int nClasses() const
126  {
127  return static_cast<unsigned int>(_classIndexToLabel.size());
128  }
129 
130  double classIndexToLabel( int classIndex) const
131  {
132  return _classIndexToLabel[classIndex];
133  }
134 
135  const std::vector<double>& classIndizesToLabels() const
136  {
137  return _classIndexToLabel;
138  }
139 
140  unsigned int classStartIndex( int classIndex) const
141  {
142  return _classStartIndizes[classIndex];
143  }
144 
145  size_t nFeatureVectors() const
146  {
147  return _featureVectors.size();
148  }
149 
150 
151 
152  FV* featureVector( unsigned int i) const
153  {
154  return _featureVectors[i];
155  }
156 
157 
158  double label( unsigned int fvIndex) const
159  {
160  if( _labelsStoredInExtraArray)
161  {
162  SVM_ASSERT( fvIndex < _labels.size());
163  return _labels[fvIndex];
164  }
165  else
166  {
167  SVM_ASSERT( fvIndex < _featureVectors.size());
168  return _featureVectors[fvIndex]->getLabel();
169  }
170  }
171 
172 
173  std::vector<FV*>& allFeatureVectors()
174  {
175  return _featureVectors;
176  }
177 
178  const std::vector<FV*>& allFeatureVectors() const
179  {
180  return _featureVectors;
181  }
182 
183  typename std::vector<FV*>::const_iterator FV_begin() const
184  {
185  return _featureVectors.begin();
186  }
187 
188  typename std::vector<FV*>::const_iterator FV_end() const
189  {
190  return _featureVectors.end();
191  }
192 
193 
194  private:
195  std::vector<FV*> _featureVectors;
196  std::vector<unsigned int> _classStartIndizes;
197  std::vector<double> _classIndexToLabel;
198  bool _labelsStoredInExtraArray;
199  std::vector<double> _labels;
200 
201  };
202 
203 }
204 
205 #include "GroupedTrainingData.icc"
206 
207 #endif
The GroupedTrainingData class is a container for feature vectors.
double label(unsigned int fvIndex) const
std::vector< FV * > & allFeatureVectors()
const std::vector< FV * > & allFeatureVectors() const
#define SVM_ASSERT(condition)
Definition: SVMError.hh:176
#define CHECK_CLASS_TEMPLATE1(c)
unsigned int nClasses() const
GroupedTrainingData(const SVM_Problem< FV > &problem)
std::vector< FV * >::const_iterator FV_begin() const
void create(const SVM_Problem< FV > &problem)
GroupedTrainingData(ForwardIter FV_begin, const ForwardIter &FV_end, Accessor accessor)
FV * featureVector(unsigned int i) const
unsigned int classStartIndex(int classIndex) const
double classIndexToLabel(int classIndex) const
std::vector< FV * >::const_iterator FV_end() const
const std::vector< double > & classIndizesToLabels() const