STK++ 0.9.13
STK_ExportToCsv.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: DManager
27 * Purpose: Declaration of the class ImportExportToCsv.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 */
31
36#ifndef STK_EXPORTCSV_H
37#define STK_EXPORTCSV_H
38
39#include "STK_ReadWriteCsv.h"
40#include "STK_DataFrame.h"
41
42namespace STK
43{
44
45namespace Csv
46{
51 static const String DEFAULT_COLUMN_PREFIX = _T("Var");
52}
53
54
63{
64 public:
71 {
72 p_data_->setReserve(df.sizeRows());
73 p_data_->resize(df.sizeRows(), df.sizeCols());
74 // for each field Try a String conversion
75 for(int iVar = df.beginCols(), irw = p_data_->begin(); iVar<df.endCols(); iVar++, irw++)
76 { if (df.elt(iVar)) df.elt(iVar)->exportAsString(p_data_->var(irw));}
77 }
81 template<class Type>
83 {
84 p_data_->setReserve(rw.sizeRows());
85 p_data_->resize(rw.sizeRows(), rw.sizeCols());
86 p_data_->setWithNames(rw.withNames());
87 // for each field Try a String conversion
88 for(int iRw = rw.beginCols(), iData = p_data_->begin(); iRw<rw.endCols(); iRw++, iData++)
89 { rw.var(iRw).exportAsString(p_data_->var(iData));}
90 }
91
97 template < class Container>
99 , bool byCol = true
100 , String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
102 {
103#ifdef STK_DMANAGER_DEBUG
104 stk_cout << "Entering ExportToCsv( ITContainer1D<Container> const& A, byCol, prefix)\n";
105 stk_cout << "A.range()= " << A.range() << "\n";
106#endif
107 // add an empty string variable (an empty column)
108 p_data_->setWithNames(true);
109 if (byCol)
110 { // add to ReadWriteCsv a new variable
111 //p_data_->
112 p_data_->push_back(Variable<String>(A.range(), prefix));
113
114 for(int i = A.begin(); i<A.end(); i++)
115 { p_data_->back()[i] = typeToString(A.at(i));}
116 }
117 else // by row
118 {
119 for(int i = A.begin(); i<A.end(); i++)
120 { p_data_->push_back( Variable<String>(1, typeToString(A.at(i)), prefix+typeToString(i)) );}
121 }
122 }
123
129 template < class Container>
131 , bool byCol = true
132 , String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
134 {
135 // add an empty string variable (an empty column)
136 p_data_->setWithNames(true);
137 if (byCol)
138 {
139 p_data_->push_back(Variable<String>(A.range(), prefix));
140
141 for(int i = A.begin(); i<A.end(); i++)
142 { p_data_->back()[i] = typeToString(A.at(i));}
143 }
144 else
145 {
146 for(int i = A.begin(); i<A.end(); i++)
147 {
149 p_data_->back().front() = typeToString(A.at(i));
150 }
151 }
152 }
158 template < class Container>
160 , bool byCol = false
161 , String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
163 {
164 // add an empty string variable (an empty column)
165 p_data_->setWithNames(true);
166 if (byCol)
167 {
168 p_data_->push_back(Variable<String>(A.range(), prefix));
169
170 for(int i = A.begin(); i<A.end(); i++)
171 { p_data_->back()[i] = typeToString(A.at(i));}
172 }
173 else
174 {
175 for(int i = A.begin(); i<A.end(); i++)
176 {
178 p_data_->back().front() = typeToString(A.at(i));
179 }
180 }
181 }
186 template <class Container >
188 , String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
190 {
191 p_data_->setWithNames(true);
192 for(int iVar = A.beginCols(); iVar<A.endCols(); iVar++)
193 {
194 // add an empty string variable (an empty column)
195 p_data_->push_back(Variable<String>(A.rows(), prefix));
196 for (int iRow=A.beginRows(); iRow<A.endRows(); iRow++)
197 { p_data_->back()[iRow] = typeToString(A.at(iRow,iVar));}
198 }
199 }
200
204 inline ~ExportToCsv(){ if (p_data_) delete p_data_;}
209 template < class Container>
210 void append( ITContainer<Container, Arrays::vector_> const& A, String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
211 {
212 // add an empty string variable
213 p_data_->push_back(Variable<String>(A.range(), prefix));
214 // add strings to the String variable
215 for(int i = A.begin(); i< A.end(); i++)
216 { p_data_->back()[i] = typeToString(A.elt(i));}
217 }
222 template < class Container>
223 void append( ITContainer<Container, Arrays::point_> const& A, String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
224 {
225 // add an empty string variable
226 p_data_->push_back(Variable<String>(A.range(), prefix));
227 // add strings to the String variable
228 for(int i = A.begin(); i< A.end(); i++)
229 { p_data_->back()[i] = typeToString(A.elt(i));}
230 }
235 template < class Container>
236 void append( ITContainer<Container, Arrays::array2D_> const& A, String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
237 {
238 // for each field try a String conversion
239 for(int iVar = A.beginCols(), iNum=1; iVar<A.endCols(); iVar++, iNum++)
240 {
241 // add an empty string variable (an empty column)
242 p_data_->push_back(Variable<String>(A.rows(), prefix+typeToString(iNum)));
243 for (int iRow=A.beginRows(); iRow<A.endRows(); iRow++)
244 { p_data_->back()[iRow] = typeToString(A.elt(iRow,iVar));}
245 }
246 }
251 template < class Container>
252 void append( ITContainer<Container, Arrays::square_> const& A, String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
253 {
254 // for each field try a String conversion
255 for(int iVar = A.beginCols(), iNum=1; iVar<A.endCols(); iVar++, iNum++)
256 {
257 // add an empty string variable (an empty column)
258 p_data_->push_back(Variable<String>(A.rows(), prefix+typeToString(iNum)));
259 for (int iRow=A.beginRows(); iRow<A.endRows(); iRow++)
260 { p_data_->back()[iRow] = typeToString(A.elt(iRow,iVar));}
261 }
262 }
267 template < class TYPE>
268 void appendData( TYPE const& A, String const& prefix=Csv::DEFAULT_COLUMN_PREFIX)
269 {
270 // add an empty string variable
271 p_data_->push_back(Variable<String>( prefix + typeToString(p_data_->lastIdx())) );
272 // add strings to the String variable
273 p_data_->back().push_back(typeToString(A));
274 }
279 inline void setColumnsNames(String const& prefix = Csv::DEFAULT_COLUMN_PREFIX)
280 {
281 for(int i = p_data_->begin(); i<p_data_->end(); i++)
282 { p_data_->setName(i, prefix + typeToString(i)) ;}
283 }
284
286 inline ReadWriteCsv* const p_readWriteCsv() const { return p_data_;}
288 inline void release() { p_data_ =0;}
289
290 protected:
293};
294
295} // namespace STK
296
297#endif /*STK_EXPORTCSV_H*/
In this file we declare the DataFrame class.
In this file we define the class TReadWriteCsv.
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
DataFrame is a List of Variable with the same number of rows.
int sizeCols() const
int beginCols() const
IVariable *& elt(int const &i)
access to an element.
int sizeRows() const
int endCols() const
Export data to a Csv.
void setColumnsNames(String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Set a name to each column of the ReadWriteCsv using the form prefix + number.
void append(ITContainer< Container, Arrays::vector_ > const &A, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Append a vector.
void release()
release the ReadWriteCsv.
ExportToCsv(ITContainer< Container > const &A, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Instantiates an instance of ExportToCvs with a general array.
~ExportToCsv()
destructor.
ReadWriteCsv *const p_readWriteCsv() const
Accesor.
ExportToCsv()
Default constructor.
ExportToCsv(ITContainer1D< Container > const &A, bool byCol=true, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Instantiates an instance of ExportToCvs with an Array1D, a list1D, etc....
ExportToCsv(TReadWriteCsv< Type > const &rw)
Constructor : create an instance of ExportToCvs with a ReadWriteCsv.
ExportToCsv(ITContainer< Container, Arrays::vector_ > const &A, bool byCol=true, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Instantiates an instance of ExportToCvs with a vector.
void append(ITContainer< Container, Arrays::square_ > const &A, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Append a 2D container.
ReadWriteCsv * p_data_
ptr on the ReadWriteCsv containing the data.
void append(ITContainer< Container, Arrays::array2D_ > const &A, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Append a 2D container.
ExportToCsv(DataFrame const &df)
Constructor : create an instance of ExportToCvs with a DataFrame.
void append(ITContainer< Container, Arrays::point_ > const &A, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Append a point (as a column vector).
void appendData(TYPE const &A, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Append a value.
ExportToCsv(ITContainer< Container, Arrays::point_ > const &A, bool byCol=false, String const &prefix=Csv::DEFAULT_COLUMN_PREFIX)
Instantiates an instance of ExportToCvs with a point.
virtual void exportAsString(VariableString &V) const =0
Overwrite the variable V by converting the data into strings.
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
std::basic_string< Char > String
STK fundamental type of a String.
The namespace STK is the main domain space of the Statistical ToolKit project.
class TReadWriteCsv< String > ReadWriteCsv