-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactor.js
More file actions
379 lines (317 loc) · 11.4 KB
/
factor.js
File metadata and controls
379 lines (317 loc) · 11.4 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
// � by Caspar Goeke and Holger Finger
/**
* This class represents an experimental factor (fixed or random).
*
* @param {ExpData} expData - The global ExpData, where all instances can be retrieved by id.
* @constructor
*/
var Factor = function (expData, factorGroup) {
var self = this;
this.expData = expData;
this.factorGroup = factorGroup;
// serialized
this.type = "Factor";
this.id = ko.observable(guid());
this.globalVar = ko.observable(null);
this.factorType = ko.observable('fixed');// either 'fixed' or 'random'
this.randomizationType = ko.observable('unbalanced');
this.balancedInFactor = ko.observable(null);
this.balancedInFactors = ko.observableArray([]);
// or maybe better: either 'allFactorialInteractions' or 'redrawRandomPerTrial' or 'balancedBetweenSubjects'
// not serialized:
this.nrLevels = ko.observable(1);
this.editName = ko.observable(false);
this.markedForDeletion = false;
};
Factor.prototype.addFactorDependency = function (factor) {
var obj = {
name: factor.globalVar().name(),
id: factor.id(),
hasDependency: ko.observable(false)
};
this.balancedInFactors.push(obj);
};
Factor.prototype.removeFactorDependency = function (index) {
this.balancedInFactors.splice(index, 1);
};
Factor.prototype.init = function (name, globVar) {
var self = this;
for (var i = 0; i < this.factorGroup.factors().length; i++) {
var fac = this.factorGroup.factors()[i];
var obj = {
name: fac.globalVar().name(),
id: fac.id(),
hasDependency: ko.observable(false)
};
this.balancedInFactors.push(obj);
}
if (globVar) {
this.globalVar(globVar);
}
else {
var globalVar = (new GlobalVar(this.expData)).initProperties('categorical', 'trial', 'nominal', name);
globalVar.isFactor(true);
globalVar.isInteracting(true);
globalVar.includeInGlobalVarList(false);
this.globalVar(globalVar);
this.expData.entities.insertIfNotExist(globalVar);
this.updateLevels();
}
this.randomizationConverter();
// add factor var to local workspace vars in all frames of this sequence
var name = this.factorGroup.name();
var found = false;
var i = 0;
var sequences = this.factorGroup.expTrialLoop.subSequencePerFactorGroup();
while (!found && i < sequences.length) {
if (sequences[i].factorGroup.name() == name) {
found = true;
}
else {
i++
}
}
if (found) {
sequences[i].addVariableToWorkspace(this.globalVar());
}
this.setVariableBackRef();
this.factorType.subscribe(function (val) {
self.factorGroup.updateCondGroups()
});
};
Factor.prototype.removeBackRef = function () {
this.globalVar().removeBackRef(this);
};
Factor.prototype.setVariableBackRef = function () {
var self = this;
if (this.globalVar() instanceof GlobalVar) {
this.globalVar().addBackRef(this, this.factorGroup, true, true, 'Factor', function (globalVar) {
// check if this factor is still beeing used:
var still_in_use = true;
if (!still_in_use) {
// TODO: remove factor from entities.
self.removeBackRef();
}
});
}
};
Factor.prototype.updateLevels = function () {
this.globalVar().levels([]);
// var nrLevel = this.globalVar().levels().length >0 ? this.globalVar().levels().length : 1;
for (var i = 0; i < this.nrLevels(); i++) {
this._addLevel();
}
};
Factor.prototype.addLevel = function () {
this._addLevel();
if (this.factorGroup) {
this.factorGroup.addLevelToCondition();
this.factorGroup.updateCondGroups();
}
if (this.factorGroup.expTrialLoop.subSequencePerFactorGroup().length > 1) {
this.factorGroup.expTrialLoop.subSequencePerFactorGroup().forEach(function (sequence) {
sequence.factorGroup.addLevelToCondition();
sequence.factorGroup.updateCondGroups();
})
}
};
Factor.prototype.removeLevel = function (lvlIdx) {
this.factorGroup.removeLevelFromFactor(this, lvlIdx);
this.globalVar().removeLevel(lvlIdx);
this.factorGroup.updateCondGroups();
// remove lvl form other task with same factor
var self = this;
var refs = this.globalVar().backRefs();
refs.forEach(function (ref) {
if (ref.refLabel == 'Factor' && ref.entity.factorGroup !== self.factorGroup) {
ref.entity.factorGroup.removeLevelFromFactor(ref.entity, lvlIdx);
ref.entity.factorGroup.updateCondGroups();
}
});
};
Factor.prototype._addLevel = function () {
this.globalVar().addLevel();
};
/**
* 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.
*/
Factor.prototype.setPointers = function (entitiesArr) {
var self = this;
var globalVar = entitiesArr.byId[this.globalVar()];
if (globalVar instanceof GlobalVar) {
this.globalVar(globalVar);
var balancedInFactor = entitiesArr.byId[this.balancedInFactor()];
if (balancedInFactor) {
this.balancedInFactor(balancedInFactor);
}
var balancedInFactors = [];
jQuery.each(this.balancedInFactors(), function (index, elem) {
if (entitiesArr.byId[elem.id] instanceof Factor) {
var obj = {
name: elem.name,
id: elem.id,
hasDependency: ko.observable(elem.hasDependency)
};
balancedInFactors.push(obj);
}
});
this.balancedInFactors(balancedInFactors);
this.randomizationConverter();
}
else { // this should not happen, variable of factor no longer exits!
this.markedForDeletion = true;
}
};
/**
* this function is automatically called after all setPointers have been executed.
*/
Factor.prototype.onFinishedLoading = function () {
if (this.markedForDeletion) {
if (this.factorGroup) {
this.factorGroup.removeFactor(this);
}
}
else {
this.setVariableBackRef();
}
};
/**
* 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.
*/
Factor.prototype.reAddEntities = function (entitiesArr) {
if (this.globalVar() instanceof GlobalVar) {
if (!entitiesArr.byId.hasOwnProperty(this.globalVar().id()))
entitiesArr.push(this.globalVar());
if (this.randomizationType() == 'balancedInFactor') {
if (this.balancedInFactor()) {
if (!entitiesArr.byId.hasOwnProperty(this.balancedInFactor().id()))
entitiesArr.push(this.balancedInFactor());
}
}
}
};
/**
* load from a json object to deserialize the states.
* @param {object} data - the json description of the states.
* @returns {Factor}
*/
Factor.prototype.fromJS = function (data) {
this.id(data.id);
this.factorType(data.factorType);
if (data.hasOwnProperty('randomizationType')) {
this.randomizationType(data.randomizationType);
}
if (data.hasOwnProperty('balancedInFactor')) {
this.balancedInFactor(data.balancedInFactor);
}
if (data.hasOwnProperty("balancedInFactors")) {
this.balancedInFactors(data.balancedInFactors);
}
this.globalVar(data.globalVar);
return this;
};
Factor.prototype.resetFactorDependencies = function () {
this.balancedInFactors([]);
if (this.factorGroup) {
for (var i = 0; i < this.factorGroup.factors().length; i++) {
var fac = this.factorGroup.factors()[i];
if (fac instanceof Factor) {
var globalV = fac.globalVar();
if (!(globalV instanceof GlobalVar)) {
globalV = this.expData.entities.byId[globalV];
}
if (globalV instanceof GlobalVar) {
var obj = {
name: globalV.name(),
id: fac.id(),
hasDependency: ko.observable(false)
};
this.balancedInFactors.push(obj);
}
}
}
}
};
Factor.prototype.randomizationConverter = function () {
if (this.balancedInFactors().length == 0) {
this.resetFactorDependencies();
}
if (this.randomizationType() == 'balancedInFactor') {
var varId = this.balancedInFactor();
for (var i = 0; i < this.balancedInFactors().length; i++) {
if (this.balancedInFactors()[i].id == varId) {
this.balancedInFactors()[i].hasDependency(true);
}
}
this.randomizationType('balancedInFactors');
this.balancedInFactor(null);
}
else if (this.randomizationType() == 'balancedConditions') {
var varIds = [];
var allFactors = this.factorGroup.factors();
for (var i = 0; i < allFactors.length; i++) {
if (allFactors[i].factorType() == 'fixed') {
varIds.push(allFactors[i].id());
}
}
for (var i = 0; i < this.balancedInFactors().length; i++) {
for (var k = 0; k < varIds.length; k++) {
var varId = varIds[k];
if (this.balancedInFactors()[i].id == varId) {
this.balancedInFactors()[i].hasDependency(true);
}
}
}
this.randomizationType('balancedInFactors');
this.balancedInFactor(null);
}
};
/**
* serialize the state of this instance into a json object, which can later be restored using the method fromJS.
* @returns {object}
*/
Factor.prototype.toJS = function () {
var balancedInFactorId = null;
if (this.randomizationType() == 'balancedInFactor') {
if (this.balancedInFactor() instanceof Factor) {
balancedInFactorId = this.balancedInFactor().id();
}
}
var balancedInFactors = [];
for (var i = 0; i < this.balancedInFactors().length; i++) {
if (this.expData.entities.byId[this.balancedInFactors()[i].id] instanceof Factor) {
var dep = null;
if (typeof this.balancedInFactors()[i].hasDependency === 'function') {
dep = this.balancedInFactors()[i].hasDependency()
}
else {
dep = this.balancedInFactors()[i].hasDependency;
}
var obj = {
name: this.balancedInFactors()[i].name,
id: this.balancedInFactors()[i].id,
hasDependency: dep
};
balancedInFactors.push(obj);
}
}
var globalVarId = null;
if (this.globalVar() instanceof GlobalVar) {
globalVarId = this.globalVar().id();
}
return {
type: this.type,
id: this.id(),
factorType: this.factorType(),
randomizationType: this.randomizationType(),
balancedInFactor: balancedInFactorId,
balancedInFactors: balancedInFactors,
globalVar: globalVarId
};
};