Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_accessor_splitting_memCSV.hpp
Go to the documentation of this file.
1#ifndef FSTDATAACCESSORSPLITTINGMEMCSV_H
2#define FSTDATAACCESSORSPLITTINGMEMCSV_H
3 /*
25=========================================================================
26Copyright and license:
27 * FST4 software (with the exception of any bundled or externally linked
28 third-party components) is Copyright (C) 2000-present Institute of
29 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
30 * FST4 is free software: you can redistribute it and/or modify it under
31 the terms of the GNU Affero General Public License as published by the
32 Free Software Foundation, version 3. The full text of the license is in
33 the file LICENSE in the distribution root and at
34 https://www.gnu.org/licenses/agpl-3.0.html.
35 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
36 WARRANTY; without even the implied warranty of MERCHANTABILITY or
37 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
38 License for more details.
39 * Closed-source use without the AGPL's copyleft obligations requires a
40 commercial license from UTIA; in case of interest write to
41 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
42 options, the contributor license agreement that every contribution
43 requires, and the contacts (technical questions and pull requests:
44 somol@utia.cas.cz, with "FST" in the subject line).
45========================================================================== */
46
47#include <memory>
48#include <iostream>
49#include <sstream>
50#include <vector>
51
52#include "error.hpp"
53#include "global.hpp"
55#include "data_file_CSV.hpp"
56
57namespace FST {
58
60template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
61class Data_Accessor_Splitting_MemCSV : public Clonable_Impl<Data_Accessor_Splitting_MemCSV<DATATYPE,IDXTYPE,INTERVALCONTAINER>,Data_Accessor_Splitting_Mem<DATATYPE,IDXTYPE,INTERVALCONTAINER>,CLONE_INHERIT,CLONE_WEAK_SHARING,CLONE_INHERIT>
62{
63 public:
70 typedef std::shared_ptr<Data_Scaler<DATATYPE> > PScaler;
71 typedef typename DASM::PSplitters PSplitters;
73 Data_Accessor_Splitting_MemCSV(const std::string _filename, const Data_File_CSV_Options &_opts, const PSplitters _dsp, const PScaler _dsc) : parent_impl(_filename, _dsp, _dsc), opts(_opts) {notify("Data_Accessor_Splitting_MemCSV constructor.");}
74 virtual ~Data_Accessor_Splitting_MemCSV(){notify("Data_Accessor_Splitting_MemCSV destructor.");}
75
76 virtual std::ostream& print(std::ostream& os) const override;
77 protected:
78 friend parent_impl;
83 protected:
84 std::unique_ptr<Data_File_CSV> CSVFile;
86
87 unsigned int clsCount;
88 unsigned int featureCount;
89 std::shared_ptr<IDXTYPE> clsSize;
90
91 virtual void initial_data_read() override ;
92 virtual void initial_file_prepare() override;
93
94 public:
95 virtual unsigned int file_getNoOfClasses() const override {return clsCount; }
96 virtual unsigned int file_getNoOfFeatures() const override {return featureCount; }
97 virtual IDXTYPE file_getClassSize(unsigned int cls) const override {return clsSize.get()[cls]; }
98};
99
100template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
102{
103 if(Clonable::is_sharing_clone()) throw fst_error("Data_Accessor_Splitting_MemCSV()::initial_file_prepare() called from shared_clone instance.");
104 CSVFile.reset(new Data_File_CSV(this->filename, opts));
105 CSVFile->prepareFile();
106 CSVFile->sortRecordsByClass();
107
108 clsCount = CSVFile->getNoOfClasses();
109 featureCount = CSVFile->getNoOfFeatures();
110 clsSize.reset(new IDXTYPE[clsCount], std::default_delete<IDXTYPE[]>());
111 for (unsigned int i=0;i<clsCount; i++) clsSize.get()[i] = CSVFile->getClassSize(i);
112}
113
114template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
116{
117 if(Clonable::is_sharing_clone()) throw fst_error("Data_Accessor_Splitting_MemCSV()::initial_data_read() called from shared_clone instance.");
118 {std::ostringstream sos; sos << "Reading data to memory..."; syncout::print(std::cout,sos);}
119 IDXTYPE idx=0;
120 for(unsigned int p=0;p<CSVFile->getTotalSize();p++)
121 {
122 for(unsigned int f=0;f<CSVFile->getNoOfFeatures();f++) {
123 this->data.get()[idx++]=(DATATYPE) CSVFile->getFeatureValue(p,f);
124 }
125 }
126 CSVFile.reset();
127 {std::ostringstream sos; sos << "done."<<std::endl; syncout::print(std::cout,sos);}
128}
129
130
131
132template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
134{
135 DASM::print(os);
136 os << std::endl << "Data_Accessor_Splitting_MemCSV()";
137 return os;
138}
139
140} // namespace
141#endif // FSTDATAACCESSORSPLITTINGMEMCSV_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
bool is_sharing_clone() const
true if this instance is BOTH a clone and sharing – i.e. specifically a sharing_clone() result,...
Definition clonable.hpp:127
Implements data access to data cached entirely in memory, read once from a CSV (delimited text) file;...
Definition data_accessor_splitting_memCSV.hpp:62
std::shared_ptr< Data_Scaler< DATATYPE > > PScaler
Definition data_accessor_splitting_memCSV.hpp:70
std::unique_ptr< Data_File_CSV > CSVFile
Definition data_accessor_splitting_memCSV.hpp:84
virtual void initial_data_read() override
copies the reader's values into the base class's data array, then releases the reader;
Definition data_accessor_splitting_memCSV.hpp:115
Data_Accessor_Splitting_Mem< DATATYPE, IDXTYPE, INTERVALLER > DASM
Definition data_accessor_splitting_memCSV.hpp:64
std::shared_ptr< IDXTYPE > clsSize
Definition data_accessor_splitting_memCSV.hpp:89
Clonable_Impl< Data_Accessor_Splitting_MemCSV, DASM, CLONE_INHERIT, CLONE_WEAK_SHARING, CLONE_INHERIT > parent_impl
Definition data_accessor_splitting_memCSV.hpp:69
Data_Accessor_Splitting_MemCSV(const Data_Accessor_Splitting_MemCSV &damt, int x)
Weak (referencing) copy-constructor used by sharing_clone(); transfers opts and the cached header cou...
Definition data_accessor_splitting_memCSV.hpp:82
Data_File_CSV_Options opts
Definition data_accessor_splitting_memCSV.hpp:85
virtual void initial_file_prepare() override
opens CSVFile, reads and class-sorts it, and caches clsCount/featureCount/clsSize; throws fst_error i...
Definition data_accessor_splitting_memCSV.hpp:101
unsigned int clsCount
Definition data_accessor_splitting_memCSV.hpp:87
virtual unsigned int file_getNoOfClasses() const override
clsCount, as required by Data_Accessor_Splitting_Mem
Definition data_accessor_splitting_memCSV.hpp:95
virtual IDXTYPE file_getClassSize(unsigned int cls) const override
clsSize[cls], as required by Data_Accessor_Splitting_Mem
Definition data_accessor_splitting_memCSV.hpp:97
virtual unsigned int file_getNoOfFeatures() const override
featureCount, as required by Data_Accessor_Splitting_Mem
Definition data_accessor_splitting_memCSV.hpp:96
Data_Accessor_Splitting_MemCSV(const std::string _filename, const Data_File_CSV_Options &_opts, const PSplitters _dsp, const PScaler _dsc)
Constructs the accessor for the CSV/TSV file _filename under dialect/class-column/missing-value optio...
Definition data_accessor_splitting_memCSV.hpp:73
virtual std::ostream & print(std::ostream &os) const override
writes the splitter stack's print() output (or "Data_Accessor_Splitting()" if the stack is empty) to ...
Definition data_accessor_splitting_memCSV.hpp:133
DASM::PSplitters PSplitters
Definition data_accessor_splitting_memCSV.hpp:71
unsigned int featureCount
Definition data_accessor_splitting_memCSV.hpp:88
Implements data access to data cached entirely in memory, concrete file type support is delegated to ...
Definition data_accessor_splitting_mem.hpp:102
DAS::PSplitters PSplitters
Definition data_accessor_splitting_mem.hpp:110
virtual std::ostream & print(std::ostream &os) const override
writes the splitter stack's print() output (or "Data_Accessor_Splitting()" if the stack is empty) to ...
Definition data_accessor_splitting_mem.hpp:412
std::shared_ptr< DATATYPE > data
Definition data_accessor_splitting_mem.hpp:199
std::string filename
Definition data_accessor_splitting_mem.hpp:196
CSV (delimited text) data format filter — the simplest spreadsheet-to-FST4 path.
Definition data_file_CSV.hpp:91
FST4 specific error passing class.
Definition error.hpp:61
static void print(std::ostream &os, std::ostringstream &sos)
Writes sos's buffered content to os, serializing concurrent writers under FST_THREADED via a shared m...
Definition global.hpp:86
Implements data access to data cached entirely in memory, concrete file type support is delegated to ...
Enables data accessor objects to read CSV (delimited text, e.g. Excel export) data files.
FST4 specific error passing class.
Global definitions.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
CSV loader options.
Definition data_file_CSV.hpp:63
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.