-
Notifications
You must be signed in to change notification settings - Fork 0
/
processRunBatch.m
281 lines (238 loc) · 12.5 KB
/
processRunBatch.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
function [errorStack, errorStackMsg, files2Check] = processRunBatch(varargin)
% applies the processRun function to a set of analysisParamFiles created
% with differente dateSubj and run variables
% Inputs:
% - varargin can have the following forms:
% - one argument of the form paramBuilderFunctionName as a string i.e.
% 'buildAnalysisParamFileSocVid'. User will be prompted to select
% directory with files.
% - two arguments, the first as described above, the 2nd in the form of
% a cell array with desired paradigms {'naturalSocial', 'headTurnCon',
% 'headTurnIso', 'familiarFace'}
% - three arguments, the first two as described above, the 3rd being
% the monkeyName, which matches the name of the folder in the typical
% convention 'dateSubjName'.
% - Look at 'processRunBatchAll()' for batch of commands for full
% processing.
[~, machine] = system('hostname');
machine = machine(~isspace(machine));
switch machine
case 'Skytech_FA'
outputVolumeBatch = 'D:\DataAnalysis'; % The output folder for analyses performed.
dataLog = 'D:\EphysData\Data\analysisParamTable.xlsx'; % Only used to find recording log, used to overwrite params.
eventDataPath = 'C:\Onedrive\Lab\ESIN_Ephys_Files\Stimuli and Code\SocialCategories\eventData.mat';
subEventBatchStructPath = sprintf('%s/subEventBatchStruct.mat',outputVolumeBatch);
case 'homeDesktopWork'
outputVolumeBatch = 'H:\Analyzed'; % The output folder for analyses performed.
dataLog = 'H:\EphysData\Data\analysisParamTable.xlsx'; % Only used to find recording log, used to overwrite params.
eventDataPath = 'C:\Onedrive\Lab\ESIN_Ephys_Files\Stimuli and Code\SocialCategories\eventData.mat';
subEventBatchStructPath = sprintf('%s/subEventBatchStruct.mat',outputVolumeBatch);
case 'vera.rockefeller.edu'
outputVolumeBatch = '/Freiwald/faboharb/EphysAnalysis/Analyzed'; % The output folder for analyses performed.
dataLog = '/Freiwald/faboharb/EphysAnalysis/EphysData/analysisParamTable.xlsx'; % Only used to find recording log, used to overwrite params.
eventDataPath = '/Freiwald/faboharb/EphysAnalysis/stimDir/Stimuli and Code/SocialCategories/eventData.mat';
subEventBatchStructPath = sprintf('%s/subEventBatchStruct.mat',outputVolumeBatch);
otherwise
error('Matching machine not found')
end
usePreprocessed = 1; % uses preprocessed version of Phyzzy, only do when changing plotSwitch or calcSwitch and nothing else.
runParallel = 1; % Use parfor loop to go through processRun. Can't be debugged within the loop.
debugMode = 0; % Allows for easier debugging of the non-parallel loop. 'runParallel' must be false.
%% Load Appropriate variables and paths
addpath('buildAnalysisParamFileLib');
addpath(genpath('dependencies'));
if nargin == 1 || nargin == 2 || nargin == 3
%If there is only 1 file, it loads the analysisParamFile and composes a
%list from all the data files in the ephysVolume.
vararginInputs = varargin;
analysisParamFile = varargin{1};
analysisParamPath = feval(analysisParamFile);
load(analysisParamPath);
varargin = vararginInputs;
runListFolder = ephysVolume;
if nargin == 2
runList = buildRunList(runListFolder, 'paradigm', varargin{2});
elseif nargin == 3
runList = buildRunList(runListFolder, 'paradigm', varargin{2}, varargin{3});
elseif nargin ~= 2
try
runList = buildRunList(runListFolder, 'nev');
catch
error('Nothing selected');
end
end
elseif nargin >= 4 || nargin == 0
disp('Must have 1 or 2 inputs.')
return
end
%% Create all of the appropriate AnalysisParamFiles as a cell array.
analysisCount = sum(cellfun(@(x) length(x{2}), runList));
[analysisParamFileList, analysisParamFileName, outDirList] = deal(cell(analysisCount,1));
meta_ind = 1;
% Load in a page from an excel sheet in the data directory.
if exist(dataLog, 'file')
paramTable = readtable(dataLog,'ReadRowNames', true, 'PreserveVariableNames', true,'Format','auto');
else
paramTable = [];
end
for dateSubj_i = 1:length(runList)
dateSubject = runList{dateSubj_i}{1};
for run_i = 1:length(runList{dateSubj_i}{2})
runNum = runList{dateSubj_i}{2}{run_i};
dateSubjRun = [dateSubject runNum];
% Look through paramTable (if available) and replace variables
% Load variables from paramTable, if the row is present.
if ~isempty(paramTable) && any(strcmp(paramTable.Properties.RowNames, {dateSubjRun}))
paramTableRow = paramTable(dateSubjRun, :);
paramTableVars = paramTable.Properties.VariableNames';
for param_i = 1:width(paramTableRow)
if isa(paramTableRow.(paramTableVars{param_i}), 'double') && ~isnan(paramTableRow.(paramTableVars{param_i}))
eval(sprintf('%s = %d;', paramTableVars{param_i}, paramTableRow.(paramTableVars{param_i})))
elseif isa(paramTableRow.(paramTableVars{param_i}), 'cell')
eval(sprintf('%s = %s;', paramTableVars{param_i}, paramTableRow.(paramTableVars{param_i}){1}))
end
end
end
% Evaluates each row of 2nd argument input as a variable argument pair.
% if nargin == 3
% for arg_i = 1:size(varargin{2},1)
% eval(sprintf('%s = %s;', varargin{2}{arg_i, 1}, num2str(varargin{2}{arg_i, 2})));
% end
% end
% Update the psthPre w/ the ITI.
if exist('paramTableVars', 'var') && any(strcmp(paramTableVars, 'psthParams.psthPre'))
% Update timing related variables for ANOVA analysis
preFix = [-500 0];
Fix = [-800 0];
stimEarly = [0 500];
stimLate = [500 psthParams.psthImDur];
reward = [psthParams.psthImDur psthParams.psthImDur + 350];
epochTargParams.times = [preFix; Fix; stimEarly; stimLate; reward];
epochSWparams.startTime = -psthParams.psthPre;
% Update subevent Analysis stuff
subEventAnalysisParams.psthParams.movingWin = psthParams.movingWin;
subEventAnalysisParams.stimPlotParams.psthPre = psthParams.psthPre;
subEventAnalysisParams.stimPlotParams.psthImDur = psthParams.psthImDur;
subEventAnalysisParams.stimPlotParams.psthPost = psthParams.psthPost;
% Update lfpalign stuff
lfpAlignParams.msPreAlign = psthParams.psthPre+tfParams.movingWin(1)/2;
lfpAlignParams.msPostAlign = psthParams.psthImDur+psthParams.psthPost+tfParams.movingWin(1)/2;
spikeAlignParams.preAlign = psthParams.psthPre+3*psthParams.smoothingWidth;
spikeAlignParams.postAlign = psthParams.psthImDur+psthParams.psthPost+3*psthParams.smoothingWidth; %#ok
% Note to self - this stuff should be turned into a function handle
% and evaluated in runAnalysis.
end
% If Channels have been changed, redefine all the relevant channel
% names, if not, check for what was parsed before.
parsedFolderName = sprintf('%s/%s/%s%s_parsed',ephysVolume,dateSubject,dateSubject,runNum);
if exist('paramTableVars', 'var') && any(strcmp(paramTableVars, 'channels2Read'))
ephysParams.spikeChannels = channels2Read; %note: spikeChannels and lfpChannels must be the same length, in the same order, if analyzing both
ephysParams.lfpChannels = channels2Read;
ephysParams.channelNames = arrayfun(@(x) {sprintf('Ch%d', x)}, channels2Read);
ephysParams.lfpChannelScaleBy = repmat(8191/32764, [length(channels2Read),1]); %converts raw values to microvolts
ephysParams.unitsToUnsort = cell(length(channels2Read),1); %these units will be re-grouped with u0
ephysParams.unitsToDiscard = cell(length(channels2Read),1); %these units will be considered noise and discarded
elseif exist(parsedFolderName,'dir') == 7 && ~isempty(dir(parsedFolderName))
% Autodetecting spike channels - If the file is parsed, retrieve channels present
[ephysParams.spikeChannels, ephysParams.lfpChannels, ephysParams.channelNames] = autoDetectChannels(parsedFolderName);
end
% Generate appropriate Paths
outputVolume = outputVolumeBatch;
analogInFilename = sprintf('%s/%s/%s%s.ns2',ephysVolume,dateSubject,dateSubject,runNum); %#ok
[lfpFilename, photodiodeFilename, lineNoiseTriggerFilename] = deal(sprintf('%s/%s/%s%s.ns5',ephysVolume,dateSubject,dateSubject,runNum));
spikeFilename = sprintf('%s/%s/%s%s.nev',ephysVolume,dateSubject,dateSubject,runNum); %note that this file also contains blackrock digital in events
taskFilename = sprintf('%s/%s/%s%s.bhv2',stimulusLogVolume,dateSubject,dateSubject,runNum); %information on stimuli and performance
outDir = deal(sprintf('%s/%s/%s/%s/',outputVolume,dateSubject,analysisLabel,runNum));
% Update the many additional parts where outDir is required (function
% specific plots, make sure to update).
[eyeStatsParams.outDir, eyeStimOverlayParams.outDir, stimSyncParams.outDir, stimSyncParams.outDir, ephysParams.outDir, subEventAnalysisParams.outDir] = deal(outDir);
analysisParamFilename = strcat(outDir,analysisParamFilenameStem);
preprocessedDataFilename = strcat(outDir,preprocessedDataFilenameStem); %#ok
% Generate Directories
if ~exist(outDir,'dir')
mkdir(outDir);
end
% In case difference logfile is being used.
if ~logical(exist(taskFilename,'file'))
[A, B, C] = fileparts(taskFilename);
switch C
case '.mat'
taskFilename = [A '/' B '.bhv2'];
case '.bhv2'
taskFilename = [A '/' B '.mat'];
end
end
% Save files
save(analysisParamFilename);
outDirList{meta_ind} = outDir;
analysisParamFileList{meta_ind} = analysisParamFilename;
analysisParamFileName{meta_ind} = [dateSubject runNum];
meta_ind = meta_ind + 1;
end
end
%% Process the runs
[errorMsg, startTimes, endTimes, analysisOutFilename] = deal(cell(size(analysisParamFileList)));
[errorMsg{:}] = deal('notRun');
tmp = load(analysisParamFileList{1}, 'outputVolume');
outputVolume = tmp.outputVolume;
if usePreprocessed
for path_ind = 1:length(analysisParamFileList)
analysisParamFileList{path_ind} = [fileparts(analysisParamFileList{path_ind}) filesep 'preprocessedData.mat'];
end
end
if license('test','Distrib_Computing_Toolbox') && runParallel
parfor run_ind = 1:length(analysisParamFileList)
fprintf('run_ind reads %d... \n', run_ind);
fprintf('Processing %s... \n', analysisParamFileList{run_ind});
try
if usePreprocessed
[~, analysisOutFilename{run_ind}] = processRun('paramBuilder','buildAnalysisParamFileSocialVids', 'preprocessed',analysisParamFileList{run_ind});
else
[~, analysisOutFilename{run_ind}] = processRun('paramFile', analysisParamFileList{run_ind});
end
errorMsg{run_ind} = 'None';
catch MyErr
errorMsg{run_ind} = MyErr;
end
close all;
fprintf('Done! \n');
end
else
for run_ind = 1:length(analysisParamFileList)
fprintf('Processing %s... \n', analysisParamFileList{run_ind});
startTimes{run_ind} = datetime('now');
if ~debugMode
try
if usePreprocessed
[~, analysisOutFilename{run_ind}] = processRun('paramBuilder','buildAnalysisParamFileSocialVids', 'preprocessed', analysisParamFileList{run_ind});
else
[~, analysisOutFilename{run_ind}] = processRun('paramFile', analysisParamFileList{run_ind});
end
errorMsg{run_ind} = 'None';
catch MyErr
errorMsg{run_ind} = MyErr;
end
else
% This loop is identical to the one above, without the try clause.
% Should be used for testing problems in batch non-parallel run.
if usePreprocessed
[~, analysisOutFilename{run_ind}] = processRun('paramBuilder','buildAnalysisParamFileSocialVids', 'preprocessed',analysisParamFileList{run_ind});
else
[~, analysisOutFilename{run_ind}] = processRun('paramFile', analysisParamFileList{run_ind});
end
end
endTimes{run_ind} = datetime('now');
close all;
fprintf('Done! \n');
end
end
%% Report on Errors - package outputs
errorInd = ~strcmp(errorMsg, 'None');
if any(errorInd)
errorMsg = errorMsg(errorInd);
errorStack = [errorMsg{:}];
errorStackMsg = {errorStack.message}';
files2Check = analysisParamFileList(errorInd);
else
[errorStack, errorStackMsg, files2Check] = deal('None');
end