forked from DeNardoLab/BehaviorDEPOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehDepo_mainscript.m
147 lines (118 loc) · 5.44 KB
/
BehDepo_mainscript.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
function BehDepo_mainscript(P)
%% Additional parameters
%Adjustable Parameters
P.hmpl = 1; %Apply hampel transformation to DLC data (outlier correction); 1 = On
P.cutoffThreshold = 0.1; % Minimum confidence value for retaining DLC-tracked points based on DLC p-value; throws out values from 0 to chosen threshold
%Smoothing Parameters
P.gapThreshold = 50; % (samples) largest gap (in samples) across which to interpolate missing values after jump removal. Defaults to 1 second of frames
P.jumpThreshold = 45; % (pixel / frame) movement speed that defines a large jumps (in pixels per sample)
P.nanRadius = 0; % (frames) radius surrounding jump event to fill in with NaNs
P.boxWidth = 10; % (frames) width of boxcar for smoothing
% Behavior Classification Parameters
% Freezing Parameters
% P.freezing_minDuration = 0.90; % (seconds) set minimum duration of freezing events to include
% P.freezing_velocityThreshold = 0.38; % (pixels per frame) set velocity maximum for freezing
% P.freezing_angleThreshold = 0.27; % (degrees) set angular movement max for freezing
P.freezing_windowWidth = P.freezing_minDuration * 0.7; % empirically-determined
P.freezing_countThreshold = 0.15; % empirically-determined
%% Batch Setup
% Collect 'video_folder_list' from 'P.video_directory'
P.script_dir = pwd; % directory with script files (avoids requiring changes to path)
disp('Select directory containing other directories for analysis'); % point to folder for analysis
P.video_directory = uigetdir('','Select the directory containing folders for analysis'); %Directory with list of folders containing videos + tracking to analyze
P.video_folder_list = prepBatch(string(P.video_directory)); %Generate list of videos to analyze
P.part_save = [];
P.part_lookup = [];
P.reuse_cue_name = [];
P.loadcueframes = [];
for j = 1:length(P.video_folder_list)
% Initialize
current_video = P.video_folder_list(j);
video_folder = strcat(P.video_directory, '\', current_video);
cd(video_folder) %Folder with data files
disp(['now doing ' video_folder])
basedir = pwd; %Assign current folder to basedir
if strcmpi(P.video_type, 'avi')
vid_extension = '*.avi';
elseif strcmpi(P.video_type, 'mp4')
vid_extension = '*.mp4';
else
disp('Video Not Recognized. Check P.video_type variable')
return
end
V = dir(vid_extension);
try
video_name = V.name;
catch ME
if (strcmp(ME.identifier,'MATLAB:needMoreRhsOutputs'))
msg = ['Error loading video. Double check P.video_type matches actual video filetype']; % common error is to not have correct video file type selected
causeException = MException('MATLAB:needMoreRhsOutputs',msg);
ME = addCause(ME,causeException);
end
rethrow(ME)
end
clear V
% back to script dir
cd(P.script_dir)
%% Collect frames for plots and draw ROIs
[frame, frame1, frame_idx, P] = videoInterface(strcat(video_folder,'\',video_name), P);
%% Save parameters to Params structure
Params = makeParamsStruct(P);
Params.basedir = basedir;
%% CSV/H5 Registration
[data, Params] = importDLCTracking(Params);
%% Convert data to Tracking structure and apply hampel correction
cd(P.script_dir);
Tracking = genTracking_custom(data, Params);
disp(['Tracked ' num2str(length(Params.part_names)) ' points']);
%% Smooth Tracking Data
Tracking = smoothTracking_custom(Tracking, Params);
% visual verification of point tracking and body part indexing
Plots.pointValidation = plotPointTracking(Tracking, Params, frame, frame_idx);
%% METRIC CALCULATION
[Metrics, Tracking, Params, P] = calculateMetrics_custom(Tracking, Params, P);
%% BEHAVIOR CLASSIFIERS
% FREEZING
if Params.do_freezing_classifier
Behavior.Freezing = calculateFreezing_custom(Metrics, Params);
end
% WALL REARING
if Params.do_wallrearing_classifier
Behavior.WallRearing = calculateRearing(Tracking, Params);
end
% MOVING CLASSIFIER
if Params.do_moving_classifier
Behavior.Moving = calculateMoving(Metrics, Params);
end
%% APPLY FILTERS TO BEHAVIORS
Behavior_Filter = struct; % separate struct to hold filtered behavior
%% APPLY SPATIAL FILTER
% separate behavior within and outside ROI
if Params.do_roi && Params.num_roi > 0
Behavior_Filter.Spatial = calculateROI(Behavior, Metrics, Params);
end
%% APPLY TEMPORAL FILTER
% seperate behavior during time-locked events
if Params.do_events
[Behavior_Filter.Temporal, Params, P] = calculateEvents(Behavior, Params, P);
end
%% INTERSECT SPATIAL AND TEMPORAL FILTERS
if Params.do_roi && Params.do_events
Behavior_Filter.Intersect = filterIntersect(Behavior, Behavior_Filter, Params);
end
%% Save
analyzed_folder_name = strcat(basedir, '\', current_video, '_analyzed');
saveAnalysis(analyzed_folder_name, Params, Tracking, Metrics, Behavior, Behavior_Filter)
%% VISUALIZATIONS
% plot freezing bouts
plot_bouts(Behavior, analyzed_folder_name);
% plot trajectory map
plotTrajectoryMap(Metrics, frame1, Params, Behavior, analyzed_folder_name);
% reset for next batch
clearvars -except j P;
disp(['Analyzed ' num2str(j) ' of ' num2str(length(P.video_folder_list))])
clf;
end
disp('Analysis completed. Results stored in data folders.')
close;
end