% journalClubWheel.m
% J. Hou 21 August 2009
% Virtual wheel that decides who is presenting for journal club

% close all;
% clear all;

warning off MATLAB:pie:NonPositiveData

%% Group members and journals (alphabetical)
names = {'Adam', 'Alex', 'Ariana', 'Daniel', 'Dougal', 'Hohjai', 'Jennifer', 'Joel',  'Lucy', 'Min Ju', 'Nan', 'Rohini', 'Sabrina', 'Yiqiao'};
journals = {'BPJ', 'Cell', 'JACS', 'Nano Letters', 'Nature', 'Nature Methods', 'Nature Physics', 'Optics Express', 'PNAS', 'PRL', 'Science'};

nNames = length(names);
nJournals = length(journals);

%% Prompt for lab directory, number of presentations, save or not

options.Resize = 'on';
options.WindowStyle = 'modal';

output = inputdlg({'Server directory:', 'Number of presenters:', 'Save results?"', 'Date of JC:'}, 'Input Options', 1, {'Z:', '2', 'y', date}, options);

subpath = '\Lab\Computer Code\Journal Club';
path = [output{1} subpath];
nPresent = str2double(output{2});
saveResults = output{3};

%% Prompt for attendance list
promptNames=cell(nNames, 1);
for iName = 1:nNames
    promptNames{iName} = [names{iName} ':'];
end

% Default is 'y' for present, user changes to 'n' if not going to be
% present for journal club date
defaultAnswer = cell(size(promptNames));
for iName = 1:nNames
    defaultAnswer{iName} = 'y';
end

present = inputdlg(promptNames, 'y/n', 1, defaultAnswer, options);
present = cell2mat(present);

%% Probabilties for being chosen

probN = double(present == 'y'); % Probabilities for names
probJ = [1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2]; % Probabilities for journals

% Reduce probability if presented in last 5 weeks

reducedProb = 0.25;
fileList = dir([path filesep 'journalClub*.mat']);
nMemory = length(fileList); %memory since summer 2011
if ~isempty(fileList)
    for iMemory = 1:nMemory
        
        tmpReducedProb = 1 - (1-reducedProb)^(nMemory/(nMemory-iMemory+1));
        lastJC = fileList(end - iMemory + 1).name; %iMemory'th last journal club
        load([path filesep lastJC]);
        
        for i = 1:length(pickNAllOld)
            iN = strcmp(namesOld(pickNAllOld(i)), names);
            iJ = strcmp(journalsOld(pickJAllOld(i)), journals);
            
            if max(iN) > 0
                probN(iN) = probN(iN)*tmpReducedProb;
            end
            
            if max(iJ) > 0
                probJ(iJ) = probJ(iJ)*tmpReducedProb;
            end
        end % loop for each presenter
    end % iMemory loop
end % if there is memory

%% Simulation of wheel

% Set figure to desired size
h = figure();
screenSize = get(0,'ScreenSize');
set(h, 'Position', [screenSize(3)/8 screenSize(4)*1/8 screenSize(3)*3/4 screenSize(4)*3/4])

% Start loop for each presentation
for iPresent = 1:nPresent
    
    nTurns = round(5 + 3*rand); % length of simulation
    
    choiceN = rand(1, nTurns); % name choice per turn
    probNcum = [0; cumsum(probN)];
    probNcum = probNcum/probNcum(end);
    
    choiceJ = rand(1, nTurns); % journal choice per turn
    probJcum = [0, cumsum(probJ)];
    probJcum = probJcum/probJcum(end);
    
    pause(3);
    
    for iTurn = 1:nTurns
        
        pickN = sum(choiceN(iTurn)>=probNcum);
        pickJ = sum(choiceJ(iTurn)>=probJcum);
        
        % Plot pie chart of names
        subplot(1,2,1)
        highlightArray = zeros(size(probN));
        highlightArray(pickN) = 1;
        pie(probN, highlightArray, names)
        
        xlim([-1.5, 1.5])
        ylim([-1.5, 1.5])
        axis equal;
        
        % Plot pie chart of journals
        subplot(1,2,2)
        highlightArray = zeros(size(probJ));
        highlightArray(pickJ) = 1;
        pie(probJ, highlightArray, journals)
        
        xlim([-1.5, 1.5])
        ylim([-1.5, 1.5])
        axis equal;
        
        % Show text of already chosen names and journals
        for iiPresent = 1:iPresent-1
            subplot(1,2,1)
            text(-2,2.5-iiPresent*0.25, ['Presenter ' num2str(iiPresent) ': ' names{pickNAll(iiPresent)}], 'fontSize', 14);
            subplot(1,2,2)
            text(-2,2.5-iiPresent*0.25, ['Journal ' num2str(iiPresent) ': ' journals{pickJAll(iiPresent)}], 'fontSize', 14);
        end
        
        pause(0.75);
        
    end %iTurn loop
    
    % Save last choice of name and journal
    pickNAll(iPresent) = pickN;
    pickJAll(iPresent) = pickJ;
    
    % Remove current choice from future selection
    probN(pickN) = 0;
    probJ(pickJ) = 0;
    
    % Show newest choice in text
    subplot(1,2,1)
    text(-2,2.5-iPresent*0.25, ['Presenter ' num2str(iPresent) ': ' names{pickNAll(iPresent)}], 'fontSize', 14);
    subplot(1,2,2)
    text(-2,2.5-iPresent*0.25, ['Journal ' num2str(iPresent) ': ' journals{pickJAll(iPresent)}], 'fontSize', 14);
    
end %iPresent loop

%% Save the data if desired

if saveResults == 'y'
    
    % Format date of journal club into desired string
    jcStr = num2str(datevec(output{4}), '%02.f');
    jcDate = [jcStr(3:4) jcStr(7:8) jcStr(11:12)];
    
    % Save as .mat data file onto server
    namesOld = names;
    journalsOld = journals;
    pickNAllOld = pickNAll;
    pickJAllOld = pickJAll;
    save([path filesep 'journalClub' jcDate '.mat'], 'namesOld', 'journalsOld', 'pickNAllOld', 'pickJAllOld');
    
    % Write latest choice into .txt file
    fileName = fopen([path filesep 'JC_history.txt'], 'a')
    fprintf(fileName, '\r\nWeek of: %s \r\n', output{4});
    fprintf(fileName, 'Presenters: \r\n');
    fprintf(fileName, '%s\t \r\n', names{pickNAll});
    fprintf(fileName, '\r\n');
    fprintf(fileName, 'Journals: \r\n');
    fprintf(fileName, '%s\t \r\n', journals{pickJAll});
    fclose(fileName);
    
end

