function varargout = sdtGUI(varargin)
% SDTGUI MATLAB code for sdtGUI.fig
%      SDTGUI, by itself, creates a new SDTGUI or raises the existing
%      singleton*.
%
%      H = SDTGUI returns the handle to a new SDTGUI or the handle to
%      the existing singleton*.
%
%      SDTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SDTGUI.M with the given input arguments.
%
%      SDTGUI('Property','Value',...) creates a new SDTGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before sdtGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to sdtGUI_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 sdtGUI

% Last Modified by GUIDE v2.5 15-Dec-2017 11:34:50

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @sdtGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @sdtGUI_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 sdtGUI is made visible.
function sdtGUI_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 sdtGUI (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = sdtGUI_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 when figure1 is resized.
function figure1_SizeChangedFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function mean1_Callback(hObject, eventdata, handles)
% hObject    handle to mean1 (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 mean1 as text
%        str2double(get(hObject,'String')) returns contents of mean1 as a double


% --- Executes during object creation, after setting all properties.
function mean1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mean1 (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



function mean2_Callback(hObject, eventdata, handles)
% hObject    handle to mean2 (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 mean2 as text
%        str2double(get(hObject,'String')) returns contents of mean2 as a double


% --- Executes during object creation, after setting all properties.
function mean2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mean2 (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



function sd1_Callback(hObject, eventdata, handles)
% hObject    handle to sd1 (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 sd1 as text
%        str2double(get(hObject,'String')) returns contents of sd1 as a double


% --- Executes during object creation, after setting all properties.
function sd1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sd1 (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



function sd2_Callback(hObject, eventdata, handles)
% hObject    handle to sd2 (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 sd2 as text
%        str2double(get(hObject,'String')) returns contents of sd2 as a double


% --- Executes during object creation, after setting all properties.
function sd2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sd2 (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 plotCurves.
function plotCurves_Callback(hObject, eventdata, handles)
% hObject    handle to plotCurves (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Calculate the mean difference by reading out from parameter fields
%Also calculate dPrime
M1 = str2num(get(handles.mean1,'String'));
M2 = str2num(get(handles.mean2,'String'));
meanDiff = M2-M1 %Mean difference
std1 = str2num(get(handles.sd1,'String'))
std2 = str2num(get(handles.sd2,'String'))
handles.dp = meanDiff./sqrt((std1^2)/2+(std2^2)/2); %d prime
set(handles.dPrime,'String',num2str(handles.dp))

%Actually plot the curves by creating x and y values
leftEdge = M1-4*std1; %Be careful if stds are radically different
rightEdge = M2+4*std2; %Hardcoding plotting 4 sds. 
handles.x = leftEdge:0.01:rightEdge; 
handles.y = normpdf(handles.x,M1,std1); 
handles.y2 = normpdf(handles.x,M2,std2); 
axes(handles.curves) %Switch to the axes
hold off %In case we change parameters, don't plot in it
plot(handles.x,handles.y, 'color','r','linewidth', 2)
hold on
plot(handles.x,handles.y2, 'color','g','linewidth', 2)
box off

%Make the axes and results show up
set(handles.curves,'visible','on')
set(handles.results,'visible','on')

handles.output = handles.x;

%Make sure to update the handles structure at the end. Otherwise, other
%functions will not have access to the updated handles structure
guidata(hObject, handles);



function dPrime_Callback(hObject, eventdata, handles)
% hObject    handle to dPrime (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 dPrime as text
%        str2double(get(hObject,'String')) returns contents of dPrime as a double


% --- Executes during object creation, after setting all properties.
function dPrime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dPrime (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 plotROC.
function plotROC_Callback(hObject, eventdata, handles)
% hObject    handle to plotROC (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%plotCurves_Callback(hObject, eventdata, handles)
%This should work, but we need to double-check nesting of functions. 
%For now, make sure to plot the curves before plotting the ROC

axes(handles.roc)
hold off
%Idea: Go through all elements of x and calculate the cumulative False
%alarm and hit rate
for ii = 1:length(handles.x) %Go through all elements of x
    FA(ii) = sum(handles.y(1,ii:length(handles.x))); %Summing from ith element to rest
    HIT(ii) = sum(handles.y2(1,ii:length(handles.x))); %Hit rate
end
HIT = HIT/100;
FA = FA/100;
plot(FA,HIT,'linewidth',3)
xlabel('False alarms')
ylabel('Hits')
%Add chance/reference
hold on
d2 = plot([0 1],[0 1]);
set(d2,'linestyle','--')
box off
set(handles.roc,'visible','on')

guidata(hObject, handles);


function hr_Callback(hObject, eventdata, handles)
% hObject    handle to hr (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 hr as text
%        str2double(get(hObject,'String')) returns contents of hr as a double


% --- Executes during object creation, after setting all properties.
function hr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to hr (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



function fa_Callback(hObject, eventdata, handles)
% hObject    handle to fa (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 fa as text
%        str2double(get(hObject,'String')) returns contents of fa as a double


% --- Executes during object creation, after setting all properties.
function fa_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fa (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 criterion.
function criterion_Callback(hObject, eventdata, handles)
% hObject    handle to criterion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.thresh = ginput(1); 
tempX = handles.x-handles.thresh(1); 
[temp1 temp2] = min(abs(tempX)); 
axes(handles.curves)
thresholdInX = temp2; 
line([handles.thresh(1) handles.thresh(1)],[min(ylim) max(ylim)],'color','k')
pHit = sum(handles.y2(thresholdInX:length(handles.x)));
set(handles.hr,'String',num2str(pHit/100,'%1.2f'))
pFA = sum(handles.y(thresholdInX:length(handles.x)))
set(handles.fa,'String',num2str(pFA/100,'%1.2f'))
axes(handles.roc)
hold on
plot(pFA/100,pHit/100,'.','color','k','markersize',20)
guidata(hObject, handles);
