Skip to content

Commit

Permalink
refactor models (#636)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
  • Loading branch information
Andrey1994 committed Jun 5, 2023
1 parent aaa651b commit 0754d73
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cpp_package/src/inc/data_filter.h
Expand Up @@ -193,7 +193,8 @@ class DataFilter
* @return oxygen level
*/
static double get_oxygen_level (double *ppg_ir, double *ppg_red, int data_len,
int sampling_rate, double coef1 = 0.0, double coef2 = -37.663, double coef3 = 114.91);
int sampling_rate, double coef1 = 1.5958422, double coef2 = -34.6596622,
double coef3 = 112.6898759);
/**
* calculate heart rate
* @param ppg_ir input 1d array
Expand Down
2 changes: 1 addition & 1 deletion csharp_package/brainflow/brainflow/data_filter.cs
Expand Up @@ -306,7 +306,7 @@ public static double get_railed_percentage (double[] data, int gain)
/// <param name="coef2">approximation coef</param>
/// /// <param name="coef3">intercept for approximation</param>
/// <returns>stddev</returns>
public static double get_oxygen_level (double[] ppg_ir, double[] ppg_red, int sampling_rate, double coef1 = 0.0, double coef2 = -37.663, double coef3 = 114.91)
public static double get_oxygen_level (double[] ppg_ir, double[] ppg_red, int sampling_rate, double coef1 = 1.5958422, double coef2 = -34.6596622, double coef3 = 112.6898759)
{
if (ppg_ir.Length != ppg_red.Length)
{
Expand Down
Expand Up @@ -251,7 +251,7 @@ public static double get_oxygen_level (double[] ppg_ir, double[] ppg_red, int sa
*/
public static double get_oxygen_level (double[] ppg_ir, double[] ppg_red, int sampling_rate) throws BrainFlowError
{
return get_oxygen_level (ppg_ir, ppg_red, sampling_rate, 0.0, -37.663, 114.91);
return get_oxygen_level (ppg_ir, ppg_red, sampling_rate, 1.5958422, -34.6596622, 112.6898759);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion julia_package/brainflow/src/data_filter.jl
Expand Up @@ -253,7 +253,7 @@ end
return output[1]
end

@brainflow_rethrow function get_oxygen_level(ppg_ir, ppg_red, sampling_rate::Integer, coef1=0.0, coef2=-37.663, coef3=114.91)
@brainflow_rethrow function get_oxygen_level(ppg_ir, ppg_red, sampling_rate::Integer, coef1=1.5958422, coef2=-34.6596622, coef3=112.6898759)
if length(ppg_ir) != length(ppg_red)
throw(BrainFlowError(string("invalid size", INVALID_ARGUMENTS_ERROR), Integer(INVALID_ARGUMENTS_ERROR)))
end
Expand Down
2 changes: 1 addition & 1 deletion python_package/brainflow/data_filter.py
Expand Up @@ -771,7 +771,7 @@ def get_railed_percentage(cls, data: NDArray[Float64], gain: int):

@classmethod
def get_oxygen_level(cls, ppg_ir: NDArray[Float64], ppg_red: NDArray[Float64], sampling_rate: int,
coef1=0.0, coef2=-37.663, coef3=114.91):
coef1=1.5958422, coef2=-34.6596622, coef3=112.6898759):
"""get oxygen level from ppg
:param ppg_ir: input array
Expand Down
4 changes: 2 additions & 2 deletions src/ml/generated/mindfulness_model.cpp
@@ -1,6 +1,6 @@

#include "mindfulness_model.h"
// clang-format off
const double mindfulness_coefficients[5] = {2.6338144674136394,4.006742906593334,-34.51389221061297,1.1950604401540308,35.78022137767881};
double mindfulness_intercept = 0.364078;
const double mindfulness_coefficients[5] = {-1.4765769283767163,2.6620930328900974,-31.43942997194057,9.464066586727622,45.512106420941684};
double mindfulness_intercept = 0.000000;
// clang-format on
Binary file modified src/ml/train/logreg_mindfulness.onnx
Binary file not shown.
20 changes: 13 additions & 7 deletions src/ml/train/train_classifiers.py
Expand Up @@ -42,6 +42,8 @@ def prepare_data(first_class, second_class, blacklisted_channels=None):
dataset_y = list()
for data_type in (first_class, second_class):
for file in glob.glob(os.path.join('data', data_type, '*', '*.csv')):
data_x_temp = list()
data_y_temp = list()
logging.info(file)
board_id = os.path.basename(os.path.dirname(file))
try:
Expand All @@ -58,13 +60,17 @@ def prepare_data(first_class, second_class, blacklisted_channels=None):
feature_vector = bands[0]
feature_vector = feature_vector.astype(float)
dataset_x.append(feature_vector)
data_x_temp.append(feature_vector)
if data_type == first_class:
dataset_y.append(0)
data_y_temp.append(0)
else:
dataset_y.append(1)
data_y_temp.append(0)
cur_pos = cur_pos + int(window_size * overlaps[num] * sampling_rate)
except Exception as e:
logging.error(str(e), exc_info=True)
print_dataset_info((data_x_temp, data_y_temp))

logging.info('1st Class: %d 2nd Class: %d' % (len([x for x in dataset_y if x == 0]), len([x for x in dataset_y if x == 1])))

Expand Down Expand Up @@ -115,7 +121,7 @@ def print_dataset_info(data):

def train_regression_mindfulness(data):
model = LogisticRegression(solver='liblinear', max_iter=4000,
penalty='l2', random_state=2, fit_intercept=True, intercept_scaling=0.2)
penalty='l2', random_state=2, fit_intercept=False, intercept_scaling=3)
logging.info('#### Logistic Regression ####')
scores = cross_val_score(model, data[0], data[1], cv=5, scoring='f1_macro', n_jobs=8)
logging.info('f1 macro %s' % str(scores))
Expand Down Expand Up @@ -207,14 +213,14 @@ def main():
dataset_y = pickle.load(f)
data = dataset_x, dataset_y
else:
data = prepare_data('relaxed', 'focused', blacklisted_channels={'T3', 'T4'})
data = prepare_data('relaxed', 'focused')
print_dataset_info(data)
train_regression_mindfulness(data)
train_svm_mindfulness(data)
train_knn_mindfulness(data)
train_random_forest_mindfulness(data)
train_mlp_mindfulness(data)
train_stacking_classifier(data)
#train_svm_mindfulness(data)
#train_knn_mindfulness(data)
#train_random_forest_mindfulness(data)
#train_mlp_mindfulness(data)
#train_stacking_classifier(data)


if __name__ == '__main__':
Expand Down

0 comments on commit 0754d73

Please sign in to comment.