STK++ 0.9.13
STK::ImportFromCsv Class Reference

import data from a Csv File in a DataFrame. More...

#include <STK_ImportFromCsv.h>

Inheritance diagram for STK::ImportFromCsv:
Inheritance graph

Public Member Functions

 ImportFromCsv (ReadWriteCsv const &import, Import::TypeImport type=Import::numeric_)
 Constructor.
 
 ImportFromCsv (ImportFromCsv const &import)
 Copy Constructor.
 
virtual ~ImportFromCsv ()
 destructor.
 
ImportFromCsv * clone () const
 clone pattern.
 
void setTypeImport (Import::TypeImport typeImport)
 set the type of import the end user want
 
DataFrame const * dataFrame () const
 
void release ()
 Release the dataFrame.
 
bool run ()
 launch the importation of the ReadWriteCsv to the DataFrame.
 
template<class Other >
bool import (Array1D< int > const &indexes)
 launch the importation of the ReadWriteCsv to the DataFrame.
 
- Public Member Functions inherited from STK::IRunnerBase
String const & error () const
 get the last error message.
 

Protected Member Functions

template<class Other >
int import (int const &iCol, Variable< Other > &var)
 convert a column of the csv in a variable of type Type
 
- Protected Member Functions inherited from STK::IRunnerBase
 IRunnerBase ()
 default constructor
 
 IRunnerBase (IRunnerBase const &runner)
 copy constructor
 
virtual ~IRunnerBase ()
 destructor
 
virtual void update ()
 update the runner.
 

Protected Attributes

DataFrame * p_dataFrame_
 A ptr on the resulting DataFrame.
 
Import::TypeImport typeImport_
 the type of import we want to perform.
 
- Protected Attributes inherited from STK::IRunnerBase
String msg_error_
 String with the last error message.
 
bool hasRun_
 true if run has been used, false otherwise
 

Private Member Functions

bool asNumeric ()
 launch the conversion from the ReadWriteCsv to a DataFrame with a numeric conversion.
 
bool asOnlyNumeric ()
 launch the conversion from the ReadWriteCsv to a DataFrame with only the successful numeric conversion.
 
bool asString ()
 launch the conversion from the ReadWriteCsv to a DataFrame as String.
 

Private Attributes

ReadWriteCsv const & import_
 a constant reference on the the original ReadWriteCsv.
 

Detailed Description

import data from a Csv File in a DataFrame.

A ImportFromCsv object create a DataFrame from a given ReadWriteCsv object. It will try to convert the given ReadWriteCsv to the predefined type given by the user.

Definition at line 56 of file STK_ImportFromCsv.h.

Constructor & Destructor Documentation

◆ ImportFromCsv() [1/2]

STK::ImportFromCsv::ImportFromCsv ( ReadWriteCsv const &  import,
Import::TypeImport  type = Import::numeric_ 
)

Constructor.

Instantiates an instance of ImportFromCvs with the readWriteCsv to import.

Parameters
importthe ReadWriteCsv to import
typethe kind of import we want to perform

Definition at line 44 of file STK_ImportFromCsv.cpp.

45 : p_dataFrame_(0)
46 , typeImport_(type)
47 , import_(import)
48{}
Import::TypeImport typeImport_
the type of import we want to perform.
DataFrame * p_dataFrame_
A ptr on the resulting DataFrame.
ReadWriteCsv const & import_
a constant reference on the the original ReadWriteCsv.

◆ ImportFromCsv() [2/2]

STK::ImportFromCsv::ImportFromCsv ( ImportFromCsv const &  import)

Copy Constructor.

Parameters
importthe ImportFromCsv to import

Definition at line 53 of file STK_ImportFromCsv.cpp.

55 , typeImport_(import.typeImport_)
56 , import_(import.import_)
57{}

◆ ~ImportFromCsv()

STK::ImportFromCsv::~ImportFromCsv ( )
virtual

destructor.

Definition at line 60 of file STK_ImportFromCsv.cpp.

61{ if (p_dataFrame_) delete p_dataFrame_; }

References p_dataFrame_.

Member Function Documentation

◆ asNumeric()

bool STK::ImportFromCsv::asNumeric ( )
private

launch the conversion from the ReadWriteCsv to a DataFrame with a numeric conversion.

Definition at line 87 of file STK_ImportFromCsv.cpp.

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
99 }
100 else
101 {
102 delete pvReal; // delete varReal
103 Variable<String>* pvString = import_.clone(j);
105 }
106 }
107 }
108 catch(const Exception& error)
109 {
111 msg_error_ += _T("\nIn ImportCsv::runNumeric()");
112 return false;
113 }
114 return true;
115}
#define _T(x)
Let x unmodified.
void pushBackVariable(IVariable *const &V)
Append a DataFrame back.
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

