-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorGroup.js
More file actions
648 lines (519 loc) · 18.9 KB
/
factorGroup.js
File metadata and controls
648 lines (519 loc) · 18.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
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
// � by Caspar Goeke and Holger Finger
/**
* A factor group is a container of several factors that are interacting. Each factorGroup has a corresponding sequence
* of frames and / or pages.
*
* @param {ExpData} expData - The global ExpData, where all instances can be retrieved by id.
* @constructor
*/
var FactorGroup = function (expData, expTrialLoop) {
var self = this;
this.expData = expData;
this.expTrialLoop = expTrialLoop;
// serialized
this.name = ko.observable("trial_group");
this.factors = ko.observableArray([]);
this.conditions = ko.observableArray([]); // multidimensional array
// not serialized
// this.editName = ko.observable(false);
this.trialOffset = ko.computed(function () {
var idx = self.expTrialLoop.factorGroups().indexOf(self);
if (idx > 0) {
var l = 0;
for (var i = 0; i < idx; i++) {
var facGroup = self.expTrialLoop.factorGroups()[i];
for (var k = 0; k < facGroup.conditionsLinear().length; k++) {
l = l + facGroup.conditionsLinear()[k].trials().length;
}
}
return l
}
else {
return 0
}
}, this);
this.hasOnlyOneFixedFactor = ko.computed(function () {
var nrFixed = 0;
for (var i = 0; i < self.factors().length; i++) {
if (self.factors()[i] instanceof Factor) {
if (self.factors()[i].factorType() == "fixed") {
nrFixed++;
}
}
}
if (nrFixed == 1) {
return true
}
else {
return false
}
}, this);
this.hasRandomFactor = ko.computed(function () {
var out = false;
for (var i = 0; i < self.factors().length; i++) {
if (self.factors()[i] instanceof Factor) {
if (self.factors()[i].factorType() == "random") {
out = true;
}
}
}
return out;
}, this);
this.conditionsLinear = ko.computed(function () {
var linearArray = [];
var conditions = self.conditions();
var trialStartIdx = 1;
function deepDive(arr, numNewLevels) {
if (arr[0] && arr[0].constructor === Array) {
// recursive call:
for (var t = 0; t < arr.length; t++) {
deepDive(arr[t], numNewLevels);
}
}
else {
// create new array of new interacting trialTypes with all combinations:
for (var t = 0; t < arr.length; t++) {
var existingCond = arr[t];
existingCond.trialStartIdx(trialStartIdx);
trialStartIdx += existingCond.trials().length;
existingCond.conditionIdx(linearArray.length + 1);
linearArray.push(existingCond);
}
}
}
// one more dimension of interacting trialTypes with all combinations:
if (conditions.length > 0) {
deepDive(conditions, conditions.length);
}
return linearArray;
}, this);
this.updateCondGroups();
this.conditionsLinear.subscribe(function (newVal) {
self.updateCondGroups();
});
};
FactorGroup.prototype.getFixedFactorConditions = function () {
var conditions = this.conditionsLinear();
var factors = this.factors();
// get factor type values (random or fixed)
var type = [];
for (var i = 0; i < factors.length; i++) {
if (factors[i].factorType) {
type.push(factors[i].factorType());
}
}
// get factor levels for each condition, remove factors levels of random factors
var factorLevels = [];
for (var i = 0; i < conditions.length; i++) {
factorLevels.push([]);
var levels = conditions[i].factorLevels();
for (var k = 0; k < levels.length; k++) {
if (type[k] == "fixed") {
if (levels[k]) {
factorLevels[i].push(levels[k].name());
}
else {
var catchi = 1;
}
}
}
}
// join factor levels. Conditions which only differ in random factors now have the identical string
var factorLevelsCombined = [];
for (var i = 0; i < factorLevels.length; i++) {
factorLevelsCombined.push(factorLevels[i].join())
}
// find matching strings and save their indicies in output array
var fixedFactorCondGroups = [];
var count = -1;
for (var i = 0; i < factorLevelsCombined.length; i++) {
var match = factorLevelsCombined[i];
if (match != '*merelyACopy*') {
count++;
fixedFactorCondGroups.push([]);
for (var k = i; k < factorLevelsCombined.length; k++) {
if (factorLevelsCombined[k] == match) {
fixedFactorCondGroups[count].push(k);
factorLevelsCombined[k] = "*merelyACopy*";
}
}
}
}
return fixedFactorCondGroups;
};
FactorGroup.prototype.updateCondGroups = function () {
var condGroups = this.getFixedFactorConditions();
this.conditionsLinear().forEach(function (condition) {
condition.setCondGroup(condGroups);
})
};
FactorGroup.prototype.getFixedFactorLevels = function () {
var conditions = this.conditionsLinear();
var factors = this.factors();
var factorNames = [];
// get factor type values (random or fixed)
var type = [];
for (var i = 0; i < factors.length; i++) {
type.push(factors[i].factorType());
if (factors[i].factorType() == "fixed") {
factorNames.push(i)
}
}
// get factor levels for each condition, remove factors levels of random factors
var factorLevels = [];
for (var i = 0; i < conditions.length; i++) {
factorLevels.push([]);
var levels = conditions[i].factorLevels();
for (var k = 0; k < levels.length; k++) {
if (type[k] == "fixed") {
if (levels[k]) {
factorLevels[i].push(levels[k].name());
}
else {
var catchi = 1;
}
}
}
}
return [factorLevels, factorNames];
};
/**
* adds a new factor to the tree.
* @param {Factor} factor - the new factor
*/
FactorGroup.prototype.addFactorToCondition = function (factor) {
var factors = this.factors();
var levels = factor.globalVar().levels();
var conditionArray = this.conditions();
// for first factor only
if (factors.length > 1) {
var self = this;
function deepClone(arr) {
if (arr[0].constructor === Array) {
// recursive call:
for (var t = 0; t < arr.length; t++) {
deepClone(arr[t]);
}
}
else {
// create new array of new interacting trialTypes with all combinations:
for (var t = 0; t < arr.length; t++) {
var existingCond = arr[t];
// add all levels of this new interacting factor:
var nrLevels = levels.length;
var conditions = [];
var tmpObj = existingCond.toJS();
for (var l = 0; l < nrLevels; l++) {
// deepCopy
var condi = new Condition(self);
condi.fromJS(tmpObj);
condi.factorLevels(existingCond.factorLevels().slice());
condi.factorLevels.push(levels[l]);
conditions.push(condi);
}
arr[t] = conditions;
}
}
}
// one more dimension of interacting trialTypes with all combinations:
deepClone(conditionArray, levels.length);
}
else {
for (var l = 0; l < levels.length; l++) {
var condi = new Condition(this);
condi.initNewInstance();
// condi.factorLevelNames.push(condi.levels()[l].name());
condi.factorLevels.push(levels[l]);
conditionArray.push(condi);
}
}
this.conditions(conditionArray);
};
/**
* updates the multi dimensional array with all new levels.
*/
FactorGroup.prototype.addLevelToCondition = function () {
var self = this;
function deepCopyOfSubArrays(arrOrCondition, factorToModify, newLevel) {
var clonedArrOrCondition = null;
if (arrOrCondition.constructor === Array) {
clonedArrOrCondition = [];
// recursive call:
for (var t = 0; t < arrOrCondition.length; t++) {
clonedArrOrCondition.push(deepCopyOfSubArrays(arrOrCondition[t], factorToModify, newLevel));
}
}
else if (arrOrCondition.constructor === Condition) {
// create new array of new interacting trialTypes with all combinations:
clonedArrOrCondition = new Condition(self);
clonedArrOrCondition.fromJS(arrOrCondition.toJS());
clonedArrOrCondition.factorLevels(arrOrCondition.factorLevels().slice());
// set new level of cloned condition:
var indexOfModifiedFactor = clonedArrOrCondition.factorGroup.factors().indexOf(factorToModify);
clonedArrOrCondition.factorLevels()[indexOfModifiedFactor] = newLevel;
}
else {
console.log("error");
}
return clonedArrOrCondition;
}
function addLevels(subarr, factorVars) {
// go into all existing levels of this factor (factorVars[0]) and make sure that all sub-factors (factorVars[1...]) have their levels:
if (factorVars.length > 1) {
for (var i = 0; i < subarr.length; i++) {
addLevels(subarr[i], factorVars.slice(1)); // recursively add levels within this level
}
}
// make sure that this factor (factorVars[0]) has all it's levels:
var desired_len = factorVars[0].globalVar().levels().length;
for (var i = subarr.length; i < desired_len; i++) {
subarr[i] = deepCopyOfSubArrays(subarr[subarr.length - 1], factorVars[0], factorVars[0].globalVar().levels()[i]); // adding new level with all sub-levels here
}
}
var factors = this.factors();
var conditionArray = this.conditions();
addLevels(conditionArray, factors);
this.conditions(conditionArray);
};
FactorGroup.prototype.getSequence = function () {
// returns the sequence of this factor group
var sequences = this.expTrialLoop.subSequencePerFactorGroup();
var i = 0;
var found = false;
var out = null;
while (!found && i < sequences.length) {
if (this === sequences[i].factorGroup) {
found = true;
out = sequences[i];
}
else {
i++;
}
}
return out;
};
/**
* updates the multi dimensional array with all new levels.
*/
FactorGroup.prototype.removeLevelFromFactor = function (factor, lvlIdx) {
var factors = this.factors();
var facIdx = factors.indexOf(factor);
// update modifiers:
var factorGroupIdx = this.expTrialLoop.factorGroups().indexOf(this);
var subSequence = this.expTrialLoop.subSequencePerFactorGroup()[factorGroupIdx];
var allModifiers = [];
if (subSequence) {
subSequence.getAllModifiers(allModifiers);
for (var i = 0; i < allModifiers.length; i++) {
allModifiers[i].removeLevelFromFactor(factor, lvlIdx);
}
}
else {
console.log("error: sequence of factor group not found.")
}
// Update conditions table:
function removeLevels(lvlIdx, requiredDepth, currentDepth, arr) {
if (currentDepth == requiredDepth) {
arr.splice(lvlIdx, 1);
}
else {
for (var t = 0; t < arr.length; t++) {
removeLevels(lvlIdx, requiredDepth, currentDepth + 1, arr[t]);
}
}
}
var conditions = this.conditions();
if (factors.length > 1) {
removeLevels(lvlIdx, facIdx, 0, conditions);
}
else {
conditions.splice(lvlIdx, 1);
}
this.conditions(conditions);
// Now really remove the level from the categorical variable:
// factors[facIdx].globalVar().removeLevel(lvlIdx);
};
/**
* add a new factor to the tree.
* @param {Factor} factor - the new factor
*/
FactorGroup.prototype.addFactor = function (factor) {
this.expData.entities.insertIfNotExist(factor.globalVar());
this.expData.entities.insertIfNotExist(factor);
this.factors.push(factor);
this.addFactorToCondition(factor);
for (var i = 0; i < this.factors().length; i++) {
this.factors()[i].addFactorDependency(factor);
}
//not serialized
this.updateCondGroups();
};
/**
* remove the factor from the tree and remove the corresponding conditions.
* @param {Factor} factor - the factor to remove
*/
FactorGroup.prototype.removeFactor = function (factor) {
var idx = this.factors().indexOf(factor);
if (factor.globalVar() && factor.globalVar() instanceof GlobalVar) {
factor.globalVar().removeBackRef(factor);
}
function deepRemove(arrMultiDim, subIndex, depth) {
var t;
if (depth == idx) {
arrMultiDim[subIndex] = arrMultiDim[subIndex][0];
}
else {
for (t = 0; t < arrMultiDim[subIndex].length; t++) {
deepRemove(arrMultiDim[subIndex], t, depth + 1);
}
}
}
// update modifiers:
var factorGroupIdx = this.expTrialLoop.factorGroups().indexOf(this);
var subSequence = this.expTrialLoop.subSequencePerFactorGroup()[factorGroupIdx];
var allModifiers = [];
subSequence.getAllModifiers(allModifiers);
for (var i = 0; i < allModifiers.length; i++) {
allModifiers[i].removeFactorDependency(factor);
}
// update conditions array:
var arrMultiDim = this.conditions();
if (idx == 0) {
arrMultiDim = arrMultiDim[0];
}
else {
for (var t = 0; t < arrMultiDim.length; t++) {
deepRemove(arrMultiDim, t, 1);
}
}
this.conditions(arrMultiDim);
jQuery.each(this.conditionsLinear(), function (index, cond) {
cond.factorLevels.splice(idx, 1);
});
for (var i = 0; i < this.factors().length; i++) {
this.factors()[i].removeFactorDependency(idx);
}
this.factors.splice(idx, 1);
this.updateCondGroups();
};
/**
* This function initializes all internal state variables to point to other instances in the same experiment. Usually
* this is called after ALL experiment instances were deserialized using fromJS(). In this function use
* 'entitiesArr.byId[id]' to retrieve an instance from the global list given some unique id.
*
* @param {ko.observableArray} entitiesArr - this is the knockout array that holds all instances.
*/
FactorGroup.prototype.setPointers = function (entitiesArr) {
var self = this;
this.factors(jQuery.map(this.factors(), function (factorId) {
var factor = entitiesArr.byId[factorId];
factor.factorGroup = self;
return factor;
}));
jQuery.each(this.conditionsLinear(), function (index, elem) {
elem.setPointers(entitiesArr);
});
};
FactorGroup.prototype.onFinishedLoading = function () {
var self = this;
var factors = this.factors();
var levels = [];
for (var t = 0; t < factors.length; t++) {
levels.push(null);
}
var condMultiDim = this.conditions();
function deepDive(condMultiDim, factors, levels, depth) {
var t;
if (condMultiDim.constructor === Array) {
// recursive call:
for (t = 0; t < condMultiDim.length; t++) {
var globalVar = factors[depth].globalVar();
if (!(globalVar instanceof GlobalVar)) {
globalVar = self.expData.entities.byId[globalVar];
}
levels[depth] = globalVar.levels()[t];
// HACK to fix experiments with diverging global var level structure vs. condition structure.
if (globalVar.levels()[t] === undefined) {
console.log("error: level not in globalVar but it should be, creating new Level to avoid corrupted file structure...");
levels[depth] = globalVar.addLevel();
}
deepDive(condMultiDim[t], factors, levels, depth + 1);
}
}
else {
// set level links:
condMultiDim.factorLevels(levels.slice(0));
}
}
deepDive(condMultiDim, factors, levels, 0);
this.conditions(condMultiDim);
};
/**
* Recursively adds all child objects that have a unique id to the global list of entities.
*
* @param {ko.observableArray} entitiesArr - this is the knockout array that holds all instances.
*/
FactorGroup.prototype.reAddEntities = function (entitiesArr) {
// add the direct child nodes:
jQuery.each(this.factors(), function (index, factor) {
if (!entitiesArr.byId.hasOwnProperty(factor.id())) {
entitiesArr.push(factor);
}
// recursively make sure that all deep tree nodes are in the entities list:
if (factor.reAddEntities)
factor.reAddEntities(entitiesArr);
});
};
/**
* load from a json object to deserialize the states.
* @param {object} data - the json description of the states.
* @returns {FactorGroup}
*/
FactorGroup.prototype.fromJS = function (data) {
var self = this;
this.name(data.name);
this.factors(data.factors);
function deepLoadConditions(condMultiDimData) {
if (condMultiDimData.constructor === Array) {
// recursive call:
var condObj = [];
for (var t = 0; t < condMultiDimData.length; t++) {
condObj.push(deepLoadConditions(condMultiDimData[t]));
}
return condObj;
}
else {
var cond = new Condition(self);
cond.fromJS(condMultiDimData);
return cond;
}
}
this.conditions(deepLoadConditions(data.conditions));
return this;
};
/**
* serialize the state of this instance into a json object, which can later be restored using the method fromJS.
* @returns {object}
*/
FactorGroup.prototype.toJS = function () {
function deepSaveConditions(condMultiDim) {
if (condMultiDim.constructor === Array) {
// recursive call:
var savedJson = [];
for (var t = 0; t < condMultiDim.length; t++) {
savedJson.push(deepSaveConditions(condMultiDim[t]));
}
return savedJson;
}
else {
return condMultiDim.toJS();
}
}
return {
name: this.name(),
factors: jQuery.map(this.factors(), function (factor) {
return factor.id();
}),
conditions: deepSaveConditions(this.conditions())
};
};