forked from DeNardoLab/BehaviorDEPOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcCueFrames_msbatch.m
201 lines (164 loc) · 6.06 KB
/
calcCueFrames_msbatch.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
%% 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
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
%%
% assume save to CSV unless indicated otherwise
if ~exist('doCSV','var')
doCSV = 1;
end
% load experiment data
data = dir('*-*-*_*-*-*.mat');
load([data.folder '\' data.name]);
% load JSON file
msdata = read_json('metaData.json');
t = msdata.recordingStartTime;
% load frame lookup with timestamps
load('frtslu.mat');
% format start time
vid0 = [t.year, t.month, t.day, t.hour, t.minute, (t.second + (t.msec/1000))];
cuetimes.msStartTime = vid0;
%% convert timestamps to video frames
% four possible timestamps: csp, csm, us, laser
% do csp
if ~isempty(ts.csp_on)
csp_frames = zeros(size(ts.csp_on,1),2);
for i = 1:size(ts.csp_on,1)
csp_frames(i,1) = calcDifSeconds(vid0,ts.csp_on(i,:));
csp_frames(i,2) = calcDifSeconds(vid0,ts.csp_off(i,:));
end
cuetimes.csp = csp_frames;
% convert dif in seconds to dif in frames
cspms = cuetimes.csp * 1000;
match = zeros(size(cspms));
for f = 1:size(cspms,1)
t = cspms(f,1);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,1) = ind;
t = cspms(f,2);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,2) = ind;
end
cueframes.csp = match;
% for alignment. may introduce rounding error of 1 frame at offest
meandif = round(mean(cueframes.csp(:,2) - cueframes.csp(:,1)));
cueframes.csp = [cueframes.csp(:,1), cueframes.csp(:,1)+meandif];
end
% do csm
if ~isempty(ts.csm_on)
csm_frames = zeros(size(ts.csm_on,1),2);
for i = 1:size(ts.csm_on,1)
csm_frames(i,1) = calcDifSeconds(vid0,ts.csm_on(i,:));
csm_frames(i,2) = calcDifSeconds(vid0,ts.csm_off(i,:));
end
cuetimes.csm = csm_frames;
% convert dif in seconds to dif in frames
csmms = cuetimes.csm * 1000;
match = zeros(size(csmms));
for f = 1:size(csmms,1)
t = csmms(f,1);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,1) = ind;
t = csmms(f,2);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,2) = ind;
end
cueframes.csm = match;
% for alignment. may introduce rounding error of 1 frame at offest
meandif = round(mean(cueframes.csm(:,2) - cueframes.csm(:,1)));
cueframes.csm = [cueframes.csm(:,1), cueframes.csm(:,1)+meandif];
end
% do us
if ~isempty(ts.us_on)
us_frames = zeros(size(ts.us_on,1),2);
for i = 1:size(ts.us_on,1)
us_frames(i,1) = calcDifSeconds(vid0,ts.us_on(i,:));
us_frames(i,2) = calcDifSeconds(vid0,ts.us_off(i,:));
end
cuetimes.us = us_frames;
% convert dif in seconds to dif in frames
usms = cuetimes.us * 1000;
match = zeros(size(usms));
for f = 1:size(usms,1)
t = usms(f,1);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,1) = ind;
t = usms(f,2);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,2) = ind;
end
cueframes.us = match;
% for alignment. may introduce rounding error of 1 frame at offest
meandif = round(mean(cueframes.us(:,2) - cueframes.us(:,1)));
cueframes.us = [cueframes.us(:,1), cueframes.us(:,1)+meandif];
end
% do laser
if ~isempty(ts.laser_on)
laser_frames = zeros(size(ts.laser_on,1),2);
for i = 1:size(ts.laser_on,1)
laser_frames(i,1) = calcDifSeconds(vid0,ts.laser_on(i,:));
laser_frames(i,2) = calcDifSeconds(vid0,ts.laser_off(i,:));
end
cuetimes.laser = laser_frames;
% convert dif in seconds to dif in frames
laserms = cuetimes.laser * 1000;
match = zeros(size(laserms));
for f = 1:size(laserms,1)
t = laserms(f,1);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,1) = ind;
t = laserms(f,2);
[~,ind] = min(abs(frtslu(:,1) - t));
match(f,2) = ind;
end
cueframes.laser = match;
% for alignment. may introduce rounding error of 1 frame at offest
meandif = round(mean(cueframes.laser(:,2) - cueframes.laser(:,1)));
cueframes.laser = [cueframes.laser(:,1), cueframes.laser(:,1)+meandif];
end
save([data.folder '\' data.name],'cuetimes', 'cueframes', '-append');
save('cueframes.mat','cueframes');
disp('appended cueframes and cuetimes struct in .mat file');
disp(['for ' pwd]);
clearvars -except doCSV fps P j;
end
function video_folder_list = prepBatch(video_directory)
cd(video_directory)
directory_contents = dir;
directory_contents(1:2) = [];
ii = 0;
for i = 1:size(directory_contents, 1)
current_structure = directory_contents(i);
if current_structure.isdir
ii = ii + 1;
video_folder_list(ii) = string(current_structure.name);
disp([num2str(i) ' directory loaded'])
end
end
end
%% supporting fxn
function dif = calcDifSeconds(start, fin)
% calcDifSeconds: calculates difference in seconds between two vector date
% inputs.
% INPUT: t1 - start time; t2 - end time. Both input as [YYYY MM DD HH MM
% SS]. t2 can be vector or vector array
% OUTPUT: dif - difference between two timepoints in seconds
start = double(start);
fin = double(fin);
d2s = 24*3600; % convert from days to seconds
d1 = datenum(start); % convert from datetime format to serial date time
d1 = d2s*datenum(d1); % convert to seconds
dif = zeros(size(fin,1),1);
for i = 1:size(fin,1)
d2 = datenum(fin(i,:));
d2 = d2s*datenum(d2);
dif(i) = (d2-d1);
end
end