forked from makkorjamal/PLSduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPLSduinoIO.cpp
More file actions
134 lines (113 loc) · 2.5 KB
/
Copy pathPLSduinoIO.cpp
File metadata and controls
134 lines (113 loc) · 2.5 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
PLSduinoIO.cpp - CSV loading helpers for PLSduino.
*/
#include "PLSduinoIO.h"
#include <stdlib.h>
namespace
{
bool parseLine(
const String &rawLine,
int expectedColumns,
MatrixXf &row )
{
String line = rawLine;
line.trim();
if (line.length() == 0) {
return false;
}
int columnCount = 1;
for (int i = 0; i < line.length(); ++i) {
if (line.charAt(i) == ',') {
columnCount++;
}
}
if (expectedColumns > 0 && columnCount != expectedColumns) {
Serial.println("CSV parse error: inconsistent column count");
return false;
}
row.resize(1, columnCount);
int start = 0;
for (int col = 0; col < columnCount; ++col) {
int end = line.indexOf(',', start);
if (end < 0) {
end = line.length();
}
String token = line.substring(start, end);
token.trim();
if (token.length() == 0) {
Serial.println("CSV parse error: empty value");
return false;
}
const char *buffer = token.c_str();
char *endPtr = NULL;
float value = strtof(buffer, &endPtr);
if (endPtr == buffer || *endPtr != '\0') {
Serial.print("CSV parse error: invalid float '");
Serial.print(token);
Serial.println("'");
return false;
}
row(0, col) = value;
start = end + 1;
}
return true;
}
}
bool PLSIO::loadMatrixFromCSV(
Stream &stream,
MatrixXf &matrix )
{
matrix.resize(0, 0);
int rowCount = 0;
int expectedColumns = -1;
while (stream.available()) {
String line = stream.readStringUntil('\n');
String trimmed = line;
trimmed.trim();
if (trimmed.length() == 0) {
continue;
}
MatrixXf row;
if (!parseLine(trimmed, expectedColumns, row)) {
matrix.resize(0, 0);
return false;
}
if (expectedColumns < 0) {
expectedColumns = row.cols();
matrix = row;
rowCount = 1;
continue;
}
matrix.conservativeResize(rowCount + 1, expectedColumns);
matrix.row(rowCount) = row;
rowCount++;
}
if (rowCount == 0) {
Serial.println("CSV parse error: no data found");
return false;
}
return true;
}
bool PLSIO::loadModelFromCSV(
Stream &bStream,
Stream &meanXStream,
Stream &meanYStream,
PLS &pls )
{
MatrixXf B;
MatrixXf meanX;
MatrixXf meanY;
if (!loadMatrixFromCSV(bStream, B)) {
Serial.println("Failed to load B matrix");
return false;
}
if (!loadMatrixFromCSV(meanXStream, meanX)) {
Serial.println("Failed to load meanX matrix");
return false;
}
if (!loadMatrixFromCSV(meanYStream, meanY)) {
Serial.println("Failed to load meanY matrix");
return false;
}
return pls.setModel(B, meanX, meanY);
}