-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDBscorerV02032023_exported.m
1183 lines (1114 loc) · 50.2 KB
/
DBscorerV02032023_exported.m
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef DBscorerV02032023_exported < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
Tool matlab.ui.Figure
LoadVideo matlab.ui.control.Button
MarkROI matlab.ui.control.Button
StartsLabel matlab.ui.control.Label
ProcessVideoStart matlab.ui.control.Spinner
EndsLabel matlab.ui.control.Label
ProcessVideoEnd matlab.ui.control.Spinner
TimeEditFieldLabel matlab.ui.control.Label
Time matlab.ui.control.NumericEditField
ProcessVideo matlab.ui.control.Button
FixBackground matlab.ui.control.Button
EnterInfo matlab.ui.control.EditField
CreateBackground matlab.ui.control.Button
ManualScoring matlab.ui.control.Button
Play matlab.ui.control.StateButton
StateButton matlab.ui.control.StateButton
Cancel matlab.ui.control.StateButton
GetThresh matlab.ui.control.Button
AreaThresholdLabel matlab.ui.control.Label
AreaThreshold matlab.ui.control.NumericEditField
TimeThresholdsLabel matlab.ui.control.Label
TimeThresh matlab.ui.control.NumericEditField
CompileAuto matlab.ui.control.Button
CompileManual matlab.ui.control.Button
StartTimesLabel matlab.ui.control.Label
StartTime matlab.ui.control.NumericEditField
EndTimesLabel matlab.ui.control.Label
EndTime matlab.ui.control.NumericEditField
ClipLabel matlab.ui.control.Label
Clip matlab.ui.control.NumericEditField
TimeBinsLabel matlab.ui.control.Label
TimeBin matlab.ui.control.NumericEditField
FigWindow matlab.ui.control.Button
DBscorerV2Label matlab.ui.control.Label
end
properties (Access = public)
v % Video
changingValue1 % Video analysis start time
changingValue2 % Video analysis end time
show1 % Video analysis first frame
show2 % Video analysis end frame
path3 % Video Path
filename
filename_3
Background
cr
crop
ClearOutsideMask
hImage
T
video
ar
cord
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: LoadVideo
function LoadVideoPushed(app, event)
% Close all the figures first
%exportapp(app.Tool,'V2.pdf')
figure
close all
% Imports selected video
[filename_2, pathname_2] = uigetfile('*.mov;*.wmv;*.mp4;*.avi','Open Video File');
if filename_2 == 0
% If user clicked the Cancel button.
return;
end
app.filename_3 = cellstr(filename_2);
pathname_3 = cellstr(pathname_2);
app.video = fullfile(pathname_3{1},app.filename_3{1});
% Get the name video and create folder with video name
[folder, baseFileNameNoExt, ~] = fileparts(app.video);
fileName = fullfile(pathname_3{1});
dotLocations = find(fileName == '.');
if isempty(dotLocations)
nameBeforeFirstDot = fileName;
else
nameBeforeFirstDot = fileName(1:dotLocations(1)-1);
end
cd(folder)
app.path3=nameBeforeFirstDot;
app.filename=baseFileNameNoExt;
% creates video reader and shows firstframe from the video
app.v = VideoReader(app.video);
frame = read(app.v, 1);
app.hImage=imshow(frame);
xlabel(baseFileNameNoExt)
app.changingValue1=0;
app.changingValue2=floor(app.v.Duration);
app.ProcessVideoStart.Value=1;
app.ProcessVideoEnd.Value=round(app.v.Duration-1);
app.Time.Value =floor(app.v.Duration);
app.LoadVideo.BackgroundColor='g';
app.CreateBackground.BackgroundColor='w';
app.CreateBackground.Text='Create Background';
app.FixBackground.Text='Fix Background';
app.FixBackground.BackgroundColor='w';
app.MarkROI.Text='Mark ROI';
app.MarkROI.BackgroundColor='w';
end
% Button pushed function: MarkROI
function MarkROIPushed(app, event)
numrois=2;
map=hsv(numrois);
custommap = map;
app.show1 = read(app.v,floor(app.changingValue1*app.v.FrameRate)+1);
imshow(app.show1)
set(gcf, 'Position', get(0,'Screensize'));
roi = drawpolygon('FaceAlpha',0,'Color',custommap(1,:));
app.ClearOutsideMask=createMask(roi);
croproi=roi.Position;
app.cord=roi.Position;
thisROI=[app.cord];
close
app.crop=[min(croproi(:,1)),min(croproi(:,2)),max(croproi(:,1))-min(croproi(:,1)),max(croproi(:,2))-min(croproi(:,2))];
Ic=imcrop(app.show1,app.crop);
app.hImage=imshow(Ic);
[~,~,o]=size(Ic);
medfilt=3;
BackgroundCropped=imcrop(app.Background,app.crop);
ClearOutsideMaskCropped=imcrop(app.ClearOutsideMask,app.crop);
[~,n,~]=size(BackgroundCropped);
rescale=(300/n);
% Average
fps=app.v.FrameRate;
wanted_frames=floor(5*fps);
a = app.changingValue1*app.v.FrameRate;
b = app.changingValue2*app.v.FrameRate;
x= floor((b-a).*rand(wanted_frames,1) + a);
indices = sort(x);
binarythresh=[];
app.MarkROI.Text='Getting Binary Threshold';
app.MarkROI.BackgroundColor='y';
% if rgb then convert to grayscale
if o==3
for k=1:length(x)
frame=read(app.v,indices(k));
gray =rgb2gray(frame);
gray=imcrop(gray,app.crop);
MaskedImage = imgaussfilt(medfilt2(imabsdiff(BackgroundCropped,(gray)),[medfilt,medfilt]),3);
MaskedImage = adapthisteq(MaskedImage,'clipLimit',0.005,'Distribution','rayleigh');
MaskedImage=imresize(MaskedImage,rescale);
level = graythresh(MaskedImage);
binarythresh=[binarythresh,level];
end
else
for k=1:length(x)
gray=read(app.v,indices(k));
gray=imcrop(gray,app.crop);
MaskedImage = imgaussfilt(medfilt2(imabsdiff(BackgroundCropped,(gray)),[medfilt,medfilt]),3);
MaskedImage = adapthisteq(MaskedImage,'clipLimit',0.005,'Distribution','rayleigh');
MaskedImage=imresize(MaskedImage,rescale);
level = graythresh(MaskedImage);
binarythresh=[binarythresh,level];
end
end
MaskedImage = imgaussfilt(medfilt2(imabsdiff(BackgroundCropped,(gray)),[medfilt,medfilt]),3);
MaskedImage = adapthisteq(MaskedImage,'clipLimit',0.005,'Distribution','rayleigh');
MaskedImage=imresize(MaskedImage,rescale);
gray=imresize(gray,rescale);
bint = mean(binarythresh);
app.T=round(bint*100);
MaskedBinary=imbinarize(MaskedImage,(app.T/100));
imshow(app.show1)
Animal=drawpolygon('Position',thisROI,'LineWidth',.1,'FaceAlpha',.10,'Color','r','MarkerSize',.1);
fig=gcf;
exportgraphics(fig,[[app.EnterInfo.Value],' ',[app.filename],'.tif'],'Resolution',300)
close
app.hImage=imshow(gray);
B = labeloverlay(gray,MaskedBinary,'Colormap','hsv','Transparency',0.75);
set(app.hImage, 'CData',B)
axis on
app.MarkROI.Text='Mark ROI';
app.MarkROI.BackgroundColor='g';
app.ProcessVideo.BackgroundColor='w';
end
% Value changing function: ProcessVideoStart
function ProcessVideoStartValueChanging(app, event)
app.changingValue1 = event.Value;
if app.changingValue1<app.v.Duration
app.show1 = read(app.v,floor(app.changingValue1*app.v.FrameRate)+1);
set(app.hImage, 'CData', app.show1)
end
app.Time.Value =0;
app.MarkROI.BackgroundColor='w';
app.ProcessVideo.BackgroundColor='w';
app.Play.BackgroundColor='w';
app.StateButton.BackgroundColor='w';
end
% Value changing function: ProcessVideoEnd
function ProcessVideoEndValueChanging(app, event)
app.changingValue2 = event.Value;
if app.changingValue2<app.v.Duration
app.show2 = read(app.v,floor(app.changingValue2*app.v.FrameRate));
set(app.hImage, 'CData', app.show2)
end
app.Time.Value =app.changingValue2-app.changingValue1;
app.MarkROI.BackgroundColor='w';
app.ProcessVideo.BackgroundColor='w';
app.Play.BackgroundColor='w';
app.StateButton.BackgroundColor='w';
end
% Button pushed function: ProcessVideo
function ProcessVideoButtonPushed(app, event)
close
app.Cancel.Value=0;
medfilt=3;
app.ProcessVideo.BackgroundColor='Yellow';
app.Play.BackgroundColor='w';
app.StateButton.BackgroundColor='w';
BackgroundCropped=imcrop(app.Background,app.crop);
clear cr
A=size(BackgroundCropped);
app.cr = struct('cdata',zeros(A(1),A(2),1,'uint8'),...
'colormap',[]);
StartFrame=floor(app.changingValue1*app.v.FrameRate)+1;
EndFrame=floor(app.changingValue2*app.v.FrameRate);
totaltime=(EndFrame-StartFrame);
[~,n,~]=size(BackgroundCropped);
rescale=(300/n);
app.ar=[];
cf=-2;
for k=StartFrame:1:EndFrame+1
cf=cf+1;
app.Time.Value=floor(k/app.v.FrameRate);
%app.Time.Value=(cf/totaltime)*100;
pause(.0001)
if app.Cancel.Value==1
break
end
frame=rgb2gray(read(app.v,k));
RandomFrame = imcrop(frame,app.crop);
MaskedImage = imgaussfilt(medfilt2(imabsdiff(BackgroundCropped,RandomFrame),[medfilt,medfilt]),3);
MaskedImage = adapthisteq(MaskedImage,'clipLimit',0.005,'Distribution','rayleigh');
MaskedImage=imresize(MaskedImage,rescale);
MaskedBinary=imbinarize(MaskedImage,(app.T/100));
All=sum(MaskedBinary,'all');%
app.ar=[app.ar,All];%
end
BINTHRES=app.T;
ar_score=app.ar;
roicord=app.cord;
fps=app.v.FrameRate;
save([[app.EnterInfo.Value],' ',[app.filename]])
app.ProcessVideo.BackgroundColor='g';
app.hImage=imshow(app.Background);
end
% Button pushed function: FixBackground
function FixBackgroundPushed(app, event)
numrois=2;
map=hsv(numrois);
custommap = map;
figure;
imshow(app.Background);
set(gcf, 'Position', get(0,'Screensize'));
xlabel('Fill')
RegionFill = drawpolygon('FaceAlpha',0,'Color',custommap(1,:));
RegionFillMask = createMask(RegionFill);
app.Background= regionfill(app.Background,RegionFillMask);
delete(RegionFill);
close
app.hImage=imshow(app.Background);
app.FixBackground.Text='Fix More Background';
app.FixBackground.BackgroundColor='g';
end
% Button pushed function: CreateBackground
function CreateBackgroundButtonPushed(app, event)
app.v = VideoReader(app.video);
fps=app.v.FrameRate;
wanted_frames=floor(1*fps);
a = app.changingValue1*app.v.FrameRate;
b = app.changingValue2*app.v.FrameRate;
x= floor((b-a).*rand(wanted_frames,1) + a);
indices = sort(x);
frame = read(app.v, wanted_frames(1));
A=size(frame);
app.cr= struct('cdata',zeros(A(1),A(2),1,'uint8'),...
'colormap',[]);
[m,n,o]=size(frame);
Abkg = zeros(m,n,1,'uint8');
% if rgb then convert to grayscale
if o==3
for k=1:length(x)
frame=read(app.v,indices(k));
frame =rgb2gray(frame);
Abkg(:,:,k) =uint8(app.cr(1).cdata);
app.cr(1).cdata = frame;
end
else
for k=1:length(x)
frame=read(app.v,indices(k));
Abkg(:,:,k) =uint8(app.cr(1).cdata);
app.cr(1).cdata = frame;
end
end
app.Background = uint8(median(double(Abkg),3));
medfilt=3;
app.Background=medfilt2(app.Background,[medfilt,medfilt]);
app.hImage=imshow(app.Background);
app.CreateBackground.BackgroundColor='g';
app.CreateBackground.Text='Recreate Background';
app.FixBackground.Text='Fix Background';
app.FixBackground.BackgroundColor='w';
end
% Button pushed function: ManualScoring
function ManualScoringButtonPushed(app, event)
figure
close all
app.Play.Value=1;
app.Cancel.Value=0;
is=0;
frame_by_frame_time_original = 1/app.v.FrameRate;
StartFrame=floor(app.changingValue1*app.v.FrameRate)+1;
EndFrame=floor(app.changingValue2*app.v.FrameRate);
Full=EndFrame-StartFrame+2;
im2 = zeros(Full,1);
BackgroundCropped=imcrop(app.Background,app.crop);
[~,n,~]=size(BackgroundCropped);
width=round(300*0.05);
rescale=(300/n);
I=imcrop(app.show1,app.crop);
I=imresize(I,rescale);
figure
app.hImage=imshow(I);
app.StateButton.Value=0;
app.StateButton.Text='Immobile';
tic
for k=StartFrame:1:EndFrame+1
if app.Cancel.Value==1
break
end
while app.Play.Value==1
app.Play.Text='Play';
app.Play.BackgroundColor='y';
app.StateButton.BackgroundColor='y';
pause(.0001)
if app.Cancel.Value==1
break
end
end
app.Play.Text='Pause';
app.Play.BackgroundColor='g';
if app.StateButton.Value==1
app.StateButton.BackgroundColor='G';
app.StateButton.Text='Mobile';
c1=yline(width,'-g','LineWidth',width);
is=is+1;
im2(k+1-StartFrame)=1;
else
app.StateButton.BackgroundColor='M';
app.StateButton.Text='Immobile';
c1=yline(width,'-m','LineWidth',width);
is=is+0;
im2(k+1-StartFrame)=0;
end
app.Time.Value=floor(k/app.v.FrameRate);
frame=read(app.v,k);
I=imcrop(frame,app.crop);
I=imresize(I,rescale);
set(app.hImage, 'CData', I)
frame_normalization = toc;
if frame_normalization < frame_by_frame_time_original
pause(frame_by_frame_time_original - frame_normalization);
end
tic
delete(c1)
end
hold off
app.StateButton.Text='State';
app.StateButton.BackgroundColor='w';
app.Play.Value=1;
app.Time.Value=floor(sum(im2,'all')/app.v.FrameRate);
fps=app.v.FrameRate;
save([[app.EnterInfo.Value],' ',[app.filename],' ','Manual'])
app.Cancel.Value=0;
close all
app.hImage=imshow(app.Background);
end
% Button pushed function: GetThresh
function GetThreshButtonPushed(app, event)
[filenames, pathname] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');
if pathname == 0
% If user clicked the Cancel button.
return;
end
filenames=filenames';
cd(pathname)
%% Variables
FileNames=[];
AllData=[];
clip=app.Clip.Value;
kfold=7;
fps=15
%% Batch Processing
FileNames=[];
AllData=[];
for name=1:length(filenames)
file=filenames{name}(1:end-4);
tf = strcmp(filenames{name}(end-9:end-4),'Manual');
if tf==1
manual=filenames{name}(1:end-4)
load(manual)
auto=[file(1:end-7),'.mat'];
%auto=filenames{name}(1:end-11);
load(auto)
%im2=im2==0; %only for old data remove it before uploading to git
rc =length(im2)- rem(length(im2),fps);
Rc=im2(1:rc);
binc = reshape(Rc,fps,[]);
meanbinpercentage1c = mean(binc,1);
meanbinpercentage2c=meanbinpercentage1c<0.5;
GT=meanbinpercentage2c;
% area change %
AreaChange=abs(diff(ar_score(1:end-1)));
AreaPrevious=ar_score(1:end-2);
AreaChangePercent=(AreaChange./AreaPrevious)*100;
AreaChangePercent(AreaChangePercent>10)=10;
r_ar =length(AreaChangePercent)- rem(length(AreaChangePercent),fps);
R_ar=AreaChangePercent(1:r_ar);
bin_ar = reshape(R_ar,fps,[]);
Area_Change=mean(bin_ar,1);
FileNames=[FileNames;{file(1:end)}];
T1=table(GT(2:end)',Area_Change');
AllData=[AllData;T1];
%close all
else
continue
end
end
%% Clip data
GT=AllData.Var1';
Area_Change=AllData.Var2;
Tillc=length(GT);
transitionsc = diff([0,GT,0]); % find where the array goes from non-zero to zero and vice versa
runstartsc = find(transitionsc == 1);
runendsc = (find(transitionsc == -1)); %one past the end
runlengthsc = abs(runendsc - runstartsc);
GT=zeros(length(GT),1);
for i=1:length(runstartsc)
GT(runstartsc(i):runendsc(i))=1;
end
rasterc=sort([0,runstartsc,runendsc,Tillc]);
clipped_Manual_score=GT;
clipped_area_change=Area_Change;
if clip>0
for i=clip+1:length(rasterc)
if rasterc(i)-clip<=0
rasterc(i)=1;
else
clipped_Manual_score(rasterc(i)-clip:rasterc(i)+clip)=50;
clipped_area_change(rasterc(i)-clip:rasterc(i)+clip)=50;
end
end
end
clipped_Manual_score(clipped_Manual_score==50)=[];
clipped_area_change(clipped_area_change==50)=[];
s=min(length(clipped_Manual_score),length(clipped_area_change));
TrainData=[clipped_Manual_score(1:s),clipped_area_change(1:s)];
% shuffle the data
AveragedThreshold=[];
AveragedAUC=[];
AveragedAccuracy=[];
AveragedPrecision=[];
AveragedRecall=[];
%AveragedMCC=[];
Thresholds=[];
Precisions=[];
Recalls=[];
dp=s;
for sfl=1:100
idx= floor((s-1).*rand(dp,1) + 1);
% k divide
if mod(s,kfold)>0
blocksize=floor(s/kfold);
else
blocksize=floor(s/kfold)-1;
end
% create valset testset
TrainSet=[];
ValSet=[];
for i=1: kfold-2
MCCs=[];
ValSet=TrainData(idx(blocksize*i:blocksize*i+blocksize),:);
TrainSet=TrainData;
TrainSet(idx(blocksize*i:blocksize*i+blocksize),:)=[];
% ROC based threshold determination
AreaChange=TrainSet(:,2);
IM=AreaChange(TrainSet(:,1)==1);
M=AreaChange(TrainSet(:,1)==0);
[~,edges] = histcounts(AreaChange',100);
Nim = histcounts(IM,edges);
Nm = histcounts(M,edges);
NmPa=[]; % tpr
for i=1:length(Nm)
Pm=(sum(Nm(i:length(Nm)))/sum(Nm));
NmPa=[NmPa,Pm];
end
NimPa=[]; % fpr
for i=1:length(Nim)
Pim=sum(Nim(i:length(Nim)))/sum(Nim);
NimPa=[NimPa,Pim];
end
%CZ
gmeansa=(NmPa.*(1-NimPa));
ThresholdPa=(edges(gmeansa==max(gmeansa)));
CalibratedThresholdMin=min(ThresholdPa);
CalibratedThresholdMax=max(ThresholdPa);
AUCa=abs(trapz(NimPa,NmPa));
CalibratedThresholdValue = max(ThresholdPa);
AveragedThreshold=[AveragedThreshold,CalibratedThresholdValue];
AveragedAUC=[AveragedAUC,AUCa];
% here calculate true positive, false positive, true negative, false
% negative
trueval=ValSet(:,1);
predicted=ValSet(:,2)<CalibratedThresholdValue;
TP=sum(trueval==1 & predicted==1);
FP=sum(trueval==0 & predicted==1);
TN=sum(trueval==0 & predicted==0);
FN=sum(trueval==1 & predicted==0);
Precision=TP/(TP+FP);
Recall=TP/(TP+FN);
AveragedPrecision=[AveragedPrecision,Precision];
AveragedRecall=[AveragedRecall,Recall];
% MCC based threshold determination
for i=1:length(edges)
th=edges(i);
trueval=TrainSet(:,1);
predicted=TrainSet(:,2)<th;
TP=sum(trueval==1 & predicted==1);
FP=sum(trueval==0 & predicted==1);
TN=sum(trueval==0 & predicted==0);
FN=sum(trueval==1 & predicted==0);
MCC=((TP*TN)-(FP*FN))/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN));
MCCs=[MCCs,MCC];
end
ThresholdMCC=(edges(MCCs==max(MCCs)));
ThresholdValue=max(ThresholdMCC);
%true positive, false positive, true negative, false negative
trueval=ValSet(:,1);
predicted=ValSet(:,2)<ThresholdValue;
TP=sum(trueval==1 & predicted==1);
FP=sum(trueval==0 & predicted==1);
TN=sum(trueval==0 & predicted==0);
FN=sum(trueval==1 & predicted==0);
Precision=TP/(TP+FP);
Recall=TP/(TP+FN);
Precisions=[Precisions,Precision];
Recalls=[Recalls,Recall];
Thresholds=[Thresholds,ThresholdValue];
end
if sfl==100
%p4=plot(NimPa,NmPa,'-k','LineWidth',2);
p4=area(NimPa,NmPa,'LineWidth',2,'LineStyle','-',"FaceColor", '#87cefa');
hold on
%p5=plot((NimPa(gmeansa==max(gmeansa))),(NmPa(gmeansa==max(gmeansa))),'g*','LineWidth',1);
hold off
pbaspect([1 1 1]);
ax = gca;
ax.Color = 'none';
ax.FontWeight='bold';
ax.FontSize = 12;
%ax.TickLength = [.005 0.035];
ax.LineWidth=1;
xlabel('1-Specificity')
ylabel('Sensitivity')
title('ROC Curve')
saveas(gcf,['ROC curve generated from Manual Quantification','.png'])
end
end
ThresholdG=mean(AveragedThreshold,'all');
PrecisionG=mean(AveragedPrecision);
RecallG=mean(AveragedRecall);
AUC=mean(AUCa);
Threshold=mean(Thresholds);
Precision=mean(Precisions);
Recall=mean(Recalls);
% MCC based threshold
Immobility=(sum(GT)/length(GT))*100;
% if 20<Immobility || Immobility>80
% Precision=mean(Precisions)
% Recall=mean(Recalls)
% Threshold=mean(Thresholds)
% end
T2=table(AUC,Threshold,Precision,Recall,Immobility);
writetable(T2,'Threshold.csv');
%str = {['Optimum Threshold-',num2str(Threshold)]};
%text(.25,.5,str)
close all
end
% Button pushed function: CompileAuto
function CompileAutoButtonPushed(app, event)
[filenames, pathname] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');
if pathname == 0
% If user clicked the Cancel button.
return;
end
cd(pathname)
filenames=filenames';
%% Variables
FileNames=[];
AllData=[];
if isempty(app.StartTime.Value)
startsec=0;
else
startsec=app.StartTime.Value;
end
if isempty(app.EndTime.Value)
endsec=0;
else
endsec=app.EndTime.Value;
end
Threshold=app.AreaThreshold.Value;
Minimumchange=app.TimeThresh.Value;
Ylimit=5;
Binsize=app.TimeBin.Value;
%% Batch Processing
FileNames=[];
AllData=[];
fps=15;
figure
for name=1:length(filenames)
file=filenames{name}(1:end-4);
tf = strcmp(filenames{name}(end-9:end-4),'Manual');
if tf==0
load([file,'.mat'])
startframe=( startsec*fps)+1;
endframe=(endsec*fps)+1;
if endframe<length(ar_score)
Area=ar_score(startframe:endframe);
else
Area=ar_score(startframe:length(ar_score));
endsec=(length(ar_score)-1)/fps;
end
AreaChange=abs(diff(Area));
AreaPrevious=Area(1:end-1);
AreaChangePercent=(AreaChange./AreaPrevious)*100;
AreaChangePercent(AreaChangePercent>10)=10;
r_ar =length(AreaChangePercent)- rem(length(AreaChangePercent),fps);
R_ar=AreaChangePercent(1:r_ar);
bin_ar = reshape(R_ar,fps,[]);
Area_Change=mean(bin_ar,1);
im=Area_Change<Threshold;
A=im;
Till=length(A);
transitions = diff([0,A,0]); % find where the array goes from non-zero to zero and vice versa
runstarts = find(transitions == 1);
runends = (find(transitions == -1)-1); %one past the end
runlengths = abs(runends - runstarts);
runstarts(runlengths<= Minimumchange) = [];
runends(runlengths<= Minimumchange) = [];
Y=zeros(length(A),1);
for i=1:length(runstarts)
Y(runstarts(i):runends(i))=1;
end
Immobility_Percentage=(sum(Y(1:Till))/Till)*100;
Number_of_bouts=length(runends);
if sum(runlengths,'all')==0
Longest_bout=0;
else
Longest_bout=max(runlengths)+1;
end
if sum(runstarts,'all')==0
Immobility_latency=NaN;
else
Immobility_latency=runstarts(1)-1;
end
% binned immobility
if Binsize<length(Y)
bin =length(Y)- rem(length(Y),Binsize);
Rim=Y(1:bin);
if length(Y)>bin
Rim2=Y(bin+1:end);
Rimbin = reshape(Rim,Binsize,[]);
Bins = [sum(Rimbin,1)/Binsize,sum(Rim2)/length(Rim2)]*100;
else
Rimbin = reshape(Rim,Binsize,[]);
Bins = (sum(Rimbin,1)/Binsize)*100;
end
else
Bins=sum(Y)/length(Y)*100;
end
% plot graphs
subplot(length(filenames),1,name,'align');
bar(Y*Ylimit,1,'FaceColor','#000000','EdgeColor','none')
hold on
bar(double(Y==0)*Ylimit,1,'FaceColor','#FFFAFA','EdgeColor','none')
%alpha(.4) % sets transparency
xticks(0:Binsize:Till)
yticklabels({})
if length(filenames)>name
xticklabels({})
end
%pbaspect([Till Till/10 1])
xlim([0 Till])
ylim([0 Ylimit])
ax = gca;
ax.Color ='none';
ax.FontWeight='bold';
ax.FontSize = 5;
ax.TickLength = [.01 0.01];
ax.LineWidth=.1;
box on
hold on
yticks([])
% subplot(length(filenames),2,name+1,'align');
% image(abs(100-Bins))
% colormap(gray(100))
% yticks([])
% ax = gca;
% ax.Color ='none';
% ax.FontWeight='bold';
% ax.FontSize = 5;
% ax.TickLength = [.01 0.01];
% ax.LineWidth=.1;
% box on
% if length(filenames)>name
% xticklabels({})
% end
T1=table(Immobility_Percentage,Immobility_latency,Longest_bout,Number_of_bouts,...
startsec,endsec,Minimumchange,Threshold,Binsize,Bins);
%writetable(T1,[file,' Results Auto.csv']);
writematrix(Y,[file,' Auto .txt'])
FileNames=[FileNames;{file(1:end)}];
AllData=[AllData;T1];
else
continue
end
end
set(gcf, 'Position', get(0,'Screensize'));
saveas(gcf,'Raster Auto.png')
saveas(gcf,'Raster Auto.pdf')
hold off
close all
T2=table(FileNames);
CombinedData=[T2,AllData];
writetable(CombinedData,' Results Auto Compiled.csv');
end
% Button pushed function: CompileManual
function CompileManualButtonPushed(app, event)
[filenames, pathname] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');
if pathname == 0
% If user clicked the Cancel button.
return;
end
cd(pathname)
filenames=filenames';
%% Variables
FileNames=[];
AllData=[];
fps=15;
%fps=app.FPS.Value;
if isempty(app.StartTime.Value)
startsec=0;
else
startsec=app.StartTime.Value;
end
if isempty(app.EndTime.Value)
endsec=0;
else
endsec=app.EndTime.Value;
end
Threshold=app.AreaThreshold.Value;
Minimumchange=app.TimeThresh.Value;
Ylimit=5;
Binsize=app.TimeBin.Value;
%% Batch Processing
FileNames=[];
AllData=[];
figure
set(gcf, 'Position', get(0,'Screensize'));
for name=1:length(filenames)
file=filenames{name}(1:end-4);
tf = strcmp(filenames{name}(end-9:end-4),'Manual');
if tf==1
load([file,'.mat'])
%im2=im2==0; %only for old data remove it before uploading to git
startframe=( startsec*fps)+1;
endframe=(endsec*fps)+1;
if endframe<length(im2)
im2=im2(startframe:endframe);
else
im2=im2(startframe:length(im2));
end
rc =length(im2)- rem(length(im2),fps);
Rc=im2(1:rc);
binc = reshape(Rc,fps,[]);
meanbinpercentage1c = mean(binc,1);
meanbinpercentage2c=meanbinpercentage1c<0.5;
A=meanbinpercentage2c;
Till=length(A);
transitions = diff([0,A,0]); % find where the array goes from non-zero to zero and vice versa
runstarts = find(transitions == 1);
runends = (find(transitions == -1)-1); %one past the end
runlengths = abs(runends - runstarts);
runstarts(runlengths<= Minimumchange) = [];
runends(runlengths<= Minimumchange) = [];
Y=zeros(length(A),1);
for i=1:length(runstarts)
Y(runstarts(i):runends(i))=1;
end
Immobility_Percentage=(sum(Y(1:Till))/Till)*100;
Number_of_bouts=length(runends);
if sum(runlengths,'all')==0
Longest_bout=0;
else
Longest_bout=max(runlengths)+1;
end
if sum(runstarts,'all')==0
Immobility_latency=Till;
else
Immobility_latency=runstarts(1)-1;
end
% binned immobility
if Binsize<length(Y)
bin =length(Y)- rem(length(Y),Binsize);
Rim=Y(1:bin);
if length(Y)>bin
Rim2=Y(bin+1:end);
Rimbin = reshape(Rim,Binsize,[]);
Bins = [sum(Rimbin,1)/Binsize,sum(Rim2)/length(Rim2)]*100;
else
Rimbin = reshape(Rim,Binsize,[]);
Bins = (sum(Rimbin,1)/Binsize)*100;
end
else
Bins=sum(Y)/length(Y)*100;
end
% plot graphs
subplot(length(filenames),1,name,'align');
bar(Y*Ylimit,1,'FaceColor','#000000','EdgeColor','none')
hold on
bar(double(Y==0)*Ylimit,1,'FaceColor','#FFFAFA','EdgeColor','none')
xticks(0:Binsize:Till)
yticklabels({})
if length(filenames)>name
xticklabels({})
end
%pbaspect([Till Till/10 1])
xlim([0 Till])
ylim([0 Ylimit])
ax = gca;
ax.Color ='none';
ax.FontWeight='bold';
ax.FontSize = 5;
ax.TickLength = [.01 0.01];
ax.LineWidth=.1;
if length(filenames)==name
xlabel('Time (Second)')
end
yticks([])
T1=table(Immobility_Percentage,Immobility_latency,Longest_bout,Number_of_bouts,...
startsec,endsec,Minimumchange,Threshold,Binsize,Bins);
FileNames=[FileNames;{file(1:end)}];
%writetable(T1,[file,' Results Manual.csv']);
writematrix(Y,[file,' Manual.txt'])
AllData=[AllData;T1];
else
continue
end
end
saveas(gcf,'Raster Manual.png')
saveas(gcf,'Raster Manual.pdf')
hold off
close all
T2=table(FileNames);
CombinedData=[T2,AllData];
writetable(CombinedData,' Results Manual Compiled.csv');
end
% Button pushed function: FigWindow
function FigWindowButtonPushed(app, event)
app.hImage=imshow(app.Background);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create Tool and hide until all components are created
app.Tool = uifigure('Visible', 'off');
app.Tool.AutoResizeChildren = 'off';
app.Tool.Color = [1 1 1];
app.Tool.Position = [100 100 358 407];
app.Tool.Name = 'DBscorerV2';
app.Tool.Resize = 'off';
% Create LoadVideo
app.LoadVideo = uibutton(app.Tool, 'push');
app.LoadVideo.ButtonPushedFcn = createCallbackFcn(app, @LoadVideoPushed, true);
app.LoadVideo.Interruptible = 'off';
app.LoadVideo.FontName = 'Arial';
app.LoadVideo.Position = [14 377 159 25];
app.LoadVideo.Text = 'Load Video';
% Create MarkROI
app.MarkROI = uibutton(app.Tool, 'push');
app.MarkROI.ButtonPushedFcn = createCallbackFcn(app, @MarkROIPushed, true);
app.MarkROI.Interruptible = 'off';
app.MarkROI.Tooltip = {''};
app.MarkROI.Position = [14 181 159 25];
app.MarkROI.Text = 'Mark ROI';
% Create StartsLabel
app.StartsLabel = uilabel(app.Tool);
app.StartsLabel.HorizontalAlignment = 'right';
app.StartsLabel.Position = [12 311 58 22];
app.StartsLabel.Text = 'Start (s)';
% Create ProcessVideoStart
app.ProcessVideoStart = uispinner(app.Tool);
app.ProcessVideoStart.ValueChangingFcn = createCallbackFcn(app, @ProcessVideoStartValueChanging, true);
app.ProcessVideoStart.Limits = [0 Inf];
app.ProcessVideoStart.Interruptible = 'off';
app.ProcessVideoStart.Position = [97 311 76 22];
% Create EndsLabel
app.EndsLabel = uilabel(app.Tool);
app.EndsLabel.HorizontalAlignment = 'right';
app.EndsLabel.Position = [14 280 56 22];
app.EndsLabel.Text = 'End (s)';
% Create ProcessVideoEnd
app.ProcessVideoEnd = uispinner(app.Tool);
app.ProcessVideoEnd.ValueChangingFcn = createCallbackFcn(app, @ProcessVideoEndValueChanging, true);
app.ProcessVideoEnd.Limits = [1 Inf];
app.ProcessVideoEnd.Interruptible = 'off';
app.ProcessVideoEnd.Position = [97 280 76 22];
app.ProcessVideoEnd.Value = 1;
% Create TimeEditFieldLabel
app.TimeEditFieldLabel = uilabel(app.Tool);
app.TimeEditFieldLabel.HorizontalAlignment = 'right';
app.TimeEditFieldLabel.Position = [39 249 31 22];
app.TimeEditFieldLabel.Text = 'Time';
% Create Time
app.Time = uieditfield(app.Tool, 'numeric');
app.Time.Interruptible = 'off';
app.Time.Editable = 'off';
app.Time.Position = [97 249 76 22];
% Create ProcessVideo
app.ProcessVideo = uibutton(app.Tool, 'push');
app.ProcessVideo.ButtonPushedFcn = createCallbackFcn(app, @ProcessVideoButtonPushed, true);
app.ProcessVideo.Interruptible = 'off';
app.ProcessVideo.Position = [14 147 159 25];
app.ProcessVideo.Text = 'Process Video';
% Create FixBackground
app.FixBackground = uibutton(app.Tool, 'push');
app.FixBackground.ButtonPushedFcn = createCallbackFcn(app, @FixBackgroundPushed, true);
app.FixBackground.Interruptible = 'off';
app.FixBackground.Tooltip = {''};
app.FixBackground.Position = [186 246 159 25];
app.FixBackground.Text = 'Fix Background';
% Create EnterInfo
app.EnterInfo = uieditfield(app.Tool, 'text');
app.EnterInfo.Position = [14 215 159 25];
% Create CreateBackground
app.CreateBackground = uibutton(app.Tool, 'push');
app.CreateBackground.ButtonPushedFcn = createCallbackFcn(app, @CreateBackgroundButtonPushed, true);