Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
lib4neuro
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MolDyn
lib4neuro
Commits
b9b1f0e5
Commit
b9b1f0e5
authored
6 years ago
by
Martin Beseda
Browse files
Options
Downloads
Patches
Plain Diff
NEW: Added new class CSVReader.
parent
b71cfe28
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/CSVReader/CSVReader.cpp
+91
-0
91 additions, 0 deletions
src/CSVReader/CSVReader.cpp
src/CSVReader/CSVReader.h
+77
-0
77 additions, 0 deletions
src/CSVReader/CSVReader.h
with
168 additions
and
0 deletions
src/CSVReader/CSVReader.cpp
+
91
−
0
View file @
b9b1f0e5
...
...
@@ -2,4 +2,95 @@
// Created by martin on 14.11.18.
//
#include
<string>
#include
<fstream>
#include
<sstream>
#include
"CSVReader.h"
namespace
lib4neuro
{
CSVReader
::
CSVReader
(
std
::
string
file_path
,
std
::
string
delimiter
,
bool
ignore_first_line
)
{
this
->
file_path
=
file_path
;
this
->
delimiter
=
delimiter
;
this
->
ignore_first_line
=
ignore_first_line
;
this
->
header_included
=
ignore_first_line
;
this
->
data
=
new
std
::
vector
<
std
::
vector
<
std
::
string
>>
;
}
void
CSVReader
::
read
()
{
std
::
ifstream
ifs
(
this
->
file_path
);
std
::
string
line
;
if
(
this
->
ignore_first_line
)
{
std
::
getline
(
ifs
,
line
);
}
/* Read single line from the file */
while
(
std
::
getline
(
ifs
,
line
))
{
/* Ignore empty line */
if
(
line
==
""
)
{
continue
;
}
/* Separate elements of the line according to the delimiter */
size_t
last
=
0
;
size_t
next
=
0
;
std
::
vector
<
std
::
string
>
separated_line
;
while
((
next
=
line
.
find
(
this
->
delimiter
,
last
))
!=
std
::
string
::
npos
)
{
separated_line
.
emplace_back
(
line
.
substr
(
last
,
next
-
last
));
last
=
next
+
1
;
}
separated_line
.
emplace_back
(
line
.
substr
(
last
));
/* Store the elements from the line to the vector with data */
this
->
data
->
emplace_back
(
separated_line
);
}
ifs
.
close
();
}
std
::
vector
<
std
::
vector
<
std
::
string
>>*
CSVReader
::
get_data
()
{
return
this
->
data
;
}
void
CSVReader
::
print_data
()
{
for
(
auto
line
:
*
this
->
data
)
{
for
(
auto
e
:
line
)
{
std
::
cout
<<
e
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
}
}
DataSet
CSVReader
::
get_data_set
(
std
::
vector
<
unsigned
int
>*
input_col_indices
,
std
::
vector
<
unsigned
int
>*
output_col_indices
)
{
std
::
vector
<
std
::
pair
<
std
::
vector
<
double
>
,
std
::
vector
<
double
>>>
data_set_contents
;
int
i
=
0
;
for
(
auto
line
:
*
this
->
data
)
{
std
::
cout
<<
i
++
<<
std
::
endl
;
//TODO check for non-numerical (or empty) values in data
std
::
vector
<
double
>
input
;
for
(
auto
ind
:
*
input_col_indices
)
{
input
.
push_back
(
std
::
stod
(
line
.
at
(
ind
)));
}
std
::
cout
<<
"a"
<<
std
::
endl
;
std
::
vector
<
double
>
output
;
for
(
auto
ind
:
*
output_col_indices
)
{
output
.
emplace_back
(
std
::
stod
(
line
.
at
(
ind
)));
}
std
::
cout
<<
"b"
<<
std
::
endl
;
data_set_contents
.
push_back
(
std
::
make_pair
(
input
,
output
));
}
int
a
=
1
;
return
DataSet
(
&
data_set_contents
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/CSVReader/CSVReader.h
+
77
−
0
View file @
b9b1f0e5
...
...
@@ -5,4 +5,81 @@
#ifndef LIB4NEURO_CSVREADER_H
#define LIB4NEURO_CSVREADER_H
#include
<string>
#include
"../settings.h"
#include
"../DataSet/DataSet.h"
namespace
lib4neuro
{
/**
*
*/
class
CSVReader
{
//TODO make more efficient - possibly with external library?
private:
/**
*
*/
std
::
string
file_path
;
/**
*
*/
bool
header_included
;
/**
*
*/
std
::
string
delimiter
;
/**
*
*/
bool
ignore_first_line
;
/**
*
*/
std
::
vector
<
std
::
vector
<
std
::
string
>>*
data
;
public:
/**
*
* @param file_path
* @param delimiter
* @param ignore_first_line
*/
LIB4NEURO_API
CSVReader
(
std
::
string
file_path
,
std
::
string
delimiter
=
","
,
bool
ignore_first_line
=
false
);
/**
*
*/
LIB4NEURO_API
void
read
();
/**
*
* @return
*/
LIB4NEURO_API
std
::
vector
<
std
::
vector
<
std
::
string
>>*
get_data
();
/**
*
* @param input_col_indices
* @param output_col_indices
* @return
*/
LIB4NEURO_API
DataSet
get_data_set
(
std
::
vector
<
unsigned
int
>*
input_col_indices
,
std
::
vector
<
unsigned
int
>*
output_col_indices
);
/**
*
*/
LIB4NEURO_API
void
print_data
();
};
}
#endif //LIB4NEURO_CSVREADER_H
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment