%  AlzhGUI.m
%           20 June 2004
%
% This is a demo of a simple model of the Alzheimer's disease based on
% the recurrent Hopfield neural network with autoassociative memory,
% represented by the weihgh matrix. The Alzheimer is modelled by "killing
% synapses",  that is, by setting indiwidual synaptic weights to zero.
%
% The  script  AlzhGUI was created partly by the GUI editor and the file
% can be modified  invoking   guide AlhGUI   or by MATLAB text editor. The
% GUI editor  guide  creates and edits  a GUI file  Alzh.fig.
%
% AlzhGUI  also invokes:  
% trnglDef.m  -- a script with definition of the input data -- composition 
%   of colour triangles by a Swedish artist Tex
% HopfConv.m  -- a script that implements the Hopfield relaxation loop.
% plotSet.m  -- a function to plot the current triangle set

function varargout = AlzhGUI(varargin)
% ALZHGUI Application M-file for AlzhGUI.fig
%   ALZHGUI, by itself, creates a new ALZHGUI or raises the existing
%   singleton*.
%
%   H = ALZHGUI returns the handle to a new ALZHGUI or the handle to
%   the existing singleton*.
%
%   ALZHGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%   function named CALLBACK in ALZHGUI.M with the given input arguments.
%
%   ALZHGUI('Property','Value',...) creates a new ALZHGUI or raises the
%   existing singleton*.  Starting from the left, property value pairs are
%   applied to the GUI before AlzhGUI_OpeningFunction gets called.  An
%   unrecognized property name or invalid value makes property application
%   stop.  All inputs are passed to AlzhGUI_OpeningFcn via varargin.
%
%   *See GUI Options - GUI allows only one instance to run (singleton).
%
% See also: GUIDE, GUIDATA, GUIHANDLES

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

% Last Modified by GUIDE v2.5 19-Jun-2004 16:00:42

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',          mfilename, ...
                   'gui_Singleton',     gui_Singleton, ...
                   'gui_OpeningFcn',    @AlzhGUI_OpeningFcn, ...
                   'gui_OutputFcn',     @AlzhGUI_OutputFcn, ...
                   'gui_LayoutFcn',     [], ...
                   'gui_Callback',      []);
