File size: 32,470 Bytes
b131c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
function [nbr_types,confusion_matrix_aut_Vis1,confusion_matrix_aut_Vis2,confusion_matrix_Vis1_Vis2,confusion_matrix_Vis2_Vis1,confusion_matrix_aut_Vis1uVis2,confusion_matrix_aut_Vis1nVis2]=assessmentMethod(filename_edf,filename_Automatic_detection,filename_Visual_scoring1,filename_Visual_scoring2,filename_Hypnogram,duration_event)
% Function assessmentMethod.m compare the automatic and visual annotations of one type of micro-event.
% It uses as inputs:
% filename_edf : filename of the polysomnographic recording (in the European Data Format) from which the annoted channel is extracted. 
% filename_Automatic_detection : filename of a textual file containing, in the first line, the name of the detection and then, 2 columns with the beginnings (first column) and durations (second column) of the automatically detected micro-events (in second)
% filename_Visual_scoring1  : filename of a textual file containing, in the first line, the name of the detection and then, 2 columns with the beginnings (first column) and durations (second column) of the visually scored micro-events by expert 1(in second)
% filename_Visual_scoring2 : filename of a textual file containing, in the first line, the name of the detection and then, 2 columns with the beginnings (first column) and durations (second column) of the visually scored micro-events by expert 2(in second)
% If this second annotation is not available, use filename_Visual_scoring2='';
% filename_Hypnogram: filename of a textual file containing from the second line the hypnogram information, with one value per 5 sec.
%   5=wake
%   4=REM stage
%   3=sleep stage S1
%   2=sleep stage S2
%   1=sleep stage S3
%   0=sleep stage S4
%   -1=sleep stage movement
%   -2 or -3 =unknow sleep stage %
% If this Hypnogram is not available, use filename_Hypnogram='';
% duration_event: is the average duration (in second) of the microevent detected (for the approximation of the True Negative number #TN) in confusion matrixes
%
% Function assessmentMethod.m returns the structure "nbr_types" with the following fields:
% nbr_types.nbrtot_scored_by_system = total number of events scored by the automatic system, in each stages (wake,REM,S1,S2,S3,S4), and in total (=> vector with 7 values)
% nbr_types.nbrtot_scored_by_visual1 = total number of events scored by the expert 1, in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbrtot_scored_by_visual2 = total number of events scored by the expert 2, in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_scored_only_by_sys__type2 = number of coverings scored by only system (of type T2), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_scored_only_by_vis1__type3A = number of coverings scored by only scorer #1 (of type T 3A), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_scored_only_by_vis2__type3B = number of coverings scored by only scorer #2 (of type T3B), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_scored_only_by_systEvis1__type1A = number of coverings scored by only system & scorer #1(of type T1A), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_scored_only_by_systEvis2__type1B = number of coverings scored by only system & scorer #2 (of type T1B), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_scored_only_by_vis1Evis2__type3C = number of coverings scored by only scorer #1 & scorer #2 (of type T3C), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C = number of coverings scored by system & scorer #1 & #2 (of type T1C), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.type3D = number of coverings of type T3D, in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.type5A = number of coverings of type T5A, in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.type5B = number of coverings of type T5B, in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.type5C = number of coverings of type T5C, in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_aut_multiple = number of automatic quotation implied in a multiple covering (3D, 5A,5B or 5C), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_vis1_multiple = number of quotation of scorer 1 implied in a multiple covering (3D, 5A,5B or 5C), in each stages (wake,REM,S1,S2,S3,S4), and in total 
% nbr_types.nbr_vis2_multiple = number of quotation of scorer 2 implied in a multiple covering (3D, 5A,5B or 5C), in each stages (wake,REM,S1,S2,S3,S4), and in total 
%
% It also returns the different possible confusion matrixes:
% confusion_matrix_aut_Vis1 = confusion matrix of the automatic detection compared to the expert 1 scoring
% confusion_matrix_aut_Vis2 = confusion matrix of the automatic detection compared to the expert 2 scoring
% confusion_matrix_Vis1_Vis2 = confusion matrix of the expert 1 scoring compared to the expert 2 scoring
% confusion_matrix_Vis2_Vis1 = confusion matrix of the expert 2 scoring compared to the expert 1 scoring
% confusion_matrix_aut_Vis1uVis2= confusion matrix of the automatic detection compared to the union of the expert 1 scoring and the expert 2 scoring
% confusion_matrix_aut_Vis1nVis2= confusion matrix of the automatic detection compared to the intersection of the expert 1 scoring and the expert 2 scoring (i.e. when a micro event is considered as real when both scorers marked it as such)
%
% NB: in these confusion matrixes, the number of true negative is aproximate by : (total duration of the excerpt/average duration of the microevent) - (number of true positives + number of false positives + number of false negatives)
%
% Examples:
% filename_edf='C:\DEVUYST\DataBaseSpindles\excerpt1.edf';
% filename_Automatic_detection='C:\DEVUYST\DataBaseSpindles\Automatic_detection_excerpt1.txt';
% filename_Visual_scoring1='C:\DEVUYST\DataBaseSpindles\Visual_scoring1_excerpt1.txt';
% filename_Visual_scoring2='C:\DEVUYST\DataBaseSpindles\Visual_scoring2_excerpt1.txt';
% filename_Hypnogram='C:\DEVUYST\DataBaseSpindles\Hypnogram_excerpt1.txt';
% duration_event=1;
% [nbr_types,confusion_matrix_aut_Vis1,confusion_matrix_aut_Vis2,confusion_matrix_Vis1_Vis2,confusion_matrix_Vis2_Vis1,confusion_matrix_aut_Vis1uVis2,confusion_matrix_aut_Vis1nVis2]=assessmentMethod(filename_edf,filename_Automatic_detection,filename_Visual_scoring1,filename_Visual_scoring2,filename_Hypnogram,duration_event)
% 
% or
% filename_edf='C:\DEVUYST\DataBasePLMs\excerpt1.edf';
% filename_Automatic_detection='C:\DEVUYST\DataBasePLMs\Automatic_detection_excerpt1.txt';
% filename_Visual_scoring1='C:\DEVUYST\DataBasePLMs\Visual_scoring1_excerpt1.txt';
% filename_Visual_scoring2='';
% filename_Hypnogram='';
% duration_event=3;
% [nbr_types,confusion_matrix_aut_Vis1,confusion_matrix_aut_Vis2,confusion_matrix_Vis1_Vis2,confusion_matrix_Vis2_Vis1,confusion_matrix_aut_Vis1uVis2,confusion_matrix_aut_Vis1nVis2]=assessmentMethod(filename_edf,filename_Automatic_detection,filename_Visual_scoring1,filename_Visual_scoring2,filename_Hypnogram,duration_event)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   DETERMINATION DE LA FREQUENCE D'ECHANTILLONAGE ET DE LA DUREE DE L'ENREGISTREMENT  %   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%determination du nom de signal concern�
%---------------------------------------
newpid=fopen(filename_Automatic_detection, 'rb+');%ouvre un fichier texte du nom de filename en lecture et �criture binaire
C_text = textscan(newpid, '%s',1);
nom_annotation=C_text{1,1};
[temp,signal_concerne]=strtok(nom_annotation{1},'/');
signal_concerne=signal_concerne(2:end-1);
fclose(newpid); 

%detrmination de fech
%---------------------
pid=fopen(filename_edf,'rb');
fseek(pid,236,-1);%on se met � la position par rapport au d�but du fichier
nb_records=str2num(sprintf('%c',fread(pid,8,'char')));
duration_record=str2num(sprintf('%c',fread(pid,8,'char')));
nb_tot_signals=str2num(sprintf('%c',fread(pid,4,'char')));
signals=[];
for i=1:nb_tot_signals
    fseek(pid,256+(i-1)*16,-1);
    signals(i).label=strtok(sprintf('%c',fread(pid,16,'char')),' ');
    fseek(pid,256+nb_tot_signals*(16+80)+(i-1)*8,-1);
    signals(i).phys_dim=strtok(sprintf('%c',fread(pid,8,'char')),' ');
    fseek(pid,256+nb_tot_signals*(16+80+8)+(i-1)*8,-1);
    signals(i).phys_min=str2num(sprintf('%c',fread(pid,8,'char')));
    fseek(pid,256+nb_tot_signals*(16+80+8+8)+(i-1)*8,-1);
    signals(i).phys_max=str2num(sprintf('%c',fread(pid,8,'char')));
    fseek(pid,256+nb_tot_signals*(16+80+8+8+8)+(i-1)*8,-1);
    signals(i).digit_min=str2num(sprintf('%c',fread(pid,8,'char')));
    fseek(pid,256+nb_tot_signals*(16+80+8+8+8+8)+(i-1)*8,-1);
    signals(i).digit_max=str2num(sprintf('%c',fread(pid,8,'char')));
    fseek(pid,256+nb_tot_signals*(16+80+8+8+8+8+8+80)+(i-1)*8,-1);
    signals(i).nb_samples=str2num(sprintf('%c',fread(pid,8,'char')));
    signals(i).fech=signals(i).nb_samples/duration_record;    
end
fseek(pid,256+nb_tot_signals*(16+80+8*5+80),-1);
for i=1:nb_tot_signals
    nr_tot_signals(i)=str2num(sprintf('%c',fread(pid,8,'char')));
end
handles.header.info_tot_signals=signals;
fclose(pid);

%[handles]=readEdf ({},filename_edf,[],1,0);%chargement de son ent�te (duration bloc etc)
fech=0;
for i=1:length(handles.header.info_tot_signals)
    if strcmp(handles.header.info_tot_signals(i).label,signal_concerne)
        fech=handles.header.info_tot_signals(i).fech;
    end
end
if fech==0
    if ismember(handles.header.info_tot_signals(1).fech ,[50, 100,200])
        fech=handles.header.info_tot_signals(1).fech;
    else
        error('blem avec fech')
    end
end

%detrmination de la duree de l'enregistrement
%---------------------------------------------
duree_enregistrement=duration_record*nb_records;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CHARGEMENT COTATION ATOMATIQUE %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
newpid=fopen(filename_Automatic_detection, 'rb+');%ouvre un fichier texte du nom de filename en lecture et �criture binaire
C_text = textscan(newpid, '%s',1);
nom_annotation=C_text{1};
C_data = textscan(newpid, '%f %f');
cotation_automatique.debut=C_data{1}*fech;
cotation_automatique.duree=C_data{2}*fech;
fclose(newpid);     

[val_sort,pos_sort]=sort(cotation_automatique.debut);
cotation_automatique.debut=cotation_automatique.debut(pos_sort);
cotation_automatique.duree=cotation_automatique.duree(pos_sort);
if size(cotation_automatique.debut,2)==1
    cotation_automatique.debut=cotation_automatique.debut';

    cotation_automatique.duree=cotation_automatique.duree';
end
temp=find(cotation_automatique.debut+cotation_automatique.duree>=[cotation_automatique.debut(2:end), cotation_automatique.debut(end)+cotation_automatique.duree(end)+1]);
milieu=floor(cotation_automatique.debut(temp+1)+((cotation_automatique.debut(temp)+cotation_automatique.duree(temp)-1)-cotation_automatique.debut(temp+1))/2);
cotation_automatique.duree(temp)=milieu-cotation_automatique.debut(temp);
cotation_automatique.duree(temp+1)=(cotation_automatique.debut(temp+1)+cotation_automatique.duree(temp+1)-1)-milieu;
cotation_automatique.debut(temp+1)=milieu+1;

    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CHARGEMENT COTATION SURLIGNEE 1 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
newpid=fopen(filename_Visual_scoring1, 'rb+');%ouvre un fichier texte du nom de filename en lecture et �criture binaire
C_text = textscan(newpid, '%s',1);
nom_annotation=C_text{1};
C_data = textscan(newpid, '%f %f');
cotation_reference.debut=C_data{1}*fech;
cotation_reference.duree=C_data{2}*fech;
fclose(newpid);     

[val_sort,pos_sort]=sort(cotation_reference.debut);
cotation_reference.debut=cotation_reference.debut(pos_sort);
cotation_reference.duree=cotation_reference.duree(pos_sort);
if size(cotation_reference.debut,2)==1
    cotation_reference.debut=cotation_reference.debut';

    cotation_reference.duree=cotation_reference.duree';
end
temp=find(cotation_reference.debut+cotation_reference.duree>=[cotation_reference.debut(2:end), cotation_reference.debut(end)+cotation_reference.duree(end)+1]);
milieu=floor(cotation_reference.debut(temp+1)+((cotation_reference.debut(temp)+cotation_reference.duree(temp)-1)-cotation_reference.debut(temp+1))/2);
cotation_reference.duree(temp)=milieu-cotation_reference.debut(temp);
cotation_reference.duree(temp+1)=(cotation_reference.debut(temp+1)+cotation_reference.duree(temp+1)-1)-milieu;
cotation_reference.debut(temp+1)=milieu+1;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CHARGEMENT COTATION SURLIGNEE 2 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~strcmp(filename_Visual_scoring2,'') 
    newpid=fopen(filename_Visual_scoring2, 'rb+');%ouvre un fichier texte du nom de filename en lecture et �criture binaire
    C_text = textscan(newpid, '%s',1);
    nom_annotation=C_text{1};
    C_data = textscan(newpid, '%f %f');
    cotation_reference.debut2=C_data{1}*fech;
    cotation_reference.duree2=C_data{2}*fech;
    fclose(newpid);     

    [val_sort,pos_sort]=sort(cotation_reference.debut2);
    cotation_reference.debut2=cotation_reference.debut2(pos_sort);
    cotation_reference.duree2=cotation_reference.duree2(pos_sort);
    if size(cotation_reference.debut2,2)==1
        cotation_reference.debut2=cotation_reference.debut2';

        cotation_reference.duree2=cotation_reference.duree2';
    end
    temp=find(cotation_reference.debut2+cotation_reference.duree2>=[cotation_reference.debut2(2:end), cotation_reference.debut2(end)+cotation_reference.duree2(end)+1]);
    milieu=floor(cotation_reference.debut2(temp+1)+((cotation_reference.debut2(temp)+cotation_reference.duree2(temp)-1)-cotation_reference.debut2(temp+1))/2);
    cotation_reference.duree2(temp)=milieu-cotation_reference.debut2(temp);
    cotation_reference.duree2(temp+1)=(cotation_reference.debut2(temp+1)+cotation_reference.duree2(temp+1)-1)-milieu;
    cotation_reference.debut2(temp+1)=milieu+1;
else
    cotation_reference.debut2=[];
    cotation_reference.duree2=[];
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% creation d'une cotation d'union  et d'une cotation d'intersection %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~strcmp(filename_Visual_scoring2,'')    
    num_inter=0;
    cotation_reference.debut_union=cotation_reference.debut;
    cotation_reference.duree_union=cotation_reference.duree;
    for i=1:length(cotation_reference.debut2)
        temp=find ( cotation_reference.debut_union<=(cotation_reference.debut2(i)+cotation_reference.duree2(i)-1)  & (cotation_reference.debut_union+cotation_reference.duree_union-1)>=cotation_reference.debut2(i)) ;
        if ~isempty(temp)
            num_inter=num_inter+1;
            deb=min([cotation_reference.debut_union(temp),cotation_reference.debut2(i)]);
            dur=max([cotation_reference.debut_union(temp)+cotation_reference.duree_union(temp)-1,cotation_reference.debut2(i)+cotation_reference.duree2(i)-1])-deb+1;
            cotation_reference.debut_union(temp(1))=deb;
            cotation_reference.duree_union(temp(1))=dur;
            cotation_reference.debut_inter(num_inter)=deb;
            cotation_reference.duree_inter(num_inter)=dur;
            if length(temp)>1
                cotation_reference.debut_union(temp(2:end))=[];
                cotation_reference.duree_union(temp(2:end))=[];
            end
        else%
            cotation_reference.debut_union=[cotation_reference.debut_union , cotation_reference.debut2(i)];
            cotation_reference.duree_union=[cotation_reference.duree_union , cotation_reference.duree2(i)];
        end                
    end
    [val_sort,pos_sort]=sort(cotation_reference.debut_inter);
    temp=find(val_sort==[val_sort(2:end) 0] & cotation_reference.duree_inter(pos_sort)==[cotation_reference.duree_inter(pos_sort(2:end)) 0]);
    cotation_reference.debut_inter(pos_sort(temp))=[];%qui est �gal � cotation_reference.debut_inter(pos_sort(temp+1))
    cotation_reference.duree_inter(pos_sort(temp))=[];
    [val_sort,pos_sort]=sort(cotation_reference.debut_union);
    temp=find(val_sort==[val_sort(2:end) 0] & cotation_reference.duree_union(pos_sort)==[cotation_reference.duree_union(pos_sort(2:end)) 0]);
    cotation_reference.debut_union(pos_sort(temp))=[];%qui est �gal � cotation_reference.debut_union(pos_sort(temp+1))
    cotation_reference.duree_union(pos_sort(temp))=[];
else
    cotation_reference.debut_union=cotation_reference.debut;
    cotation_reference.duree_union=cotation_reference.duree;
    cotation_reference.debut_inter=[];
    cotation_reference.duree_inter=[];
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  CALCUL DES DIFFERENTS TYPE D'EVENEMENTS  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isempty(cotation_automatique.debut)
    if isempty(cotation_reference.debut2)
        caca=zeros(1,ceil(max([cotation_reference.debut(end)+cotation_reference.duree(end),cotation_automatique.debut(end)+cotation_automatique.duree(end)])));
    else
        caca=zeros(1,ceil(max([cotation_reference.debut2(end)+cotation_reference.duree2(end),cotation_reference.debut(end)+cotation_reference.duree(end),cotation_automatique.debut(end)+cotation_automatique.duree(end)])));
    end
else
    if isempty(cotation_reference.debut2)
        caca=zeros(1,ceil(max([cotation_reference.debut(end)+cotation_reference.duree(end)])));
    else
        caca=zeros(1,ceil(max([cotation_reference.debut2(end)+cotation_reference.duree2(end),cotation_reference.debut(end)+cotation_reference.duree(end)])));
    end
end
for i=1:length(cotation_automatique.debut)
    caca(round(cotation_automatique.debut(i):(cotation_automatique.debut(i)+cotation_automatique.duree(i)-1)))=caca(round(cotation_automatique.debut(i):(cotation_automatique.debut(i)+cotation_automatique.duree(i)-1)))+3;
end
for i=1:length(cotation_reference.debut)
    caca(round(cotation_reference.debut(i):(cotation_reference.debut(i)+cotation_reference.duree(i)-1)))=caca(round(cotation_reference.debut(i):(cotation_reference.debut(i)+cotation_reference.duree(i)-1)))+5;
end
for i=1:length(cotation_reference.debut2)
    caca(round(cotation_reference.debut2(i):(cotation_reference.debut2(i)+cotation_reference.duree2(i)-1)))=caca(round(cotation_reference.debut2(i):(cotation_reference.debut2(i)+cotation_reference.duree2(i)-1)))+7;
end
caca=[0 caca 0];
pos_deb_type=find(caca(1:end-1)==0 & caca(2:end)~=0);
pos_fin_type=find(caca(2:end)==0 & caca(1:end-1)~=0)+1;

type1A=zeros(1,7);
type1B=zeros(1,7);
type1C=zeros(1,7);
type2=zeros(1,7);
type3A=zeros(1,7);
type3B=zeros(1,7);
type3C=zeros(1,7);
type3D=zeros(1,7);
type5A=zeros(1,7);
type5B=zeros(1,7);
type5C=zeros(1,7);

nombre_auto=zeros(1,7);
nombre_visu1=zeros(1,7);
nombre_visu2=zeros(1,7);

nbr_aut_multiple=zeros(1,7);
nbr_vis1_multiple=zeros(1,7);
nbr_vis2_multiple=zeros(1,7);

if ~strcmp(filename_Hypnogram,'')
    newpid=fopen(filename_Hypnogram, 'rb+');%ouvre un fichier texte du nom de filename en lecture et �criture binaire
    C_text = textscan(newpid, '%s',1);
    nom_annotation=C_text{1};
    C_data = textscan(newpid, '%f');
    num_stade=C_data{1};
    fclose(newpid);     
    data_hypn=reshape((num_stade*ones(1,5*fech))',1,size(num_stade,1)*5*fech);

    

    if (length(data_hypn)~=duree_enregistrement*fech)

        error('la duree de l''hypnogramme ne correspond pas � la duree de l''enregistrement')

    end

    for i=1:length(cotation_automatique.debut)

        numstade=abs(data_hypn(round(cotation_automatique.debut(i)+cotation_automatique.duree(i)/2))-5)+1;

        nombre_auto(numstade)=nombre_auto(numstade)+1;

    end

    for i=1:length(cotation_reference.debut)

        numstade=abs(data_hypn(round(cotation_reference.debut(i)+cotation_reference.duree(i)/2))-5)+1;

        nombre_visu1(numstade)=nombre_visu1(numstade)+1;

    end

    for i=1:length(cotation_reference.debut2)

        numstade=abs(data_hypn(round(cotation_reference.debut2(i)+cotation_reference.duree2(i)/2))-5)+1;

        nombre_visu2(numstade)=nombre_visu2(numstade)+1;

    end

end

nombre_auto(7)=length(cotation_automatique.debut);

nombre_visu1(7)=length(cotation_reference.debut);

nombre_visu2(7)=length(cotation_reference.debut2);





for i=1:length(pos_deb_type)

    if ~strcmp(filename_Hypnogram,'')

        numstade=abs(data_hypn(round((pos_deb_type(i)+pos_fin_type(i))/2))-5)+1;

    else

        numstade=7;

    end

    type=caca(pos_deb_type(i):pos_fin_type(i));

    if ~any(type==8 | type==10 | type==12 | type==15)%y'a aucune sommme =>c'est type 2 ou 3A ou 3B

        if any(type==5)%c'est type 3A (vis1 seul)
            type3A(numstade)=type3A(numstade)+1;
        elseif any(type==3)%c'est type 2 (aut seul)
            type2(numstade)=type2(numstade)+1;
        elseif any(type==7)%c'est type 3B (vis2 seul)
            type3B(numstade)=type3B(numstade)+1;
        end
    else %on a une somme
        if ~any(type==3 |type==8 | type==10 | type==15)%on a pas d'intervention de automatique dans la somme => type 3C ou 3D
            nbr_tras_de5vers12=length(find(type(1:end-1)==5 & type(2:end)==12));
            nbr_tras_de7vers12=length(find(type(1:end-1)==7 & type(2:end)==12));
            nbr_tras_de0vers12=length(find(type(1:end-1)==0 & type(2:end)==12));
            if (nbr_tras_de5vers12+nbr_tras_de7vers12+nbr_tras_de0vers12)>1 %on a des transition multiple=> type 3D
                type3D(numstade)=type3D(numstade)+1;
                nbr_vis1_multiple(numstade)=nbr_vis1_multiple(numstade)+length(find(cotation_reference.debut>=pos_deb_type(i)-1 & (cotation_reference.debut+cotation_reference.duree)<=pos_fin_type(i)+1));
                nbr_vis2_multiple(numstade)=nbr_vis2_multiple(numstade)+length(find(cotation_reference.debut2>=pos_deb_type(i)-1 & (cotation_reference.debut2+cotation_reference.duree2)<=pos_fin_type(i)+1));
            else %type 1
                type3C(numstade)=type3C(numstade)+1;
            end
        else%on a une somme o� aut intervient
            if ~any(type==5 |type==12 |type==8 | type==15)%on n'a pas d'intervention de Vis1 => type 1B ou 5B
                nbr_tras_de3vers10=length(find(type(1:end-1)==3 & type(2:end)==10));
                nbr_tras_de5vers10=length(find(type(1:end-1)==5 & type(2:end)==10));
                nbr_tras_de0vers10=length(find(type(1:end-1)==0 & type(2:end)==10));
                if (nbr_tras_de3vers10+nbr_tras_de5vers10+nbr_tras_de0vers10)>1 %on a des transition multiple=> type 5B
                    type5B(numstade)=type5B(numstade)+1;
                    nbr_aut_multiple(numstade)=nbr_aut_multiple(numstade)+length(find(cotation_automatique.debut>=pos_deb_type(i)-1 & (cotation_automatique.debut+cotation_automatique.duree)<=pos_fin_type(i)+1));
                    nbr_vis2_multiple(numstade)=nbr_vis2_multiple(numstade)+length(find(cotation_reference.debut2>=pos_deb_type(i)-1 & (cotation_reference.debut2+cotation_reference.duree2)<=pos_fin_type(i)+1));

                else %type 1B
                    type1B(numstade)=type1B(numstade)+1;
                end
            else
                if ~any(type==7 |type==12 |type==10 | type==15)%on n'a pas d'intervention de Vis2 => type 1A ou 5A
                    nbr_tras_de3vers8=length(find(type(1:end-1)==3 & type(2:end)==8));
                    nbr_tras_de5vers8=length(find(type(1:end-1)==5 & type(2:end)==8));
                    nbr_tras_de0vers8=length(find(type(1:end-1)==0 & type(2:end)==8));
                    if (nbr_tras_de3vers8+nbr_tras_de5vers8+nbr_tras_de0vers8)>1 %on a des transition multiple=> type 5A
                        type5A(numstade)=type5A(numstade)+1;
                        nbr_vis1_multiple(numstade)=nbr_vis1_multiple(numstade)+length(find(cotation_reference.debut>=pos_deb_type(i)-1 & (cotation_reference.debut+cotation_reference.duree)<=pos_fin_type(i)+1));
                        nbr_aut_multiple(numstade)=nbr_aut_multiple(numstade)+length(find(cotation_automatique.debut>=pos_deb_type(i)-1 & (cotation_automatique.debut+cotation_automatique.duree)<=pos_fin_type(i)+1));
                    else %type 1A
                        type1A(numstade)=type1A(numstade)+1;
                    end
                else%type 1C ou C
                    nbr_cot_aut=length(find(cotation_automatique.debut>=pos_deb_type(i)-1 & (cotation_automatique.debut+cotation_automatique.duree)<=pos_fin_type(i)+1));
                    nbr_cot_vis1=length(find(cotation_reference.debut>=pos_deb_type(i)-1 & (cotation_reference.debut+cotation_reference.duree)<=pos_fin_type(i)+1));
                    nbr_cot_vis2=length(find(cotation_reference.debut2>=pos_deb_type(i)-1 & (cotation_reference.debut2+cotation_reference.duree2)<=pos_fin_type(i)+1));
                    if (nbr_cot_aut>1 | nbr_cot_vis1>1 | nbr_cot_vis2>1)%si on a au moins deux mouvement dans une cotation
                        type5C(numstade)=type5C(numstade)+1;
                        nbr_vis1_multiple(numstade)=nbr_vis1_multiple(numstade)+length(find(cotation_reference.debut>=pos_deb_type(i)-1 & (cotation_reference.debut+cotation_reference.duree)<=pos_fin_type(i)+1));
                        nbr_vis2_multiple(numstade)=nbr_vis2_multiple(numstade)+length(find(cotation_reference.debut2>=pos_deb_type(i)-1 & (cotation_reference.debut2+cotation_reference.duree2)<=pos_fin_type(i)+1));
                        nbr_aut_multiple(numstade)=nbr_aut_multiple(numstade)+length(find(cotation_automatique.debut>=pos_deb_type(i)-1 & (cotation_automatique.debut+cotation_automatique.duree)<=pos_fin_type(i)+1));
                    else
                        type1C(numstade)=type1C(numstade)+1;
                    end
                end
            end
        end
    end
end
type1A(7)=sum(type1A);
type1B(7)=sum(type1B);
type1C(7)=sum(type1C);
type2(7)=sum(type2);
type3A(7)=sum(type3A);
type3B(7)=sum(type3B);
type3C(7)=sum(type3C);
type3D(7)=sum(type3D);
type5A(7)=sum(type5A);
type5B(7)=sum(type5B);
type5C(7)=sum(type5C);
nbr_aut_multiple(7)=sum(nbr_aut_multiple);
nbr_vis1_multiple(7)=sum(nbr_vis1_multiple);
nbr_vis2_multiple(7)=sum(nbr_vis2_multiple);

nbr_types={};
nbr_types.nbrtot_scored_by_system=nombre_auto;
nbr_types.nbrtot_scored_by_visual1=nombre_visu1;
nbr_types.nbrtot_scored_by_visual2=nombre_visu2;
nbr_types.nbr_scored_only_by_sys__type2=type2;
nbr_types.nbr_scored_only_by_vis1__type3A=type3A;
nbr_types.nbr_scored_only_by_vis2__type3B=type3B;
nbr_types.nbr_scored_only_by_systEvis1__type1A=type1A;
nbr_types.nbr_scored_only_by_systEvis2__type1B=type1B;
nbr_types.nbr_scored_only_by_vis1Evis2__type3C=type3C;
nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C=type1C;
nbr_types.type3D=type3D;
nbr_types.type5A=type5A;
nbr_types.type5B=type5B;
nbr_types.type5C=type5C;
nbr_types.nbr_aut_multiple=nbr_aut_multiple;
nbr_types.nbr_vis1_multiple=nbr_vis1_multiple;
nbr_types.nbr_vis2_multiple=nbr_vis2_multiple;

%(Reference,'Aut/Vis1')
syst_Reference=zeros(2,2);
syst_Reference(1,1)=(nbr_types.nbr_scored_only_by_systEvis1__type1A(7))+(nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C(7)) ;
syst_Reference(1,2)=(nbr_types.nbr_scored_only_by_systEvis2__type1B(7))+(nbr_types.nbr_scored_only_by_sys__type2(7))    ;
syst_Reference(2,1)=(nbr_types.nbr_scored_only_by_vis1__type3A(7))+(nbr_types.nbr_scored_only_by_vis1Evis2__type3C(7));
syst_Reference(2,1)=syst_Reference(2,1)+(nbr_types.type3D(7));
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5A(7));
syst_Reference(1,2)=syst_Reference(1,2)+(nbr_types.type5B(7));
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5C(7));
syst_Reference(2,2)=round((duree_enregistrement)/duration_event)-syst_Reference(1,1)-syst_Reference(1,2)-syst_Reference(2,1);
confusion_matrix_aut_Vis1=syst_Reference;
%(Reference,'syst/Vis2')
syst_Reference=zeros(2,2);
syst_Reference(1,1)=(nbr_types.nbr_scored_only_by_systEvis2__type1B(7)) +(nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C(7))  ;
syst_Reference(1,2)=(nbr_types.nbr_scored_only_by_systEvis1__type1A(7)) +(nbr_types.nbr_scored_only_by_sys__type2(7))  ; 
syst_Reference(2,1)=(nbr_types.nbr_scored_only_by_vis2__type3B(7)) +(nbr_types.nbr_scored_only_by_vis1Evis2__type3C(7));
syst_Reference(2,1)=syst_Reference(2,1)+(nbr_types.type3D(7));
syst_Reference(1,2)=syst_Reference(1,2)+(nbr_types.type5A(7));
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5B(7));
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5C(7));
syst_Reference(2,2)=round((duree_enregistrement)/duration_event)-syst_Reference(1,1)-syst_Reference(1,2)-syst_Reference(2,1);
confusion_matrix_aut_Vis2=syst_Reference;
%(Reference,'Vis1/Vis2')
syst_Reference=zeros(2,2);
syst_Reference(1,1)=(nbr_types.nbr_scored_only_by_vis1Evis2__type3C(7)) +(nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C(7)) ; 
syst_Reference(2,1)=(nbr_types.nbr_scored_only_by_vis2__type3B(7)) +(nbr_types.nbr_scored_only_by_systEvis2__type1B(7))  ;  
syst_Reference(1,2)=(nbr_types.nbr_scored_only_by_vis1__type3A(7)) +(nbr_types.nbr_scored_only_by_systEvis1__type1A(7)) ;
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type3D(7));
syst_Reference(1,2)=syst_Reference(1,2)+(nbr_types.type5A(7));
syst_Reference(2,1)=syst_Reference(2,1)+(nbr_types.type5B(7));
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5C(7));
syst_Reference(2,2)=round((duree_enregistrement)/duration_event)-syst_Reference(1,1)-syst_Reference(1,2)-syst_Reference(2,1);
confusion_matrix_Vis1_Vis2=syst_Reference;
% (Reference,'Vis2/Vis1')
syst_Reference=zeros(2,2);
syst_Reference(1,1)=(nbr_types.nbr_scored_only_by_vis1Evis2__type3C(7)) +(nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C(7)) ; 
syst_Reference(2,1)=(nbr_types.nbr_scored_only_by_vis2__type3B(7)) +(nbr_types.nbr_scored_only_by_systEvis2__type1B(7))  ; 
syst_Reference(1,2)=(nbr_types.nbr_scored_only_by_vis1__type3A(7)) +(nbr_types.nbr_scored_only_by_systEvis1__type1A(7)) ;
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type3D(7));
syst_Reference(1,2)=syst_Reference(1,2)+(nbr_types.type5A(7));
syst_Reference(2,1)=syst_Reference(2,1)+(nbr_types.type5B(7));
syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5C(7));
syst_Reference=syst_Reference';

