-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparamReader.js
209 lines (178 loc) · 5.98 KB
/
paramReader.js
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
import Papa from "papaparse";
import { GLOSSARY, SUPER_MATCHING_PARAMS } from "./glossary.ts";
import { INTERNAL_GLOSSARY } from "./internal.ts";
export class ParamReader {
constructor(experimentFilePath = "conditions", callback) {
this._experimentFilePath = experimentFilePath;
this._experiment = null; // [{ block: 1 }, { block: 1 }, {}...]
this._blockCount = null;
this._loadFile(callback);
}
read(name, blockOrConditionName) {
if (typeof blockOrConditionName === "undefined") {
// Set here instead of in fn signature so that .ts doesn't believe blockOrConditionName has to be a number
blockOrConditionName = 1;
}
if (
typeof blockOrConditionName === "number" &&
blockOrConditionName > this.blockCount
)
throw "[READER] Invalid Block Number";
if (
!(name in GLOSSARY) &&
!this._superMatchParam(name) &&
!this._matchInternalParam(name)
)
throw `[paramReader.read] Invalid parameter name ${name}`;
if (this.has(name)) return this._getParam(name, blockOrConditionName);
else return this._getParamGlossary(name, blockOrConditionName);
}
has(name) {
if (!this.conditions) return false;
return name in this.conditions[0];
}
_getParam(name, blockOrConditionName) {
// ! String - condition name
if (typeof blockOrConditionName === "string")
if (blockOrConditionName !== "__ALL_BLOCKS__") {
for (let b of this._experiment) {
if (
b.block_condition === blockOrConditionName ||
b.conditionName === blockOrConditionName
) {
return b[name];
}
}
} else {
// ! __ALL_BLOCKS__
const returner = [];
for (let b of this._experiment) returner.push(b[name]);
return returner;
}
// ! Number - block
const returner = [];
for (let b of this._experiment)
if (Number(b.block) === blockOrConditionName) returner.push(b[name]);
return returner;
}
_validateExperiment() {
let valid = false;
for (let b of this._experiment) {
if (Number(b.block) === 1) valid = true;
}
return valid;
}
_superMatchParam(parameter) {
for (const superMatchingParameter of SUPER_MATCHING_PARAMS) {
const possibleSharedString = superMatchingParameter.replace(/@/g, "");
if (
parameter.includes(possibleSharedString) &&
superMatchingParameter.replace(possibleSharedString, "").length ===
parameter.replace(possibleSharedString, "").length
)
return true;
}
return false;
}
_matchInternalParam(parameter) {
return parameter in INTERNAL_GLOSSARY;
}
_getParamGlossary(name, blockOrConditionName) {
// String
if (typeof blockOrConditionName === "string") {
if (blockOrConditionName !== "__ALL_BLOCKS__") {
if (name in GLOSSARY) {
if (this._nameInGlossary(name))
return this.parse(GLOSSARY[name].default, GLOSSARY[name].type);
} else return undefined;
} else {
// __ALL_BLOCKS__
const returner = [];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (let _i in this._experiment) {
if (this._nameInGlossary(name))
returner.push(
this.parse(GLOSSARY[name].default, GLOSSARY[name].type),
);
}
return returner;
}
}
// Number
let counter = 0;
for (let b of this._experiment) {
if (Number(b.block) === blockOrConditionName) counter++;
}
if (this._nameInGlossary(name))
return Array(counter).fill(
this.parse(GLOSSARY[name].default, GLOSSARY[name].type),
);
else return Array(counter).fill(undefined);
}
_loadFile(callback) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
Papa.parse(`./${this._experimentFilePath}/blockCount.csv`, {
download: true,
complete: ({ data }) => {
if (!isNaN(parseInt(data[data.length - 1][0]))) {
this._blockCount = Number(data[data.length - 1][0]) + 1;
}
// Last line in blockCount is space
else this._blockCount = Number(data[data.length - 2][0]) + 1;
this._experiment = [];
for (let i = 1; i <= this._blockCount; i++) {
Papa.parse(`./${this._experimentFilePath}/block_${i}.csv`, {
download: true,
complete: ({ data }) => {
const headlines = data[0];
for (let row in data)
if (data[row].length < headlines.length) data.splice(row, 1);
for (let r = 1; r < data.length; r++) {
const thisCondition = {};
for (let c in headlines) {
thisCondition[headlines[c]] = this.parse(data[r][c]);
}
this._experiment.push(thisCondition);
}
// reached only when all blocks are loaded
if (i === this._blockCount) {
if (callback && typeof callback === "function") {
////
const _validateInterval = setInterval(() => {
if (this._validateExperiment()) {
clearInterval(_validateInterval);
callback(that);
}
}, 500);
////
}
}
},
});
}
},
});
}
_nameInGlossary(name) {
if (name in GLOSSARY) return true;
throw `[paramReader] Invalid parameter name ${name}`;
}
parse(s) {
// Translate TRUE and FALSE
// Translate number to number
if (s.toLowerCase() === "TRUE".toLowerCase()) return true;
else if (s.toLowerCase() === "FALSE".toLowerCase()) return false;
if (!isNaN(s) && s !== "") return Number(s);
return s;
}
get blockCount() {
return this._blockCount;
}
get conditions() {
return this._experiment;
}
get block_conditions() {
return this._experiment.map((o) => o.block_condition);
}
}