-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConditionDescriptor.m
3558 lines (3025 loc) · 146 KB
/
ConditionDescriptor.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef ConditionDescriptor
% ConditionDescriptor is a static representation of a A-dimensional combinatorial
% list of attribute values
% the following properties are computed dynamically on the fly as they
% are easy to compute
properties(Dependent, Transient)
nAttributes % how many attributes: ndims(values)
nValuesByAttribute % how many values per attribute: size(values)
nAxes % how many dimensions of grouping axe
nValuesAlongAxes % X x 1 array of number of elements along the ax`is
nConditions % how many total conditions
conditionsSize % size of conditions, will have trailing 1 for single axis so can be passed to e.g. ones(conditionsSize)
conditionsSizeNoExpand % size of conditions, will be length nAxes
axisValueListIsManual
allAxisValueListsManual
attributeValueListIsManual
allAttributeValueListsManual
allValueListsManual % true if all attribute lists and axis lists are manually (not automatically determined)
attributeValueListIsBinned
axisValueListIsBinned
end
% the following properties are computed dynamically on the fly as they
% are easy to compute
properties(Dependent, Transient)
attributeDisplayAs
attributeDescriptions
attributeAlongWhichAxis % A x 1 array indicating which axis an attribute contributes to (or NaN)
attributeValueModes % A x 1 array of AttributeValue* constants above
attributeActsAsFilter % A x 1 logical array : does this attribute have a
% value list or manual bin setup that would invalidate trials?
axisNames % strcell with a short name for each axis
axisDescriptions % strcell describing each axis
axisValueListModesAsStrings
axisRandomizeModesAsStrings
conditionsAsLinearInds % linear index corresponding to each condition if flattened
conditionColors % A x 3 cell of colors
% nConditions x 1 mask which indicates which conditions will be
% suppressed in the final grouping into conditions
% this is set using .setConditionIncludeMask after all grouping is
% taken care of. the only effect will be to ignore trials that fall
% into the specific conditions where conditionIncludeMask is true
conditionIncludeMask
% a short string summarizing the randomization applied to this
% conditionDescriptor
randomizationDescription
% boolean determining whether there is any randomization
hasRandomization
end
properties
description (1, 1) string = "";
% updates cache on set
% function which maps .values(i) struct --> name of condition i
% called as nameFn(conditionInfo, 'multiline', tf) where
% multiline=true means use \n to separate lines when appropriate
nameFn
% updates cache on set
appearanceFn; % function which takes struct('attrName1', attrVal1, 'attrName2', attrVal2)
% and returns struct('color', 'k', 'lineWidth', 2, ...);
logicalNotPrefix (1, 1) string = "Non-";
includeUnits = true;
end
properties(SetAccess=protected)
% modifications to appearances generated by appearanceFn,
appearanceColorByAttributesList
appearanceColorByAttributesCmap
appearanceColorByAxesList
appearanceColorByAxesCmap
appearanceLineStyleByAttributesList
appearanceLineStyleByAttributesMap
appearanceLineStyleByAxesList
appearanceLineStyleByAxesMap
appearanceLineWidthByAttributesList
appearanceLineWidthByAttributesMap
appearanceLineWidthByAxesList
appearanceLineWidthByAxesMap
% A x 1 : by attribute
attributeNames (:, 1) string = strings(0, 1); % A x 1 cell array : list of attributes for each dimension
% attributeRequestAs = {}; % A x 1 cell array : list of names by which each attribute should be requested corresponding to attributeNames
attributeDisplayAsManual = {}; % .attributeName = attribute display name for attributeName,
axisAttributes % G x 1 cell : each is cellstr of attributes utilized along that grouping axis
attributeSortByList (:, 1) string = strings(0, 1); % cellstr of attribute names (or '-attribute') specifying how to sort trials within each condition list
end
properties(SetAccess=protected, Hidden)
attributeUnits (:, 1) string = strings(0, 1); % A x 1 cellstr array of units associated with each attribute, used for generating names
attributeNumeric = []; % A x 1 logical array : is this attribute a numeric value?
attributeAsVector = []; % A x 1 logical array : is this attribute collectible as a vector
attributeValueListsManual = {}; % A x 1 cell array of permitted values (or cells of values) for this attribute
attributeValueBinsManual = {}; % A x 1 cell array of value Nbins x 2 value bins to use for numeric lists
attributeValueBinsAutoCount % A x 1 numeric array of Nbins to use when auto computing the bins, NaN if not in use
attributeValueBinsAutoModes % A x 1 numeric array of either AttributeValueBinsAutoUniform or AttributeValueBinsAutoQuantiles
attributeValueListsAsStringsManual = {}; % A x 1 array of cellstr which remap the attribute values with value lists
axisValueListsManual % G x 1 cell of cells: each contains a struct specifying an attribute specification for each element along the axis
axisValueListsOccupiedOnly % G x 1 logical indicating whether to constrain the combinatorial valueList to only occupied elements (with > 0 trials)
axisValueListsAsStringsManual (:, 1) cell = {}; % G x 1 array of cellstr which define manual settings for axisValueListsAsStrings
axisValueListsAsStringsShortManual (:, 1) cell = {}; % G x 1 array of cells which define manual settings for axisValueListsAsStringsShort
axisRandomizeModes % G x 1 numeric of constants beginning with Axis* (see below)
axisRandomizeWithReplacement % G x 1 logical indicating whether ot not to use replacement
axisRandomizeResampleFromList % G x 1 cell of cells: one for each axis
% for each axis, a cell whose length matches the number of
% values for that axis, specifying which positions to draw
% conditions from for that position
%
% e.g. if axis..List{1} contained {2 2}, we would be sampling along axis 1
% from trials with valueList{2} to fill conditions at subscript 1 and 2
%
% e.g. if axis..List{1} contained {1 2 3}, we would be doing
% the equivalent of axisResampleFromSame along axis 1
isResampledWithinConditions = false; % boolean flag indicating whether to resampleFromSame the listByCondition
% after building it, which resamples with replacement
% without changing condition labels.
randomSeed = 1;
% scalar numeric seed initializing the RandStream which will generate shuffling or resampling along each axis
% the persistence of this seed ensures that the randomization can reliably be repeated, but the results may change if anything
% about any of the attributes / axes is changed.
% see conditionIncludeMask above
conditionIncludeMaskManual
namesManual % conditionsSize cell array of condition names for manual specification
namesShortManual
end
% END OF STORED TO DISK PROPERTIES
properties(Hidden, Access=protected)
odc % handle to a ConditionDescriptorOnDemandCache
end
% THE FOLLOWING PROPERTIES WRAP EQUIVALENT PROPERTIES IN ODC
% on get: retrieve from odc, if empty {call build<Property>, store in odc, return it}
% on set: make copy of odc to alleviate dependency, store in odc
%
% Note: we use the build<Property> methods because property getters
% cannot be inherited, so subclasses can override the build method
% instead.
properties(Transient, Dependent, SetAccess=protected)
% These are generated on the fly by property get, but cached for speed, see invalidateCache to reset them
% these are X-dimensional objects where X is nAxes
conditions % X-dimensional struct where values(...idx...).attribute is the value of that attribute on that condition
conditionsAxisAttributesOnly % includes only the attributes actively selected for
conditionsAsStrings % includes attribute values as strings rather than numeric
conditionsAsStringsIncludingFilters
appearances % A-dimensional struct of appearance values
names % A-dimensional cellstr array with names of each condition
namesShort
namesMultiline % A-dimension cellstr array with names of each condition separated into multiple lines via \n
namesMultilineShort % A-dimension cellstr array with names of each condition separated into multiple lines via \n
attributeValueLists % A x 1 cell array of values allowed for this attribute
% here just computed from attributeValueListManual, but in ConditionInfo
% can be automatically computed from the data
attributeValueListsAsStrings % same as above, but everything is a string
axisValueLists % G dimensional cell array of structs which select attribute values for that position along an axis
axisValueListsAsStrings % G dimensional cell array of cellstr which give names for the values along each axis (including attribute name)
axisValueListsAsStringsShort % G dimensional cell array of cellstr which give shortened names for the values along each axis (just values)
axisValueListModes % G dimensional array of AxisValueList* constants below indicating how axis value lists are generated
end
% how are attribute values determined for a given attribute?
properties(Constant, Hidden)
% for attributeValueListModes
AttributeValueListManual = 1;
AttributeValueListAuto = 2;
AttributeValueBinsManual = 3;
AttributeValueBinsAutoUniform = 4;
AttributeValueBinsAutoQuantiles = 5;
% for axisRandomizeModes
AxisOriginal = 1; % use original axis ordering
AxisShuffled = 2; % shuffle the labels along this axis preserving the original counts within each bin
AxisResampledFromSpecified = 3; % resample with replacement from a different bin (see axisRandomizeResampleFromList)
% for axisValueListModes
AxisValueListAutoAll = 1;
AxisValueListAutoOccupied = 2;
AxisValueListManual = 3;
end
% Constructor, load, save methods
methods
function ci = ConditionDescriptor()
ci.odc = ConditionDescriptorOnDemandCache();
end
end
methods % General methods, setters and getters
function ci = notifyConditionsChanged(ci)
% when the condition tensor's shape or size changes, we need to
% reset the condition include mask.
% This does not include changes to condition include mask or the condition membership manual, which should
% just call invalidateCache
ci.warnIfNoArgOut(nargout);
ci.conditionIncludeMaskManual = [];
ci = ci.invalidateCache();
end
% flush the contents of odc as they are invalid
% call this at the end of any methods which would want to
% regenerate these values
function ci = invalidateCache(ci)
ci.warnIfNoArgOut(nargout);
if ~isempty(ci.odc)
ci.odc = ci.odc.copy();
ci.odc.flush();
end
% clear the conditionIncludeMask if it's no longer valid
% no longer should need to do this, notifyConditionsChanged
% should do this for us, but just to be sure
if ~ci.allAxisValueListsManual
ci.conditionIncludeMaskManual = [];
end
end
function ci = invalidateNames(ci)
ci.warnIfNoArgOut(nargout);
% here we precompute these things to save time,
% but each of these things also has a get method that will
% recompute this for us
if ~isempty(ci.odc)
ci.odc = ci.odc.copy();
ci.odc.flushNames();
end
end
function ci = invalidateAppearanceInfo(ci)
ci.warnIfNoArgOut(nargout);
if ~isempty(ci.odc)
ci.odc = ci.odc.copy();
ci.odc.flushAppearanceInfo();
end
end
function ci = set.nameFn(ci, fn)
ci.nameFn = fn;
ci = ci.invalidateCache();
end
function ci = set.appearanceFn(ci, fn)
ci.appearanceFn = fn;
ci = ci.clearAppearanceModifications();
% only need to flush appearance info to save time
ci = ci.invalidateAppearanceInfo();
end
function ci = freezeAppearances(ci)
% cache the current condition appearances
frozenConditions = ci.conditionsAxisAttributesOnly;
frozenAppearances = ci.appearances;
ci.appearanceFn = @frozenAppearanceLookup;
function a = frozenAppearanceLookup(ci, varargin)
a = repmat(AppearanceSpec(), ci.conditionsSize);
[cFrozen, cNew] = matchFields(frozenConditions, ci.conditions);
if numel(fieldnames(cFrozen)) == 0
warning('No attributes in common with frozen condition appearances');
return;
end
nDuplicate = 0;
nMissing = 0;
for i = 1:numel(a)
mask = arrayfun(@(frozen) isequal(cNew(i), frozen), cFrozen);
if nnz(mask) == 0
nMissing = nMissing + 1;
continue;
elseif nnz(mask) > 1
nDuplicate = nDuplicate + 1;
end
a(i) = frozenAppearances(find(mask, 1));
end
if nMissing > 0
warning('Unable to find frozen AppearanceSpec for %d conditions', nMissing);
end
if nDuplicate > 0
warning('Encountered multiple frozen AppearanceSpecs for %d conditions', nDuplicate);
end
end
function [m1, m2] = matchFields(s1, s2)
f1 = fieldnames(s1);
f2 = fieldnames(s2);
m1 = rmfield(s1, setdiff(f1, f2));
m2 = rmfield(s2, setdiff(f2, f1));
m2 = orderfields(m2, m1);
end
end
function printDescription(ci)
if isa(ci, 'ConditionInfo') && ci.applied %#ok<MCNPN>
occupiedConditionsStr = sprintf(' (%d w/ trials)', nnz(ci.countByCondition)); %#ok<MCNPN>
else
occupiedConditionsStr = '';
end
if any(~ci.conditionIncludeMask)
TrialData.cprintf('inline', '{yellow}%s: {none}%d conditions%s, {bright red}%d selected\n', ...
class(ci), ci.nConditions, occupiedConditionsStr, nnz(ci.conditionIncludeMask));
else
TrialData.cprintf('inline', '{yellow}%s: {none}%d conditions%s\n', ...
class(ci), ci.nConditions, occupiedConditionsStr);
end
TrialData.cprintf('inline', ' {bright blue}Attributes:\n');
attrDesc = ci.generateAttributeDescriptions(true);
for i = 1:ci.nAttributes
TrialData.cprintf('inline', ' %s: {white}%s\n', attrDesc{i}, ...
tcprintfEscape(TrialDataUtilities.String.strjoin(ci.attributeValueListsAsStrings{i}, ', ')));
end
axisDesc = ci.generateAxisDescriptions(true);
TrialData.cprintf('inline', ' {bright blue}Axes:\n');
for i = 1:ci.nAxes
TrialData.cprintf('inline', ' %s: {white}%s\n', axisDesc{i}, ...
tcprintfEscape(TrialDataUtilities.String.strjoin(ci.axisValueListsAsStringsShort{i}, ', ')));
end
nRandom = nnz(ci.axisRandomizeModes ~= ci.AxisOriginal);
if nRandom > 0
if nRandom == 1
s = 'axis';
else
s = 'axes';
end
TrialData.cprintf('inline', ' {bright red}%d %s with randomization applied\n', nRandom, s);
end
if ~isempty(ci.attributeSortByList)
TrialData.cprintf('inline', ' {bright blue}Sort: {purple}%s\n', TrialDataUtilities.String.strjoin(ci.attributeSortByList, ', '));
end
if ci.isResampledWithinConditions
TrialData.cprintf('inline', ' {bright red}Randomization active: {green}(seed=%g) {none}Trials resampled within conditions\n', ci.randomSeed);
end
end
function printOneLineDescription(ci)
if ci.nAxes == 0
axisStr = 'no grouping axes';
else
axisStr = TrialDataUtilities.String.strjoin(ci.axisDescriptions, ' , ');
end
attrFilter = ci.attributeNames(ci.attributeActsAsFilter);
if isempty(attrFilter)
filterStr = 'no filtering';
else
filterStr = sprintf('filtering by %s', strjoin(attrFilter));
end
TrialData.cprintf('inline', '{yellow}%s: {none}%s, %s\n', ...
class(ci), axisStr, filterStr);
end
function disp(ci)
ci.printDescription();
fprintf('\n');
builtin('disp', ci);
end
function tf = get.axisValueListIsManual(ci)
tf = ci.axisValueListModes == ci.AxisValueListManual;
end
function tf = get.allAxisValueListsManual(ci)
% returns true if all axis value
% lists are manually specified, false otherwise if anything is
% automatically determined
tf = all(ci.axisValueListIsManual);
end
function assertAllAxisValueListsManual(ci)
assert(ci.allAxisValueListsManual, 'All axis value lists must be manual mode (fixed set of values). Use .fixAllAxisValueLists or fixValueListsByApplyingToTrialData');
end
function tf = get.attributeValueListIsManual(ci)
tf = ismember(ci.attributeValueModes, [ci.AttributeValueListManual, ci.AttributeValueBinsManual]);
end
function tf = get.attributeValueListIsBinned(ci)
tf = ismember(ci.attributeValueModes, [ci.AttributeValueBinsManual, ci.AttributeValueBinsAutoUniform, ci.AttributeValueBinsAutoQuantiles]);
end
function tf = get.axisValueListIsBinned(ci)
nA = ci.nAxes;
tf = false(nA, 1);
attr_binned = ci.attributeValueListIsBinned;
for iA = 1:ci.nAxes
if numel(ci.axisAttributes{iA}) == 1
tf(iA) = attr_binned(ci.getAttributeIdx(ci.axisAttributes{iA}));
end
end
end
function tf = get.allAttributeValueListsManual(ci)
% returns true if all attribute value lists
% are manually specified, false otherwise if anything is
% automatically determined
tf = all(ci.attributeValueListIsManual);
end
function tf = get.allValueListsManual(ci)
% returns true if all attribute value lists and axis value
% lists are manually specified, false otherwise if anything is
% automatically determined
tf = ci.allAxisValueListsManual && ci.allAttributeValueListsManual;
end
end
methods % Axis related
function n = get.nAxes(ci)
n = numel(ci.axisAttributes);
end
function a = get.attributeAlongWhichAxis(ci)
a = nanvec(ci.nAttributes);
for iX = 1:ci.nAxes
a(ci.getAttributeIdx(ci.axisAttributes{iX})) = iX;
end
end
function modes = get.axisValueListModes(ci)
modes = nanvec(ci.nAxes);
for iX = 1:ci.nAxes
if ~isempty(ci.axisValueListsManual{iX})
modes(iX) = ci.AxisValueListManual;
elseif ci.axisValueListsOccupiedOnly(iX)
modes(iX) = ci.AxisValueListAutoOccupied;
else
modes(iX) = ci.AxisValueListAutoAll;
end
end
end
function counts = get.nValuesAlongAxes(ci)
counts = cellfun(@numel, ci.axisValueLists);
end
% determine whether each attribute acts to filter valid trials
function tf = get.attributeActsAsFilter(ci)
modes = ci.attributeValueModes;
tf = ismember(modes, [ci.AttributeValueListManual, ci.AttributeValueBinsManual]);
end
function names = get.axisNames(ci)
names = strings(ci.nAxes, 1);
for iX = 1:ci.nAxes
attr = string(ci.axisAttributes{iX});
names(iX) = TrialDataUtilities.String.strjoin(attr, " x ");
end
end
function desc = get.axisDescriptions(ci)
desc = ci.generateAxisDescriptions();
end
function str = get.randomizationDescription(ci)
isRand = ci.axisRandomizeModes ~= ci.AxisOriginal;
axisModeStr = cellfun(@(name, mode) sprintf("%s %s", name, mode), ...
ci.axisNames(isRand), ci.axisRandomizeModesAsStrings(isRand));
if ci.isResampledWithinConditions
axisModeStr(end+1) = "trials resampled within conditions";
end
str = TrialDataUtilities.String.strjoin(axisModeStr, ", ");
end
function desc = generateAxisDescriptions(ci, useColor)
if nargin < 2
useColor = false;
end
desc = cellvec(ci.nAxes);
vlStrCell = ci.axisValueListModesAsStrings;
randStrCell = ci.axisRandomizeModesAsStrings;
for iX = 1:ci.nAxes
attr = ci.axisAttributes{iX};
nv = ci.conditionsSize(iX);
vlStr = vlStrCell{iX};
if strcmp(vlStr, 'manual')
filterStr = ' filter';
else
filterStr = '';
end
randStr = randStrCell{iX};
if ~isempty(vlStr)
vlStr = [' ' vlStr]; %#ok<AGROW>
end
if ~isempty(randStr)
randStr = [' ' randStr]; %#ok<AGROW>
end
if useColor
desc{iX} = sprintf('{purple}%s {green}(%d%s{bright red}%s%s{green})', ...
TrialDataUtilities.String.strjoin(attr, ' x '), nv, vlStr, filterStr, randStr);
else
desc{iX} = sprintf('%s (%d%s%s)', ...
TrialDataUtilities.String.strjoin(attr, ' x '), nv, vlStr, randStr);
end
end
end
function strCell = get.axisValueListModesAsStrings(ci)
strCell = cellvec(ci.nAxes);
for iX = 1:ci.nAxes
switch ci.axisValueListModes(iX)
case ci.AxisValueListAutoAll
vlStr = 'auto';
case ci.AxisValueListAutoOccupied
vlStr = 'autoOccupied';
case ci.AxisValueListManual
vlStr = 'manual';
otherwise
error('Unknown axisValueListMode for axis %d', iX);
end
strCell{iX} = vlStr;
end
end
function strCell = get.axisRandomizeModesAsStrings(ci)
strCell = cellvec(ci.nAxes);
for iX = 1:ci.nAxes
if ci.axisRandomizeWithReplacement(iX)
replaceStr = 'WithReplacement';
else
replaceStr = '';
end
switch ci.axisRandomizeModes(iX)
case ci.AxisOriginal
randStr = '';
case ci.AxisShuffled
randStr = ['shuffled' replaceStr];
case ci.AxisResampledFromSpecified
randStr = ['resampled' replaceStr];
otherwise
error('Unknown axisRandomizeMode for axis %d', iX);
end
strCell{iX} = randStr;
end
end
function ci = addAxis(ci, varargin)
ci.warnIfNoArgOut(nargout);
p = inputParser;
p.addOptional('attributes', {}, @(x) ischar(x) || iscellstr(x) || isstring(x));
p.addParameter('name', '', @ischar);
p.addParameter('valueList', {}, @(x) true);
p.parse(varargin{:});
if ~iscell(p.Results.attributes)
attr = cellstr(p.Results.attributes);
else
attr = p.Results.attributes;
end
ci.assertHasAttribute(attr);
attr = unique(attr, 'stable');
ci = ci.removeAttributesFromAxes(attr);
valueList = p.Results.valueList;
if ~isempty(valueList) && ~isstruct(valueList)
assert(numel(attr) == 1, 'Must specify valueList as struct for multi-attribute axes');
for iV = 1:numel(valueList)
if iscell(valueList)
val = valueList{iV};
else
val = valueList(iV);
end
valueStruct(iV).(attr{1}) = val; %#ok<AGROW>
end
valueList = valueStruct;
end
% create a grouping axis
idx = ci.nAxes + 1;
ci.axisAttributes{idx} = makecol(attr);
ci.axisAttributes = makecol(ci.axisAttributes);
ci.axisValueListsManual{idx} = valueList;
ci.axisValueListsAsStringsManual{idx} = strings(0, 1);
ci.axisValueListsAsStringsShortManual{idx} = strings(0, 1);
ci.axisRandomizeModes(idx) = ci.AxisOriginal;
ci.axisRandomizeWithReplacement(idx) = false;
ci.axisRandomizeResampleFromList{idx} = [];
valueListMode = ci.getAttributeValueListMode(attr);
if all(ismember(valueListMode, [ci.AttributeValueListAuto, ci.AttributeValueBinsAutoUniform, ci.AttributeValueBinsAutoQuantiles]))
% all attributes are auto, make the axis auto too
ci.axisValueListsOccupiedOnly(idx) = true;
else
% some attributes have been manually specified, so make the
% axis default to auto all
ci.axisValueListsOccupiedOnly(idx) = false;
end
ci = ci.notifyConditionsChanged();
end
function ci = replaceAxis(ci, idx, varargin)
ci.warnIfNoArgOut(nargout);
p = inputParser;
p.addOptional('attributes', {}, @(x) ischar(x) || iscellstr(x));
p.addParameter('name', '', @ischar);
p.addParameter('valueList', {}, @(x) true);
p.parse(varargin{:});
if ~iscell(p.Results.attributes)
attr = {p.Results.attributes};
else
attr = p.Results.attributes;
end
ci.assertHasAttribute(attr);
attr = unique(attr, 'stable');
ci = ci.removeAttributesFromAxes(attr, 'ignoreAxes', idx);
valueList = p.Results.valueList;
if ~isempty(valueList) && ~isstruct(valueList)
assert(numel(attr) == 1, 'Must specify valueList as struct for multi-attribute axes');
for iV = 1:numel(valueList)
if iscell(valueList)
val = valueList{iV};
else
val = valueList(iV);
end
valueStruct(iV).(attr{1}) = val; %#ok<AGROW>
end
valueList = valueStruct;
end
% create a grouping axis
ci.axisAttributes{idx} = makecol(attr);
ci.axisValueListsManual{idx} = valueList;
ci.axisValueListsAsStringsManual{idx} = {};
ci.axisValueListsAsStringsManual{idx} = {};
ci.axisValueListsAsStringsShortManual{idx} = {};
ci.axisRandomizeModes(idx) = ci.AxisOriginal;
ci.axisRandomizeWithReplacement(idx) = false;
ci.axisRandomizeResampleFromList{idx} = [];
valueListMode = ci.getAttributeValueListMode(attr);
if all(ismember(valueListMode, [ci.AttributeValueListAuto, ci.AttributeValueBinsAutoUniform, ci.AttributeValueBinsAutoQuantiles]))
% all attributes are auto, make the axis auto too
ci.axisValueListsOccupiedOnly(idx) = true;
else
% some attributes have been manually specified, so make the
% axis default to auto all
ci.axisValueListsOccupiedOnly(idx) = false;
end
ci = ci.notifyConditionsChanged();
end
function ci = maskAxes(ci, mask)
ci.warnIfNoArgOut(nargout);
ci.axisAttributes = makecol(ci.axisAttributes(mask));
ci.axisValueListsManual = ci.axisValueListsManual(mask);
ci.axisValueListsAsStringsManual = ci.axisValueListsAsStringsShortManual(mask);
ci.axisValueListsAsStringsShortManual = ci.axisValueListsAsStringsShortManual(mask);
ci.axisRandomizeModes = ci.axisRandomizeModes(mask);
ci.axisRandomizeWithReplacement = ci.axisRandomizeWithReplacement(mask);
ci.axisRandomizeResampleFromList = ci.axisRandomizeResampleFromList(mask);
ci.axisValueListsOccupiedOnly = ci.axisValueListsOccupiedOnly(mask);
ci = ci.notifyConditionsChanged();
end
function ci = permuteAxes(ci, mask)
ci = ci.maskAxes(mask);
end
function ci = transposeAxes(ci)
ci.warnIfNoArgOut(nargout);
assert(ci.nAxes == 2, 'ConditionDescriptor must have 2 axes for transpose');
ci = ci.permuteAxes([2 1]);
end
function ci = removeAxis(ci, axisSpec)
aIdx = ci.axisLookupByAttributes(axisSpec);
mask = ~TensorUtils.vectorIndicesToMask(aIdx, ci.nAxes);
ci = ci.maskAxes(mask);
end
% wipe out existing axes and creates simple auto axes along each
function ci = groupBy(ci, varargin)
ci.warnIfNoArgOut(nargout);
ci = ci.clearAxes();
for i = 1:numel(varargin)
ci = ci.addAxis(varargin{i});
end
end
function ci = groupByAll(ci)
ci.warnIfNoArgOut(nargout);
ci = ci.groupBy(ci.attributeNames{:});
end
% remove all axes
function ci = clearAxes(ci)
ci.warnIfNoArgOut(nargout);
ci = ci.maskAxes([]);
ci = ci.notifyConditionsChanged();
end
function ci = removeAttributesFromAxes(ci, namesOrIdx, varargin)
p = inputParser();
p.addParameter('ignoreAxes', [], @(x) isvector(x)); % added for replaceAxis
p.parse(varargin{:});
ignoreAxes = TensorUtils.vectorIndicesToMask(p.Results.ignoreAxes, ci.nAxes);
ci.warnIfNoArgOut(nargout);
attrIdx = ci.getAttributeIdx(namesOrIdx);
attrNames = ci.attributeNames(attrIdx);
if ci.nAxes == 0
return;
end
whichAxis = ci.attributeAlongWhichAxis;
removeAxisMask = falsevec(ci.nAxes);
for iAI = 1:numel(attrIdx)
iA = attrIdx(iAI);
iX = whichAxis(iA);
if isnan(iX) || ignoreAxes(iX)
continue;
end
% remove this attribute from axis iX
if ci.axisRandomizeModes(iX) ~= ci.AxisOriginal
error('Cowardly refusing to remove attributes from axis with randomization applied');
end
if ci.axisValueListModes(iX) == ci.AxisValueListManual
error('Cowardly refusing to remove attributes from axis with manual value list specified');
end
maskInAxis = strcmp(ci.axisAttributes{iX}, attrNames{iAI});
if all(maskInAxis)
removeAxisMask(iX) = true;
else
ci.axisAttributes{iX} = makecol(ci.axisAttributes{iX}(~maskInAxis));
% clear out manual value list as it's likely invalid now
ci.axisValueListsManual{iX} = [];
ci.axisValueListsAsStringsManual{iX} = {};
ci.axisValueListsAsStringsShortManual{iX} = {};
% and reset the randomization
ci.axisRandomizeModes(iX) = ci.AxisOriginal;
end
end
ci = ci.maskAxes(~removeAxisMask);
end
function ci = setAxisValueList(ci, axisSpec, valueList, varargin)
p = inputParser();
p.addParameter('asStrings', {}, @iscellstr);
p.addParameter('asStringsShort', {}, @iscellstr);
p.parse(varargin{:});
ci.warnIfNoArgOut(nargout);
idx = ci.axisLookupByAttributes(axisSpec);
if numel(idx) == 1 && numel(ci.axisAttributes{idx}) == 1
if isvector(valueList) && ~isstruct(valueList) && ~iscell(valueList)
valueList = num2cell(valueList);
end
if ismatrix(valueList) && size(valueList, 2) == 2 && ci.axisValueListIsBinned(idx)
% matrix of bins, split by rows
valueList = mat2cell(valueList, ones(size(valueList, 1), 1), 2);
end
if iscell(valueList) && ~isstruct(valueList{1})
% for one axis with single attribute, valueList can simply be a cell
% array of values
%debug('Auto converting value list for single attribute axis\n');
valueCell = valueList;
if ~iscell(valueCell), valueCell = num2cell(valueCell); end
valueList = makecol(struct(ci.axisAttributes{idx}{1}, valueCell));
end
end
assert(isstruct(valueList) && ismatrix(valueList) && size(valueList, 2) <= 2, ....
'Value list must be a struct vector');
assert(isempty(setxor(fieldnames(valueList), ci.axisAttributes{idx})), ...
'Value list fields must match axis attributes');
ci.axisValueListsManual{idx} = valueList;
ci.axisValueListsAsStringsManual{idx} = p.Results.asStrings;
% if strings short not specified, use asStrings instead
if isempty(p.Results.asStringsShort) && ~isempty(p.Results.asStrings)
ci.axisValueListsAsStringsShortManual{idx} = p.Results.asStrings;
else
ci.axisValueListsAsStringsShortManual{idx} = p.Results.asStringsShort;
end
ci = ci.notifyConditionsChanged();
end
function ci = setAxisValueListDisplayAs(ci, axisSpec, strCell)
ci.warnIfNoArgOut(nargout);
idx = ci.axisLookupByAttributes(axisSpec);
assert(isscalar(idx));
assert(ci.axisValueListIsManual(idx), 'You must call setAxisValueList to make this value list manual first');
N = numel(ci.axisValueLists{idx});
assert(numel(strCell) == N);
ci.axisValueListsAsStringsManual{idx} = strCell;
if isempty(ci.axisValueListsAsStringsShortManual{idx})
% also set the axisValueListAsStringsShort if not already
% specified
ci.axisValueListsAsStringsShortManual{idx} = strCell;
end
ci = ci.invalidateNames();
end
function ci = setAxisValueListDisplayAsShort(ci, axisSpec, strCell)
ci.warnIfNoArgOut(nargout);
idx = ci.axisLookupByAttributes(axisSpec);
assert(isscalar(idx));
assert(ci.axisValueListIsManual(idx), 'You must call setAxisValueList to make this value list manual first');
N = numel(ci.axisValueLists{idx});
assert(numel(strCell) == N);
ci.axisValueListsAsStringsShortManual{idx} = strCell;
ci = ci.invalidateNames();
end
function ci = setConditionNames(ci, names, namesShort)
ci.warnIfNoArgOut(nargout);
assert(iscellstr(names) && TensorUtils.compareSizeVectors(size(names), ci.conditionsSize), 'Names must be cellstr with conditionsSize');
if nargin < 3
namesShort = names;
end
ci.namesManual = names;
ci.namesShortManual = namesShort;
ci = ci.invalidateNames();
end
function ci = fixAxisValueList(ci, axisSpec)
ci.warnIfNoArgOut(nargout);
idx = ci.axisLookupByAttributes(axisSpec);
for i = 1:numel(idx)
ci = ci.setAxisValueList(idx(i), ci.axisValueLists{idx(i)});
end
end
function ci = fixAllAxisValueLists(ci)
ci.warnIfNoArgOut(nargout);
for iA = 1:ci.nAxes
ci = ci.fixAxisValueList(iA);
end
end
function valueList = getAxisValueList(ci, axisSpec)
idx = ci.axisLookupByAttributes(axisSpec);
valueList = makecol(ci.axisValueLists{idx});
end
function ci = setAllAxisValueListsAutoAll(ci)
ci.warnIfNoArgOut(nargout);
for idx = 1:ci.nAxes
ci.axisValueListsManual(idx) = {[]};
ci.axisValueListsAsStringsManual{idx} = {};
ci.axisValueListsAsStringsShortManual{idx} = {};
ci.axisValueListsOccupiedOnly(idx) = false;
end
ci = ci.notifyConditionsChanged();
end
function ci = setAxisValueListAutoAll(ci, axisSpec)
ci.warnIfNoArgOut(nargout);
idx = ci.axisLookupByAttributes(axisSpec);
ci.axisValueListsManual{idx} = {};
ci.axisValueListsAsStringsManual{idx} = {};
ci.axisValueListsAsStringsShortManual{idx} = {};
ci.axisValueListsOccupiedOnly(idx) = false;
ci = ci.notifyConditionsChanged();
end
function ci = setAxisValueListAutoOccupied(ci, axisSpec)
ci.warnIfNoArgOut(nargout);
idx = ci.axisLookupByAttributes(axisSpec);
ci.axisValueListsManual{idx} = {};
ci.axisValueListsAsStringsManual{idx} = {};
ci.axisValueListsAsStringsShortManual{idx} = {};
ci.axisValueListsOccupiedOnly(idx) = true;
ci = ci.notifyConditionsChanged();
end
function nv = get.conditionsSize(ci)
nv = TensorUtils.expandSizeToNDims(size(ci.conditions), ci.nAxes);
end
function nv = get.conditionsSizeNoExpand(ci)
% ensure nv has the same length as ci.nAxes
if ci.nAxes <= 1
nv = size(ci.conditions, 1);
else
nv = TensorUtils.expandSizeToNDims(size(ci.conditions), ci.nAxes);
end
end
function linearInds = get.conditionsAsLinearInds(ci)
linearInds = TensorUtils.containingLinearInds(ci.conditionsSize);
end
function n = get.nConditions(ci)
n = prod(ci.conditionsSize);
end
function cmap = get.conditionColors(ci)
cmap = cat(1, ci.appearances.Color);
end
% lookup axis idx by attribute char or cellstr, or cell of attribute cellstr
% if a numeric indices are passed in, returns them through.
% if not found, throws an error
% useful for accepting either axis idx or attributes in methods
function idx = axisLookupByAttributes(ci, attr)
if isnumeric(attr)
assert(all(attr >= 1 & attr <= ci.nAxes), 'Axis index out of range');
idx = attr;
return;
end
if isstringlike(attr)
% this is a spec for a single axis, we need to wrap it in a cell,
% to support multi-axis lookup
attr = {string(attr)};
end
for iAttr = 1:numel(attr)
if ~isstring(attr{iAttr})
attr{iAttr} = string(attr{iAttr});
end
end
% attr is a cell of cellstr of attributes, and axisAttributes is a cell
% of such cellstr (the attributes along each axis).
% Consequently, we're looking for an EXACT match between attr
% and an axis
idx = nanvec(numel(attr));
for iAttr = 1:numel(attr)