iRoCS Toolbox  1.1.0
Kernel_FILE.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: Kernel_FILE
26 ** $RCSfile$
27 ** $Revision: 740 $$Name$
28 ** $Date: 2005-07-19 15:42:59 +0200 (Tue, 19 Jul 2005) $
29 ** Copyright: LGPL $Author: haasdonk $
30 ** Description: kernel accessing complete kernel matrix from File
31 **
32 **-------------------------------------------------------------------------
33 **
34 ** $Log$
35 ** Revision 1.2 2005/07/19 13:42:59 haasdonk
36 ** adapted header for automatic CVS-Information extraction
37 **
38 **
39 **************************************************************************/
40 
41 #ifndef KERNEL_FILE_HH
42 #define KERNEL_FILE_HH
43 
44 #ifdef HAVE_CONFIG_H
45 #include <config.hh>
46 #endif
47 
48 // std includes
49 #include <vector>
50 #include <algorithm>
51 // libsvmtl includes
52 #include "svm_defines.hh"
53 #include "SVMError.hh"
54 #include "ProgressReporter.hh"
55 
56 // requirements of template parameters
61 
62 namespace svt
63 {
64  template< typename FILETYPE>
66  {
67 
68  public:
70  : _file(),
71  _data(0),
72  _rowStarts(0),
73  _gIDCache1(0),
74  _gIDCache2(0),
75  _maxgID1(0),
76  _maxgID2(0),
77  _height(0),
78  _width(0),
79  _cacheIsUpToDate(false)
80  {}
81 
82 /*-------------------------------------------------------------------------
83  * if you really want to copy Kernel_FILE's, which might be very
84  * expensive, you can define ALLOW_KERNEL_MATRIX_COPY
85  *-------------------------------------------------------------------------*/
86 #ifdef ALLOW_KERNEL_MATRIX_COPY
87  public:
88 #else
89  private:
90 #endif
91 
93  : _file(0),
94  _data(0),
95  _rowStarts(0),
96  _gIDCache1(0),
97  _gIDCache2(0),
98  _maxgID1(0),
99  _maxgID2(0),
100  _height(0),
101  _width(0),
102  _cacheIsUpToDate(false)
103  {
104  operator=( orig);
105  }
106 
107  void operator=( const Kernel_FILE<FILETYPE>& orig)
108  {
109  _cacheIsUpToDate = orig._cacheIsUpToDate;
110  _file = orig._file;
111 
112  resizeCache( orig._height, orig._width,
113  orig._maxgID1, orig._maxgID2);
114 
115 
116  if (orig._gIDCache1 != 0) std::copy(orig._gIDCache1,
117  _gIDCache1+_maxgID1+1,
118  _gIDCache1);
119  else // delete single entry allocated by resizeCache
120  {
121  delete[] _gIDCache1;
122  _gIDCache1 = 0;
123  }
124 
125 
126  if (orig._gIDCache2 != 0) std::copy(orig._gIDCache2,
127  _gIDCache2+_maxgID2+1,
128  _gIDCache2);
129  else // delete single entry allocated by resizeCache
130  {
131  delete[] _gIDCache2;
132  _gIDCache2 = 0;
133  }
134 
135  if( orig._height !=0 && orig._width != 0)
136  {
137  unsigned int size = _height*_width;
138  std::copy( orig._data, orig._data + size, _data);
139  }
140  }
141 
142  public:
143 
144 
146  {
147  clearCache();
148  };
149 
150  void resizeCache( unsigned int height, unsigned int width,
151  unsigned int maxgID1, unsigned int maxgID2) const
152  {
153 
154  clearCache();
155 
156  _width = width;
157  _height = height;
158  _maxgID1 = maxgID1;
159  _maxgID2 = maxgID2;
160 
161  if( _width != 0 && _height != 0)
162  {
163  _data = new float[height*width];
164  _rowStarts = new float*[height];
165 
166  for( unsigned int i = 0; i < height; ++i)
167  {
168  _rowStarts[i] = _data + i*width;
169  }
170  }
171  else
172  {
173  _data = 0;
174  _rowStarts = 0;
175  }
176 
177  _gIDCache1 = new unsigned int[maxgID1+1];
178  _gIDCache2 = new unsigned int[maxgID2+1];
179  }
180 
181  void clearCache() const
182  {
183  if( _data != 0)
184  {
185  delete[] _data;
186  _data = 0;
187  }
188 
189  if( _rowStarts != 0)
190  {
191  delete[] _rowStarts;
192  _rowStarts = 0;
193  }
194 
195  if( _gIDCache1 != 0)
196  {
197  delete[] _gIDCache1;
198  _gIDCache1 = 0;
199  }
200 
201  if( _gIDCache2 != 0)
202  {
203  delete[] _gIDCache2;
204  _gIDCache2 = 0;
205  }
206 
207  _maxgID1 = 0;
208  _maxgID2 = 0;
209  _width = 0;
210  _height = 0;
211  _cacheIsUpToDate = false;
212  }
213 
214  // updateCache version with one FV-list
215 
216  template< typename ForwardIter, typename Accessor>
217  void updateCache( const ForwardIter& fvBegin,
218  const ForwardIter& fvEnd,
219  Accessor accessor,
220  ProgressReporter* pr = 0) const
221  {
222  updateCache(fvBegin,fvEnd,accessor,fvBegin,fvEnd,accessor,pr);
223  }
224 
225  // updateCache version with two FV-lists, using globalIDs
226 
227  template< typename ForwardIter1, typename Accessor1,
228  typename ForwardIter2, typename Accessor2>
229  void updateCache( const ForwardIter1& fvBegin1,
230  const ForwardIter1& fvEnd1,
231  Accessor1 accessor1,
232  const ForwardIter2& fvBegin2,
233  const ForwardIter2& fvEnd2,
234  Accessor2 accessor2,
235  ProgressReporter* pr = 0) const
236  {
237  // collect global indices and sort rising
238  std::vector<unsigned int> gIDs1;
239  std::vector<unsigned int> gIDs2;
240  std::vector<unsigned int>::iterator git;
241 
242  for (ForwardIter1 fi = fvBegin1; fi != fvEnd1; fi++)
243  gIDs1.push_back(accessor1(fi).getGlobalID());
244  std::sort(gIDs1.begin(),gIDs1.end());
245 
246  for (ForwardIter2 fi = fvBegin2; fi != fvEnd2; fi++)
247  gIDs2.push_back(accessor2(fi).getGlobalID());
248  std::sort(gIDs2.begin(),gIDs2.end());
249 
250  // determine maximum global indices
251  _maxgID1 = 0;
252  _maxgID2 = 0;
253 
254  if (gIDs1.begin() != gIDs1.end())
255  _maxgID1 = *(gIDs1.end()-1);
256  if (gIDs2.begin() != gIDs2.end())
257  _maxgID2 = *(gIDs2.end()-1);
258 
259  resizeCache(gIDs1.size(),gIDs2.size(),_maxgID1,_maxgID2);
260 
261  // fill index-tables
262  int i = 0;
263  for (git=gIDs1.begin();
264  git!=gIDs1.end();git++,i++)
265  _gIDCache1[*git]=i;
266 
267  i = 0;
268  for (git=gIDs2.begin();
269  git!=gIDs2.end();git++,i++)
270  _gIDCache2[*git]=i;
271 
272  // fill Cache-matrix
273  _file.load_matrix(_rowStarts,gIDs1,gIDs2);
274 
275  if( pr != 0)
276  {
277  pr->reportProgress( TASK_LEVEL_CROSS_VAL,
278  "full kernel matrix read", 1.0,
279  "");
280  }
281 
282 // output read kernel matrix
283 // std::cout << "kernel matrix: \n";
284 // for (unsigned int i = 0; i< _height; i++)
285 // {
286 // std::cout << "km-row " << i << ": ";
287 // for (unsigned int j = 0; j< _width; j++)
288 // std::cout << _rowStarts[i][j] << " ";
289 // std::cout << "\n";
290 // }
291 
292 // std::cout << "gid-list 1: \n";
293 // for (unsigned int i = 0; i< _height; i++)
294 // {
295 // std::cout << gIDs1[i] << " ";
296 // }
297 // std::cout << "\n";
298 // std::cout << "gid-list 2: \n";
299 // for (unsigned int i = 0; i< _width; i++)
300 // {
301 // std::cout << gIDs2[i] << " ";
302 // }
303 // std::cout << "\n";
304 
305  _cacheIsUpToDate = true;
306  }
307 
308 
309  template< typename FV>
310  double k_function( const FV& x, const FV& y) const
311  {
313  SVM_ASSERT( _cacheIsUpToDate == true );
314 
315  unsigned int gID1 = x.getGlobalID();
316  unsigned int gID2 = y.getGlobalID();
317 
318  SVM_ASSERT( gID1 <= _maxgID1 );
319  SVM_ASSERT( gID2 <= _maxgID2 );
320 
321  return _rowStarts[ _gIDCache1[gID1] ][ _gIDCache2[gID2] ];
322  };
323 
324  template<typename STDATA>
325  void loadParameters( STDATA& stData)
326  {
327  _file.loadParameters( stData);
328  };
329 
330  template<typename STDATA>
331  void saveParameters( STDATA& stData) const
332  {
334  _file.saveParameters( stData);
335  stData.setValue( "kernel_type", name());
336  };
337 
338  static std::string name()
339  {
340  return std::string( "file_")+ FILETYPE::name();
341  }
342 
343  static std::string description()
344  {
345  return std::string( "cached kernel matrix file, ")+
346  FILETYPE::description();
347  }
348 
349  static void getParamInfos( std::vector<ParamInfo>& p)
350  {
351  FILETYPE::getParamInfos( p);
352  }
353 
354  private:
355  mutable FILETYPE _file;
356  mutable float* _data;
357  mutable float** _rowStarts;
358  mutable unsigned int* _gIDCache1;
359  mutable unsigned int* _gIDCache2;
360  mutable unsigned int _maxgID1;
361  mutable unsigned int _maxgID2;
362  mutable unsigned int _height;
363  mutable unsigned int _width;
364  mutable bool _cacheIsUpToDate;
365  };
366 
367 }
368 
369 #endif
void loadParameters(STDATA &stData)
Definition: Kernel_FILE.hh:325
#define CHECK_MEMBER_TEMPLATE(c)
#define SVM_ASSERT(condition)
Definition: SVMError.hh:176
static std::string name()
Definition: Kernel_FILE.hh:338
void saveParameters(STDATA &stData) const
Definition: Kernel_FILE.hh:331
void updateCache(const ForwardIter &fvBegin, const ForwardIter &fvEnd, Accessor accessor, ProgressReporter *pr=0) const
Definition: Kernel_FILE.hh:217
void updateCache(const ForwardIter1 &fvBegin1, const ForwardIter1 &fvEnd1, Accessor1 accessor1, const ForwardIter2 &fvBegin2, const ForwardIter2 &fvEnd2, Accessor2 accessor2, ProgressReporter *pr=0) const
Definition: Kernel_FILE.hh:229
static std::string description()
Definition: Kernel_FILE.hh:343
const int TASK_LEVEL_CROSS_VAL
double k_function(const FV &x, const FV &y) const
Definition: Kernel_FILE.hh:310
void resizeCache(unsigned int height, unsigned int width, unsigned int maxgID1, unsigned int maxgID2) const
Definition: Kernel_FILE.hh:150
static void getParamInfos(std::vector< ParamInfo > &p)
Definition: Kernel_FILE.hh:349
void clearCache() const
Definition: Kernel_FILE.hh:181