if nargin & isstr(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 AlzhGUI is made visible.
function AlzhGUI_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 AlzhGUI (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles) ;

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

% --- Outputs from this function are returned to the command line.
function varargout = AlzhGUI_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;

% --------------------------------------------------------------------
axes(handles.iniSet),   cla reset, axis off
axes(handles.testSet),  cla reset, axis off
axes(handles.finalSet), cla reset, axis off

% --------------------------------------------------------------------

% --- Executes during object creation, after setting all properties.
function menuComp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to menuComp (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes on selection change in menuComp.
function menuComp_Callback(hObject, eventdata, handles)
% hObject    handle to menuComp (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 menuComp contents as cell array
%        contents{get(hObject,'Value')} returns selected item from menuComp

% --- Executes during object creation, after setting all properties.
function menuTest_CreateFcn(hObject, eventdata, handles)
% hObject    handle to menuTest (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes on selection change in menuTest.
function menuTest_Callback(hObject, eventdata, handles)
% hObject    handle to menuTest (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 menuTest contents as cell array
%        contents{get(hObject,'Value')} returns selected item from menuTest

function SurvFac_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SurvFac (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

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

% --- Executes on slider movement.
function sliderSurvFac_Callback(hObject, eventdata, handles)
% hObject    handle to sliderSurvFac (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% get the value from the slider sliderSurvFac:
 SurvivalFactor = get(handles.sliderSurvFac,'Value') ;
 
% write the value back in the SurvFac data box
 set(handles.SurvFac,'String', SurvivalFactor) ;

% START button 
function varargout = Start_Callback(h, eventdata, handles, varargin)

% get data from the SurvFac data box
val = str2double(get(handles.SurvFac,'String'));
SurvivalFactor = val ;

% Determine whether val is a number between 0 and 1
if isnumeric(val) & length(val)==1 & ...
    val >= get(handles.sliderSurvFac,'Min') & ...
    val <= get(handles.sliderSurvFac,'Max')
else
    val = 0.9 ;  
  % if an incorrect value is input we chnge it to  val
    set(handles.SurvFac, 'String', val);  
end
   % Write the correct value back to the slider sliderSurvFac:
    set(handles.sliderSurvFac,'Value',val);

axes(handles.iniSet),   cla reset, 
axes(handles.testSet),  cla reset, 
axes(handles.finalSet), cla reset, 


p = 87 ;  %  size of an input vector
 
 trnglDef   %  specification of triangle sets

Ncomp = 4 ;

%  selecting  the composition to be learned
CompSet = get(handles.menuComp,'String') ;
eval(['LearningSet = ' , CompSet{get(handles.menuComp,'Value')}, ';']) ;

% selecting the test composition
CompSet = get(handles.menuTest,'String') ;
eval(['TestSet = ' , CompSet{get(handles.menuTest,'Value')}, ';']) ;

% lengths of triangle sides  in mm
edg = [12 25 41];

 Dxx = 70;  % lateral size of one  triangle set
 Dy = 90 ;  % vertical size

for komp = 1:Ncomp  % plotting Initial Composition
    
  % extract the current triangle set represented by a 
  % p by 1 vector of +-1s to a 3 by p/3 binary matrix 
  trB = reshape((LearningSet(:,komp) + 1)/2, p/3, 3)  ;
  
  % lateral shift of a triangle set
  Dx = (komp-1)*Dxx; 
  
  axes(handles.iniSet)
  plotSet(trB, edg, Dxx, Dx, Dy)
  axis([0  Ncomp*Dxx 0 Dy]), axis off
  plot([Ncomp*Dxx 0 0 Ncomp*Dxx], [0 0 Dy Dy]) 

end  % initial Composition
 
for komp = 1:Ncomp  % plotting Test Composition
    
  % extract the current triangle set represented by a 
  % p by 1 vector of +-1s to a 3 by p/3 binary matrix 
  trB = reshape((TestSet(:,komp) + 1)/2, p/3, 3)  ;
  
  % lateral shift of a triangle set
  Dxx = 70;  Dx = (komp-1)*Dxx; Dy = 90 ;
  % Vertices of three triangels in the current set

  axes(handles.testSet)
  plotSet(trB, edg, Dxx, Dx, Dy)
  axis([0  Ncomp*Dxx 0 Dy]), axis off
  plot([Ncomp*Dxx 0 0 Ncomp*Dxx], [0 0 Dy Dy]) 

end  % Test Composition

% Hopfield relaxation loop
 HopfConv  

for komp = 1:Ncomp  % plotting final Attractors
    
  % extract the current triangle set represented by a 
  % p by 1 vector of +-1s to a 3 by p/3 binary matrix  
  trB = reshape((Attr(:,komp) + 1)/2, p/3, 3)  ;
  
  % lateral shift of a triangle set
  Dxx = 70;  Dx = (komp-1)*Dxx; Dy = 90 ;
  % Vertices of three triangels in the current set

  axes(handles.finalSet)
  plotSet(trB, edg, Dxx, Dx, Dy)
  axis([0  Ncomp*Dxx 0 Dy]), axis off
  plot([Ncomp*Dxx 0 0 Ncomp*Dxx], [0 0 Dy Dy]) 

end  % final attractors (compositions)

% plotting the final numerical results in the GUI

 set(handles.results,'String', ...
 ['# Killed synapses:', sprintf('\n'), ...
  num2str(NofKilledSynapses), sprintf('\n'), sprintf('\n'), ...
 'Fraction of killed synapses:', sprintf('\n'), ...
  num2str(relativeKill, '%1.2f '), sprintf('\n'), sprintf('\n'), ...
 '# Different Elements:', sprintf('\n'), ...
  num2str(NofDiffElements), sprintf('\n'), sprintf('\n'), ...
'# Relaxation runs:', ...
  sprintf('\n'), num2str(cntS)]) ;
  
hold off




