-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusion_integrated.m
96 lines (68 loc) · 2.88 KB
/
fusion_integrated.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
close all; clc; clear;
addpath(genpath(pwd))
%% path configs
lsim_fusion_path = './results/lsim fusion'; % A folder path for saving results
lsim_path = './chmm-lsim-matlab-toolbox'; % download from https://github.com/sajjadkarimi91/chmm-lsim-matlab-toolbox
addpath(lsim_path)
mkdir(lsim_fusion_path)
%% model config
model_name_all = {'dgdss', 'tiny', 'seq', 'x_joint'};
channel_num = 2; % can be 2 or 3 for channel fusion
sleepedf_num = 20;
feature_sel = 1;
load(['data_split_scratch_trainingchk_',num2str(sleepedf_num),'.mat'])
for km = 1:length(model_name_all)
% testing 2or3 -channel LSIM
model_name = model_name_all{km};
load(['output_',model_name,'.mat'])
load(['flbss_',num2str(channel_num),'ch_',model_name,'.mat'])
%LSIM test on all models
max_r = size(lbss,3); % repeat number for LSIM that was set 1 for saving time
num_lsim = size(lbss,1);
CV_number = size(lbss,2);
y_test = [];
y_true = [];
for i = 1:CV_number
clear set_features
disp([km,i])
response = true_label{1,i};
response= response(:);
if sum(response==0)>0
response = response+1;
end
this_fold_number = fold_number{1,i};
test_set = this_fold_number==i;
train_all_set = this_fold_number~=i;
eval_set = ismember(this_fold_number,eval_sub{i});
train_set = ismember(this_fold_number,train_sub{i});
kappa_cv = [];
predictors = [];
for repeat_num = 1:max_r
for ss = 1:num_lsim
predictors = [predictors,lbss{ss,i,repeat_num}'];
end
end
for k = 1:10
Mdl = fitcknn(predictors(train_set, :), response(train_set, :),'NumNeighbors',10*k,'Distance','euclidean','NSMethod','exhaustive');
yhat = predict(Mdl, predictors(eval_set, :));
% a correction for kappa computation
if length(unique(yhat)) < length(unique(response))
d_set = setdiff(unique(response),unique(yhat));
ind_rand = randperm(length(yhat));
yhat(ind_rand(1:length(d_set))) = d_set;
end
[ acc_cv(k,1), kapp, f1, sens, spec] = calculate_overall_metrics(response(eval_set), yhat);
end
M = acc_cv;
[CC,I1] = max(acc_cv);
k = I1;
Mdl = fitcknn(predictors(train_all_set, :), response(train_all_set, :),'NumNeighbors',10*k,'Distance','euclidean','NSMethod','exhaustive');
yhat = predict(Mdl, predictors(test_set,:));
y_test = [y_test;yhat];
y_true = [y_true;response(test_set)];
end
[acc, kappa , f1, sens, spec] = calculate_overall_metrics(y_true, y_test);
save([lsim_fusion_path,'/poolres_',num2str(channel_num),'ch_',model_name,'_',num2str(feature_sel),'.mat'],'kappa','acc',"y_true","y_test")
disp(['poolres_',num2str(channel_num),'ch_',model_name])
disp([acc,kappa,f1])
end