STK++ 0.9.13
STK_ImportFromCsv.cpp
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 * Purpose: Implementation of the class ImportFromCsv.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 */
31
35
36#include <Sdk.h>
37
39
40namespace STK
41{
42
43// Default constructor for ImportCsv
49
50/* Copy Constructor.
51 * @param import the ImportFromCsv to import
52 **/
58
59// virtual dtor
62
63/* launch the conversion from the ReadWriteCsv to a Frame. */
65{
66 msg_error_ = _T("");
68 switch (typeImport_)
69 {
71 return asNumeric();
72 break;
74 return asOnlyNumeric();
75 break;
76 case Import::string_:
77 return asString();
78 break;
79 default:
80 return false;
81 break;
82 };
83 return false; // avoid warning
84}
85
86/* launch the conversion from the ReadWriteCsv to a DataFrame. */
88{
89 try
90 {
91 // for each field Try a numeric conversion
92 for (int j =import_.begin(); j <=import_.lastIdx(); j++)
93 {
94 Variable<Real>* pvReal = new Variable<Real>();
95 // test number of successful conversion
96 if (import<Real>(j, *pvReal)==import_.sizeRow(j))
97 { // if no failure add variable to the dataframe
98 p_dataFrame_->pushBackVariable(pvReal);
99 }
100 else
101 {
102 delete pvReal; // delete varReal
103 Variable<String>* pvString = import_.clone(j);
104 p_dataFrame_->pushBackVariable(pvString);
105 }
106 }
107 }
108 catch(const Exception& error)
109 {
110 msg_error_ = error.error();
111 msg_error_ += _T("\nIn ImportCsv::runNumeric()");
112 return false;
113 }
114 return true;
115}
116
119{
120 try
121 {
122 // for each field Try a numeric conversion
123 for (int j =import_.begin(); j <import_.end(); j++)
124 {
125 Variable<Real>* pvReal = new Variable<Real>;
126 // test number of successful conversion
127 if (import<Real>(j, *pvReal) == import_.sizeRow(j))
128 { // if no failure add variable to the dataframe
129 p_dataFrame_->pushBackVariable(pvReal);
130 }
131 else delete pvReal;
132 }
133 }
134 catch(const Exception& error)
135 {
136 msg_error_ += _T("Error in ImportCsv::runOnlyNumeric()\n");
137 msg_error_ += error.error();
138 return false;
139 }
140 return true;
141}
142
143/* launch the conversion from the ReadWriteCsv to a DataFrame. */
145{
146 try
147 {
148 // for each field Try a numeric conversion
149 for (int j =import_.begin(); j <=import_.lastIdx(); j++)
150 {
151 Variable<String>* pvString = import_.clone(j);
152 p_dataFrame_->pushBackVariable(pvString);
153 }
154 }
155 catch(const Exception& error)
156 {
157 msg_error_ += _T("Error in ImportCsv::runString()\n");
158 msg_error_ += error.error();
159 return false;
160 }
161 return true;
162}
163
164
165} // namespace STK
In this file we define the class ImportFromCsv.
#define _T(x)
Let x unmodified.
This file include all the other header files of the project Sdk.
DataFrame is a List of Variable with the same number of rows.
Base class for all Exceptions thrown by the STK++ library.
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
Import::TypeImport typeImport_
the type of import we want to perform.
DataFrame * p_dataFrame_
A ptr on the resulting DataFrame.
ImportFromCsv(ReadWriteCsv const &import, Import::TypeImport type=Import::numeric_)
Constructor.
bool asOnlyNumeric()
launch the conversion from the ReadWriteCsv to a DataFrame with only the successful numeric conversio...
bool asString()
launch the conversion from the ReadWriteCsv to a DataFrame as String.
ReadWriteCsv const & import_
a constant reference on the the original ReadWriteCsv.
bool asNumeric()
launch the conversion from the ReadWriteCsv to a DataFrame with a numeric conversion.
bool import(Array1D< int > const &indexes)
launch the importation of the ReadWriteCsv to the DataFrame.
virtual ~ImportFromCsv()
destructor.
bool run()
launch the importation of the ReadWriteCsv to the DataFrame.
Variable is an implementation of the Base class IVariable using The Array1D class for storing the dat...
TypeImport
define the type of import we want to perform.
@ numeric_
try to convert the columns in numeric values and let the others as string
@ only_numeric_
conserve only the columns with numeric values
@ string_
copy the columns
The namespace STK is the main domain space of the Statistical ToolKit project.
class TReadWriteCsv< String > ReadWriteCsv