-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinderrors.m
More file actions
213 lines (163 loc) · 7.25 KB
/
finderrors.m
File metadata and controls
213 lines (163 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
% The variables of interest are
% falseneg
% falsepos
% hammingdist_avg_unsorted
% hammingdist_var_unsorted
% hammingdist_avg_sorted
% hammingdist_var_sorted
% permutation_error_avg
% permutation_error_var
% load('findingerrors.mat');
clear;
hname=char(importdata('humandataname.txt'));
htime=(importdata('humandatatime.txt'));
hstate=(importdata('humandatastate.txt'));
cname=char(importdata('compdataname.txt'));
ctime=(importdata('compdatatime.txt'));
cstate=(importdata('compdatastate.txt'));
twindow=10;% We will consdier the events in the range of 2*twindow around the current event
clip=2;%Clip number (Change this to see error parameters for different clip numbers)
hnoofevents=find(diff(htime)<0);%number of events recorded by human. Looking at where the difference between adjacent times is negative we can know where a new clip starts
hnoofevents=[0;hnoofevents;length(htime)];
cnoofevents=find(diff(ctime)<0);%number of events recorded by computer
cnoofevents=[0;cnoofevents;length(ctime)];
hname=hname(hnoofevents(clip)+1:hnoofevents(clip+1),:);%ant names recorded by human
hstate=hstate(hnoofevents(clip)+1:hnoofevents(clip+1));%state of ant as recorded by human. Can be - started drinking (SD) or ended drinking (ED)
hstate=char(hstate);
hstate=hstate(:,1);%Discard 'D' from 'SD' and 'ED'
htime=htime(hnoofevents(clip)+1:hnoofevents(clip+1));%timing of events as recorded by human
cname=cname(cnoofevents(clip)+1:cnoofevents(clip+1),:);%ant names recorded by computer
cstate=cstate(cnoofevents(clip)+1:cnoofevents(clip+1));%state of ant as recorded by computer. Can be - started drinking (SD) or ended drinking (ED)
cstate=char(cstate);
cstate=cstate(:,2);
ctime=ctime(cnoofevents(clip)+1:cnoofevents(clip+1));%timing of events as recorded by human
falseneg=0;
falsepos=0;
% PART 1
% loop to calculate false positive error (incorrectly detected events)
for i=1:(cnoofevents(clip+1)-cnoofevents(clip))
t_error=twindow;
tmin=ctime(i)-twindow;
tmax=ctime(i)+twindow;
temp=find(htime<tmax);
temp2=find(htime>tmin);
temp=intersect(temp2,temp);%indexes where htime belongs to the range ctime-5 and ctime+5
flag=0;
if(~isempty(temp))
for j=1:length(temp)
if(strcmp(hstate(temp(j)),cstate(i))&&strcmp(hname(temp(j),:),cname(i,:))) % find an event where the ant name state recorded by the computer matches that recorded by the human
flag=1;
if(abs(htime(temp(j))-ctime(i))<t_error)%Find time difference between closest similar event
t_error=abs(htime(temp(j))-ctime(i));%make terror an accumulator if looking for average error
end
end
end
end
if(flag==0)
falsepos=falsepos+1/(cnoofevents(clip+1)-cnoofevents(clip));
end
end
% loop to calculate false negative error (incorrectly missed events)
for i=1:(hnoofevents(clip+1)-hnoofevents(clip))
t_error=twindow;
tmin=htime(i)-twindow;
tmax=htime(i)+twindow;
temp=find(ctime<tmax);
temp2=find(ctime>tmin);
temp=intersect(temp2,temp);%indexes where htime belongs to the range ctime-5 and ctime+5
flag=0;
if(~isempty(temp))
for j=1:length(temp)
if(strcmp(cstate(temp(j)),hstate(i))&&strcmp(cname(temp(j),:),hname(i,:))) % find an event where the human name state recorded by the human matches that recorded by the computer
flag=1;
if(abs(ctime(temp(j))-htime(i))<t_error)
t_error=abs(ctime(temp(j))-htime(i));
end
end
end
end
if(flag==0)
falseneg=falseneg+1/(hnoofevents(clip+1)-hnoofevents(clip));
end
end
%PART 2
% loop to calculate average hamming distance for unsorted strings (not considering presence of anagrams)
flag=0;
for i=1:(cnoofevents(clip+1)-cnoofevents(clip))
tmin=ctime(i)-twindow;
tmax=ctime(i)+twindow;
temp=find(htime<tmax);
temp2=find(htime>tmin);
temp=intersect(temp2,temp);%indexes where htime belongs to the range ctime-5 and ctime+5
if(~isempty(temp))
flag=flag+1;
hamming_error_unsorted(flag)=4;%maximum hamming distance for strings of length 4
for j=1:length(temp)
if(strcmp(hstate(temp(j)),cstate(i))) % find an event where the ant name state recorded by the computer matches that recorded by the human
if(sum(hname(temp(j),:)~=cname(i,:))<hamming_error_unsorted(flag))%Find time difference between closest similar event
hamming_error_unsorted(flag)=sum(hname(temp(j),:)~=cname(i,:));
end
end
end
end
end
hammingdist_avg_unsorted=mean(hamming_error_unsorted);
hammingdist_var_unsorted=var(hamming_error_unsorted);
%PART 3
% loop to calculate average hamming distance for sorted strings (considering presence of anagrams)
flag=0;
for i=1:(cnoofevents(clip+1)-cnoofevents(clip))
tmin=ctime(i)-twindow;
tmax=ctime(i)+twindow;
temp=find(htime<tmax);
temp2=find(htime>tmin);
temp=intersect(temp2,temp);%indexes where htime belongs to the range ctime-5 and ctime+5
if(~isempty(temp))
flag=flag+1;
hamming_error_sorted(flag)=4;%maximum hamming distance for strings of length 4
tempname1=sort(cname(i,:));
for j=1:length(temp)
tempname2=sort(hname(temp(j),:));
if(strcmp(hstate(temp(j)),cstate(i))) % find an event where the ant name state recorded by the computer matches that recorded by the human
if(sum(tempname1~=tempname2)<hamming_error_sorted(flag))%Find time difference between closest similar event
hamming_error_sorted(flag)=sum(tempname1~=tempname2);
end
end
end
end
end
hammingdist_avg_sorted=mean(hamming_error_sorted);
hammingdist_var_sorted=var(hamming_error_sorted);
%PART 4
% loop to find average minimum distance between cname strings and
% permutations of hname strings
flag=0;
for i=1:(cnoofevents(clip+1)-cnoofevents(clip))
tmin=ctime(i)-twindow;
tmax=ctime(i)+twindow;
temp=find(htime<tmax);
temp2=find(htime>tmin);
temp=intersect(temp2,temp);%indexes where htime belongs to the range ctime-5 and ctime+5
if(~isempty(temp))
flag=flag+1;
permutation_error(flag)=4;%maximum distance between permutation for strings of length 4
temperror=0;
for j=1:length(temp)
if(strcmp(hstate(temp(j)),cstate(i))) % find an event where the ant name state recorded by the computer matches that recorded by the human
strintersect=intersect(hname(temp(j),:),cname(i,:));
strunique=setdiff(cname(i,:),hname(temp(j),:));
for k=1:length(strintersect)
temperror=temperror+abs(length(find(cname(i,:)==strintersect(k)))-length(find(hname(temp(j),:)==strintersect(k))));
end
for k=1:length(strunique)
temperror=temperror+length(find(cname(i,:)==strunique(k)));
end
if(temperror<permutation_error(flag))
permutation_error(flag)=temperror;
end
end
end
end
end
permutation_error_avg=mean(permutation_error);
permutation_error_var=var(permutation_error);