syst_Reference(2,2)=round((duree_enregistrement)/duration_event)-syst_Reference(1,1)-syst_Reference(1,2)-syst_Reference(2,1);

confusion_matrix_Vis2_Vis1=syst_Reference;

% (Reference,'syst/Vis1uVis2')

syst_Reference=zeros(2,2);

syst_Reference(1,1)=(nbr_types.nbr_scored_only_by_systEvis1__type1A(7)) +(nbr_types.nbr_scored_only_by_systEvis2__type1B(7))+(nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C(7)) ;

syst_Reference(1,2)=(nbr_types.nbr_scored_only_by_sys__type2(7))  ; 

syst_Reference(2,1)= (nbr_types.nbr_scored_only_by_vis1__type3A(7)) +(nbr_types.nbr_scored_only_by_vis2__type3B(7)) +(nbr_types.nbr_scored_only_by_vis1Evis2__type3C(7)) ;

syst_Reference(2,1)=syst_Reference(2,1)+(nbr_types.type3D(7));

syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5A(7));

syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5B(7));

syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5C(7));

syst_Reference(2,2)=round((duree_enregistrement)/duration_event)-syst_Reference(1,1)-syst_Reference(1,2)-syst_Reference(2,1);

confusion_matrix_aut_Vis1uVis2=syst_Reference;



%(Reference,'syst/Vis1nVis2')

syst_Reference=zeros(2,2);

syst_Reference(1,1)=(nbr_types.nbr_scored_by_vis1Evis2Esyst__type1C(7)) ;

syst_Reference(1,2)=(nbr_types.nbr_scored_only_by_systEvis1__type1A(7))+(nbr_types.nbr_scored_only_by_systEvis2__type1B(7))+(nbr_types.nbr_scored_only_by_sys__type2(7));

syst_Reference(2,1)=(nbr_types.nbr_scored_only_by_vis1Evis2__type3C(7)) ;

syst_Reference(2,1)=syst_Reference(2,1)+ (nbr_types.type3D(7));

syst_Reference(1,2)=syst_Reference(1,2)+(nbr_types.type5A(7))+(nbr_types.type5B(7));

syst_Reference(1,1)=syst_Reference(1,1)+(nbr_types.type5C(7));

syst_Reference(2,2)=round((duree_enregistrement)/duration_event)-syst_Reference(1,1)-syst_Reference(1,2)-syst_Reference(2,1);

confusion_matrix_aut_Vis1nVis2=syst_Reference;