-
Notifications
You must be signed in to change notification settings - Fork 1
/
stimulus.cc
91 lines (67 loc) · 1.93 KB
/
stimulus.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stimulus.h"
#include <csv.h>
#include <fs.h>
namespace colortilt {
std::vector<stimulus> stimulus::from_csv(const std::string &path) {
iris::csv_file fd(path);
std::vector<stimulus> stimuli;
bool is_header = true;
for(const auto &rec : fd) {
if (rec.is_empty() || rec.is_comment()) {
continue;
}
if (is_header) {
is_header = false;
continue;
}
if (rec.nfields() != 4) {
throw std::runtime_error("Invalid CSV data");
}
stimulus s;
s.phi_bg = rec.get_double(0);
s.phi_fg = rec.get_double(1);
s.size = rec.get_float(2);
s.side = rec.get_char(3);
stimuli.push_back(s);
}
return stimuli;
}
bool stimulus::to_csv(const std::vector<stimulus> &stimuli, std::ostream &stream) {
stream << " bg, fg, size, side";
for (stimulus s : stimuli) {
stream << std::endl;
stream.width(8);
stream << s.phi_bg << ", ";
stream.width(8);
stream << s.phi_fg << ", ";
stream.width(4);
stream << s.size << ", ";
stream.width(1);
stream << s.side;
}
return true;
}
std::vector<size_t> load_rnd_data(const fs::file &f) {
typedef iris::csv_iterator<std::string::const_iterator> csv_siterator;
std::string data = f.read_all();
bool is_header = true;
std::vector<size_t> rnddata;
for (auto iter = csv_siterator(data.cbegin(), data.cend(), ',');
iter != csv_siterator();
++iter) {
auto rec = *iter;
if (rec.is_comment() || rec.is_empty()) {
continue;
}
if (is_header) {
is_header = false;
continue;
}
if (rec.nfields() != 1) {
throw std::runtime_error("Invalid CSV data for rnd file");
}
rnddata.push_back(rec.get_size_t(0));
}
return rnddata;
}
}