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
36#include <Sdk.h>
37
38#include "../include/STK_ImportFromCsv.h"
39
40namespace STK
41{
42
43// Default constructor for ImportCsv
45 : p_dataFrame_(0)
46 , typeImport_(type)
47 , import_(import)
48{}
49
50/* Copy Constructor.
51 * @param import the ImportFromCsv to import
52 **/
54 : p_dataFrame_(import.p_dataFrame_)
55 , typeImport_(import.typeImport_)
56 , import_(import.import_)
57{}
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 {
95 // test number of successful conversion
96 if (import<Real>(j, *pvReal)==import_.sizeRow(j))
97 { // if no failure add variable to the dataframe
99 }
100 else
101 {
102 delete pvReal; // delete varReal
105 }
106 }
107 }
108 catch(const Exception& error)
109 {
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 {
126 // test number of successful conversion
127 if (import<Real>(j, *pvReal) == import_.sizeRow(j))
128 { // if no failure add variable to the dataframe
130 }
131 else delete pvReal;
132 }
133 }
134 catch(const Exception& error)
135 {
136 msg_error_ += _T("Error in ImportCsv::runOnlyNumeric()\n");
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 {
153 }
154 }
155 catch(const Exception& error)
156 {
157 msg_error_ += _T("Error in ImportCsv::runString()\n");
159 return false;
160 }
161 return true;
162}
163
164
165} // namespace STK
#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.
void pushBackVariable(IVariable *const &V)
Append a DataFrame back.
Sdk class for all library Exceptions.
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 data from a Csv File in a DataFrame.
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.
virtual ~ImportFromCsv()
destructor.
bool run()
launch the importation of the ReadWriteCsv to the DataFrame.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
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