%% function FixInVid
%% Input
% path: path to data
% efed: experiment (EFE1/EFE2; EFED1/EFED2)
% mini: low limit for interval of video
% maxi: high limit for interval of video
% limitup: limit to duration of fixation
% limitdwn: lower limit to duration of fixation
%% Output
% graph showing fixation in video and duration.

function FixInVid(path, efed, mini, maxi, limitup, limitdwn)
load(path)
r=struct;
if strcmp(efed,'EFE1') ||  strcmp(efed,'EFE2')
    nom='image';
else
    nom='video';
end
for a=1:118
    fieldname=sprintf('%s_%d', nom, a)
    r(:).(fieldname)=0;
end

for i=1:length(fieldnames(EventFix.(efed))) % for all subjects
    subj=fieldnames(EventFix.(efed));
    subj=subj(i,1);
    subj=subj{1,1};      % picks i subject
    for j=1:length(EventFix.(efed).(subj))
        RealName=EventFix.(efed).(subj)(1,j).TrueName
        if isempty(EventFix.(efed).(subj)(1,j).Roi) %if doesnt contain ROI, then move on
        r.(RealName)=r.(RealName);
        else
            mat=EventFix.(efed).(subj)(1, j).FixInVid; % load the matrix of fixations                
         %% cut to interval and optimalization (takes the whole duration of fixation that belongs in the interval)
            l=1;
            m=1;
            if sum(mat(:,mini))==1 
                while sum(mat(:,mini-l))==1
                    l=l+1;
                end
                minim=mini-l;
            else 
                minim=mini;
            end
            if sum(mat(:,maxi))==1 
                while sum(mat(:,maxi+m))==1
                    m=m+1;
                end
                maxim=maxi+m;
            else 
                maxim=maxi;
            end
            mat(:,[1:minim])=0;
            mat(:,[maxim:end])=0; % now we have ones only where the fixation appear in given interval    
            %% Number of fixations in the interval
            fix=zeros(1,13);
            duration=zeros(1,13);
            B.NumObjects=[];
            for pp=1:9     % number of rows (just ROI!!!)
                B = bwconncomp(mat(pp, [mini:maxi])); % connects components in each row (1 next to each other)
                pom=0;
                dur=0;
                for o=1:B.NumObjects % Check length of every fixation if it fits the condition.
                    if (length(B.PixelIdxList{1,o}))>=limitup || (length(B.PixelIdxList{1,o}))<=limitdwn
                        pom=pom+1;
                     else
                        pom=pom+0;
                        dur=length(B.PixelIdxList{1,o})+dur; % sums all durations in the row that belong to the interval
                    end
                end
                fix(pp)=B.NumObjects-pom; % gives number of fixations in each row (sum gives total number of fix)
            end
            r.(RealName)=r.(RealName)+sum(fix) ;
        end
    end
end

for i =1:70
    name=fieldnames(r)
    name=name(i,1)
    name=name{1,1}
    vector(i)=(r.(name)/length(fieldnames(EventFix.(efed))))
end
figure()
subplot(2,2,1)
bar(vector(1:12))
xlabel('index of the stimuli')
ylabel('avg number of fixations')
title('Happiness')
subplot(2,2,2)
bar(vector(13:24))
xlabel('index of the stimuli')
ylabel('avg number of fixations')
title('Surprise')
subplot(2,2,3)
bar(vector(25:34))
xlabel('index of the stimuli')
ylabel('avg number of fixations')
title('Fear')
subplot(2,2,4)
bar(vector(35:46))
xlabel('index of the stimuli')
ylabel('avg number of fixations')
title('Disgust')
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');

text(0.5, 1,'\bf Average number of fixations in video per emotion','HorizontalAlignment','center','VerticalAlignment', 'top')

