STK++ 0.9.13
STK_CsvToArray.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: 9 juin 2011
28 * Purpose: Create an utility class in order to transfer the Data from
29 * a DataFrame in an Array.
30 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
31 *
32 **/
33
38#ifndef STK_CSVTOARRAY_H
39#define STK_CSVTOARRAY_H
40
41#include "STK_ReadWriteCsv.h"
43
44namespace STK
45{
46namespace hidden
47{
51template<class Type, class Array> struct CsvToArrayImpl;
52
53template<class OtherType>
55{
56 static void run(TReadWriteCsv<String> const& rw, Array2D<OtherType>* p_data, Real const propMiss)
57 {
58 int jSize = 0;
59 for(int jVar = rw.begin(); jVar<=rw.lastIdx(); jVar++)
60 {
61 if ( (rw.var(jVar).nbMiss()/Real(rw.var(jVar).size())) <= propMiss)
62 { jSize++;}
63 }
64 // resize
65 p_data->resize(rw.rows(), Range(rw.begin(), jSize));
66 for(int jVar = rw.begin(), jCol=rw.begin(); jVar<=rw.lastIdx(); jVar++)
67 {
68 if ( (rw.var(jVar).nbMiss()/Real(rw.var(jVar).size())) <= propMiss)
69 for (int i =p_data->beginRows(); i<= p_data->lastIdxRows(); ++i)
70 { p_data->elt(i, jCol) = stringToType<OtherType>(rw(i,jVar));}
71 jCol++;
72 }
73 }
74};
75
76template<class Type>
77struct CsvToArrayImpl<Type, Array2D<Type> >
78{
79 static void run(TReadWriteCsv<Type> const& rw, Array2D<Type>* p_data, Real const propMiss)
80 {
81 p_data->reserveCols(rw.size());
82 for(int jVar = rw.begin(); jVar<rw.end(); jVar++)
83 {
84 if ( (rw.var(jVar).nbMiss()/Real(rw.var(jVar).size())) <= propMiss)
85 p_data->merge(rw.var(jVar));
86 }
87 }
88};
89
90template<class Type>
91struct CsvToArrayImpl<Type, CArray<Type > >
92{
93 static void run(TReadWriteCsv<Type> const& rw, CArray<Type >* p_data, Real const propMiss)
94 {
95 int jSize = 0;
96 for(int jVar = rw.begin(); jVar<=rw.lastIdx(); jVar++)
97 {
98 if ( (rw.var(jVar).nbMiss()/Real(rw.var(jVar).size())) <= propMiss)
99 { jSize++;}
100 }
101 // resize
102 p_data->resize(rw.rows(), Range(rw.begin(), jSize));
103 for(int jVar = rw.begin(), jCol=rw.begin(); jVar<=rw.lastIdx(); jVar++)
104 {
105 if ( (rw.var(jVar).nbMiss()/Real(rw.var(jVar).size())) <= propMiss)
106 {
107 for (int i =p_data->beginRows(); i<= p_data->lastIdxRows(); ++i)
108 { p_data->elt(i, jCol) = rw(i,jVar);}
109 jCol++;
110 }
111 }
112 }
113};
114
115} // namespace hidden
116
133template<class Array, class Type = typename Array::Type>
135{
136 public:
142 CsvToArray( Rw const& rw, Real const& propMiss = 0.)
144 {}
148 CsvToArray( CsvToArray const& exporter)
149 : rw_(exporter.rw_), p_data_(exporter.p_data_), propMiss_(exporter.propMiss_)
150 {}
152 virtual ~CsvToArray() { if (p_data_) delete p_data_;}
154 CsvToArray* clone() const { return new CsvToArray(*this);}
156 inline Real const& propMiss() const { return propMiss_;}
160 inline void setPropMiss(Real const& propMiss) { propMiss_ = propMiss;}
162 bool run()
163 {
164 try
165 {
166 p_data_ = new Array;
168 return true;
169 }
170 catch (Exception const& e)
171 { msg_error_ = e.error();}
172 return false;
173 }
178 inline Array* p_data() { return p_data_;}
180 inline void release() { p_data_ =0;}
181
182 private:
184 Rw const& rw_;
186 Array* p_data_;
189};
190
191} // namespace STK
192
193#endif /* STK_CSVTOARRAY_H */
In this file we implement the final class CArray.
In this file we define the class TReadWriteCsv.
template two dimensional column (vertically) oriented Array.
A CArray is a two dimensional array with a continuous storage like a C-array.
Definition STK_CArray.h:130
The CsvToArray class allow to export the data of some Type stored in a TReadWriteCsv in an Array.
Array * p_data()
Accessor.
bool run()
run the export.
void release()
release the Array.
CsvToArray * clone() const
clone pattern
Rw const & rw_
A reference to the TReadWriteCsv we want to export.
TReadWriteCsv< Type > Rw
CsvToArray(CsvToArray const &exporter)
copy constructor.
CsvToArray(Rw const &rw, Real const &propMiss=0.)
Constructor.
Real propMiss_
proportion of missing values allowed
virtual ~CsvToArray()
destructor
Array * p_data_
A pointer on the 2D Container the class will create.
Real const & propMiss() const
void setPropMiss(Real const &propMiss)
Set the maximal proportion of missing value allowed.
Sdk class for all library Exceptions.
Abstract base class for all classes having a.
Definition STK_IRunner.h:65
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
std::basic_string< Char > String
STK fundamental type of a String.
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
TRange< UnknownSize > Range
Definition STK_Range.h:59
static void run(TReadWriteCsv< String > const &rw, Array2D< OtherType > *p_data, Real const propMiss)
static void run(TReadWriteCsv< Type > const &rw, Array2D< Type > *p_data, Real const propMiss)
static void run(TReadWriteCsv< Type > const &rw, CArray< Type > *p_data, Real const propMiss)
Implementation of CsvToArray.