compileprofile-spacetests 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +115 -0
- package/README.md +147 -34
- package/dist/cjs/common-interfaces.d.ts +60 -0
- package/dist/cjs/common-interfaces.js +2 -0
- package/dist/cjs/emotions.d.ts +4 -9
- package/dist/cjs/emotions.js +56 -22
- package/dist/cjs/gonogo.d.ts +4 -9
- package/dist/cjs/gonogo.js +61 -14
- package/dist/cjs/index.d.ts +6 -5
- package/dist/cjs/index.js +11 -1
- package/dist/cjs/nback.d.ts +4 -14
- package/dist/cjs/nback.js +27 -13
- package/dist/cjs/spatial-cognition.d.ts +4 -9
- package/dist/cjs/spatial-cognition.js +69 -14
- package/dist/esm/common-interfaces.d.ts +60 -0
- package/dist/esm/common-interfaces.js +1 -0
- package/dist/esm/emotions.d.ts +4 -9
- package/dist/esm/emotions.js +55 -21
- package/dist/esm/gonogo.d.ts +4 -9
- package/dist/esm/gonogo.js +61 -14
- package/dist/esm/index.d.ts +6 -5
- package/dist/esm/index.js +9 -1
- package/dist/esm/nback.d.ts +4 -14
- package/dist/esm/nback.js +27 -13
- package/dist/esm/spatial-cognition.d.ts +4 -9
- package/dist/esm/spatial-cognition.js +69 -14
- package/example-data/emotions.json +807 -807
- package/example-data/gonogo.json +5392 -5392
- package/example-data/nback.json +892 -892
- package/example-data/participant.json +6 -6
- package/example-data/spatial-cognition.json +462 -462
- package/jest.config.js +13 -13
- package/original/emotion-recognition.R +39 -39
- package/original/gonogo.R +32 -32
- package/package.json +40 -40
- package/src/__tests__/integration/browserInfo.test.ts +16 -16
- package/src/__tests__/integration/emotions.test.ts +172 -64
- package/src/__tests__/integration/gonogo.test.ts +183 -80
- package/src/__tests__/integration/nback.test.ts +148 -55
- package/src/__tests__/integration/spatial-cognition.test.ts +171 -66
- package/src/__tests__/test-utils/testData.ts +79 -79
- package/src/common-interfaces.ts +79 -0
- package/src/emotions.ts +98 -59
- package/src/getters.ts +21 -21
- package/src/gonogo.ts +127 -76
- package/src/index.ts +39 -12
- package/src/nback.ts +61 -53
- package/src/spatial-cognition.ts +113 -55
- package/tsconfig.cjs.json +12 -12
- package/tsconfig.esm.json +14 -14
package/src/getters.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
function getBrowserInformation(data: any[]): any {
|
|
2
|
-
const browserInfo = data.find(row => row.trial_type === "browser-check");
|
|
3
|
-
if (!browserInfo) {
|
|
4
|
-
return null;
|
|
5
|
-
}
|
|
6
|
-
return {
|
|
7
|
-
os: browserInfo.os,
|
|
8
|
-
mobile: browserInfo.mobile,
|
|
9
|
-
fullscreen: browserInfo.fullscreen,
|
|
10
|
-
vsync_rate: browserInfo.vsync_rate,
|
|
11
|
-
webcam: browserInfo.webcam,
|
|
12
|
-
microphone: browserInfo.microphone,
|
|
13
|
-
browser: browserInfo.browser,
|
|
14
|
-
browser_version: browserInfo.browser_version,
|
|
15
|
-
webaudio: browserInfo.webaudio,
|
|
16
|
-
width: browserInfo.width,
|
|
17
|
-
height: browserInfo.height
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export { getBrowserInformation };
|
|
1
|
+
function getBrowserInformation(data: any[]): any {
|
|
2
|
+
const browserInfo = data.find(row => row.trial_type === "browser-check");
|
|
3
|
+
if (!browserInfo) {
|
|
4
|
+
return null;
|
|
5
|
+
}
|
|
6
|
+
return {
|
|
7
|
+
os: browserInfo.os,
|
|
8
|
+
mobile: browserInfo.mobile,
|
|
9
|
+
fullscreen: browserInfo.fullscreen,
|
|
10
|
+
vsync_rate: browserInfo.vsync_rate,
|
|
11
|
+
webcam: browserInfo.webcam,
|
|
12
|
+
microphone: browserInfo.microphone,
|
|
13
|
+
browser: browserInfo.browser,
|
|
14
|
+
browser_version: browserInfo.browser_version,
|
|
15
|
+
webaudio: browserInfo.webaudio,
|
|
16
|
+
width: browserInfo.width,
|
|
17
|
+
height: browserInfo.height
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { getBrowserInformation };
|
package/src/gonogo.ts
CHANGED
|
@@ -1,76 +1,127 @@
|
|
|
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
|
-
case
|
|
36
|
-
o.should_go = o.stimulus == "
|
|
37
|
-
break;
|
|
38
|
-
case
|
|
39
|
-
o.should_go = o.stimulus == "
|
|
40
|
-
break;
|
|
41
|
-
|
|
42
|
-
o.should_go =
|
|
43
|
-
break;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
1
|
+
import { GonogoResultsStructure, CommonResults, GonogoSpecialResults } from './common-interfaces';
|
|
2
|
+
const BEST_SCORE = 300;
|
|
3
|
+
const WORST_SCORE = 5000;
|
|
4
|
+
|
|
5
|
+
const gonogo_calculate_results = (data: any[]): GonogoResultsStructure => {
|
|
6
|
+
const trials = gonogo_extract_trials(data);
|
|
7
|
+
const results = gonogo_results(trials);
|
|
8
|
+
return results;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const gonogo_extract_trials = (obj: any[]): any[] => {
|
|
12
|
+
// GONOGO_IMAGE_PATH <- "https://unyavwofnmwihnpvkjji.supabase.co/storage/v1/object/public/test-stimuli/gonogo/"
|
|
13
|
+
let trials = obj.filter((o: any) => 'image' in o && o.image !== undefined);
|
|
14
|
+
trials.forEach((o: any) => {
|
|
15
|
+
// image is in format <stimulus>_trans_<number>.png
|
|
16
|
+
// "image": "https://unyavwofnmwihnpvkjji.supabase.co/storage/v1/object/public/test-stimuli/gonogo/aliens/astronaut_trans_1.png",
|
|
17
|
+
const match = o.image.match(/([^/]+)_(\d+)\.png$/);
|
|
18
|
+
if (match) {
|
|
19
|
+
o.stimulus = match[0];
|
|
20
|
+
o.stimulustype = match[1];
|
|
21
|
+
o.image_number = match[2];
|
|
22
|
+
} else {
|
|
23
|
+
console.warn('Image format did not match expected pattern:', o.image);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
o.should_go = false;
|
|
27
|
+
o.did_go = o.response !== null;
|
|
28
|
+
switch (o.block_number) {
|
|
29
|
+
case 1:
|
|
30
|
+
o.should_go = o.stimulus == "astronaut_trans_1.png";
|
|
31
|
+
break;
|
|
32
|
+
case 2:
|
|
33
|
+
o.should_go = o.stimulus == "alien_trans_1.png";
|
|
34
|
+
break;
|
|
35
|
+
case 3:
|
|
36
|
+
o.should_go = o.stimulus == "rocket_trans_1.png";
|
|
37
|
+
break;
|
|
38
|
+
case 4:
|
|
39
|
+
o.should_go = !(o.stimulus == "rocket_trans_1.png");
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
o.should_go = false;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
o.correct = o.should_go == o.did_go;
|
|
46
|
+
})
|
|
47
|
+
return trials;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// TODO this
|
|
51
|
+
const calculate_performance_score = (avg_rt: number, avg_acc: number): number => {
|
|
52
|
+
let performance = avg_rt / (Math.pow(avg_acc, 5) + 0.001);
|
|
53
|
+
performance = Math.min(Math.max(performance, BEST_SCORE), WORST_SCORE);
|
|
54
|
+
const performance_score = (1 - ((performance - BEST_SCORE) / (WORST_SCORE - BEST_SCORE))) * 100;
|
|
55
|
+
return performance_score;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const gonogo_results = (data: any[]): GonogoResultsStructure => {
|
|
59
|
+
// throw error if obj is empty
|
|
60
|
+
if (data.length === 0) {
|
|
61
|
+
throw new Error("No trials found");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const correct_trials = data.filter((o: any) => o.correct === true);
|
|
65
|
+
const incorrect_trials = data.filter((o: any) => o.correct === false);
|
|
66
|
+
const missing_trials = []; // technically not possible in GNG, as no response is just incorrect
|
|
67
|
+
|
|
68
|
+
const go_trials = data.filter((o: any) => o.should_go === true);
|
|
69
|
+
const nogo_trials = data.filter((o: any) => o.should_go === false);
|
|
70
|
+
|
|
71
|
+
const total_rt = data.reduce((a, b) => a + (b.rt || 0), 0);
|
|
72
|
+
const correct_rt = correct_trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
73
|
+
const incorrect_rt = incorrect_trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
74
|
+
|
|
75
|
+
const responded_trials = data.filter((o: any) => o.response !== null);
|
|
76
|
+
const correct_responded_trials = responded_trials.filter((o: any) => o.correct === true);
|
|
77
|
+
const incorrect_responded_trials = responded_trials.filter((o: any) => o.correct === false);
|
|
78
|
+
|
|
79
|
+
const avg_rt = responded_trials.length > 0 ? total_rt / responded_trials.length : 0;
|
|
80
|
+
const avg_acc = correct_trials.length / data.length;
|
|
81
|
+
|
|
82
|
+
// Calculate performance score using the existing formula
|
|
83
|
+
const performance_score = calculate_performance_score(avg_rt, avg_acc);
|
|
84
|
+
|
|
85
|
+
const common: CommonResults = {
|
|
86
|
+
n_trials: data.length,
|
|
87
|
+
n_correct_trials: correct_trials.length,
|
|
88
|
+
n_incorrect_trials: incorrect_trials.length,
|
|
89
|
+
avg_rt_total: avg_rt,
|
|
90
|
+
avg_rt_correct: correct_trials.length > 0 ? correct_rt / correct_responded_trials.length : 0,
|
|
91
|
+
avg_rt_incorrect: incorrect_trials.length > 0 ? incorrect_rt / incorrect_responded_trials.length : 0,
|
|
92
|
+
performance_score
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Group by block for special results
|
|
96
|
+
const by_block = Object.values(data.reduce((acc: any, trial: any) => {
|
|
97
|
+
const key = trial.block_number || 'unknown';
|
|
98
|
+
if (!acc[key]) acc[key] = { block: key, trials: [], avg_rt: 0, avg_acc: 0 };
|
|
99
|
+
acc[key].trials.push(trial);
|
|
100
|
+
return acc;
|
|
101
|
+
}, {})).map((group: any) => {
|
|
102
|
+
group.avg_rt = group.trials.reduce((a: number, b: any) => a + (b.rt || 0), 0) / group.trials.length;
|
|
103
|
+
group.avg_acc = group.trials.reduce((a: number, b: any) => a + (b.correct ? 1 : 0), 0) / group.trials.length;
|
|
104
|
+
return group;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const special: GonogoSpecialResults = {
|
|
108
|
+
by_block,
|
|
109
|
+
go_trials: {
|
|
110
|
+
n: go_trials.length,
|
|
111
|
+
avg_rt: go_trials.length > 0 ? go_trials.reduce((a, b) => a + (b.rt || 0), 0) / go_trials.length : 0,
|
|
112
|
+
avg_acc: go_trials.length > 0 ? go_trials.reduce((a, b) => a + (b.correct ? 1 : 0), 0) / go_trials.length : 0,
|
|
113
|
+
},
|
|
114
|
+
nogo_trials: {
|
|
115
|
+
n: nogo_trials.length,
|
|
116
|
+
avg_rt: nogo_trials.length > 0 ? nogo_trials.reduce((a, b) => a + (b.rt || 0), 0) / nogo_trials.length : 0,
|
|
117
|
+
avg_acc: nogo_trials.length > 0 ? nogo_trials.reduce((a, b) => a + (b.correct ? 1 : 0), 0) / nogo_trials.length : 0,
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
common,
|
|
123
|
+
special
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { GonogoResultsStructure, gonogo_calculate_results, gonogo_extract_trials, gonogo_results };
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { spatial_calculate_results
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { nback_calculate_results } from './nback';
|
|
2
|
+
import { spatial_calculate_results } from './spatial-cognition';
|
|
3
|
+
import { gonogo_calculate_results } from './gonogo';
|
|
4
|
+
import { emotion_calculate_results } from './emotions';
|
|
5
|
+
import {
|
|
6
|
+
NbackResultsStructure,
|
|
7
|
+
SpatialResultsStructure,
|
|
8
|
+
GonogoResultsStructure,
|
|
9
|
+
EmotionResultsStructure,
|
|
10
|
+
CommonResults
|
|
11
|
+
} from './common-interfaces';
|
|
12
|
+
|
|
13
|
+
const NbackResults = (obj: any[]): NbackResultsStructure => {
|
|
14
|
+
return nback_calculate_results(obj);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const SpatialCognitionResults = (obj: any[]): SpatialResultsStructure => {
|
|
18
|
+
return spatial_calculate_results(obj);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const GonogoResults = (obj: any[]): GonogoResultsStructure => {
|
|
22
|
+
return gonogo_calculate_results(obj);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const EmotionResults = (obj: any[]): EmotionResultsStructure => {
|
|
26
|
+
return emotion_calculate_results(obj);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
NbackResults,
|
|
31
|
+
SpatialCognitionResults,
|
|
32
|
+
EmotionResults,
|
|
33
|
+
GonogoResults,
|
|
34
|
+
NbackResultsStructure,
|
|
35
|
+
SpatialResultsStructure,
|
|
36
|
+
GonogoResultsStructure,
|
|
37
|
+
EmotionResultsStructure,
|
|
38
|
+
CommonResults
|
|
39
|
+
};
|
package/src/nback.ts
CHANGED
|
@@ -1,53 +1,61 @@
|
|
|
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
|
-
const
|
|
26
|
-
const
|
|
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
|
-
|
|
1
|
+
import { NbackResultsStructure, CommonResults, NbackSpecialResults } from './common-interfaces';
|
|
2
|
+
|
|
3
|
+
// from the passed json extract those objects that have field is_target
|
|
4
|
+
const nback_calculate_results = (obj: any[]): NbackResultsStructure => {
|
|
5
|
+
const trials = nback_extract_trials(obj);
|
|
6
|
+
return nback_results(trials);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const nback_extract_trials = (obj: any[]): any[] => {
|
|
10
|
+
const trials = obj.filter((o: any) => 'is_target' in o);
|
|
11
|
+
return trials;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const nback_results = (trials: any[]): NbackResultsStructure => {
|
|
15
|
+
const target_trials = trials.filter((o: any) => o.is_target);
|
|
16
|
+
const non_target_trials = trials.filter((o: any) => !o.is_target);
|
|
17
|
+
const correct_trials = trials.filter((o: any) => o.correct === true);
|
|
18
|
+
const incorrect_trials = trials.filter((o: any) => o.correct === false);
|
|
19
|
+
|
|
20
|
+
const total_rt = trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
21
|
+
const correct_rt = correct_trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
22
|
+
const incorrect_rt = incorrect_trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
23
|
+
|
|
24
|
+
const responded_trials = trials.filter((o: any) => o.response !== null);
|
|
25
|
+
const correct_responded_trials = responded_trials.filter((o: any) => o.correct === true);
|
|
26
|
+
const incorrect_responded_trials = responded_trials.filter((o: any) => o.correct === false);
|
|
27
|
+
|
|
28
|
+
const common: CommonResults = {
|
|
29
|
+
n_trials: trials.length,
|
|
30
|
+
n_correct_trials: correct_trials.length,
|
|
31
|
+
n_incorrect_trials: incorrect_trials.length,
|
|
32
|
+
avg_rt_total: trials.length > 0 ? total_rt / responded_trials.length : 0,
|
|
33
|
+
avg_rt_correct: correct_trials.length > 0 ? correct_rt / correct_responded_trials.length : 0,
|
|
34
|
+
avg_rt_incorrect: incorrect_trials.length > 0 ? incorrect_rt / incorrect_responded_trials.length : 0,
|
|
35
|
+
performance_score: trials.length > 0 ? correct_trials.length * 100 / trials.length : 0
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const special: NbackSpecialResults = {
|
|
39
|
+
target_trials: {
|
|
40
|
+
n: target_trials.length,
|
|
41
|
+
avg_rt: target_trials.length > 0 ? target_trials.reduce((a, b) => a + (b.rt || 0), 0) / target_trials.length : 0,
|
|
42
|
+
avg_acc: target_trials.length > 0 ? target_trials.reduce((a, b) => a + (b.correct ? 1 : 0), 0) * 100 / target_trials.length : 0,
|
|
43
|
+
},
|
|
44
|
+
non_target_trials: {
|
|
45
|
+
n: non_target_trials.length,
|
|
46
|
+
avg_rt: non_target_trials.length > 0 ? non_target_trials.reduce((a, b) => a + (b.rt || 0), 0) / non_target_trials.length : 0,
|
|
47
|
+
avg_acc: non_target_trials.length > 0 ? non_target_trials.reduce((a, b) => a + (b.correct ? 1 : 0), 0) * 100 / non_target_trials.length : 0,
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const results: NbackResultsStructure = {
|
|
52
|
+
common,
|
|
53
|
+
special
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
console.log(results);
|
|
57
|
+
return results;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { nback_extract_trials, nback_results,
|
|
61
|
+
nback_calculate_results, NbackResultsStructure };
|
package/src/spatial-cognition.ts
CHANGED
|
@@ -1,55 +1,113 @@
|
|
|
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
|
-
const
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
1
|
+
import { SpatialResultsStructure, CommonResults, SpatialSpecialResults } from './common-interfaces';
|
|
2
|
+
|
|
3
|
+
const BEST_SCORE = 1000;
|
|
4
|
+
const WORST_SCORE = 10000;
|
|
5
|
+
|
|
6
|
+
const spatial_calculate_results = (obj: any[]): SpatialResultsStructure => {
|
|
7
|
+
const trials = spatial_extract_trials(obj);
|
|
8
|
+
return spatial_results(trials);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const calculate_performance_score = (avg_rt: number, avg_acc: number): number => {
|
|
12
|
+
let performance = avg_rt / (avg_acc + 0.001);
|
|
13
|
+
performance = Math.min(Math.max(performance, BEST_SCORE), WORST_SCORE);
|
|
14
|
+
const performance_score = (1 - ((performance - BEST_SCORE) / (WORST_SCORE - BEST_SCORE))) * 100;
|
|
15
|
+
return performance_score;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const spatial_extract_trials = (obj: any[]) => {
|
|
19
|
+
let trials = obj.filter((o: any) => 'stimulus' in o &&
|
|
20
|
+
o.stimulus.startsWith("<img src="));
|
|
21
|
+
trials = spatial_extract_image_info(trials);
|
|
22
|
+
return trials;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const spatial_results = (obj: any[]): SpatialResultsStructure => {
|
|
26
|
+
const correct_trials = obj.filter((o: any) => o.correct === 1 || o.correct === true);
|
|
27
|
+
const incorrect_trials = obj.filter((o: any) => o.correct === 0 || o.correct === false);
|
|
28
|
+
const missing_trials = obj.filter((o: any) => o.response === null || o.response === undefined);
|
|
29
|
+
|
|
30
|
+
const total_rt = obj.reduce((a, b) => a + (b.rt || 0), 0);
|
|
31
|
+
const correct_rt = correct_trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
32
|
+
const incorrect_rt = incorrect_trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
33
|
+
const missing_rt = missing_trials.reduce((a, b) => a + (b.rt || 0), 0);
|
|
34
|
+
|
|
35
|
+
const avg_rt = obj.length > 0 ? total_rt / obj.length : 0;
|
|
36
|
+
const avg_acc = obj.length > 0 ? correct_trials.length / obj.length : 0;
|
|
37
|
+
|
|
38
|
+
const performance_score = calculate_performance_score(avg_rt, avg_acc);
|
|
39
|
+
|
|
40
|
+
const common: CommonResults = {
|
|
41
|
+
n_trials: obj.length,
|
|
42
|
+
n_correct_trials: correct_trials.length,
|
|
43
|
+
n_incorrect_trials: incorrect_trials.length,
|
|
44
|
+
avg_rt_total: avg_rt,
|
|
45
|
+
avg_rt_correct: correct_trials.length > 0 ? correct_rt / correct_trials.length : 0,
|
|
46
|
+
avg_rt_incorrect: incorrect_trials.length > 0 ? incorrect_rt / incorrect_trials.length : 0,
|
|
47
|
+
performance_score
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Group by distance, angle, and rotation for special results
|
|
51
|
+
const by_distance = Object.values(obj.reduce((acc: any, trial: any) => {
|
|
52
|
+
const key = trial.distance || 'unknown';
|
|
53
|
+
if (!acc[key]) acc[key] = { distance: key, trials: [], avg_rt: 0, avg_acc: 0 };
|
|
54
|
+
acc[key].trials.push(trial);
|
|
55
|
+
return acc;
|
|
56
|
+
}, {})).map((group: any) => {
|
|
57
|
+
group.avg_rt = group.trials.reduce((a: number, b: any) => a + (b.rt || 0), 0) / group.trials.length;
|
|
58
|
+
group.avg_acc = group.trials.reduce((a: number, b: any) => a + (b.correct ? 1 : 0), 0) / group.trials.length;
|
|
59
|
+
return group;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const by_angle = Object.values(obj.reduce((acc: any, trial: any) => {
|
|
63
|
+
const key = trial.angle || 'unknown';
|
|
64
|
+
if (!acc[key]) acc[key] = { angle: key, trials: [], avg_rt: 0, avg_acc: 0 };
|
|
65
|
+
acc[key].trials.push(trial);
|
|
66
|
+
return acc;
|
|
67
|
+
}, {})).map((group: any) => {
|
|
68
|
+
group.avg_rt = group.trials.reduce((a: number, b: any) => a + (b.rt || 0), 0) / group.trials.length;
|
|
69
|
+
group.avg_acc = group.trials.reduce((a: number, b: any) => a + (b.correct ? 1 : 0), 0) / group.trials.length;
|
|
70
|
+
return group;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const by_rotation = Object.values(obj.reduce((acc: any, trial: any) => {
|
|
74
|
+
const key = trial.rotation || 'unknown';
|
|
75
|
+
if (!acc[key]) acc[key] = { rotation: key, trials: [], avg_rt: 0, avg_acc: 0 };
|
|
76
|
+
acc[key].trials.push(trial);
|
|
77
|
+
return acc;
|
|
78
|
+
}, {})).map((group: any) => {
|
|
79
|
+
group.avg_rt = group.trials.reduce((a: number, b: any) => a + (b.rt || 0), 0) / group.trials.length;
|
|
80
|
+
group.avg_acc = group.trials.reduce((a: number, b: any) => a + (b.correct ? 1 : 0), 0) / group.trials.length;
|
|
81
|
+
return group;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const special: SpatialSpecialResults = {
|
|
85
|
+
by_distance,
|
|
86
|
+
by_angle,
|
|
87
|
+
by_rotation
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
common,
|
|
92
|
+
special
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const spatial_extract_image_info = (obj: any[]) => {
|
|
97
|
+
// from the following field, extract the final image name (map_left_1_90.png), and rotation from the class attribute (rotate_0)
|
|
98
|
+
// "stimulus": "<img src=\"https://unyavwofnmwihnpvkjji.supabase.co/storage/v1/object/public/test-stimuli/spatialcognition/map_left_1_90.png\" class=\"max-w-none rotate_0\" width=\"512\" height=\"512\" alt=\"Mapa\">",
|
|
99
|
+
obj.forEach((o: any) => {
|
|
100
|
+
// the image name is map_left_1_90.png
|
|
101
|
+
o.image = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[0];
|
|
102
|
+
o.direction = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[1];
|
|
103
|
+
o.distance = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[2];
|
|
104
|
+
o.angle = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[3];
|
|
105
|
+
o.rotation = o.stimulus.match(/rotate_(\d+)/)?.[1];
|
|
106
|
+
o.response = o.response === 1 ? "right" : "left";
|
|
107
|
+
o.correct = o.response === o.direction;
|
|
108
|
+
});
|
|
109
|
+
return obj;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { spatial_extract_image_info, spatial_extract_trials,
|
|
113
|
+
spatial_results, spatial_calculate_results, SpatialResultsStructure };
|
package/tsconfig.cjs.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "commonjs",
|
|
4
|
-
"declaration": true,
|
|
5
|
-
"outDir": "./dist/cjs",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
-
"skipLibCheck": true,
|
|
9
|
-
"forceConsistentCasingInFileNames": true
|
|
10
|
-
},
|
|
11
|
-
"include": ["src"],
|
|
12
|
-
"exclude": ["node_modules", "**/__tests__/*"]
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "commonjs",
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"outDir": "./dist/cjs",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true
|
|
10
|
+
},
|
|
11
|
+
"include": ["src"],
|
|
12
|
+
"exclude": ["node_modules", "**/__tests__/*"]
|
|
13
13
|
}
|
package/tsconfig.esm.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
// tsconfig.esm.json - For ES modules
|
|
2
|
-
{
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"module": "esnext",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "./dist/esm",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"moduleResolution": "node",
|
|
11
|
-
"forceConsistentCasingInFileNames": true
|
|
12
|
-
},
|
|
13
|
-
"include": ["src"],
|
|
14
|
-
"exclude": ["node_modules", "**/__tests__/*"]
|
|
1
|
+
// tsconfig.esm.json - For ES modules
|
|
2
|
+
{
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist/esm",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"forceConsistentCasingInFileNames": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["src"],
|
|
14
|
+
"exclude": ["node_modules", "**/__tests__/*"]
|
|
15
15
|
}
|