This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain-debug.m
153 lines (135 loc) · 4.6 KB
/
Main-debug.m
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%%%%%%%
% This is the main script for debugging
%%%%%%%
%% Preliminaries
% Add toolbox folder to path
clear; clc; close all;
addpath 'wfdb-app-toolbox-0-10-0\mcode'
%% Read in and construct data to stack using for-loop
X_stack = [];
Y_stack = [];
for i = 1:20
[X_AccTempEDA, Y] = AccTempEDAToMatrix(char('nonEEGdataset/Subject' + string(i) + '_AccTempEDA'), 0);
[X_SpO2HR] = SpO2HRToMatrix(char('nonEEGdataset/Subject' + string(i) + '_SpO2HR'), 0);
X_stack = [
X_AccTempEDA X_SpO2HR(1:height(X_AccTempEDA), :);
X_stack];
Y_stack = [Y; Y_stack];
end
XY = [X_stack array2table(Y_stack)];
%% LEGACY/DEBUG ONLY --- Read in AccTempEDA / SpO2HR
% [X1_AccTempEDA, Y1] = AccTempEDAToMatrix('nonEEGdataset/Subject1_AccTempEDA', 0);
% [X2_AccTempEDA, Y2] = AccTempEDAToMatrix('nonEEGdataset/Subject2_AccTempEDA', 0);
% ...
% [X20_AccTempEDA, Y20] = AccTempEDAToMatrix('nonEEGdataset/Subject20_AccTempEDA', 0);
%
% [X1_SpO2HR] = SpO2HRToMatrix('nonEEGdataset/Subject1_SpO2HR', 0);
% [X2_SpO2HR] = SpO2HRToMatrix('nonEEGdataset/Subject2_SpO2HR', 0);
% ...
% [X20_SpO2HR] = SpO2HRToMatrix('nonEEGdataset/Subject20_SpO2HR', 0);
%
% % Split data
% X_stack = [
% X1_AccTempEDA X1_SpO2HR(1:height(X1_AccTempEDA), :);
% X2_AccTempEDA X2_SpO2HR(1:height(X2_AccTempEDA), :);
% ...;
% X19_AccTempEDA X19_SpO2HR(1:height(X19_AccTempEDA), :)];
% Y_stack = [Y1; Y2; ...; Y19];
% XY = [X_stack array2table(Y_stack)];
%% %%%%%%%%%%%%%%%%%%%%Classification%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% kNN Train & Test
mdl = fitcknn(X_stack, Y_stack, 'NumNeighbors', 3, 'Standardize', 1);
% label = predict(mdl, [X20_AccTempEDA X20_SpO2HR(1:height(X20_AccTempEDA), :)]);
label = predict(mdl, X_stack);
% success rate count
correct_mark = zeros(length(label), 1);
for i = 1:length(label)
if label(i) == Y_stack(i)
correct_mark(i) = 1;
else
correct_mark(i) = 0;
end
end
success_rate = sum(correct_mark) / length(label)
% rloss below seems to equals 1-success_rate above
rloss = resubLoss(mdl)
%% Classification Model (use 'Classification Learner' app)
% Classification Model can be generated using 'Classification Learner'
% Evaluate confusion matrix using Classificaton Learner
% Classification Model Using script
% Accuracy using resubloss
%% Prepare trained classification model for Coder/Conversion
% use above code to train model named 'mdl'
% saveLearnerForCoder(mdl,'RF_model');
% Classification Learning APP and export compact model, then call below
% Refer to MATLAB doc: Code Generation and Classification Learner App
saveLearnerForCoder(trainedModel.ClassificationEnsemble,'RF_model');
%% LEGACY/DEBUG ONLY - Evaluate Classification Model - Cross Validation
% X = X_stack;
% Y = num2cell(Y_stack);
% cp = classperf(Y)
% for i = 1:10
% [train,test] = crossvalind('LeaveMOut',Y,5);
% mdl = fitcknn(X(train,:),Y(train),'NumNeighbors',3);
% predictions = predict(mdl,X(test,:));
% classperf(cp,predictions,test);
% end
%% %%%%%%%%%%%%%%%%%%%%Neural Network%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% Neural Network Train & Test
% % reorganize training data/label
% X_array = table2array(X_stack);
% Y_array = Y_stack;
% Y_array_flag = [];
% for i = 1: length(Y_array)
% if Y_array(i) == 0
% Y_array_flag = [Y_array_flag; [1 0 0 0]];
% elseif Y_array(i) == 1
% Y_array_flag = [Y_array_flag; [0 1 0 0]];
% elseif Y_array(i) == 2
% Y_array_flag = [Y_array_flag; [0 0 1 0]];
% elseif Y_array(i) == 3
% Y_array_flag = [Y_array_flag; [0 0 0 1]];
% end
% end
%
% % design/train network
% net = fitnet(10,'traingdx');
% net.adaptParam.lr=0.3; % learning rate
% net.trainParam.mc = 0.2; % momentum
% [net,tr] = train(net, X_array', Y_array_flag');
%
% % test network
% y = net(X_array'); % net is not a function, its just the 'net' we trained above
% perf = perform(net,y,Y_array_flag');
%
% % Saving the network weights and biases
% net.IW;
% net.LW;
% net.b;
%
% % save network for coder
% % genFunction(net, 'NN_model', 'MatrixOnly','yes');
%
% %% Get final prediction of NN as 0, 1, 2, 3, rather than [1, 0, 0, 0], [0, 1, 0, 0], ...
% % get result (4*14497-->1*14497)
% Y_predict = [];
% for i = 1: length(y)
% column_buff = y(:, i);
% max_idx = find(column_buff == max(column_buff(:)))-1;
% Y_predict = [max_idx; Y_predict];
% end
%
% %% Confusion Matrix
% figure; plotconfusion(Y_array_flag', y);
%
% correct_mark = zeros(length(Y_stack), 1);
% for i = 1:length(Y_stack)
%
% if Y_predict(i) == Y_stack(i)
% correct_mark(i) = 1;
% else
% correct_mark(i) = 0;
% end
%
% end
% success_rate = sum(correct_mark) / length(Y_stack)