-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexampleAlignment.m
More file actions
122 lines (85 loc) · 3.69 KB
/
exampleAlignment.m
File metadata and controls
122 lines (85 loc) · 3.69 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
% Example alignment script for Kilotrode rig
% set mouseName, thisDate, and expNum first
%
% - Extracts times of rewards, beeps, wheel turns, licks
% - Loads timestamps of frames of eye and face movies
% - Loads timestamps of widefield if present
% - Computes example alignment with ephys
%
% For choiceworld:
% - Computes alignment between block and TL to get stimulus onset times
%
% For mpep:
% - Computes stimulus onset times and IDs
%
%
%% load timeline
load(dat.expFilePath(mouseName, thisDate, expNum, 'Timeline', 'master'));
tt = Timeline.rawDAQTimestamps;
%% Reward times: compute directly from timeline
rew = Timeline.rawDAQData(:, strcmp({Timeline.hw.inputs.name}, 'rewardEcho'));
[~, rewardOnsets] = schmittTimes(tt,rew, [2 3]);
%% sounds played: compute directly from timeline
aud = Timeline.rawDAQData(:, strcmp({Timeline.hw.inputs.name}, 'audioMonitor'));
aud = conv(aud.^2, gausswin(100), 'same'); % smooth
[~, soundOnsets] = schmittTimes(tt,aud, [0.02 0.03]);
% may have to choose this threshold by inspection:
% figure; plot(tt, aud);
%% laser times: compute directly from timeline
las = Timeline.rawDAQData(:, strcmp({Timeline.hw.inputs.name}, 'waveOutput'));
[~, laserOnsets] = schmittTimes(tt,aud, [0.2 0.3]);
% may have to choose this threshold by inspection, depending on your settings:
% figure; plot(tt, las);
% if using sine wave and only want the first, e.g., then:
laserOnsets = laserOnsets(diff([0;laserOnsets])>0.1); % choose events that don't have another event within preceding 100ms
%% eye and face videos (this may take some time, like five or ten minutes, to run)
alignVideo(mouseName, thisDate, expNum, 'eye');
tsPath = dat.expFilePath(mouseName, thisDate, expNum, 'eyetracking', 'master');
tsPath = fullfile(fileparts(tsPath), 'eye_timeStamps.mat');
load(tsPath);
eyeT = tVid;
alignVideo(mouseName, thisDate, expNum, 'face');
tsPath = dat.expFilePath(mouseName, thisDate, expNum, 'eyetracking', 'master');
tsPath = fullfile(fileparts(tsPath), 'face_timeStamps.mat');
load(tsPath);
faceT = tVid;
%%
load(dat.expFilePath(mouseName, thisDate, expNum, 'block', 'master'));
%% Some things from the block
tr = block.trial; tr = tr(1:block.numCompletedTrials);
cond = [tr.condition];
stimOn = [tr.stimulusCueStartedTime];
vcc = [cond.visCueContrast];
contrastLeft = vcc(1,:);
contrastRight = vcc(2,:);
choice = [tr.responseMadeID];
goCue = [tr.interactiveStartedTime];
responseTime = [tr.responseMadeTime];
reactionTime = [tr.responseMadeTime]-goCue;
feedback = [tr.feedbackType];
repNum = [cond.repeatNum];
trialStarts = [tr.trialStartedTime];
trialEnds = [tr.trialEndedTime];
[contrastCondDefs, ia, contrastCondsRaw] = unique(vcc', 'rows');
sw = block.stimWindowUpdateTimes;
%% get alignment between block and timeline: now using reward times
rew = Timeline.rawDAQData(:, strcmp({Timeline.hw.inputs.name}, 'rewardEcho'));
[~, rewardOnsets] = schmittTimes(tt,rew, [2 3]);
blockRewTimes = block.rewardDeliveryTimes(block.rewardDeliveredSizes(:,1)>0);
blockToTL = makeCorrection(rewardOnsets, blockRewTimes, true);
% old version, using photodiode times, susceptible to missed frames
% pd = Timeline.rawDAQData(:,strcmp({Timeline.hw.inputs.name}, 'photoDiode'));
% pdFlips = schmittTimes(tt,pd, [3 5]);
%
% blockToTL = makeCorrection(pdFlips(2:end-1), sw, true);
% if this does not work, you may need to pick different photodiode events.
% Try looking at this plot to figure out whether to drop one at the
% beginning or end or what.
% figure;
% plot(sw, ones(size(sw)), '.');
% hold on;
% plot(pdFlips, ones(size(pdFlips))+1, '.');
% ylim([-3 6])
%% save the correction
fp = fileparts(dat.expFilePath(mouseName, thisDate, expNum, 'block', 'master'));
save(fullfile(fp, 'blockToTL.mat'), 'blockToTL');