-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB01_getTraj.m
More file actions
313 lines (236 loc) · 8.9 KB
/
B01_getTraj.m
File metadata and controls
313 lines (236 loc) · 8.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
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
%%
% HT-LMD - High-Throughput Learning, Memory, and Drug-Testing Platform for Small Aquatic Models
% Copyright (C) 2025 Gokul Rajan
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <https://www.gnu.org/licenses/>.
%%
clear all; clc;
%%%%%%%%%%%%%%%%%%%%%%%%% IMPORTANT
% only use the coords flipping in this script if you used stimulus flipping
% in yuour experimental
% protocol.
% if you use coord fliiping, make sure the variable addStim is set correctly based on your stimulus flipping time in the protocol.
% check the stimulus protocol file from athe day of experiment if there is
% any confusion
%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%%%%%%%%%%%%%%%%%%%% how to navigate
%B01_getTraj -> B02_calAllParams --> B03_cleanData --> run tests like B04_stats
%%%%%%%%%%%%%%%%%%%%%%%%%
%%
camscale_px_per_mm =8.4;
data_rate_hz=1;
if ismac
addpath(genpath('/Users/gokulrajan/Documents/MATLAB/ZebranalysisSystem/zebraFunctions'));
rootDir='F:\GR\Dc_LnM_data';
elseif ispc
addpath(genpath('G:\GR\matlab_GR\usefulFunctions'));
addpath(genpath('G:\GR\matlab_GR\LnM-main'));
addpath(genpath('F:\GR\Dc_LnM_data'));
rootDir='F:\GR\Dc_LnM_data\9-arena-data';
end
%% get the calibration points here
% there will always be a "calib" folder inside the "xxxx-xx-xx" experiment
% folder
% irrespective of the number of imgs or the file name, this peogram will
% pick one img and provide a GUI interface to select 9 points of interest
disp('select the file for calibration');
if ismac
myCal = uigetdir(rootDir);
if ~isdir(myCal)
uiwait(warndlg('The selected directory does not exist'));
return;
end
calibfilePattern = fullfile(myCal,'*.png');
mycalibFiles = dir(calibfilePattern); %all files
cd(mycalibFiles(1).folder)
elseif ispc
myCal = uigetdir(rootDir);
if ~isdir(myCal)
uiwait(warndlg('The selected directory does not exist'));
return;
end
calibfilePattern = fullfile(myCal,'*.png');
mycalibFiles = dir(calibfilePattern); %all files
cd(mycalibFiles(1).folder);
end
disp('select the 9 mid points');
imshow(mycalibFiles(1).name);
[xc, yc] = ginput(9);
close all;
disp('mid points selected!');
pause(5);
%% assign xc and yx to the respective arena in the arena strcuture
for aa=1:length(xc)
arena(aa).center=[xc(aa),yc(aa)];
end
%% select the folder to analyse vdo
if ismac
myDir = uigetdir(rootDir);
if ~isdir(myDir)
uiwait(warndlg('The selected directory does not exist'));
return;
end
filePattern = fullfile(myDir,'*.avi');
myFiles = dir(filePattern); %all txt files
cd(myFiles(1).folder);
elseif ispc
myDir = uigetdir(rootDir);
if ~isdir(myDir)
uiwait(warndlg('The selected directory does not exist'));
return;
end
filePattern = fullfile(myDir,'*.avi');
myFiles = dir(filePattern); %all avi files
cd(myFiles(1).folder);
end
%%
for ff = 1:length(myFiles) % go through each file
tic;
dirName = myDir;
fileName = myFiles(ff).name;
inputName = fullfile(myDir,fileName);
obj = VideoReader(fileName);
%% read video and display
% read the file
mov1=cell(1,obj.NumFrames);
k = 1;
while hasFrame(obj)
mov1{k} = rgb2gray(readFrame(obj));
k = k+1;
end
mov3=cell2mat(reshape(mov1,1,1,[]));
clearvars mov1;
% imshow3D(mov3);
%% for subsequent loops
for aa=1:length(xc)
arena(aa).center=[xc(aa),yc(aa)];
end
% for aa=1:length(arena)
% [xc(aa)]=arena(aa).center(1);
% [yc(aa)]=arena(aa).center(2);
% end
%% preprocessing
% median image
medImage=median(mov3(:,:,1:10:300),3);
% imshow(medImage);
%% roi
disp('select the 3 LEDs');
% slecte the leds
cs1=roipoly(medImage);
cs2=roipoly(medImage);
csFull=roipoly(medImage);
disp('LEDs selected!');
disp('now select the arenas');
pause(5);
%% select the smaller arenas
imshow(medImage);
% Loop over the number of ROIs
for i = 1:9
% Let the user draw a rectangle on the image
title(sprintf('Draw ROI %d and double-click when done.', i))
roi = drawrectangle;
% Wait for the user to finish drawing the rectangle
wait(roi);
% Get the position of the rectangle
roiPosition = roi.Position;
% Create a binary mask that is the same size as the image
mask = false(size(medImage));
% Set the pixels inside the rectangle to 1
mask(round(roiPosition(2)):(round(roiPosition(2))+round(roiPosition(4))), ...
round(roiPosition(1)):(round(roiPosition(1))+round(roiPosition(3)))) = true;
% Save the mask in the arena structure
arena(i).roi = mask;
end
disp('checkpoint 1'); %uiwait();
%% extract the stim
addStim= 300;
mov4= medImage-mov3;
maskT1=cs1;hold on;
cs1tmp=(mov3.*uint8(maskT1));
maskT2=cs2;hold on;
cs2tmp=(mov3.*uint8(maskT2));
maskT3=csFull;
%csfulltmp;%(mov3.*uint8(maskT3));
maskT=maskT1+maskT2+maskT3;
baselineImg=(medImage.*uint8(maskT));
cs1tmp_=[0; round(diff(squeeze(mean(mean(cs1tmp-baselineImg,1),2)))/norm(diff(squeeze(mean(mean(cs1tmp-baselineImg,1),2))),inf))];
cs2tmp_=[0; round(diff(squeeze(mean(mean(cs2tmp-baselineImg,1),2)))/norm(diff(squeeze(mean(mean(cs2tmp-baselineImg,1),2))),inf))];
%csfulltmp_=[0; round(diff(squeeze(mean(mean(csfulltmp-baselineImg,1),2)))/norm(diff(squeeze(mean(mean(csfulltmp-baselineImg,1),2))),inf))];
cs1_start_idx=find(cs1tmp_==1);
cs1_end_idx=cs1_start_idx+addStim;
cs2_start_idx=find(cs2tmp_==1);
cs2_end_idx=cs2_start_idx+addStim;
clearvars mov1 mov2 mov3;
%% go through each ROI per frame here
% Pre-allocate the X and Y fields in the arena structure
for whichArena = 1:length(arena)
arena(whichArena).Xtmp = nan(size(mov4,3),1);
arena(whichArena).Ytmp = nan(size(mov4,3),1);
arena(whichArena).X = nan(size(mov4,3),1);
arena(whichArena).Y = nan(size(mov4,3),1);
end
% Loop over frames
for ikk = 1:size(mov4,3)
% Loop over arenas
for whichArena = 1:length(arena)
maskROI = arena(whichArena).roi;
mask=uint8(maskROI);
img_real = mov4(:,:,ikk).*mask;
img = img_real;
blob_img= (imgaussfilt(uint8(img),5));
blob_img=imbinarize(blob_img,0.1);
SE = strel('diamond',10);
blob_img=imdilate(blob_img,SE);
labeledImage = bwlabel(blob_img, 8);
blobMeasurements = regionprops(labeledImage, img, 'all');
numberOfBlobs = size(blobMeasurements, 1);
allBlobCentroids = [blobMeasurements.Centroid];
% Check if any blobs were found
if isempty(blobMeasurements)
% If no blobs were found, assign NaN
arena(whichArena).Xtmp(ikk) = NaN;
arena(whichArena).Ytmp(ikk) = NaN;
else
% Find the largest blob
[~, idx] = max([blobMeasurements.Area]);
% Assign the X and Y coordinates of the largest blob
arena(whichArena).Xtmp(ikk) = blobMeasurements(idx).Centroid(1);
arena(whichArena).Ytmp(ikk) = blobMeasurements(idx).Centroid(2);
end
end
clearvars -except addStim whichArena xc yc myDir myFiles fileName filePattern rootDir ff mov4 ikk X Y cs2_start_idx cs1_start_idx cs2_end_idx cs1_end_idx arena
%disp((ikk/size(mov4,3))*100);
end
disp('checkpoint 2'); %uiwait();
%% mirror the coords for the time period when the stimulus was flipped
for whichArena=1:size(arena,2)
flip_start1 = cs2_start_idx(1); % The stimulus was flipped
flip_end1 = flip_start1+addStim; % The stimulus flipped period %addStim
% Flip the x-coordinates during the times when the stimulus was flipped
flipped_data = arena(whichArena).Xtmp(:);
flipped_data(flip_start1:flip_end1) = 2*arena(whichArena).center(1) - flipped_data(flip_start1:flip_end1);
if length(cs2_start_idx)>1
flip_start2 = cs2_start_idx(2); % The stimulus was flipped again at 900 seconds
flip_end2 = flip_start2+addStim; % The stimulus was flipped back at 1200 seconds
flipped_data(flip_start2:flip_end2) = 2*arena(whichArena).center(1) - flipped_data(flip_start2:flip_end2);
end
arena(whichArena).X = flipped_data;
arena(whichArena).Y= arena(whichArena).Ytmp(:);
clearvars flipped_data;
end
%%
toc; % time for processing one video
save(strcat(myDir,fileName,'.mat'),'cs1_start_idx','cs2_start_idx', 'arena','-v7.3');
clearvars -except addStim myDir myFiles xc yc filePattern rootDir ff
end