% _________________________________________________________________________
% ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
% PREDICTION OF ACUTE HYPOTENSIVE EPISODES USING NEURAL NETWORK MULTIMODELS
% _________________________________________________________________________
% ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
% Copyright (C) 2009
% : Jorge Henriques ... <jh@dei.uc.pt>,
% : Teresa Rocha ... <teresa@sun.isec.pt>
% This software is released under the terms of the GNU
% General Public License (http://www.gnu.org/copyleft/gpl.html)
% ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
% mainTrainABPSignal.m
%__________________________________________________________________________
% ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨

%__________________________________________________________________________
% ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
% Traing GRNN models based on datasets (H, C)
% 1. Load a signal from SOURCE
% ..\dataTrain
% 2. Computation of optimal ORDER and SIZE
% 3. Define strutucture and parameters of GRNN models for each signal
%ºººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººº OUTPUT
% ORDER, SIZE and GRNN Models.
% To be saved in folder
% ..\NNModels
% .. OSTOTAL=[ ORDER, SIZE ] (60x2) corresponding to the 60 dataSets(H+C)
% .. NETXi_j models - 60*D neural networks, D window size
% .. X ={H,C}, i=1..15, j=1..W (W windows)
%__________________________________________________________________________
% ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨

clear; clc; close all
warning off

showTrain = 0;
saveTrain = 1;

disp('___________________________________________________________________')
disp('¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨')
disp(' mainTrainABPSignal ')
disp('___________________________________________________________________')
disp('¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨')
pause(1)

%ºººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººº
% DEFINITIONS 1.
%ºººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººº

%__________________________________________________________________________
% Data Set - 1.1
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
pathTrain= '..\dataTrain\';
LIST = {'H1', 'H2', 'C1', 'C2'}
sinal = 'BP'
DATNUM = 15
T0 = 600; %.. Start of AHE (assume data knowledge 10 hours before)
FORECAST = 60 %.. One hour (forecast window)

%__________________________________________________________________________
% Multi Models - 1.2
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
D = 15; %.. Window prediction size
NUMNET = 4; %.. num Networks
AFTER = 4; %.. num Windows = (D*AFTER minutes)
tolERROR = 20; %.. Maximum prediction error for each signal
firstORD = 60; %.. minutes
lastORD = 80; %.. minutes
firstSIZE= 120; %.. minutes
lastSIZE = 170; %.. minutes
INC = 10; %.. minutes

%ºººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººº
% TRAINING 2.
%ºººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººººº

%__________________________________________________________________________
% Each dataSet
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
for itList=1:length(LIST)

% CYCLE FOR ORDER / SIZE
%----------------------------------------------------------------------
ORD =[ firstORD: INC: lastORD ] ;
SIZE =[ firstSIZE: INC: lastSIZE ] ;
ORDSIZ=[ ];
% Each signal
%----------------------------------------------------------------------
for itData=1:DATNUM

str=char(LIST(itList));
dataSet=str(1);
numSet =str(2);

% 1.
%------------------------------------------- Load a signal (660,3)
cmd=['load ' char(pathTrain) char(sinal) '_' ...
char(dataSet) char(numSet) '_' num2str(itData) '.mat'];
eval(cmd);
DAT0=DAT;

% 2.
%------------------------------------------------ Find ORD/SIZ/NET
minERROR=realmax;
for itSIZE=SIZE
for itORD=ORD

if (itSIZE) < (FORECAST+itORD)
continue
end

clc
[':::::::::::::::::::::::::::::::::::::: ' dataSet numSet]
[ itData itSIZE itORD ]

% Window data
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[DAT, idB, idE, id0 ]=mmWindowDat(DAT0,T0,D,AFTER,itSIZE);
[DAT, dmin,dmax, dbia]=mmNormalize(DAT, -1, 1, 0, id0 );

% Data train:Input(XX) Target(YY)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
outY= DAT(1:id0);
outN= DAT(1:id0);
XX = mmInputXX(DAT, id0, itORD);
for NET=1:NUMNET
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ YREAL
YR = [];
first= itORD+(NET-1)*D+1;
last = id0;
for id = first:last
iyy= id:id+D-1;
yy = DAT(iyy,:);
yy = yy(:);
YR = [YR yy ];
end
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ YPrediction
YP = [];
first= id0+1;
last = id0+(NET-1)*D;
for id = first:last
iyy= id:id+D-1;
yy = DAT(iyy,:);
yy = yy(:);
YP = [YP yy ];
end
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ TRAINING GRNN
YY = [YR YP];
[net,YNi,tr]= mmGRNNTrain(XX, YY, tolERROR, FORECAST);
disp(['... Training : ' ...
num2str(NET) ' --- ' num2str(tr,5) ])

%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Net temp
for numN=1:NUMNET
if NET==numN
cmd=['net' num2str(numN) '=net;'];
eval(cmd);
end
end

%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Update
YN = sim(net,XX);
yn = YN(:,end);
yn = yn-yn(1)+outN(end);
XX = [XX ; YN ];
outN = [outN; yn ];
outY = [outY; DAT(iyy) ];

%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Show
if showTrain==2 %.. hide
close all
figure(1)
plot(1:length(outY),outY,'g',...
1:length(outN),outN,'b' )
hold on
plot(1:length(outY),1*ones(size(outY)),'k.' );
plot([id0 id0],...
[min(outY) max(outY)],'c-','LineWidth',5 );
plot([FORECAST+id0 FORECAST+id0],...
[min(outY) max(outY)],'c-','LineWidth',5 );
pause
end

end

% Optimal NET/ORD/SIZ
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
errFOR=outY(end-FORECAST+1:end)-outN(end-FORECAST+1:end);
ERROR= sum(errFOR.^2);
if ERROR<minERROR
minERROR=ERROR;
minORD =itORD;
minSIZ =itSIZE;
for numN=1:NUMNET
cmd=['minN' num2str(numN) '=net' num2str(numN) ';'];
eval(cmd);
end
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if showTrain==2 %.. hide
figure(1)
plot(1:length(outY),outY,'g',...
1:length(outN),outN,'b','LineWidth',3 )
xlabel(num2str(ERROR))
pause
end

end
end

%__________________________________________________________________
% Each SIGNAL
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨

%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ ORD / SIZE
ORDSIZ=[ORDSIZ; minORD minSIZ];

OS=[ minORD minSIZ];
if saveTrain
cmd=['save ..\NNmodels\OS' char(dataSet) char(numSet) ...
num2str(itData) ' OS'];
eval(cmd)
end

%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ GRNN
for numN=1:NUMNET
cmd=['net=minN' num2str(numN) ';'];
eval(cmd);
if saveTrain
cmd=['save ..\NNmodels\NET' char(dataSet) char(numSet) ...
num2str(itData) '_' num2str(numN) ' net'];
eval(cmd);
end
end

%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Show
if showTrain
DIM=length(outY);
mmMaximize
figure(1)
hold on

plot(1:DIM,outY,'g','LineWidth',5)
plot(1:DIM,outN,'r','LineWidth',2)
plot([id0 id0],...
[min(min(outY),min(outN))-5 min(max(outY),max(outN))+5 ],'k:','LineWidth',3 );
axis([0 DIM min(min(outY),min(outN))-1 min(max(outY),max(outN))+1])
legend('Signal','Neural model','T0')
strTitle=[ char(dataSet) char(numSet) ];
strLabel=[ 'ORDER: ' num2str(minORD) ' ' ...
' SIZE: ' num2str(minSIZ) ];
mmLabel(strTitle,strLabel,'[-1..1]')

%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
pause
close all
end
%----------------------------------------------- itData ----------

end
%--------------------------------------------------- itList ----------

end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Matrix (60,2) - All [ORDER SIZE]
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSTOTAL=[];
for id=1:DATNUM
cmd=['load ..\NNmodels\OSH1' num2str(id) ];
eval(cmd)
OSTOTAL=[OSTOTAL;OS];
end
for id=1:DATNUM
cmd=['load ..\NNmodels\OSH2' num2str(id) ];
eval(cmd)
OSTOTAL=[OSTOTAL;OS];
end
for id=1:DATNUM
cmd=['load ..\NNmodels\OSC1' num2str(id) ];
eval(cmd)
OSTOTAL=[OSTOTAL;OS];
end
for id=1:DATNUM
cmd=['load ..\NNmodels\OSC2' num2str(id) ];
eval(cmd)
OSTOTAL=[OSTOTAL;OS];
end
OS=OSTOTAL;
if saveTrain
save ..\NNModels\OSTOTAL OS
end
option=1;