References _T, STK::IRunnerBase::error(), import_, STK::IRunnerBase::msg_error_, p_dataFrame_, and STK::DataFrame::pushBackVariable().

Referenced by run().

◆ asOnlyNumeric()

bool STK::ImportFromCsv::asOnlyNumeric ( )
private

launch the conversion from the ReadWriteCsv to a DataFrame with only the successful numeric conversion.

launch the conversion from the ReadWriteCsv to a Frame.

Definition at line 118 of file STK_ImportFromCsv.cpp.

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
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}

References _T, STK::IRunnerBase::error(), import_, STK::IRunnerBase::msg_error_, p_dataFrame_, and STK::DataFrame::pushBackVariable().

Referenced by run().

◆ asString()

bool STK::ImportFromCsv::asString ( )
private

launch the conversion from the ReadWriteCsv to a DataFrame as String.

Definition at line 144 of file STK_ImportFromCsv.cpp.

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);
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}

References _T, STK::IRunnerBase::error(), import_, STK::IRunnerBase::msg_error_, p_dataFrame_, and STK::DataFrame::pushBackVariable().

Referenced by run().

◆ clone()

ImportFromCsv * STK::ImportFromCsv::clone ( ) const
inline

clone pattern.

Definition at line 72 of file STK_ImportFromCsv.h.

72{ return new ImportFromCsv(*this);}
ImportFromCsv(ReadWriteCsv const &import, Import::TypeImport type=Import::numeric_)
Constructor.

◆ dataFrame()

DataFrame const * STK::ImportFromCsv::dataFrame ( ) const
inline
Returns
a ptr on the the data read.

Definition at line 77 of file STK_ImportFromCsv.h.

77{ return p_dataFrame_;}

References p_dataFrame_.

◆ import() [1/2]

template<class Other >
bool STK::ImportFromCsv::import ( Array1D< int > const &  indexes)
inline

launch the importation of the ReadWriteCsv to the DataFrame.

Returns
true if the importation is successful, false otherwise

Definition at line 94 of file STK_ImportFromCsv.h.

95 { return false;}

◆ import() [2/2]

template<class Other >
int STK::ImportFromCsv::import ( int const &  iCol,
Variable< Other > &  var 
)
inlineprotected

convert a column of the csv in a variable of type Type

Parameters
iColthe column to convert
varthe variable storing the result
Returns
true if the conversion is successful, false otherwise

Definition at line 108 of file STK_ImportFromCsv.h.

109 { return var.importFromString(import_[iCol]);}

References import_.

◆ release()

void STK::ImportFromCsv::release ( )
inline

Release the dataFrame.

Definition at line 79 of file STK_ImportFromCsv.h.

79{ p_dataFrame_ = 0;}

References p_dataFrame_.

◆ run()

bool STK::ImportFromCsv::run ( )
virtual

launch the importation of the ReadWriteCsv to the DataFrame.

The call to this method will be without effect if the TypeImport is directed_. In this case use the

bool import( Array1D<int> const& indexes)
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...

method.

Returns
true if the importation is successful, false otherwise

Implements STK::IRunnerBase.

Definition at line 64 of file STK_ImportFromCsv.cpp.

65{
66 msg_error_ = _T("");
67 if (!p_dataFrame_) p_dataFrame_ = new DataFrame;
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}
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.
bool asNumeric()
launch the conversion from the ReadWriteCsv to a DataFrame with a numeric conversion.
@ 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

References _T, asNumeric(), asOnlyNumeric(), asString(), STK::IRunnerBase::msg_error_, STK::Import::numeric_, STK::Import::only_numeric_, p_dataFrame_, STK::Import::string_, and typeImport_.

◆ setTypeImport()

void STK::ImportFromCsv::setTypeImport ( Import::TypeImport  typeImport)
inline

set the type of import the end user want

Definition at line 74 of file STK_ImportFromCsv.h.

74{ typeImport_ = typeImport;}

References typeImport_.

Member Data Documentation

◆ import_

ReadWriteCsv const& STK::ImportFromCsv::import_
private

a constant reference on the the original ReadWriteCsv.

Definition at line 113 of file STK_ImportFromCsv.h.

Referenced by asNumeric(), asOnlyNumeric(), asString(), and import().

◆ p_dataFrame_

DataFrame* STK::ImportFromCsv::p_dataFrame_
protected

A ptr on the resulting DataFrame.

Definition at line 99 of file STK_ImportFromCsv.h.

Referenced by asNumeric(), asOnlyNumeric(), asString(), dataFrame(), release(), run(), and ~ImportFromCsv().

◆ typeImport_

Import::TypeImport STK::ImportFromCsv::typeImport_
protected

the type of import we want to perform.

Definition at line 101 of file STK_ImportFromCsv.h.

Referenced by run(), and setTypeImport().


The documentation for this class was generated from the following files: