-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessImagingData.m
235 lines (200 loc) · 6.08 KB
/
ProcessImagingData.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
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
clear
close all
% Extracting voltage & Ca imaging data for f_getExperimentalData
output_file = 'DMG242PF_Fselected.xlsx' ;
% sheetname = 'DMG242PF_Dofetilide50nM_0.5Hz' ;
sheetname = 'Dofetilide50nM_0.5Hz' ;
headers = {'t', 'AP', 'CaT'} ;
t_end = 11000 ; % ms
data = zeros(1000000, length(headers)) ;
% Get AP data
file = uigetfile_n_dir([pwd, '/ExperimentalData'] , 'Select file(s) containing AP data (ch2):') ;
AP = imread(file{1}) ;
AP = AP' ;
% Select image sections to process
figure
imagesc(AP)
disp('Click on the upper and lower limits of the cell region')
clickpoints = ginput(2) ;
flashpixels = clickpoints(:,2) ;
pixel1 = round(min(flashpixels)) ;
pixel2 = round(max(flashpixels)) ;
flashregion = pixel1:pixel2 ;
flash = mean(AP(flashregion,:)) ;
disp('Click on left and right limits of F0 (background signal) region')
F0clickpoints = ginput(2) ;
baseline = F0clickpoints(:,1) ;
pixel1 = round(min(baseline)) ;
pixel2 = round(max(baseline)) ;
F0 = mean(flash(pixel1:pixel2)) ;
% figure
% hold on
% plot(1:length(flash), flash)
% plot(1:length(flash), ones(1, length(flash))*F0)
%
% figure
% hold on
% plot(1:length(flash), flash/F0)
AP_signal = flash/F0 ;
close
% AP_signal = mean(AP, 2) ;
data(1:length(AP_signal), 1) = linspace(0, t_end, length(AP_signal)) ; % time
data(1:length(AP_signal), 2) = AP_signal ; % AP
% Get CaT data
file = uigetfile_n_dir([pwd, '/ExperimentalData'], 'Select file(s) containing CaT data (ch1):') ;
CaT = imread(file{1}) ;
CaT = CaT' ;
% Select image section to process
figure
imagesc(CaT)
disp('Click on the upper and lower limits of the cell region')
clickpoints = ginput(2) ;
flashpixels = clickpoints(:,2) ;
pixel1 = round(min(flashpixels)) ;
pixel2 = round(max(flashpixels)) ;
flashregion = pixel1:pixel2 ;
flash = mean(CaT(flashregion,:)) ;
% disp('Click on left and right limits of F0 (background signal) region')
% clickpoints = ginput(2) ; % Use same location as AP
baseline = F0clickpoints(:,1) ;
pixel1 = round(min(baseline)) ;
pixel2 = round(max(baseline)) ;
F0 = mean(flash(pixel1:pixel2)) ;
CaT_signal = flash/F0 ;
close
% CaT_signal = mean(CaT, 2) ;
data(1:length(CaT_signal), 3) = CaT_signal ; % CaT
% Remove trailing zeros
i_end = find(data(:,1), 1, 'last') ;
data = data(1:i_end, :) ;
writematrix(data, ['ExperimentalData/', output_file], 'Sheet', sheetname) ;
% Manually fill in headers & stimV times :(
figure
subplot(2,2,1)
image(AP, 'CDataMapping', 'scaled')
yticks([])
yticklabels([])
ylabel('28 um')
xticks([])
xticklabels([])
xlabel('Time')
subplot(2,2,2)
image(CaT, 'CDataMapping', 'scaled')
yticks([])
yticklabels([])
ylabel('28 um')
xticks([])
xticklabels([])
xlabel('Time')
subplot(2,2,3)
plot(data(:,1), data(:,2), 'k-')
ylabel('F/F_0')
xlabel('Time (ms)')
title('AP')
xlim([0 t_end])
subplot(2,2,4)
plot(data(:,1), data(:,3), 'k-')
ylabel('F/F_0')
xlabel('Time (ms)')
title('CaT')
xlim([0 t_end])
sgtitle(sheetname)
% tbl = table(data, 'VariableNames', headers) ;
% writetable(tbl, output_file, 'Sheet', sheetname) ;
%% Mean filter test (post-erosion and last 5 beats tests)
n_filters = 5 ;
% n_erodes = 600:100:1000 ;
% n_filter = 5 ;
n_erode = 1000 ;
p = 1 ; % Subplot positioning
nbeats = 11 ;
n_diff = 1 ;
figure
set(gcf, 'Position', [0 0 500 length(n_filters)*500])
hold on
for i=1:length(n_filters)
expT = data(:, 1) ;
expV = data(:, 2) ;
expCaT = data(:, 3) ;
n_filter = n_filters(i) ;
% Find upstroke times
d2V = diff(expV, n_diff) ;
[~, stim_idx] = maxk(d2V, nbeats) ;
stim_idx = sort(stim_idx, 'ascend') ;
stimtimes = expT(stim_idx) ;
% Last 5
n = 5 ;
start_idx = stim_idx(end-5) ;
expT = expT(start_idx:end) ;
expT = expT - expT(1) ;
expV = expV(start_idx:end) ;
expCaT = expCaT(start_idx:end) ;
% Noise processing
erodedSignal = imerode(expV, ones(n_erode, 1)) ;
expV = expV - erodedSignal ;
% expV = medfilt1(expV, n_filter) ;
erodedSignal = imerode(expCaT, ones(n_erode, 1)) ;
expCaT = expCaT - erodedSignal ;
% expCaT = medfilt1(expCaT, n_filter) ;
% Trace averaging
% Find upstroke times again
d2V = diff(expV, n_diff) ;
[~, stim_idx] = maxk(d2V, 5) ;
stim_idx = sort(stim_idx, 'ascend') ;
stimtimes = expT(stim_idx) ;
% Average last 5 APs and CaTs
n = 5 ;
last_idx = stim_idx ;
avgV = [0;0] ;
avgCaT = [0;0] ;
for ii=1:length(last_idx)-1
addV = expV(last_idx(ii):last_idx(ii+1)) ;
if length(addV) > length(avgV)
avgV(numel(addV)) = 0 ;
elseif length(addV) < length(avgV)
addV(numel(avgV)) = 0 ;
end
avgV = avgV + addV ;
addCaT = expCaT(last_idx(ii):last_idx(ii+1)) ;
if length(addCaT) > length(avgCaT)
avgCaT(numel(addCaT)) = 0 ;
elseif length(addCaT) < length(avgCaT)
addCaT(numel(avgCaT)) = 0 ;
end
avgCaT = avgCaT + addCaT ;
end
v = avgV ./ n ;
ca = avgCaT ./ n ;
% expT = expT(last_idx(end)-length(v):last_idx(end)-1) ;
expT = linspace(0, expT(last_idx(end)) - expT(last_idx(end-1)), length(v)) ;
% expT = linspace(0, 1000, length(v)) ;
keepT = expT - expT(1) ;
expT = keepT ;
expV = v ;
expCaT = ca ;
% % Noise processing
% erodedSignal = imerode(expV, ones(n_erode, 1)) ;
% expV = expV - erodedSignal ;
expV = medfilt1(expV, n_filter) ;
% erodedSignal = imerode(expCaT, ones(n_erode, 1)) ;
% expCaT = expCaT - erodedSignal ;
expCaT = medfilt1(expCaT, n_filter) ;
subplot(length(n_filters), 2, p)
hold on
% plot(data(:,1), data(:, 2), ...
plot(expT, expV)
xlabel('Time (ms)')
title(['AP, filter size ' num2str(n_filter)])
% title('AP')
subplot(length(n_filters), 2, p+1)
hold on
% plot(data(:,1), data(:, 3), ...
plot(expT, expCaT)
xlabel('Time (ms)')
title(['CaT, filter size ' num2str(n_filter)])
% title('CaT')
p = p + 2 ;
end
%% Normalization
normV = (expV - min(expV)) ./ max(expV - min(expV)) ;
normCaT = (expCaT - min(expCaT)) ./ max(expCaT - min(expCaT)) ;