forked from djoshea/trial-data
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrialDataConditionAlign.m
More file actions
586 lines (488 loc) · 21.7 KB
/
TrialDataConditionAlign.m
File metadata and controls
586 lines (488 loc) · 21.7 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
classdef TrialDataConditionAlign < TrialData
properties(SetAccess=protected)
conditionInfo
alignInfo
end
% Properties which read through to ConditionInfo
properties(Dependent)
nConditions
listByCondition
conditionIdx
conditionSubs
conditions
conditionsAsStrings
conditionNames
conditionAppearances
axisValueLists
axisValueListsAsStrings
conditionAppearanceFn
end
% Initializing and building
methods
function td = TrialDataConditionAlign(varargin)
td = td@TrialData(varargin{:});
td = td.initializeConditionInfo();
td = td.initializeAlignInfo();
td = td.updateValid();
end
end
% General utilites
methods
% print a short description
function disp(td)
td.printDescriptionShort();
td.alignInfo.printOneLineDescription();
td.conditionInfo.printOneLineDescription();
fprintf('\n');
td.printChannelInfo();
fprintf('\n');
end
% synchronize valid between AlignInfo and ConditionINfo
function td = updateValid(td)
td.warnIfNoArgOut(nargout);
cvalid = td.conditionInfo.computedValid;
avalid = td.alignInfo.computedValid;
if isempty(td.manualValid)
td.manualValid = truevec(td.nTrials);
end
valid = td.manualValid & cvalid & avalid;
td.conditionInfo = td.conditionInfo.setInvalid(~valid);
td.alignInfo = td.alignInfo.setInvalid(~valid);
end
function valid = buildValid(td)
valid = td.conditionInfo.valid & td.alignInfo.valid;
if ~isempty(td.manualValid)
valid = valid & td.manualValid;
end
end
function td = dropChannels(td, names)
names = wrapCell(names);
% check whether any of the alignInfo events and error if so
alignEvents = td.alignInfo.getEventList();
mask = ismember(names, alignEvents);
if any(mask)
error('TrialData alignment depends on event %s', ...
strjoin(alignEvents(mask)));
end
% remove from condition info
conditionParams = td.conditionInfo.attributeNames;
mask = ismember(names, conditionParams);
if any(mask)
warning('TrialData condition depends on params %s, removing from grouping axes', ...
strjoin(conditionParams(mask)));
td.conditionInfo = td.conditionInfo.removeAttribute(names(mask));
end
td = dropChannels@ConditionDescriptor(td, names);
end
end
% ConditionInfo control
methods
% parameters that are either scalar or strings
function names = listConditionInfoCompatibleParamChannels(td)
channelDescriptors = td.getChannelDescriptorArray();
mask = arrayfun(@(cd) isa(cd, 'ParamChannelDescriptor') && ...
(cd.isScalarByField{1} || cd.isStringByField{1}), ...
channelDescriptors);
names = {channelDescriptors(mask).name}';
end
function paramStruct = getConditionInfoCompatibleParamStruct(td)
names = td.listConditionInfoCompatibleParamChannels();
paramStruct = keepfields(td.data, names);
end
function td = initializeConditionInfo(td)
td.warnIfNoArgOut(nargout);
paramStruct = emptyStructArray(td.nTrials);
td.conditionInfo = ConditionInfo.fromStruct(paramStruct);
end
function td = addChannel(td, varargin)
td.warnIfNoArgOut(nargout);
td = addChannel@TrialData(td, varargin{:});
% Don't do this anymore, we no longer auto-add attributes to
% conditionInfo until they are needed
% % detect whether any new condition info compatible params
% % have been added
% namesOld = td.listConditionInfoCompatibleParamChannels();
% td = addChannel@TrialData(td, varargin{:});
% names = td.listConditionInfoCompatibleParamChannels();
%
% % if so, add them to the condition info with valueLists
% % specified
% newAttr = setdiff(names, namesOld);
% for iA = 1:numel(newAttr)
% td.conditionInfo = td.conditionInfo.addAttribute(newAttr{iA}, ...
% 'values', td.getParam(newAttr{iA}));
% end
end
function td = selectTrials(td, mask)
td.warnIfNoArgOut(nargout);
td = selectTrials@TrialData(td, mask);
td.conditionInfo = td.conditionInfo.selectTrials(mask);
td.alignInfo = td.alignInfo.selectTrials(mask);
end
function td = addAttribute(td, names)
% add attributes in names that aren't already in ConditionInfo
new = setdiff(names, td.conditionInfo.attributeNames);
for i = 1:numel(new)
td.conditionInfo = td.conditionInfo.addAttribute(new{i}, ...
'values', td.getParamRaw(new{i}));
end
end
function td = groupBy(td, varargin)
td.warnIfNoArgOut(nargout);
% add any needed attributes to condition info
for i = 1:numel(varargin)
td = td.addAttribute(varargin{i});
end
td.conditionInfo = td.conditionInfo.groupBy(varargin{:});
td = td.postUpdateConditionInfo();
end
function td = ungroup(td)
td.warnIfNoArgOut(nargout);
td = td.groupBy();
end
% will undo any filtering by attribute value lists and removing
% binning
function td = setAllAttributeValueListsAuto(td)
td.warnIfNoArgOut(nargout);
td.conditionInfo = td.conditionInfo.setAllAttributeValueListsAuto();
td = td.postUpdateConditionInfo();
end
function td = setAttributeValueListAuto(td, attr)
td.warnIfNoArgOut(nargout);
td.conditionInfo = td.conditionInfo.setAttributeValueListAuto(attr);
td = td.postUpdateConditionInfo();
end
function td = binAttribute(td, varargin)
td.warnIfNoArgOut(nargout);
td = td.addAttribute(varargin{1});
td.conditionInfo = td.conditionInfo.binAttribute(varargin{:});
td = td.postUpdateConditionInfo();
end
function td = binAttributeUniform(td, varargin)
td.warnIfNoArgOut(nargout);
td = td.addAttribute(varargin{1});
td.conditionInfo = td.conditionInfo.binAttributeUniform(varargin{:});
td = td.postUpdateConditionInfo();
end
function td = binAttributeQuantiles(td, varargin)
td.warnIfNoArgOut(nargout);
td = td.addAttribute(varargin{1});
td.conditionInfo = td.conditionInfo.binAttributeQuantiles(varargin{:});
td = td.postUpdateConditionInfo();
end
function td = setAttributeValueList(td, attrName, valueList)
td.warnIfNoArgOut(nargout);
td = td.addAttribute(attrName);
td.conditionInfo = td.conditionInfo.setAttributeValueList(attrName, valueList);
end
function valueList = getAttributeValueList(td, attrName)
td = td.addAttrbute(attrName);
valueList = td.conditionInfo.getAttributeValueList(attrName);
end
function td = postUpdateConditionInfo(td)
td.warnIfNoArgOut(nargout);
td = td.updateValid();
end
% filter trials that are valid based on ConditionInfo
function td = filterValidTrialsConditionInfo(td, varargin)
td.warnIfNoArgOut(nargout);
td = td.selectTrials(td.conditionInfo.valid);
end
% filter trials where a specific attribute matches a specific value
% pass-thru to ConditionInfo
function td = filteredByAttribute(td, varargin)
% call on ConditionInfo, and filter based on its valid trials
td.warnIfNoArgOut(nargout);
% no need to copy, filteredBy already copies the conditionInfo
td.conditionInfo = td.conditionInfo.filteredByAttribute(varargin{:});
td = td.filterValidTrialsConditionInfo();
end
% filter trials based on matching attribute values,
% pass thru to ConditionInfo
function td = filteredByAttributeStruct(td, varargin)
% call on ConditionInfo, and filter based on its valid trials
td.warnIfNoArgOut(nargout);
% no need to copy, filteredBy already copies the conditionInfo
td.conditionInfo = td.conditionInfo.filteredByAttributeStruct(varargin{:});
td = td.filterValidTrialsConditionInfo();
end
function tdCell = getTrialDataByCondition(td)
tdCell = arrayfun(@(condition) td.filteredByAttributeStruct(condition), ...
td.conditions, 'UniformOutput', false);
end
function v = get.conditions(td)
v = td.conditionInfo.conditions;
end
function v = get.conditionsAsStrings(td)
v = td.conditionInfo.conditionsAsStrings;
end
function n = get.nConditions(td)
n = td.conditionInfo.nConditions;
end
function v = get.axisValueLists(td)
v = td.conditionInfo.axisValueLists;
end
function v = get.axisValueListsAsStrings(td)
v = td.conditionInfo.axisValueListsAsStrings;
end
function v = get.conditionNames(td)
v = td.conditionInfo.names;
end
function v = get.conditionAppearances(td)
v = td.conditionInfo.appearances;
end
function td = set.conditionAppearanceFn(td, v)
td.conditionInfo.appearanceFn = v;
end
function v = get.conditionAppearanceFn(td)
v = td.conditionInfo.appearanceFn;
end
function v = get.listByCondition(td)
v = td.conditionInfo.listByCondition;
end
function v = get.conditionIdx(td)
v = td.conditionInfo.conditionIdx;
end
function v = get.conditionSubs(td)
v = td.conditionInfo.conditionSubs;
end
end
% Data access by group via ConditionInfo
methods
% given a cellvec or nmeric vector, group its elements
function varargout = groupElements(td, varargin)
varargout = cell(nargout, 1);
for i = 1:numel(varargin)
data = varargin{i};
assert(size(data,1) == td.nTrials, ...
'Data must have size nTrials along 1st dimension');
varargout{i} = cellfun(@(idx) data(idx,:), td.listByCondition, ...
'UniformOutput', false);
end
end
% given data with dimension 1 with size nTrials, group by condition
% and map out{i} = fn(group{i})
function out = mapByGroup(td, fn, varargin)
dataByGroup = td.groupElements(varargin{:});
out = cellfun(fn, dataByGroup, 'UniformOutput', false);
end
function [dCell, tCell] = getAnalogGrouped(td, name)
[dataCell, timeCell] = td.getAnalog(name);
[dCell, tCell] = td.groupElements(dataCell, timeCell);
end
function [dataCell, timeCell] = getAnalogSampleGrouped(td, name, varargin)
[dataVec, timeVec] = td.getAnalogSample(name);
[dataCell, timeCell] = td.groupElements(dataVec, timeVec);
end
function dCell = getEventGrouped(td, name)
dCell = td.groupElements(td.getAnalog(name));
end
function dCell = getParamGrouped(td, name)
dCell = td.groupElements(td.getParam(name));
end
end
% AlignInfo control
methods
function td = initializeAlignInfo(td)
td.warnIfNoArgOut(nargout);
td.alignInfo = AlignInfo();
td.alignInfo = td.alignInfo.applyToTrialData(td);
end
function td = postUpdateAlignInfo(td)
td.warnIfNoArgOut(nargout);
td = td.updateValid();
end
function td = align(td, ad)
td.warnIfNoArgOut(nargout);
if ischar(ad)
ad = AlignInfo(ad);
else
assert(isa(ad, 'AlignDescriptor'));
if ~isa(ad, 'AlignInfo')
ad = AlignInfo.fromAlignDescriptor(ad);
end
end
td.alignInfo = ad.applyToTrialData(td);
td = td.postUpdateAlignInfo();
end
function td = unalign(td)
td.warnIfNoArgOut(nargout);
td = td.align('TrialStart:TrialEnd');
end
% add a padding window to the AlignInfo
% may change which trials are valid
% usage: pad([pre post]) or pad(pre, post)
% pre > 0 means add padding before the start (typical case)
function td = pad(td, varargin)
td.warnIfNoArgOut(nargout);
td.alignInfo = td.alignInfo.pad(varargin{:});
td = td.postUpdateAlignInfo();
end
% filter trials that are valid based on AlignInfo
function td = filterValidTrialsAlignInfo(td, varargin)
td.warnIfNoArgOut(nargout);
td = td.selectTrials(td.alignInfo.computedValid);
end
% get the time window for each trial
function durations = getValidDurations(td)
durations = td.alignInfo.getValidDurationByTrial();
end
end
% Aligned data access via AlignInfo
methods
function offsets = getTimeOffsetsFromZeroEachTrial(td)
offsets = td.alignInfo.getZeroByTrial();
end
% return aligned analog channel
function [data, time] = getAnalog(td, name)
[data, time] = getAnalog@TrialData(td, name);
[data, time] = td.alignInfo.getAlignedTimeseries(data, time, false);
end
% return aligned event times
function timesCell = getEvent(td, name)
timesCell = getEvent@TrialData(td, name);
timesCell = td.alignInfo.getAlignedTimes(timesCell, false);
end
% return aligned unit spike times
function [timesCell] = getSpikeTimes(td, unitName)
timesCell = getSpikeTimes@TrialData(td, unitName);
timesCell = td.alignInfo.getAlignedTimes(timesCell, true);
end
end
% Spike data
methods
function sr = buildSpikeRaster(td, unitName)
sr = SpikeRaster(td, unitName, 'conditionInfo', td.conditionInfo, 'alignInfo', td.alignInfo);
sr.useWidestCommonValidTimeWindow = false;
end
function [rates, tvec] = getSpikeRateFilteredEachTrial(td, unitName, varargin)
p = inputParser;
p.addParamValue('tWindow', [], @isvector);
p.addParamValue('spikeFilter', SpikeFilter.getDefaultFilter(), @(x) isa(x, 'SpikeFilter'));
p.parse(varargin{:});
sf = p.Results.spikeFilter;
% Pad trial data alignment for spike filter
td = td.pad([sf.preWindow sf.postWindow]);
spikeCell = td.getSpikeTimes(unitName);
timeInfo = td.alignInfo.timeInfo;
% convert to .zero relative times since that's what spikeCell
% will be in (when called in this class)
tWindow = p.Results.tWindow;
tMinByTrial = [timeInfo.start] - [timeInfo.zero];
tMaxByTrial = [timeInfo.stop] - [timeInfo.zero];
[rates, tvec] = sf.filterSpikeTrainsWindowByTrial(spikeCell, tMinByTrial, tMaxByTrial, tWindow);
end
function [rateCell, tvec] = getSpikeRateFilteredGrouped(td, unitName, varargin)
[rateMat, tvec] = td.getSpikeRateFilteredEachTrial(unitName, varargin{:});
rateCell = td.groupElements(rateMat);
end
function [psthMatrix, tvec] = getSpikeRateFilteredMeanByGroup(td, unitName, varargin)
[rateCell, tvec] = getFilteredSpikeRateGrouped(td, unitName, varargin{:});
psthMatrix = cell2mat(cellfun(@(r) nanmean(r, 1), rateCell, 'UniformOutput', false));
end
function timesCellofCells = getSpikeTimesGrouped(td, unitName)
timesCell = td.getSpikeTimes(unitName);
timesCellofCells = td.groupElements(timesCell);
end
function countsCell = getSpikeCountsGrouped(td, unitName)
counts = td.getSpikeCounts(unitName);
countsCell = td.groupElements(counts);
end
function rateCell = getSpikeMeanRateGrouped(td, unitName)
rates = td.getSpikeMeanRate(unitName);
rateCell = td.groupElements(rates);
end
end
% Plotting
methods
function plotAnalogGroupedEachTrial(td, name, varargin)
p = inputParser();
p.addParamValue('plotOptions', {}, @(x) iscell(x));
p.addParamValue('patchline', false, @islogical); % use patchline to enable transparency
p.KeepUnmatched;
p.parse(varargin{:});
axh = td.getRequestedPlotAxis(p.Unmatched);
[dataByGroup, timeByGroup] = td.getAnalogGrouped(name);
app = td.conditionAppearances;
for iCond = 1:td.nConditions
dataCell = dataByGroup{iCond};
timeCell = timeByGroup{iCond};
for iTrial = 1:numel(dataCell)
if ~isempty(timeCell{iTrial}) && ~isempty(dataCell{iTrial})
if p.Results.patchline
patchline(double(timeCell{iTrial}), dataCell{iTrial}, ...
'Color', app(iCond).color, ...
'LineWidth', app(iCond).lineWidth, p.Results.plotOptions{:});
else
plot(axh, double(timeCell{iTrial}), dataCell{iTrial}, '-', 'Color', app(iCond).color, ...
'LineWidth', app(iCond).lineWidth, p.Results.plotOptions{:});
end
end
if iTrial == 1, hold(axh, 'on'); end
end
end
box(axh, 'off');
axis(axh, 'tight');
xlabel(td.getTimeAxisLabel());
ylabel(td.getAxisLabelForChannel(name));
end
function plotAnalogGroupedEachTrial2D(td, name1, name2, varargin)
p = inputParser();
p.addParamValue('plotOptions', {}, @(x) iscell(x));
p.addParamValue('patchline', false, @islogical); % use patchline to enable transparency
p.KeepUnmatched;
p.parse(varargin{:});
axh = td.getRequestedPlotAxis(p.Unmatched);
dataByGroup1 = td.getAnalogGrouped(name1);
dataByGroup2 = td.getAnalogGrouped(name2);
app = td.conditionAppearances;
for iCond = 1:td.nConditions
dataCell1 = dataByGroup1{iCond};
dataCell2 = dataByGroup2{iCond};
for iTrial = 1:numel(dataCell1)
if p.Results.patchline
patchline(dataCell1{iTrial}, dataCell2{iTrial}, ...
'EdgeColor', app(iCond).color, ...
'LineWidth', app(iCond).lineWidth, p.Results.plotOptions{:});
else
plot(axh, dataCell1{iTrial}, dataCell2{iTrial}, '-', 'Color', app(iCond).color, ...
'LineWidth', app(iCond).lineWidth, p.Results.plotOptions{:});
end
if iTrial == 1, hold(axh, 'on'); end
end
end
box(axh, 'off');
axis(axh, 'tight');
xlabel(td.getAxisLabelForChannel(name1));
ylabel(td.getAxisLabelForChannel(name2));
end
function plotAnalogGroupedEachTrial3D(td, name1, name2, name3, varargin)
p = inputParser();
p.addParamValue('plotOptions', {}, @(x) iscell(x));
p.KeepUnmatched;
p.parse(varargin{:});
axh = td.getRequestedPlotAxis(p.Unmatched);
dataByGroup1 = td.getAnalogGrouped(name1);
dataByGroup2 = td.getAnalogGrouped(name2);
dataByGroup3 = td.getAnalogGrouped(name3);
app = td.conditionAppearances;
for iCond = 1:td.nConditions
dataCell1 = dataByGroup1{iCond};
dataCell2 = dataByGroup2{iCond};
dataCell3 = dataByGroup3{iCond};
for iTrial = 1:numel(dataCell1)
plot3(axh, dataCell1{iTrial}, dataCell2{iTrial}, dataCell3{iTrial}, ...
'-', 'Color', app(iCond).color, ...
'LineWidth', app(iCond).lineWidth, p.Results.plotOptions{:});
if iTrial == 1, hold(axh, 'on'); end
end
end
box(axh, 'off');
axis(axh, 'tight');
axis(axh, 'vis3d');
xlabel(td.getAxisLabelForChannel(name1));
ylabel(td.getAxisLabelForChannel(name2));
zlabel(td.getAxisLabelForChannel(name3));
end
end
end