STK++ 0.9.13
STK_DataHandler.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::DManager
27 * created on: 15 nov. 2013
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_DATAHANDLER_H
37#define STK_DATAHANDLER_H
38
39#include <vector>
41
42#include "STK_DataHandlerBase.h"
43#include "STK_ReadWriteCsv.h"
44
45namespace STK
46{
47// forward declaration
48class DataHandler;
49
50namespace hidden
51{
55template<typename Type>
57{
59};
60
61} // namespace hidden
62
69class DataHandler: public DataHandlerBase<DataHandler>
70{
71 public:
76 { data_.setWithNames(false); descriptor_.setWithNames(false);}
78 inline ~DataHandler() {}
80 inline ReadWriteCsv const& data() const { return data_;}
82 inline ReadWriteCsv const& descriptor() const { return descriptor_;}
84 inline int nbSample() const { return data_.sizeRows();}
86 inline int nbVariable() const { return data_.size();}
87
89 inline void setWithNames(bool withNames) { withNames_ = withNames;}
91 bool readDataFromCsvFile(std::string const& datafile, std::string descriptorfile);
98 bool readDataFromCsvFile(std::string const& datafile, std::string const& idData, std::string const& idModel);
108 template<typename Type>
109 bool readDataFromArray2D(Array2D<Type> const& data, std::string const& idData, std::string const& idModel);
119 template<typename Array>
120 bool readDataFromArray(ExprBase<Array> const& data, std::string const& idData, std::string const& idModel);
122 template<typename Type>
123 void getData(std::string const& idData, Array2D<Type>& data) const;
125 void removeData(std::string const& idData);
126
127 protected:
132 std::vector<int> colIndex(std::string const& idData) const;
133
134 private:
143};
144
145
146
147template<typename Type>
148void DataHandler::getData(std::string const& idData, Array2D<Type>& data) const
149{
150 std::vector<int> indexes = colIndex(idData);
151#ifdef STK_MIXTURE_VERY_VERBOSE
152 stk_cout << _T("In DataHandler::getData, idData = ") << idData << _T("\n");
153 stk_cout << _T("columns found = ");
154 for (std::vector<int>::const_iterator it = indexes.begin(); it != indexes.end(); ++it)
155 { stk_cout << (*it) << _T(" ");}
156 stk_cout << _T("\n");
157#endif
158 int nbVariable = indexes.size();
159 data.resize(nbSample(), nbVariable);
160 int j= data.beginCols();
161 for (std::vector<int>::const_iterator it = indexes.begin(); it != indexes.end(); ++it, ++j)
162 {
163 for (int i = data_.firstRow(*it); i <= data_.lastRow(*it); ++i)
164 { data(i, j) = stringToType<Type>(data_(i,*it));}
165 }
166}
167
168template<typename Type>
170 , std::string const& idData
171 , std::string const& idModel)
172{
173 // add descriptor
175 desc[baseIdx] = idModel ; desc[baseIdx+1] = idData;
176 if (!addInfo(idData, idModel)) return false;
177 // store data at the end of the ReadWriteCsv array in a string format
178 for (int j=data.beginCols(); j<= data.lastIdxCols(); ++j)
179 {
180 data_.push_back();
181 data_.back().resize(data.rows());
182 for (int i= data.beginRows(); i < data.endRows(); ++i)
183 { data_.back()[i] = typeToString(data(i,j), std::scientific);}
184 // store descriptor : this is the same for all the columns added
185 descriptor_.push_back(desc);
186 }
187 return true;
188}
189
190template<typename Array>
192 , std::string const& idData
193 , std::string const& idModel)
194{
195 // add descriptor
197 desc[baseIdx] = idModel ; desc[baseIdx+1] = idData;
198 if (!addInfo(idData, idModel)) return false;
199 // store data at the end of the ReadWriteCsv array in a string format
200 for (int j=data.beginCols(); j<= data.lastIdxCols(); ++j)
201 {
202 data_.push_back();
203 data_.back().resize(data.rows());
204 for (int i= data.beginRows(); i < data.endRows(); ++i)
205 { data_.back()[i] = typeToString(data.elt(i,j), std::scientific);}
206 // store descriptor : this is the same for all the columns added
207 descriptor_.push_back(desc);
208 }
209 return true;
210}
211
212
213} // namespace STK
214
215#endif /* STK_DATAHANDLER_H */
In this file, we define the final class Array2D.
In this file we define the class TReadWriteCsv.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
A class derived from a DataHandlerBase allows to store various data sets identified by an idData and ...
std::map< std::string, std::string > InfoMap
bool addInfo(std::string const &idData, std::string const &idModel)
Add an info descriptor to the data handler.
implementation of the DataHandlerBase class using ReadWriteCsv and Array2D.
~DataHandler()
destructor
void getData(std::string const &idData, Array2D< Type > &data) const
void setWithNames(bool withNames)
set withNames flag
ReadWriteCsv descriptor_
descriptor files with two rows.
ReadWriteCsv const & descriptor() const
get the whole descriptor set
bool withNames_
first line with names ?
int nbSample() const
bool readDataFromArray(ExprBase< Array > const &data, std::string const &idData, std::string const &idModel)
read a data set from an Array or Expression.
ReadWriteCsv data_
data files
void removeData(std::string const &idData)
remove the data with the given idData
DataHandlerBase< DataHandler >::InfoMap InfoMap
int nbVariable() const
std::vector< int > colIndex(std::string const &idData) const
lookup on the descriptors in order to get the columns of the ReadWriteCsv with the Id idData.
bool readDataFromArray2D(Array2D< Type > const &data, std::string const &idData, std::string const &idModel)
read a data set from an Array2D.
DataHandler()
default constructor
bool readDataFromCsvFile(std::string const &datafile, std::string descriptorfile)
read a data file and its companion description file.
ReadWriteCsv const & data() const
get the whole data set
DataHandlerBase< DataHandler > Base
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
String typeToString(Type const &t, std::ios_base &(*f)(std::ios_base &)=std::dec)
convert a Type to String
Definition STK_String.h:235
String stringNa
Representation of a Not Available value.
const int baseIdx
base index of the containers created in STK++.
The namespace STK is the main domain space of the Statistical ToolKit project.
The DataHandlerTraits will give the type of container furnished by the concrete implementations of th...