-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskeletonAlignmentTrial2.m
More file actions
159 lines (116 loc) · 5.9 KB
/
skeletonAlignmentTrial2.m
File metadata and controls
159 lines (116 loc) · 5.9 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
% Import Fluorescent traces from ROIs, with information about the plane of origin (nxm: n ROI number, m is total frame number/4, nx1: rows are plane the ROI is in, 4xm: m number of frames in ROI data, 4 planes)
% TimeFrame matrix: ()
%
%
%
% Bonsai 2P file: FrameTime
% Concatenate files
options.OPhysPath = 'Z:\ibn-vision\DATA\SUBJECTS\M25135\OPhys\20260219\';
allFilesinFolder = dir(options.OPhysPath);
allFilesinFolder = struct2table(allFilesinFolder);
contrastSessions = {'Contrast_'};
firstContrastSession = {'_00001_'};
contrastFilesIdx = find(contains(allFilesinFolder.name,contrastSessions));
contrastFiles = allFilesinFolder.name(contrastFilesIdx);
firstContrastFilesIdx = find(contains(contrastFiles,firstContrastSession));
secondContrastFilesIdx = find(~contains(contrastFiles,firstContrastSession));
firstContrastFiles = contrastFiles(firstContrastFilesIdx);
secondContrastFiles = contrastFiles(secondContrastFilesIdx);
% Name the Tiff files to create, because we will append to them - use the
% 'a' option
for thisContrastFile = 10:11
thisTiff = tiffreadVolume(append(options.OPhysPath,firstContrastFiles{thisContrastFile}));
tPlane1 = thisTiff(:,:,1:4:end);
tPlane2 = thisTiff(:,:,2:4:end);
tPlane3 = thisTiff(:,:,3:4:end);
tPlane4 = thisTiff(:,:,4:4:end);
for thisSlice = 1:size(tPlane1,3)
% Create the file to append to
plane1Tiff = Tiff("Z:\ibn-vision\DATA\SUBJECTS\M25135\OPhys\20260219\ConcatenatedPlanes\M25135_Contrast_20260219_00001_plane12.tif",'a');
% Set parameters for writing image
tagstruct.ImageLength = 512;
tagstruct.ImageWidth = 512;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.BitsPerSample = 16;
tagstruct.SampleFormat = Tiff.SampleFormat.Int;
tagstruct.SamplesPerPixel = 1;
% Append
setTag(plane1Tiff,tagstruct)
write(plane1Tiff,tPlane1(:,:,thisSlice))
clear plane1Tiff tagstruct
end
% Clear variables
clear thisTiff tPlane1 tPlane2 tPlane3 tPlane4
end
%%
% Load 2P Bonsai info
twoPLog = readtable("Z:\ibn-vision\DATA\SUBJECTS\M25135\Bonsai\20260219\M25135_Contrast_20260219_00001_2P2026-02-19T12_46_39.csv");
% Load ROI
ROImatrix = readtable("Z:\ibn-vision\DATA\SUBJECTS\M25135\Processed2P\20260219\M25135_Contrast_20260219_00001_plane1_ROImeasurement_combined.csv");
ROImatrix = removevars(ROImatrix,'Var1');
ROImatrix = table2array(ROImatrix);
ROImatrix = ROImatrix';
% plane Info
planeMatrix = repmat(1,size(ROImatrix,1),1);
% Split twoPLog TwoPFrametime by plane
planeFrameTime = twoPLog.TwoPFrameTime(1:4:end,:);
% Interpolate ROI measurements to full 2pFrameTime
for thisROI = 1:height(ROImatrix)
newROImatrix(thisROI,:) = interp1(planeFrameTime,ROImatrix(thisROI,:),twoPLog.TwoPFrameTime);
end
% Calculate deltaF
F0 = prctile(newROImatrix,10,2);
denominator = repmat(F0,1,length(newROImatrix));
deltaFOverF = newROImatrix-denominator;
deltaFOverF = deltaFOverF./denominator;
% Remove NaNs that were generated by the interpolation
idxNaN = find(isnan(deltaFOverF(:,:)));
%deltaFOverF(:,idxNaN) = [];
% Load Wheel
WheelDat = readtable("Z:\ibn-vision\DATA\SUBJECTS\M25135\Bonsai\20260219\M25135_Contrast_20260219_00001_Wheel2026-02-19T12_46_44.csv");
newWheelDat = interp1(WheelDat.ArduinoTime,WheelDat.Wheel,twoPLog.TwoPFrameTime);
% Pupil Data
EyeTimestamps = readtable("Z:\ibn-vision\DATA\SUBJECTS\M25135\EyeTracking\20260219\M25135_Contrast_20260219_00001_EyeCamTimeStamps2026-02-19T12_46_36.csv");
% Remove duplicates of samples in EyeTimeStamps
[tt,tp] = unique(EyeTimestamps.ArduinoTime);
EyeTimestamps = EyeTimestamps(tp,:);
EyeData = readtable("Z:\ibn-vision\DATA\SUBJECTS\M25135\EyeTracking\20260219\M25135_Contrast_20260219_00001_EyeCamLog_2026-02-19T12_46_32.csv");
EyeTimestamps.newArea = interp1(EyeData.Item1_eyeMillisSinceStartOfDay,EyeData.Item2_Area,EyeTimestamps.EyeCamTime,'linear','extrap');
uSyncEye = unique(EyeTimestamps.LastSyncPulseTime);
uSyncTwoP = unique(twoPLog.LastSyncPulseTime);
EyeTimestamps.newArduinoTime = align2PSyncPulses(uSyncEye,uSyncTwoP,EyeTimestamps.ArduinoTime);
newPupilArea = interp1(EyeTimestamps.newArduinoTime,EyeTimestamps.newArea,twoPLog.TwoPFrameTime,'linear','extrap');
% Load PD
PD = readtable("Z:\ibn-vision\DATA\SUBJECTS\M25139\Bonsai\20260220\M25139_Contrast_20260220_00001_Photodiode2026-02-20T13_00_38.csv");
% Find up and downphases from PD data
options.TwoPSampleRate = 30;
% Interpolate to twoPFrameTime
newPD.PDOutput = interp1(PD.ArduinoTime, PD.PDOutput, twoPLog.TwoPFrameTime);
[upPhases, downPhases] = PDUpDownPhases(newPD.PDOutput,options);
% Load stim info
stimInfo = readtable("Z:\ibn-vision\DATA\SUBJECTS\M25135\Bonsai\20260219\M25135_Contrast_20260219_00001_stimEvents2026-02-19T12_46_39.csv");
stimInfo = renamevars(stimInfo,"Var5",'ContrastLevel');
% Find trials
columnIdx = [-8:40];
stimFrame = [];
for thisStim = 1:size(upPhases,1)
[t,idx] = min(abs(twoPLog.TwoPFrameTime(upPhases(thisStim,1))-twoPLog.TwoPFrameTime(1:end-3)));
stimFrame(thisStim,1) = idx;
end
lowContrastFrameTime = stimFrame(stimInfo.ContrastLevel<0.5);
highContrastFrameTime = stimFrame(stimInfo.ContrastLevel>0.5);
lowContrastTrials = repmat(lowContrastFrameTime,1,length(columnIdx)) + repmat(columnIdx,length(lowContrastFrameTime),1);
highContrastTrials = repmat(highContrastFrameTime,1,length(columnIdx)) + repmat(columnIdx,length(highContrastFrameTime),1);
tdat = newROImatrix(4,:);
deltaFOverF = calculateDeltaF(newROImatrix,twoPLog.TwoPFrameTime,[-5 0],'deltafOverf');
tdat = mean(deltaFOverF,1);
lowContrastData = tdat(lowContrastTrials);
highContrastData = tdat(highContrastTrials);
subplot(421)
imagesc(columnIdx,1:size(lowContrastData,1),lowContrastData)
subplot(423)
imagesc(columnIdx,1:size(highContrastData,1),highContrastData)
subplot(223)
plot(columnIdx,mean(lowContrastData,1),'b'); hold on
plot(columnIdx,mean(highContrastData,1),'r')