-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathml_model.cpp
137 lines (118 loc) · 3.4 KB
/
ml_model.cpp
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
135
136
137
#include <cstdarg>
#include <stdlib.h>
#include "brainflow_constants.h"
#include "json.hpp"
#include "ml_model.h"
#include "ml_module.h"
using json = nlohmann::json;
void MLModel::release_all ()
{
int res = ::release_all ();
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
throw BrainFlowException ("failed to release classifiers", res);
}
}
std::string params_to_string (struct BrainFlowModelParams params)
{
json j;
j["metric"] = params.metric;
j["classifier"] = params.classifier;
j["file"] = params.file;
j["other_info"] = params.other_info;
j["output_name"] = params.output_name;
j["max_array_size"] = params.max_array_size;
std::string post_str = j.dump ();
return post_str;
}
MLModel::MLModel (struct BrainFlowModelParams model_params) : params (model_params)
{
serialized_params = params_to_string (model_params);
}
void MLModel::prepare ()
{
int res = ::prepare (serialized_params.c_str ());
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
throw BrainFlowException ("failed to prepare classifier", res);
}
}
std::vector<double> MLModel::predict (double *data, int data_len)
{
double *output = new double[params.max_array_size];
int size = 0;
int res = ::predict (data, data_len, output, &size, serialized_params.c_str ());
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
delete[] output;
throw BrainFlowException ("failed to predict", res);
}
std::vector<double> result (size);
std::copy (output, output + size, result.begin ());
delete[] output;
return result;
}
void MLModel::release ()
{
int res = ::release (serialized_params.c_str ());
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
throw BrainFlowException ("failed to release classifier", res);
}
}
/////////////////////////////////////////
//////////// logging methods ////////////
/////////////////////////////////////////
void MLModel::set_log_file (std::string log_file)
{
int res = set_log_file_ml_module (log_file.c_str ());
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
throw BrainFlowException ("failed to set log file", res);
}
}
void MLModel::set_log_level (int log_level)
{
int res = set_log_level_ml_module (log_level);
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
throw BrainFlowException ("failed to set log level", res);
}
}
void MLModel::log_message (int log_level, const char *format, ...)
{
char buffer[1024];
va_list ap;
va_start (ap, format);
vsnprintf (buffer, 1024, format, ap);
va_end (ap);
int res = log_message_ml_module (log_level, buffer);
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
throw BrainFlowException ("failed to write log message", res);
}
}
void MLModel::enable_ml_logger ()
{
MLModel::set_log_level ((int)LogLevels::LEVEL_INFO);
}
void MLModel::disable_ml_logger ()
{
MLModel::set_log_level ((int)LogLevels::LEVEL_OFF);
}
void MLModel::enable_dev_ml_logger ()
{
MLModel::set_log_level ((int)LogLevels::LEVEL_TRACE);
}
std::string MLModel::get_version ()
{
char version[64];
int string_len = 0;
int res = ::get_version_ml_module (version, &string_len, 64);
if (res != (int)BrainFlowExitCodes::STATUS_OK)
{
throw BrainFlowException ("failed to get board info", res);
}
std::string verion_str (version, string_len);
return verion_str;
}