iRoCS Toolbox  1.1.0
KFile_BINARY.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: KFile_BINARY
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: Class for accessing complete kernel matrix from single
31 ** binary float file
32 ** first int32 is expected to be the number of rows
33 ** second int32 is expected to be the number of columns
34 ** following array of floats is expected to be the
35 ** kernel data.
36 ** the filename is expected to be of format
37 ** somefilebase-rows-00-20-cols-400-500.kmf
38 **
39 **-------------------------------------------------------------------------
40 **
41 ** $Log$
42 ** Revision 1.2 2005/07/19 13:42:59 haasdonk
43 ** adapted header for automatic CVS-Information extraction
44 **
45 **
46 **************************************************************************/
47 
48 #ifndef KFILE_BINARY_HH
49 #define KFILE_BINARY_HH
50 
51 #ifdef HAVE_CONFIG_H
52 #include <config.hh>
53 #endif
54 
55 // std includes
56 #include <string>
57 #include <vector>
58 #include <iostream>
59 #include <fstream>
60 //#include <algorithm>
61 // libsvmtl includes
62 #include "svm_defines.hh"
63 //#include "SVMError.hh"
64 #include "ProgressReporter.hh"
65 
66 // requirements of template parameters
71 
72 namespace svt
73 {
75  {
76 
77  public:
79  : is(0)
80  {}
81 
82  KFile_BINARY( const KFile_BINARY& orig)
83  : is(0)
84 
85  {
86  operator=( orig);
87  }
88 
89 
90  void operator=( const KFile_BINARY& orig)
91  {
92  is = orig.is;
93  kmatrix_file = orig.kmatrix_file;
94  }
95 
96  public:
97 
98  template<typename STDATA>
99  void loadParameters( STDATA& stData)
100  {
102  stData.getValue("kmatrix_file",kmatrix_file);
103  };
104 
105  template<typename STDATA>
106  void saveParameters( STDATA& stData) const
107  {
109  stData.setValue( "kmatrix_file", kmatrix_file);
110  };
111 
112  static std::string name()
113  {
114  return std::string( "binary");
115  }
116 
117  static std::string description()
118  {
119  return std::string( "binary format");
120  }
121 
122  static void getParamInfos( std::vector<ParamInfo>& p)
123  {
124  p.push_back(
125  ParamInfo( "kmatrix_file", "kmf", "string",
126  "filename of the float kernel-matrix"));
127  }
128 
129  // set kmatrix_filename to be used for next load_matrix
130 
131  void setKmatrix_file(std::string kmf)
132  {
133  kmatrix_file = kmf;
134  }
135 
136 
137  // load float matrix from file into preallocated array.
138  // Assuming sorted lists of IDs!!!
139 
140  void load_matrix(float** rowStarts,
141  std::vector<unsigned int>& gIDs1,
142  std::vector<unsigned int>& gIDs2)
143  {
144  // allocate temporary memory for row
145  unsigned int ID2min = gIDs2[0];
146  unsigned int ID2max = gIDs2[gIDs2.size()-1];
147  unsigned int ID1min = gIDs1[0];
148  unsigned int ID1max = gIDs1[gIDs1.size()-1];
149  unsigned int rmin,rmax,cmin,cmax;
150  std::vector<unsigned int>::iterator git,git2;
151 
152  float* row_data = new float[ID2max-ID2min+1];
153 
154  // open file
155  is = new std::ifstream(kmatrix_file.c_str());
156  if (!is || !(*is))
157  {
158  std::cerr << "error in opening kernel file \n";
159  exit(-1);
160  }
161 
162  // get indices from filename
163  // e.g. MNIST-IDM-rows-10-999-cols-0-999.kmf
164 
165  std::string::size_type pr = kmatrix_file.find("rows",0);
166  std::string::size_type pc = kmatrix_file.find("cols",0);
167  std::string::size_type pe = kmatrix_file.find(".kmf",0);
168  if (pr==std::string::npos || pc==std::string::npos ||
169  pe==std::string::npos)
170  {
171  std::cerr << "error in getting index-ranges from filename \n";
172  std::cerr << "required format of filename: \n";
173  std::cerr << "*-rows-RMIN-RMAX-cols-CMIN-CMAX.kmf\n";
174  exit(-1);
175  }
176 
177  std::string rrange = kmatrix_file.substr(pr+5,pc-pr-6);
178  std::string crange = kmatrix_file.substr(pc+5,pe-pc-5);
179 
180  pr = rrange.find("-",0);
181  pc = crange.find("-",0);
182 
183  rmin = atol(rrange.substr(0,pr).c_str());
184  rmax = atol(rrange.substr(pr+1).c_str());
185  cmin = atol(crange.substr(0,pc).c_str());
186  cmax = atol(crange.substr(pc+1).c_str());
187 
188 // for debugging:
189 // std::cout << "parsed rrange = " << rmin << " - " << rmax
190 // << " , crange = " << cmin << " - " << cmax << std::endl;
191 
192  // check that index ranges of gID are within file-index-range!
193 
194  if (ID1min<rmin || ID1max>rmax || ID2min<cmin || ID2max>cmax)
195  {
196  std::cerr << "index range in ID-lists exceed file-ID-range.\n";
197  exit(-1);
198  }
199 
200  // for all lines
201  unsigned int rind = 0;
202  for (git = gIDs1.begin();git!=gIDs1.end(); git++,rind++)
203  {
204  // set correct position in file. Assuming 4-byte float files
205  unsigned int pos_row_start = ((*git)-rmin)*4*(cmax-cmin+1);
206  pos_row_start += 8; // skip leading 2 integers
207  is->seekg(pos_row_start + 4 * (ID2min-cmin) );
208  // read row_data
209  is->read( (char*)row_data,(ID2max-ID2min+1)*4);
210  // select required entries and assign to output-matrix
211  float* rStart = rowStarts[rind];
212  for (unsigned int cind = 0; cind < gIDs2.size();cind++)
213  rStart[cind] = row_data[gIDs2[cind]-ID2min];
214  }
215 
216  delete[] row_data;
217  is->close();
218  delete is;
219  is = 0;
220  }
221 
222 
223  private:
224  std::ifstream* is;
225  std::string kmatrix_file;
226  };
227 
228 }
229 
230 #endif
#define CHECK_MEMBER_TEMPLATE(c)
void loadParameters(STDATA &stData)
Definition: KFile_BINARY.hh:99
static std::string description()
void saveParameters(STDATA &stData) const
void operator=(const KFile_BINARY &orig)
Definition: KFile_BINARY.hh:90
static std::string name()
void setKmatrix_file(std::string kmf)
static void getParamInfos(std::vector< ParamInfo > &p)
KFile_BINARY(const KFile_BINARY &orig)
Definition: KFile_BINARY.hh:82
void load_matrix(float **rowStarts, std::vector< unsigned int > &gIDs1, std::vector< unsigned int > &gIDs2)
The ParamInfo class contains informations about one parameter like key, help text, guiHints etc.
Definition: ParamInfo.hh:82