% EpilepsyGUI - Graphical User Interface developed for the work presented on
% the paper: Epileptic Seizure Classification Using Neural Networks With 14 Features
%
%   How to SIMULATE:
%       1º - Select the neural network that you want to simulate;
%       2º - Select the correspondent training type (in Trained with) and data;
%           2.1 - Type/Data selection:
%                 Single / pac1.xls or pac2.xls
%                 Different(1:2) / pac2.xls
%                 Different(2:1) / pac1.xls
%                 Multiple(1+2:1) / pac1.xls
%                 Multiple(1+2:2) / pac2.xls
%       3º - Run the simulation.
%
%   How to TRAIN:
%       1º - Select the type of neural network that you want to train;
%       2º - Select the correspondent training type (in Train with) and data;
%       3º - Run the training.
%       4º - Save the simulation inside kes2008nnets if you want to
%            simulate it in the future (it will appear in the simulation popup)
%       NOTE: Some nnets can take several minutes or even hours to run
%
% Copyright 2007-2008 Rui P. Costa (http://student.dei.uc.pt/~racosta)
%
function varargout = EpilepsyGUI(varargin)
% EPILEPSYGUI M-file for EpilepsyGUI.fig
%      EPILEPSYGUI, by itself, creates a new EPILEPSYGUI or raises the existing
%      singleton*.
%
%      H = EPILEPSYGUI returns the handle to a new EPILEPSYGUI or the handle to
%      the existing singleton*.
%
%      EPILEPSYGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EPILEPSYGUI.M with the given input arguments.
%
%      EPILEPSYGUI('Property','Value',...) creates a new EPILEPSYGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before EpilepsyGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to EpilepsyGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help EpilepsyGUI

% Last Modified by GUIDE v2.5 30-Mar-2008 03:23:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @EpilepsyGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @EpilepsyGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before EpilepsyGUI is made visible.
function EpilepsyGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to EpilepsyGUI (see VARARGIN)
global data;

% Choose default command line output for EpilepsyGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% Load train_nnets into data.train_nnets
data.data.Pac1.path='data/pac1.xls';
data.data.Pac2.path='data/pac2.xls';
%[data.data.Pac1.P data.data.Pac1.T data.data.Pac1.Test data.data.Pac1.T2]=readEpi({strcat(pathname,filename)},'ii');


% UIWAIT makes EpilepsyGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = EpilepsyGUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in Simulate.
function Simulate_Callback(hObject, eventdata, handles)
% hObject    handle to Simulate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;
popup_sel_index = get(handles.train_nnets, 'Value');
switch popup_sel_index
    case 1 %Time-delay
        A=sim(data.nnets.td.net,data.Test); 
    case 2 %Elman
        A=sim(data.nnets.elman.net,data.Test); 
    case 3 %Distributed Time-delay
        A=sim(data.nnets.dtd.net,data.Test);            
    case 4 %Recurrent
        A=sim(data.nnets.lrn.net,data.Test);                     
    case 5 %FFBP
        A=sim(data.nnets.ffbp.net,data.Test);            
    case 6 %RBF
        A=sim(data.nnets.rbf.net,data.Test);     
end
[E S PC]=calcPerformance(A,data.T2);
set(handles.sensitivity,'String',strcat('Sensitivity = ',int2str(S),'%'));
set(handles.specificity,'String',strcat('Specificity = ',int2str(E),'%'));
set(handles.correctClassifications,'String',strcat('Correct Classifications = ',int2str(PC),'%'));

% --- Executes on button press in loadDataSet.
function loadDataSet_Callback(hObject, eventdata, handles)
% hObject    handle to loadDataSet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;

[ filename , pathname ] = uigetfile (  {'*.xls'} , 'Choose xls dataset file' );
if length(filename)<1
    disp('You must select a file!');
else    
    strcat(pathname,filename)
    [P T data.Test data.T2]=readEpi({strcat(pathname,filename)},'ii');
end    



% --- Executes on selection change in train_nnets.
function train_nnets_Callback(hObject, eventdata, handles)
% hObject    handle to train_nnets (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns train_nnets contents as cell array
%        contents{get(hObject,'Value')} returns selected item from train_nnets


% --- Executes during object creation, after setting all properties.
function train_nnets_CreateFcn(hObject, eventdata, handles)
% hObject    handle to train_nnets (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in sim_nnets.
function sim_nnets_Callback(hObject, eventdata, handles)
% hObject    handle to sim_nnets (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns sim_nnets contents as cell array
%        contents{get(hObject,'Value')} returns selected item from sim_nnets


% --- Executes during object creation, after setting all properties.
function sim_nnets_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sim_nnets (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu4 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu4


% --- Executes during object creation, after setting all properties.
function popupmenu4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in popupmenu5.
function popupmenu5_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu5 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu5


% --- Executes during object creation, after setting all properties.
function popupmenu5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu7.
function popupmenu7_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu7 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu7


% --- Executes during object creation, after setting all properties.
function popupmenu7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in popupmenu6.
function popupmenu6_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu6 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu6


% --- Executes during object creation, after setting all properties.
function popupmenu6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in Multi.
function Multi_Callback(hObject, eventdata, handles)
% hObject    handle to Multi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns Multi contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Multi


% --- Executes during object creation, after setting all properties.
function Multi_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Multi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in save_button.
function save_button_Callback(hObject, eventdata, handles)
% hObject    handle to save_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in loadOtherData.
function loadOtherData_Callback(hObject, eventdata, handles)
% hObject    handle to loadOtherData (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;

[ filename , pathname ] = uigetfile (  {'*.xls'} , 'Choose a xls dataset file' );
if length(filename)<1
    disp('You must select a file!');
else    
    strcat(pathname,filename)
    data.data.Other.path=strcat(pathname,filename);
    %[data.data.Other.P data.data.Other.T data.data.Other.Test data.data.Other.T2]=readEpi({strcat(pathname,filename)},'ii');
end 

% --- Executes on selection change in type_list.
function type_list_Callback(hObject, eventdata, handles)
% hObject    handle to type_list (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns type_list contents as cell array
%        contents{get(hObject,'Value')} returns selected item from type_list


% --- Executes during object creation, after setting all properties.
function type_list_CreateFcn(hObject, eventdata, handles)
% hObject    handle to type_list (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in data_list.
function data_list_Callback(hObject, eventdata, handles)
% hObject    handle to data_list (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
list_sel_index = get(handles.data_list, 'Value');

if length(list_sel_index) < 2 && length(list_sel_index) > 0
    switch list_sel_index
        case 1 %pac1.xls
            set(handles.loadOtherData,'Enable','Off')
        case 2 %pac2.xls
            set(handles.loadOtherData,'Enable','Off')
        case 3 %Other...
            set(handles.loadOtherData,'Enable','On')
    end
else
    set(handles.loadOtherData,'Enable','Off')
end    
% [E S PC]=calcPerformance(A,data.T2);
% set(handles.sensitivity,'String',strcat('Sensitivity = ',int2str(S),'%'));
% set(handles.specificity,'String',strcat('Specificity = ',int2str(E),'%'));
% set(handles.correctClassifications,'String',strcat('Correct Classifications = ',int2str(PC),'%'));

% Hints: contents = get(hObject,'String') returns data_list contents as cell array
%        contents{get(hObject,'Value')} returns selected item from data_list


% --- Executes during object creation, after setting all properties.
function data_list_CreateFcn(hObject, eventdata, handles)
% hObject    handle to data_list (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in nnet_train.
function nnet_train_Callback(hObject, eventdata, handles)
% hObject    handle to nnet_train (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns nnet_train contents as cell array
%        contents{get(hObject,'Value')} returns selected item from nnet_train


% --- Executes during object creation, after setting all properties.
function nnet_train_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nnet_train (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in trainButton.
function trainButton_Callback(hObject, eventdata, handles)
% hObject    handle to trainButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;
type_index = get(handles.type_list, 'Value');
string_list = get(handles.type_list,'String');
type_selected=string_list(type_index);

val = get(handles.nnet_train,'Value');
string_list = get(handles.nnet_train,'String');
nnet_selected=string_list(val);

data_index = get(handles.data_list, 'Value');


switch(type_index)    
    case 1 %Single        
        if length(data_index) == 1
            switch data_index
                case 1 %pac1.xls                    
    %                   h = waitbar(0,'Loading nnet... Please wait...','CreateCancelBtn','cancelButton_callback');                  
                    h = waitbar(0,'Loading data... Please wait...');
                        [P T Test T2]=readEpi({data.data.Pac1.path},'i');
                    close(h)                   
                case 2 %pac2.xls
                    h = waitbar(0,'Loading data... Please wait...');
                        [P T Test T2]=readEpi({data.data.Pac2.path},'i');
                    close(h)                   
                case 3 %Other...
                    try
                        data.data.Other.path;
                        h = waitbar(0,'Loading data... Please wait...');
                            [P T Test T2]=readEpi({data.data.Other.path},'i');
                        close(h)
                    catch
                        msgbox('Define other dataset in data','Epilepsy Message','error');
                        return;
                    end                    
            end
            h = waitbar(0,'Training... Please wait (this can take some minutes)...');
                [data.net SP SS AC] = simEpi(P , T , Test, T2 , char(nnet_selected), char(type_selected));
                set(handles.sensitivity,'String',strcat('Sensitivity = ',num2str(SS),'%'));
                set(handles.specificity,'String',strcat('Specificity = ',num2str(SP),'%'));
                set(handles.accuracy,'String',strcat('Accuracy = ',num2str(AC),'%'));
            close(h) 
            msgbox('Train complete!','Epilepsy Message','warn');
            set(handles.saveButton,'Enable','On');
        else
            msgbox('Select just one dataset in data','Epilepsy Message','error');
        end 
        
    case 2 %Differente (1:2)
        if length(data_index) ~= 2 
            msgbox('Select two datasets in data','Epilepsy Message','error');
        else
            datas = {};
            for i=1:length(data_index)
                switch data_index(i)
                    case 1 %append pac1.xls                    
                        datas = [datas {data.data.Pac1.path}];            
                    case 2 %pac2.xls
                        datas = [datas {data.data.Pac2.path}];
                    case 3 %Other...
                        try
                            datas = [datas {data.data.Other.path}];
                        catch
                            msgbox('Define other dataset in data','Epilepsy Message','error');
                            return;
                        end  
                end
            end    
                
            h = waitbar(0,'Loading data... Please wait...');
                [P T Test T2]=readEpi(datas,'ii');
            close(h)       
            
            h = waitbar(0,'Training... Please wait (this can take some minutes)...');
                [data.net SP SS AC] = simEpi(P , T , Test, T2 , char(nnet_selected), char(type_selected));
                set(handles.sensitivity,'String',strcat('Sensitivity = ',num2str(SS),'%'));
                set(handles.specificity,'String',strcat('Specificity = ',num2str(SP),'%'));
                set(handles.accuracy,'String',strcat('Accuracy = ',num2str(AC),'%'));
            close(h) 
            msgbox('Train complete!','Epilepsy Message','warn');
            set(handles.saveButton,'Enable','On');
        end 
        
    case 3 %Different (2:1)
        if length(data_index) ~= 2 
            msgbox('Select two datasets in data','Epilepsy Message','error');
        else
            datas = {};
            i=length(data_index);
            while (i>0)
                switch data_index(i)
                    case 1 %append pac1.xls                    
                        datas = [datas {data.data.Pac1.path}];            
                    case 2 %pac2.xls
                        datas = [datas {data.data.Pac2.path}];
                    case 3 %Other...
                        try
                            datas = [datas {data.data.Other.path}];
                        catch
                            msgbox('Define other dataset in data','Epilepsy Message','error');
                            return;
                        end  
                end
                i=i-1;
            end    
                
            h = waitbar(0,'Loading data... Please wait...');
                [P T Test T2]=readEpi(datas,'ii');
            close(h)       
            
            h = waitbar(0,'Training... Please wait (this can take some minutes)...');
                [data.net SP SS AC] = simEpi(P , T , Test, T2 , char(nnet_selected), char(type_selected));
                set(handles.sensitivity,'String',strcat('Sensitivity = ',num2str(SS),'%'));
                set(handles.specificity,'String',strcat('Specificity = ',num2str(SP),'%'));
                set(handles.accuracy,'String',strcat('Accuracy = ',num2str(AC),'%'));
            close(h) 
            msgbox('Train complete!','Epilepsy Message','warn');
            set(handles.saveButton,'Enable','On');
        end    
        
    case 4 %Multiple (1+2:1)
        if length(data_index) ~= 2 
            msgbox('Select two datasets in data','Epilepsy Message','error');
        else
            datas = {};
            data_index = [data_index 1];
            
            for i=1:length(data_index)
                switch data_index(i)
                    case 1 %append pac1.xls                    
                        datas = [datas {data.data.Pac1.path}];            
                    case 2 %pac2.xls
                        datas = [datas {data.data.Pac2.path}];
                    case 3 %Other...
                        try
                            datas = [datas {data.data.Other.path}];
                        catch
                            msgbox('Define other dataset in data','Epilepsy Message','error');
                            return;
                        end  
                end
            end      
                
            h = waitbar(0,'Loading data... Please wait...');
                [P T Test T2]=readEpi(datas,'ii');
            close(h)       
            
            h = waitbar(0,'Training... Please wait (this can take some minutes)...');
                [data.net SP SS AC] = simEpi(P , T , Test, T2 , char(nnet_selected), char(type_selected));
                set(handles.sensitivity,'String',strcat('Sensitivity = ',num2str(SS),'%'));
                set(handles.specificity,'String',strcat('Specificity = ',num2str(SP),'%'));
                set(handles.accuracy,'String',strcat('Accuracy = ',num2str(AC),'%'));
            close(h) 
            msgbox('Train complete!','Epilepsy Message','warn');
            set(handles.saveButton,'Enable','On');
        end
        
     case 5 %Multiple (1+2:2)
        if length(data_index) ~= 2 
            msgbox('Select two datasets in data','Epilepsy Message','error');
        else
            datas = {};
            data_index = [data_index 2];
            
            for i=1:length(data_index)
                switch data_index(i)
                    case 1 %append pac1.xls                    
                        datas = [datas {data.data.Pac1.path}];            
                    case 2 %pac2.xls
                        datas = [datas {data.data.Pac2.path}];
                    case 3 %Other...
                        try
                            datas = [datas {data.data.Other.path}];
                        catch
                            msgbox('Define other dataset in data','Epilepsy Message','error');
                            return;
                        end  
                end
            end     

            h = waitbar(0,'Loading data... Please wait...');
                [P T Test T2]=readEpi(datas,'ii');
            close(h)       
            
            h = waitbar(0,'Training... Please wait (this can take some minutes)...');
                [data.net SP SS AC] = simEpi(P , T , Test, T2 , char(nnet_selected), char(type_selected));
                set(handles.sensitivity,'String',strcat('Sensitivity = ',num2str(SS),'%'));
                set(handles.specificity,'String',strcat('Specificity = ',num2str(SP),'%'));
                set(handles.accuracy,'String',strcat('Accuracy = ',num2str(AC),'%'));
            close(h) 
            msgbox('Train complete!','Epilepsy Message','warn');
            set(handles.saveButton,'Enable','On');
        end      
end

% --- Executes on button press in saveButton.
function saveButton_Callback(hObject, eventdata, handles)
% hObject    handle to saveButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;

[ filename , pathname ] = uiputfile('*.mat','Save file name');
net=data.net
save(strcat(pathname,filename),'net');
strcat('Nnet saved in:',pathname,filename)


% --- Executes on selection change in sim_popup.
function sim_popup_Callback(hObject, eventdata, handles)
% hObject    handle to sim_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns sim_popup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from sim_popup


% --- Executes during object creation, after setting all properties.
function sim_popup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sim_popup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

dirs=dir('kes2008nnets');

filenames = {};
for i=3:length(dirs)
    filenames = [filenames ; strcat('kes2008nnets\',{dirs(i).name})];
end    

set(hObject,'String',filenames);


% --- Executes on button press in sim_button.
function sim_button_Callback(hObject, eventdata, handles)
% hObject    handle to sim_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;
type_index = get(handles.type_list, 'Value');
val = get(handles.sim_popup,'Value');
string_list = get(handles.sim_popup,'String');
nnet_selected=string_list(val);

data_index = get(handles.data_list, 'Value');

if type_index==1
    if length(data_index) < 2 
        h = waitbar(0,'Loading nnet... Please wait...');
            load(char(nnet_selected));
        close(h)
        switch data_index
            case 1 %pac1.xls                    
%                   h = waitbar(0,'Loading nnet... Please wait...','CreateCancelBtn','cancelButton_callback');                  
                h = waitbar(0,'Loading data... Please wait...');
                    [P T Test T2]=readEpi({data.data.Pac1.path},'i');
                close(h)                   
            case 2 %pac2.xls
                h = waitbar(0,'Loading data... Please wait...');
                    [P T Test T2]=readEpi({data.data.Pac2.path},'i');
                close(h)                   
            case 3 %Other...
                try
                    data.data.Other.path;
                    h = waitbar(0,'Loading data... Please wait...');
                        [P T Test T2]=readEpi({data.data.Other.path},'i');
                    close(h)
                catch
                    msgbox('Define other dataset in data','Epilepsy Message','error');
                    return;
                end                    
        end
        h = waitbar(0,'Simulating... Please wait...');
            A = sim(net,Test);        
            [SP SS AC]=calcPerformance(A,T2);                                            
            set(handles.sensitivity,'String',strcat('Sensitivity = ',num2str(SS),'%'));
            set(handles.specificity,'String',strcat('Specificity = ',num2str(SP),'%'));
            set(handles.accuracy,'String',strcat('Accuracy = ',num2str(AC),'%'));
        close(h) 
        msgbox('Simulation complete!','Epilepsy Message','warn');
    else
        msgbox('Select just one dataset in data','Epilepsy Message','error');
    end 
else
    if length(data_index) > 1 
        msgbox('Select just one dataset in data','Epilepsy Message','error');
    else
        h = waitbar(0,'Loading nnet... Please wait...');
            load(char(nnet_selected));
        close(h)

        switch data_index
            case 1 %append pac1.xls                    
                datas = [{data.data.Pac1.path} {data.data.Pac1.path}];            
            case 2 %pac2.xls
                datas = [{data.data.Pac2.path} {data.data.Pac2.path}];
            case 3 %Other...
                try
                    datas = [{data.data.Other.path} {data.data.Other.path}];
                catch
                    msgbox('Define other dataset in data','Epilepsy Message','error');
                    return;
                end  
        end

        h = waitbar(0,'Loading data... Please wait...');
            [P T Test T2]=readEpi(datas,'ii');
        close(h)                   

        h = waitbar(0,'Simulating... Please wait...');
            A = sim(net,Test);        
            [SP SS AC]=calcPerformance(A,T2);                                            
            set(handles.sensitivity,'String',strcat('Sensitivity = ',num2str(SS),'%'));
            set(handles.specificity,'String',strcat('Specificity = ',num2str(SP),'%'));
            set(handles.accuracy,'String',strcat('Accuracy = ',num2str(AC),'%'));
        close(h)
        msgbox('Simulation complete!','Epilepsy Message','warn');
    end 
end


% --- Executes on button press in cancelButton in waitbar.
function cancelButton_callback(hObject, eventdata, handles)
% hObject    handle to sim_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function Help_menu_Callback(hObject, eventdata, handles)
% hObject    handle to Help_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
helpwin epilepsyGUI


% --- Executes during object creation, after setting all properties.
function loadOtherData_CreateFcn(hObject, eventdata, handles)
% hObject    handle to loadOtherData (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


