-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoBuilder.m
308 lines (247 loc) · 10.6 KB
/
autoBuilder.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
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
%% USER SETTING
%{
startDir = 'X:\Video\PHILLIP\';
startDir = 'X:\Video\PHILLIP\';
cells2run = [15 58 63 64];
cells2run = cells2run+2000
deleteFilesOn = true;
[catchcell] = autoBuilder(startDir, cells2run, deleteFilesOn)
%}
function [catchcell] = autoBuilder(startDir, cells2run, deleteFilesOn)
if deleteFilesOn
continue1 = false;
question1 = 'Are you sure you want to delete ALL .WL .WST .WT and .WTLIA files in ALL THE DIRECTORIES LISTED?';
question2 = 'ARE YOU REALLY REALLY SURE YOU WANT TO DO THAT? IF YOU DONT KNOW WHAT YOU ARE DOING THEN STOP!!';
tmp1 = questdlg(question1);
if strcmp(tmp1, 'Yes')
tmp1 = questdlg(question2);
if strcmp(tmp1, 'Yes')
continue1 = true;
end
end
end
if continue1 == false
error('stopped becasue user wasn''t ready to delete all kinds of files, probs a good idea')
end
%%
tic
disp('finding all directories, this can take up to 1 min')
dirs = regexp(genpath(startDir),['[^;]*'],'match');
toc
%%
allcellBuilders = {};
% allCellDirs = {};
builderFileNums = [];
for k= 1:length(dirs)
fprintf('searching in directory %d of %d\n', k, length(dirs))
cd(dirs{k});
tmp1 = dir('*_BUILDERSAVE.mat');
for kk = 1:length(tmp1)
% keyboard
% if min(size(tmp1))>1
% error('2 BULDER FILES IN THE SAME DIRECTORY');
% elseif min(size(tmp1))==1
allcellBuilders{end+1} = [dirs{k}, filesep, tmp1(kk).name];
% allCellDirs{end+1} = dirs{k};
ind2 = strfind(allcellBuilders{end}, '_BUILDERSAVE.mat') - 1;
ind1 = strfind(allcellBuilders{end}(1:ind2), '_');
ind1 = ind1(end)+1;
builderFileNums(end+1) = str2num(allcellBuilders{end}(ind1:ind2));
end
end
if length(unique(builderFileNums)) ~= length(builderFileNums)
keyboard
end
[cells2run2, ~, cells2runInd] = intersect(cells2run, builderFileNums, 'stable');
allcellBuilders2 = allcellBuilders(cells2runInd);
%% load teh necesarry info
catchcell = {};
for k = 1:length(allcellBuilders2)
try
crush
pause(5)
clearvars -except allcellBuilders2 dirs k catchcell deleteFilesOn
sectionComp = 0;
load(allcellBuilders2{k})
sectionComp = 1
sectionComp = 2
%%
%% Step 3 - run everything
% DONT FORGET TO DELETE OLD FILES IF YOU HAVE MULTIPLE OF THE SAME TRIAL IN HERE!
% select matching files
%tmp = cellfun(@(x)str2num(x(15:end)),includef);
%incf_idx = find(tmp>= 12 & tmp <=85);
tic
if ~exist('follicleExtrapDistInPix')
follicleExtrapDistInPix = 33
end
if deleteFilesOn
cd(vidDir)
delete *_WL.mat
delete *_WST.mat
delete *_WT.mat
delete *-WTLIA.mat
end
try
vidFile.Width
catch
tmpVdir = dir( [vidDir, filesep, '*.mp4']);
vidNum = Sample(1:length(tmpVdir));
vidName = [tmpVdir(vidNum).folder , filesep, tmpVdir(vidNum).name];
try
vidFile = VideoReader(vidName);
frame = readFrame(vidFile);
catch
vidFile = mmread(vidName);
frame = vidFile.frames(1).cdata;
vidFile.Width = vidFile.width;
vidFile.Height = vidFile.height;
end
end
try
% Whisker.makeAllDirectory_WhiskerTrial(vidDir,0,'mask',maskPoints,...
Whisker.makeAllDirectory_WhiskerTrial(vidDir,0,'mask',maskPoints,...
'trial_nums',trialNums,'include_files',includef,...
'barRadius',10,'faceSideInImage', 'top', 'framePeriodInSec',.001,...
'imagePixelDimsXY',[vidFile.Width vidFile.Height],'pxPerMm',33,...
'mouseName',mouseName,'sessionName',sessionName,'protractionDirection','leftward')
catch
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
for k = 1:length(includef)
includef{k}(9) = '9';
end
Whisker.makeAllDirectory_WhiskerTrial(vidDir,0,'mask',maskPoints,...
'trial_nums',trialNums,'include_files',includef,...
'barRadius',10,'faceSideInImage', 'top', 'framePeriodInSec',.001,...
'imagePixelDimsXY',[vidFile.Width vidFile.Height],'pxPerMm',33,...
'mouseName',mouseName,'sessionName',sessionName,'protractionDirection','leftward')
end
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%%
%% i changed teh name of the video files and so this try catch is to change 18**** to 19****
%%
sectionComp = 3
Whisker.makeAllDirectory_WhiskerSignalTrial(vidDir,'include_files',includef,'polyRoiInPix',[99-33 99+33],'follicleExtrapDistInPix',follicleExtrapDistInPix);
sectionComp = 4
Whisker.makeAllDirectory_WhiskerTrialLiteI(vidDir,'include_files',includef,'r_in_mm',3,'calc_forces',true,'whisker_radius_at_base', 36.5,'whisker_length', 18,'baseline_time_or_kappa_value',0);
sectionComp = 5
wl = Whisker.WhiskerTrialLiteArray(vidDir);
sectionComp = 6
save([vidDir filesep mouseName sessionName '-WTLIA.mat'],'wl');
sectionComp = 7
tid = 0; % Set trajectory ID to view
Whisker.view_WhiskerTrialLiteArray(wl,tid)
timeSpent = toc
hours = timeSpent/(60*60)
sectionComp = 8
%%########%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%%#################%%%%%
%%%%%%%%%%%%%%%%WHISKER ARRAY BUILDER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%..........................................................%%%%%%%
%%
sectionComp = 10
spikes_trials = s.get_spike_times; % takes into account allgood to replace spikes
for i = 1:length(spikes_trials.spikesTrials)
spikes_trials.spikesTrials{i}.spikeTimes = spikes_trials.spikesTrials{i}.spikeTimes(allgood{i});
end
save(['C:\Users\maire\Dropbox\HIRES_LAB\PHIL\Data\SpikesData\SweepArrays\sweepArray_' mouseName '_' sessionName '_' cellnum '_' code '_' cellNumberForProject '.mat'],'s')
% save(['C:\Users\maire\Dropbox\HIRES_LAB\PHIL\Data\SpikesData\indexingVars\indexingMat_' mouseName '_' sessionName '_' cellnum '_' code '_' cellNumberForProject '.mat'],...
% 'indexingMat','spksToPlotGood' ,'spksToPlotBad', 'good', 'bad', 'TrialIndCount')
save(['C:\Users\maire\Dropbox\HIRES_LAB\PHIL\Data\SpikesData\SpikeArrays\spikes_trials_' mouseName '_' sessionName '_' cellnum '_' code '_' cellNumberForProject '.mat'],'spikes_trials')
T = LCA.TrialArray(b,spikes_trials,wl);
% to make this work projectDetails you must make that a property in
% +LCA/@TrialArray/TrialArray.m file and then copy over the @projectDetails
% folder into +LCA folder. Won't affect anyone elses stuff-PSM
T.projectDetails = LCA.projectDetails;
T.projectDetails.cellNumberForProject = cellNumberForProject;
T.projectDetails.projectName = projectName;
T.projectDetails.category1 = category1;
T.projectDetails.category2 = category2;
T.projectDetails.category3 = category3;
T.projectDetails.category4 = category4;
T.projectDetails.category5 = category5;
T.projectDetails.cellNotes = cellNotes;
T.projectDetails.prePerformanceRegion = prePerformanceRegion;
T.projectDetails.goodPerformanceRegion = goodPerformanceRegion;
T.projectDetails.postPerformanceRegion = postPerformanceRegion;
T.projectDetails.spikesNormalRegion = spikesNormalRegion;
T.projectDetails.spikesFastBrokeInCellRegion = spikesFastBrokeInCellRegion;
T.whiskerTrialTimeOffset = whiskerTrialTimeOffset;
T.depth = depth;
T.recordingLocation = recordingLocation;
for i = 1:length(T.trials)
T.trials{i}.spikesTrial.spikeTimes = T.trials{i}.spikesTrial.spikeTimes(:);
end
save(['C:\Users\maire\Dropbox\HIRES_LAB\PHIL\Data\TrialArrayBuilders\trial_array_' mouseName '_' sessionName '_' cellnum '_' code '_' cellNumberForProject '.mat'],'T')
cd('C:\Users\maire\Dropbox\HIRES_LAB\PHIL\Data\TrialArrayBuilders\');
figure;T.plot_spike_raster(0,'BehavTrialNum')
T
save(['C:\Users\maire\Dropbox\HIRES_LAB\PHIL\Data\Characterization\trial_array_' cellNumberForProject '.mat'],'T')
%date and time to make unique files in case of accidental overwirte can
%delete these when not needed anymore
save(['C:\Users\maire\Dropbox\HIRES_LAB\PHIL\Data\Characterization\trial_array_' cellNumberForProject '_' dateString '_.mat'],'T')
beep
clipboard('copy',vidDir)
try
messageForPB = ['SUCCESS cellname ', num2str(cellNumberForProject)]
pBullet = Pushbullet('o.LLzZkkdcd6WN8MG5HyQjFDsCCSmRBAzw')
pBullet.pushNote([],'Cell TArray Builder',messageForPB)
catch
end
catch catchMes
if sectionComp~= 0
cd(vidDir)
save([num2str(cellNumberForProject), '_erroredOutSoSaved'])
messageForPB = ['failpoint ', num2str(sectionComp), ' cellname ', ...
num2str(cellNumberForProject)];
pBullet = Pushbullet('o.LLzZkkdcd6WN8MG5HyQjFDsCCSmRBAzw')
pBullet.pushNote([],'Cell TArray Builder',messageForPB)
try
pBullet.pushNote([],' ', catchMes.stack.file)
pBullet = Pushbullet('o.LLzZkkdcd6WN8MG5HyQjFDsCCSmRBAzw')
pBullet.pushNote([],'', catchMes.stack.name)
pBullet = Pushbullet('o.LLzZkkdcd6WN8MG5HyQjFDsCCSmRBAzw')
pBullet.pushNote([],'', num2str(catchMes.stack.line))
catch
end
catchcell{end+1} = messageForPB
end
end
end