-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessCams.m
More file actions
647 lines (596 loc) · 26.8 KB
/
Copy pathProcessCams.m
File metadata and controls
647 lines (596 loc) · 26.8 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
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
function [BehaviorFile] = ProcessCams(varargin)
% This function will save a .mat file named 'Behavior_mouse-ID##_YYYY-MM-DDTHH_MM_SS'.
% The file contains a matlab structure for each camera used in the experiment.
% Each structure contains all the important information for the specific camera.
% If DLC has been run for a specific camera, ProcessCams will save the data
% in the camera's structure.
%
% This function must be run in the bonsai directory you want to process
%Input1:
%For Rig2 before blackfly: 'Rig2old'
%For Rig2 after blackfly: 'Rig2'
%For Rig4: no input
%% Step1: Process information from the cameras:
if nargin>=1
if isequal(varargin{1},'Rig2')
[Sky] = GetSkyVideo();
[Head] = GetAnalogVideo('Head',Sky.TTtimes);
[Reye] = GetAnalogVideo('Reye',Sky.TTtimes);
elseif isequal(varargin{1},'Rig2old')
[Sky] = GetSkyVideoRig2old();
[Head] = GetAnalogVideo('Head',Sky.TTtimes);
[Reye] = GetAnalogVideo('Reye',Sky.TTtimes);
else
[Sky] = GetSkyVideo();
[Head] = GetAnalogVideo('Head',Sky.TTtimes);
[Lear] = GetAnalogVideo('Lear',Sky.TTtimes);
[Rear] = GetAnalogVideo('Rear',Sky.TTtimes);
end
else
[Sky] = GetSkyVideo();
end
%% Step2: Save the camera structures in a .mat file named 'Behavior_mouse-IDnm_YYYY-MM-DDTHH_MM_SS.mat'
try
BehaviorFile = strcat(pwd, filesep, 'Behavior', Sky.vid.name(4:34),'.mat');
catch %Rig2old
BehaviorFile = strcat(pwd,filesep,'Behavior_mouse-', Sky.vid.name(5:28),'.mat');
end
Sky = AddOEinfo(Sky); %mw 10.6.23
if nargin>=1
if isequal(varargin{1},'Rig2')
save(BehaviorFile,'Sky','Head','Reye');
elseif isequal(varargin{1},'Rig2old')
save(BehaviorFile,'Sky','Head','Reye');
else
save(BehaviorFile,'Sky','Head','Lear','Rear')
end
else
save(BehaviorFile,'Sky');
end
end
%%%%%%%%%%%% Functions %%%%%%%%%%%%
function Sky = AddOEinfo(Sky)
% manually add ephysfolders and other openephys info to Sky
%this is saved in a OEinfo-*.mat file in the Bonsai directory by social_approach_messing_around
%mw 10.5.23
OEinfodir=dir('OEinfo-*.mat');
OEinfofilename=OEinfodir.name;
load(OEinfofilename)
Sky.EphysPath=EphysPath;
Sky.EphysPath_KS=EphysPath_KS;
Sky.ephysfolder=EphysPath_KS;
Sky.BonsaiPath=BonsaiPath;
Sky.OEversion=OEversion;
Sky.messages=messages;
Sky.OEsamplerate=OEsamplerate;
Sky.numsoundcardtriggers=numsoundcardtriggers;
Sky.TTL=TTL;
end
function [Sky] = GetSkyVideo(varargin) %Returns a structure with video information
Sky.vid = dir('Sky_m*.mp4'); %raw video from bonsai
if length(Sky.vid) < 1
error('Couldnt find Sky video')
end
if length(Sky.vid) > 1
for i = 1:length(Sky.vid)
if length(Sky.vid(i).name) == 38
Sky.vid = Sky.vid(i); %choose raw video instead of DLC-labeled video if present in the folder
break
end
end
end
obj = VideoReader(Sky.vid.name);
Sky.vid.framerate = obj.FrameRate;
Sky.vid.size = [obj.Width,obj.Height];
Sky.csv = dir('Sky_m*.csv');
if length(Sky.csv) > 1
for i = 1:length(Sky.csv)
if length(Sky.csv(i).name) == 38
Sky.csv = Sky.csv(i); %choose timestamp csv instead of DLC csv if one is present in the folder
break
end
end
end
Sky.times = textscan(fopen(Sky.csv.name),'%q');
Sky.times = Sky.times{1,1};
%mike 11.10.25
%Bonsai csvs, e.g. Sky_mouse-3179_2025-01-27T09_04_10.csv, are an array
%of frame numbers and timestamps. They used to be written with a space
%separating them, like this: '2129680413494840 2025-01-27T09:04:11.3879936-08:00 '
% But at some point Bonsai switch to a comma, like this: '6509744929697624,2025-10-21T18:38:58.8197632-07:00'
%so we have to determine which separator is used and split accordingly
if length(Sky.times{1})==length(Sky.times{2})
%rows are equal, so there's 1 comma-separated row per frame
Sky.times = cell2mat(Sky.times);
Sky.length = length(Sky.times);
for i=1:Sky.length
tmp(i,:)= extractAfter(Sky.times(i,:), ',');
end
Sky.times=tmp;
else
%rows are different, so there's a frame on odd rows and timestamp on even rows
Sky.times = Sky.times(2:2:end,:);
Sky.times = cell2mat(Sky.times);
Sky.length = length(Sky.times);
end
%Nick's original way
% Sky.times = Sky.times(2:2:end,:); Sky.times = cell2mat(Sky.times); Sky.times = Sky.times(:,1:27);
%Mike's new way, 4.24.2024 - do it more robustly by
%finding and splitting at the comma, but it only worked when Bonsai
%used a comma. So I updated it above (11.10.25)
for i=1:Sky.length
Sky.times(i,:) = strrep(Sky.times(i,:),'T','_');
end
Sky.times = Sky.times(:,1:end-6);
Sky.times = datetime(Sky.times, 'Format','yyyy-MM-dd_HH:mm:ss.SSSSSSS');
if ~isequal(Sky.length,obj.NumberOfFrames)
Sky.length = obj.NumberOfFrames;
try
Sky.times = Sky.times(1:Sky.length);
catch
% flagVariable = Sky;
% save('Flag_discordant.mat','flagVariable');
end
end
Sky.TTL = dir('TTL_m*.csv'); Sky.TTL = textscan(fopen(Sky.TTL.name),'%q'); Sky.TTL = Sky.TTL{1,1};
Sky.TTL = Sky.TTL(1:2:end,:); %trigger values
Sky.TTs = find(~cellfun(@isempty,strfind(Sky.TTL,'True'))); %Framenumber for all triggered SkyCam frames
%%%% This snippet here identifies the onset frame for each LED pulse,
%%%% and saves them as Sky.TTs (which stands for 'TrueTriggers')
temp = ones(size(length(Sky.TTs))); temp(1) = 90;
for i=2:length(Sky.TTs)
temp(i)=Sky.TTs(i)-Sky.TTs(i-1); %subtract previous trigger framenumber.
end
temp = temp - ones(size(temp)); temp = find(temp);
try
Sky.TTs = Sky.TTs(temp(1,:),1);
catch
Sky.TTs = [];
disp('No LED triggers found!')
end
clear temp;
%%%% End of that snippet
Sky.TTtimes = Sky.times(Sky.TTs,1); %timestamps for each trigger
Sky.NumberOfTrigs = length(Sky.TTs); %number of triggers detected
try
Sky.TTdur = time(between(Sky.TTtimes(1),Sky.TTtimes(end),'time')); %duration of video between first and last trigger
catch
Sky.TTdur = [];
end
Sky.dur = time(between(Sky.times(1),Sky.times(end),'time')); %duration of video
%%%% We should incorporate a comparison with the number of SCTs for a sanity check here in the future
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DLC tracks
try %if present, add tracked DLC points to the camera structure
Sky.dlc.vid = dir('Sky_m*0_labeled.mp4');
Sky.dlc.csv = dir('Sky_m*000.csv');
Sky.dlc.raw = textscan(fopen(Sky.dlc.csv.name),'%q'); Sky.dlc.raw = Sky.dlc.raw{1};
[Sky] = readDLCOutput(Sky);
end
try %if present, add filtered DLC points to the camera structure
Sky.fdlc.vid = dir('Sky_m*filtered_labeled.mp4');
Sky.fdlc.csv = dir('Sky_m*0_filtered.csv');
Sky.fdlc.raw = textscan(fopen(Sky.fdlc.csv.name),'%q'); Sky.fdlc.raw = Sky.fdlc.raw{1};
[Sky] = readfDLCOutput(Sky);
end
try %if present, add maDLC points to the camera structure
Sky.madlc.vid = dir('Sky_m*0_el_bp_labeled.mp4');
Sky.madlc.csv = dir('Sky_m*0_el_filtered.csv');
Sky.madlc.raw = textscan(fopen(Sky.madlc.csv.name),'%q'); Sky.madlc.raw = Sky.madlc.raw{1};
[Sky] = readmaDLCOutput(Sky);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Add in path to ephys folder
test = dir(); narrowdown = find([test.isdir]); %identifies folders present in our main experiment folder
for k = narrowdown
testing = strsplit(test(k).name,'_mouse-');
if length(testing) > 1 %then it is a folder with '_mouse-' in its name, so it's almost certainly the OE folder...
ephysfolder = strcat(test(k).folder,'\',test(k).name);
ephysfolderName = test(k).name;
end
end
try
Sky.ephysfolder = ephysfolder;
catch
Sky.ephysfolder = []; %no ephys folder found for this experiment
end
[Sky.DataRoot,Sky.BdirName,~] = fileparts(pwd);
Sky.DataRoot = strcat(Sky.DataRoot,filesep);
if ~isempty(Sky.ephysfolder)
Sky.dirName = ephysfolderName;
end
end
function [video] = GetAnalogVideo(varargin) %Returns a structure with video information
CamName = varargin{1}; TrueTrigTimes = varargin{2};
vidsearch = strcat(CamName,'_m*.mp4');
video.vid = dir(vidsearch);
if length(video.vid) < 1
errormessage = strcat('Couldnt find ',CamName,' video');
error(errormessage)
end
if length(video.vid) > 1
for i = 1:length(video.vid)
if length(video.vid(i).name) == 39
video.vid = video.vid(i); %choose raw video instead of DLC-labeled video if present in the folder
break
end
end
end
obj = VideoReader(video.vid.name);
video.vid.framerate = obj.FrameRate;
video.vid.size = [obj.Width,obj.Height];
csvsearch = strcat(CamName,'_m*.csv');
video.csv = dir(csvsearch); %timestamps from bonsai
if length(video.csv) < 1
errormessage = strcat('Couldnt find ',CamName,' csv file');
error(errormessage)
end
if length(video.csv) > 1
for i = 1:length(video.csv)
if length(video.csv(i).name) == 39
video.csv = video.csv(i); %choose timestamp csv instead of DLC csv if one is present in the folder
break
end
end
end
video.times = readtable(video.csv.name);
video.times = video.times(:,17); video.times = table2cell(video.times);
video.length = length(video.times);
for i=1:video.length
video.times{i,:} = video.times{i,:}(1:27);
video.times(i,:) = strrep(video.times(i,:),'T','_');
end
video.times = datetime(video.times, 'Format','yyyy-MM-dd_HH:mm:ss.SSSSSSS');
for i=1:length(TrueTrigTimes)
video.TTs(i) = find(TrueTrigTimes(i)<video.times, 1); %framenumber of trigger
end
try
video.TTtimes = video.times(video.TTs); %timestamps for each trigger
video.TTdur = time(between(video.TTtimes(1),video.TTtimes(end),'time')); %duration of video between first and last trigger
catch
video.TTtimes = []; %no triggers were found
video.TTdur = []; %no triggers were found
end
% % % interframeintervals = milliseconds(diff(video.times));
% % % figure;
% % % plot(interframeintervals,'.');
% % % idealNframes = (milliseconds(video.dur)/1000)*30;
% % % estimatedDroppedFrames = idealNframes - video.length;
% % % figure;
% % % plot(video.times,'.'); hold on;
fieldBframes = interp1(1:video.length,video.times,(1:video.length)+0.5);
fieldBframes(end) = video.times(end) + milliseconds(0.0167*1000);
% % % plot((1:video.length)+0.5,fieldBframes,'.'); hold on;
video.times = [video.times';fieldBframes];
video.times = video.times(:)';
% % % plot(video.times,'k.');
% % % video.length = video.length*2;
vidobject = VideoReader(video.vid.name);
video.length = vidobject.NumberOfFrames;
if ~isequal(video.length,length(video.times))
try
video.times = video.times(1:video.length);
catch
% flagVariable = video;
% save('Flag_discordant.mat','flagVariable');
% video.length = length(video.times);
end
end
video.TTs = (video.TTs*2)-1;
video.dur = time(between(video.times(1),video.times(end),'time')); %duration of video
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DLC tracks
try %if present, add tracked DLC points to the camera structure
vidsearch = strcat(CamName,'_m*labeled.mp4'); csvsearch = strcat(CamName,'_m*000.csv');
video.dlc.vid = dir(vidsearch);
video.dlc.csv = dir(csvsearch);
video.dlc.raw = textscan(fopen(video.dlc.csv.name),'%q'); video.dlc.raw = video.dlc.raw{1};
% [Sky] = readmaDLCOutput(Sky);
[video] = readDLCOutput(video);
end
try %if present, add filtered DLC points to the camera structure
vidsearch = strcat(CamName,'_m*full.mp4'); csvsearch = strcat(CamName,'_m*filtered.csv');
video.fdlc.vid = dir(vidsearch);
video.fdlc.csv = dir(csvsearch);
video.fdlc.raw = textscan(fopen(video.fdlc.csv.name),'%q'); video.fdlc.raw = video.fdlc.raw{1};
% [Sky] = readmaDLCOutput(Sky);
[video] = readfDLCOutput(video);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Add in path to ephys folder
test = dir(); narrowdown = find([test.isdir]); %identifies folders present in our main experiment folder
for k = narrowdown
testing = strsplit(test(k).name,'_mouse-');
if length(testing) > 1 %then it is a folder with '_mouse-' in its name, so it's almost certainly the OE folder...
ephysfolder = strcat(test(k).folder,'\',test(k).name);
ephysfolderName = test(k).name;
end
end
try
video.ephysfolder = ephysfolder;
catch
video.ephysfolder = []; %no ephys folder found for this experiment
end
[video.DataRoot,video.BdirName,~] = fileparts(pwd);
video.DataRoot = strcat(video.DataRoot,'\');
if ~isempty(video.ephysfolder)
video.dirName = ephysfolderName;
end
end
function [outputstructure] = readDLCOutput(inputstructure) %detects the unique points tracked and integrates them into the camera's matlab structure
titles = strsplit(inputstructure.dlc.raw{2,1},','); titles = titles(2:end);
numberofpoints = (length(titles))/3; %number of unique points tracked
u = unique(titles); %the names of each unique point tracked
%This snippet identifies the X,Y,&P values in the csv for each unique tracked point in the csv file
for i = 1:numberofpoints
columnIdx = [];
for ii = 1:length(titles)
columnIdx = [columnIdx;isequal(u(i),{titles{ii}})];
end
columns{1,i} = u(i);
columns{2,i} = find(columnIdx);
end
numberofframes = length(inputstructure.dlc.raw)-3;
for i = 1:numberofpoints
name = string(columns{1,i});
inputstructure.dlc.(char(name)) = dlmread(inputstructure.dlc.csv.name,',',[3,columns{2,i}(1),numberofframes+2,columns{2,i}(end)]);
end
%End of snippet
inputstructure.numberofpoints = numberofpoints;
outputstructure = inputstructure;
end
function [outputstructure] = readfDLCOutput(inputstructure) %detects the unique points tracked and integrates them into the camera's matlab structure
titles = strsplit(inputstructure.fdlc.raw{2,1},','); titles = titles(2:end);
numberofpoints = (length(titles))/3; %number of unique points tracked
u = unique(titles); %the names of each unique point tracked
%This snippet identifies the X,Y,&P values in the csv for each unique tracked point in the csv file
for i = 1:numberofpoints
columnIdx = [];
for ii = 1:length(titles)
columnIdx = [columnIdx;isequal(u(i),{titles{ii}})];
end
columns{1,i} = u(i);
columns{2,i} = find(columnIdx);
end
numberofframes = length(inputstructure.fdlc.raw)-3;
for i = 1:numberofpoints
name = string(columns{1,i});
inputstructure.fdlc.(char(name)) = dlmread(inputstructure.fdlc.csv.name,',',[3,columns{2,i}(1),numberofframes+2,columns{2,i}(end)]);
end
%End of snippet
inputstructure.numberofpoints = numberofpoints;
outputstructure = inputstructure;
end
function [outputstructure] = readmaDLCOutput(inputstructure) %detects the unique points tracked and integrates them into the camera's matlab structure
individuals = strsplit(inputstructure.madlc.raw{2,1},','); individuals = individuals(2:end);
titles = strsplit(inputstructure.madlc.raw{3,1},','); titles = titles(2:end);
numberofpoints = (length(titles))/3; %number of unique points tracked
u = unique(titles); %the names of each unique point tracked
%the below snippet assumes body parts are not repeated across
%individuals (e.g. for mouse and cricket)
% if instead we have 2 mice with the same body parts, we have to re-use
% those body part names mw 12.4.22
if numberofpoints==length(u)
%This snippet identifies the X,Y,&P values in the csv for each unique tracked point in the csv file
for i = 1:numberofpoints
columnIdx = [];
for ii = 1:length(titles)
columnIdx = [columnIdx;isequal(u(i),{titles{ii}})];
end
columns{1,i} = u(i);
columns{2,i} = find(columnIdx);
end
numberofframes = length(inputstructure.madlc.raw)-4;
for i = 1:numberofpoints
name = string(columns{1,i});
inputstructure.madlc.(char(name)) = dlmread(inputstructure.madlc.csv.name,',',[4,columns{2,i}(1),numberofframes+3,columns{2,i}(end)]);
end
%End of snippet
else %numberofpoints may exceed number of unique titles
for i = 1:numberofpoints
columnIdx = [];
for ii = 1:length(titles)
columnIdx = [columnIdx;isequal(titles(i),{titles{ii}})];
end
columns{1,i} = titles(i);
columns{2,i} = find(columnIdx);
end
numberofframes = length(inputstructure.madlc.raw)-4;
for i = 1:numberofpoints
name = string(columns{1,i});
inputstructure.madlc.(char(name)) = dlmread(inputstructure.madlc.csv.name,',',[4,columns{2,i}(1),numberofframes+3,columns{2,i}(end)]);
end
end
inputstructure.numberofpoints = numberofpoints;
outputstructure = inputstructure;
end
function [Sky] = GetSkyVideoRig2old(varargin) %Returns a structure with video information
Sky.vid = dir('Sky_*.mp4'); %raw video from bonsai
if length(Sky.vid) < 1
error('Couldnt find Sky video')
end
if length(Sky.vid) > 1
for i = 1:length(Sky.vid)
if length(Sky.vid(i).name) == 32
Sky.vid = Sky.vid(i); %choose raw video instead of DLC-labeled video if present in the folder
break
end
end
end
obj = VideoReader(Sky.vid.name);
try
Sky.vid.framerate = obj.FrameRate;
Sky.vid.size = [obj.Width,obj.Height];
catch
%on a mac, some bonsai mp4s cannot be read because of an invalid
%audio codec. So we can use a system call to ffprobe to get the
%framerate (which is all we need)
[status, framerate]=system(sprintf('/usr/local/bin/ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate %s', Sky.vid.name))
Sky.vid.framerate=str2num(strtok(framerate, '/'));
end
Sky.csv = dir('Sky_*.csv');
if length(Sky.csv) > 1
for i = 1:length(Sky.csv)
if length(Sky.csv(i).name) == 32
Sky.csv = Sky.csv(i); %choose timestamp csv instead of DLC csv if one is present in the folder
break
end
end
end
Sky.times = textscan(fopen(Sky.csv.name),'%q'); Sky.times = Sky.times{1,1};
Sky.times = Sky.times(17:17:end,:); Sky.times = cell2mat(Sky.times); Sky.times = Sky.times(:,1:27);
Sky.length = length(Sky.times);
for i=1:Sky.length
Sky.times(i,:) = strrep(Sky.times(i,:),'T','_');
end
Sky.times = datetime(Sky.times, 'Format','yyyy-MM-dd_HH:mm:ss.SSSSSSS');
if ~isequal(Sky.length,obj.NumberOfFrames)
Sky.length = obj.NumberOfFrames;
try
Sky.times = Sky.times(1:Sky.length);
catch
% flagVariable = Sky;
% save('Flag_discordant.mat','flagVariable');
end
end
Sky.TTL = dir('TTL_*.csv'); Sky.TTL = textscan(fopen(Sky.TTL.name),'%q'); Sky.TTL = Sky.TTL{1,1};
Sky.TTL = Sky.TTL(1:2:end,:); %trigger values
Sky.TTs = find(~cellfun(@isempty,strfind(Sky.TTL,'True'))); %Framenumber for all triggered SkyCam frames
%%%% This snippet here identifies the onset frame for each LED pulse,
%%%% and saves them as Sky.TTs (which stands for 'TrueTriggers')
temp = ones(size(length(Sky.TTs))); temp(1) = 90;
for i=2:length(Sky.TTs)
temp(i)=Sky.TTs(i)-Sky.TTs(i-1); %subtract previous trigger framenumber.
end
temp = temp - ones(size(temp)); temp = find(temp);
try
Sky.TTs = Sky.TTs(temp(1,:),1);
catch
Sky.TTs = [];
disp('No LED triggers found!')
end
clear temp;
%%%% End of that snippet
Sky.TTtimes = Sky.times(Sky.TTs,1); %timestamps for each trigger
Sky.NumberOfTrigs = length(Sky.TTs); %number of triggers detected
try
Sky.TTdur = time(between(Sky.TTtimes(1),Sky.TTtimes(end),'time')); %duration of video between first and last trigger
catch
Sky.TTdur = [];
end
Sky.dur = time(between(Sky.times(1),Sky.times(end),'time')); %duration of video
%%%% We should incorporate a comparison with the number of SCTs for a sanity check here in the future
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DLC tracks
try %if present, add tracked DLC points to the camera structure
Sky.dlc.vid = dir('Sky_m*0_labeled.mp4');
Sky.dlc.csv = dir('Sky_m*000.csv');
Sky.dlc.raw = textscan(fopen(Sky.dlc.csv.name),'%q'); Sky.dlc.raw = Sky.dlc.raw{1};
[Sky] = readDLCOutput(Sky);
end
try %if present, add filtered DLC points to the camera structure
Sky.fdlc.vid = dir('Sky_m*filtered_labeled.mp4');
Sky.fdlc.csv = dir('Sky_m*0_filtered.csv');
Sky.fdlc.raw = textscan(fopen(Sky.fdlc.csv.name),'%q'); Sky.fdlc.raw = Sky.fdlc.raw{1};
[Sky] = readfDLCOutput(Sky);
end
try %if present, add maDLC points to the camera structure
Sky.madlc.vid = dir('Sky_m*0_el_bp_labeled.mp4');
Sky.madlc.csv = dir('Sky_m*0_el_filtered.csv');
Sky.madlc.raw = textscan(fopen(Sky.madlc.csv.name),'%q'); Sky.madlc.raw = Sky.madlc.raw{1};
[Sky] = readmaDLCOutput(Sky);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Add in path to ephys folder
test = dir(); narrowdown = find([test.isdir]); %identifies folders present in our main experiment folder
for k = narrowdown
testing = strsplit(test(k).name,'_mouse-');
if length(testing) > 1 %then it is a folder with '_mouse-' in its name, so it's almost certainly the OE folder...
ephysfolder = strcat(test(k).folder,'\',test(k).name);
ephysfolderName = test(k).name;
end
end
try
Sky.ephysfolder = ephysfolder;
catch
Sky.ephysfolder = []; %no ephys folder found for this experiment
end
[Sky.DataRoot,Sky.BdirName,~] = fileparts(pwd);
Sky.DataRoot = strcat(Sky.DataRoot,'\');
if ~isempty(Sky.ephysfolder)
Sky.dirName = ephysfolderName;
end
%%%%%% Resolving potential mismatches between SCT and Events:
notebookfile = fullfile(Sky.ephysfolder,'notebook.mat'); load(notebookfile);
if ~isequal(Sky.NumberOfTrigs,length(stimlog)) %If the number of Sky.TTs doesnt match the number of SCTs
currentdir = pwd; cd(Sky.ephysfolder)
[~,~,~,chans.sampleRate,Events,~] = LoadExperiment();
chans.start = Events(1).soundcard_trigger_timestamp_sec*chans.sampleRate;
chans.stop = Events(end).soundcard_trigger_timestamp_sec*chans.sampleRate;
curr_aux1_chan = dir('*X1.continuous');
[rawdata, ~, ~] = load_open_ephys_data(curr_aux1_chan.name);
chans.Length = length(rawdata); clear rawdata;
cd(currentdir);
[Sky] = FixMissingTTLs(Sky,Events,chans); %Essentially assimilates the events using the first and last trigger and "corrects" Sky.TTs and associated fields
end
%%%%%%
end
function [Sky] = FixMissingTTLs(Sky,Events,chans)
for idx = 1:length(Events) %Get the OpenEphys sample for each event
EventSamples(idx) = round(Events(idx).soundcard_trigger_timestamp_sec*chans.sampleRate);
end
[OutputIndices] = ThisToThat_Lite(EventSamples,Events,Sky); %Get the corresponding Sky frame for each event
% "Correct" Sky.TTs and associated fields:
Sky.BrokenTTs = Sky.TTs;
Sky.TTs = OutputIndices';
Sky.TTtimes = Sky.times(Sky.TTs,1); %timestamps for each trigger
Sky.NumberOfTrigs = length(Sky.TTs); %number of triggers detected
end
function [OutputIndices] = ThisToThat_Lite(InputEventIndex,Events,Sky) %Run in either the bonsai folder, or it's OE folder]
%%%%% get position between trigs:
if ~isnan(Events(1).soundcard_trigger_timestamp_sec) %default to using the SCTs
Trig1_in = (Events(1).soundcard_trigger_timestamp_sec)*30000;
Trig2_in = (Events(end).soundcard_trigger_timestamp_sec)*30000;
[TrigRatio] = GetTrigRatio(InputEventIndex,Trig1_in,Trig2_in);
else %but use the events if SCTs not recorded (GetEventsAndSCT_Timestamps will warn you in this case)
Trig1_in = (Events(1).message_timestamp_samples);
Trig2_in = (Events(end).message_timestamp_samples);
[TrigRatio] = GetTrigRatio(InputEventIndex,Trig1_in,Trig2_in);
end
%%%%%% find equivalent sample between trigs, add offset by trig1 %%%%%%%
% Trig1_out = Sky.TTtimes(1);
% Trig2_out = Sky.TTtimes(end);
% [IdealTime] = GetOutputIndex(TrigRatio,Trig1_out,Trig2_out);
% [OutputIndices] = Time2Index(IdealTime, Sky);
Trig1_out = Sky.TTs(1);
Trig2_out = Sky.TTs(end);
[OutputIndices] = GetOutputIndex(TrigRatio,Trig1_out,Trig2_out);
OutputIndices = round(OutputIndices);
% [OutputIndices] = Time2Index(IdealTime, Sky);
function [TrigRatio] = GetTrigRatio(InputEventIndex,Trig1_in,Trig2_in)
TSBTinput = (Trig2_in)-(Trig1_in); %TotalSamplesBetweenTrigs
for i = 1:length(InputEventIndex)
TrigRatio(i) = (InputEventIndex(i)-Trig1_in(1))/(TSBTinput);
end
end
function [OutputIndex] = GetOutputIndex(TrigRatio,Trig1_out,Trig2_out)
TSBToutput = (Trig2_out)-(Trig1_out); %TotalSamplesBetweenTrigs
for i = 1:length(TrigRatio)
OutputIndex(i) = (TSBToutput*TrigRatio(i))+(Trig1_out);
end
end
function [OutputIndex] = Time2Index(IdealTime, Sky)
if isequal(IdealTime, 1)
for i = 1:length(IdealTime)
[~, OutputIndex(i)] = min(abs(Sky.times-IdealTime(i)));
end
else
[SortedIdealTime,Idx] = sort(IdealTime);
i1 =1;
for i = 1:length(SortedIdealTime)
while (Sky.times(i1)-SortedIdealTime(i))<0
i1=i1+1;
end
[~,x] = min(abs(Sky.times(i1-1:i1)-SortedIdealTime(i)));
OutputIndex(i) = i1+x-2;
i1 = i1-1;
end
newInd(Idx) = 1:length(IdealTime);
OutputIndex = OutputIndex(newInd);
end
end
end