compileprofile-spacetests 0.2.1 → 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.
Files changed (48) hide show
  1. package/CLAUDE.md +115 -0
  2. package/README.md +147 -34
  3. package/dist/cjs/common-interfaces.d.ts +60 -0
  4. package/dist/cjs/common-interfaces.js +2 -0
  5. package/dist/cjs/emotions.d.ts +2 -7
  6. package/dist/cjs/emotions.js +56 -22
  7. package/dist/cjs/gonogo.d.ts +1 -6
  8. package/dist/cjs/gonogo.js +61 -14
  9. package/dist/cjs/index.d.ts +2 -5
  10. package/dist/cjs/nback.d.ts +1 -11
  11. package/dist/cjs/nback.js +27 -13
  12. package/dist/cjs/spatial-cognition.d.ts +1 -6
  13. package/dist/cjs/spatial-cognition.js +69 -14
  14. package/dist/esm/common-interfaces.d.ts +60 -0
  15. package/dist/esm/common-interfaces.js +1 -0
  16. package/dist/esm/emotions.d.ts +2 -7
  17. package/dist/esm/emotions.js +55 -21
  18. package/dist/esm/gonogo.d.ts +1 -6
  19. package/dist/esm/gonogo.js +61 -14
  20. package/dist/esm/index.d.ts +2 -5
  21. package/dist/esm/nback.d.ts +1 -11
  22. package/dist/esm/nback.js +27 -13
  23. package/dist/esm/spatial-cognition.d.ts +1 -6
  24. package/dist/esm/spatial-cognition.js +69 -14
  25. package/example-data/emotions.json +807 -807
  26. package/example-data/gonogo.json +5392 -5392
  27. package/example-data/nback.json +892 -892
  28. package/example-data/participant.json +6 -6
  29. package/example-data/spatial-cognition.json +462 -462
  30. package/jest.config.js +13 -13
  31. package/original/emotion-recognition.R +39 -39
  32. package/original/gonogo.R +32 -32
  33. package/package.json +40 -40
  34. package/src/__tests__/integration/browserInfo.test.ts +16 -16
  35. package/src/__tests__/integration/emotions.test.ts +172 -64
  36. package/src/__tests__/integration/gonogo.test.ts +183 -80
  37. package/src/__tests__/integration/nback.test.ts +148 -55
  38. package/src/__tests__/integration/spatial-cognition.test.ts +171 -66
  39. package/src/__tests__/test-utils/testData.ts +79 -79
  40. package/src/common-interfaces.ts +79 -0
  41. package/src/emotions.ts +98 -59
  42. package/src/getters.ts +21 -21
  43. package/src/gonogo.ts +126 -75
  44. package/src/index.ts +39 -23
  45. package/src/nback.ts +61 -53
  46. package/src/spatial-cognition.ts +112 -54
  47. package/tsconfig.cjs.json +12 -12
  48. 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
- interface GonogoResultsStructure {
2
- n: number;
3
- avg_rt: number;
4
- avg_acc: number;
5
- performance: number;
6
- }
7
-
8
- const gonogo_calculate_results = (data: any[]): GonogoResultsStructure => {
9
- const trials = gonogo_extract_trials(data);
10
- const results = gonogo_results(trials);
11
- return results;
12
- }
13
-
14
- const gonogo_extract_trials = (obj: any[]): any[] => {
15
- // GONOGO_IMAGE_PATH <- "https://unyavwofnmwihnpvkjji.supabase.co/storage/v1/object/public/test-stimuli/gonogo/"
16
- let trials = obj.filter((o: any) => 'image' in o && o.image !== undefined);
17
- trials.forEach((o: any) => {
18
- // image is in format <stimulus>_trans_<number>.png
19
- // extract the stimulus and number
20
-
21
- // "image": "https://unyavwofnmwihnpvkjji.supabase.co/storage/v1/object/public/test-stimuli/gonogo/aliens/astronaut_trans_1.png",
22
- // i need to extract the iamge name from the url
23
- const match = o.image.match(/([^/]+)_(\d+)\.png$/);
24
- if (match) {
25
- o.stimulus = match[0];
26
- o.stimulustype = match[1];
27
- o.image_number = match[2];
28
- } else {
29
- console.warn('Image format did not match expected pattern:', o.image);
30
- return;
31
- }
32
- o.should_go = false;
33
- o.did_go = o.response !== null;
34
- switch (o.block_number) {
35
- case 1:
36
- o.should_go = o.stimulus == "astronaut_trans_1.png";
37
- break;
38
- case 2:
39
- o.should_go = o.stimulus == "alien_trans_1.png";
40
- break;
41
- case 3:
42
- o.should_go = o.stimulus == "rocket_trans_1.png";
43
- break;
44
- case 4:
45
- // should go in all cases where the goal is NOT the rocket
46
- o.should_go = !(o.stimulus == "rocket_trans_1.png");
47
- break;
48
- default:
49
- o.should_go = false;
50
- break;
51
- }
52
- o.correct = o.should_go == o.did_go;
53
- })
54
- return trials;
55
- }
56
-
57
- const gonogo_results = (data: any[]): GonogoResultsStructure => {
58
- // throw error if obj is empty
59
- if (data.length === 0) {
60
- throw new Error("No trials found");
61
- }
62
- let results = {
63
- n: data.length,
64
- avg_rt: data.reduce((a, b) => a + b.rt, 0) / data.length,
65
- avg_acc: data.reduce((a, b) => a + b.correct, 0) / data.length,
66
- performance: NaN
67
- };
68
- const best_score = 300;
69
- const worst_score = 5000;
70
- let performance = results.avg_rt / (Math.pow(results.avg_acc, 5) + 0.001);
71
- performance = Math.min(Math.max(performance, best_score), worst_score);
72
- results.performance = (1 - ((performance - best_score) / (worst_score - best_score))) * 100;
73
- return results;
74
- }
75
-
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
+
76
127
  export { GonogoResultsStructure, gonogo_calculate_results, gonogo_extract_trials, gonogo_results };
package/src/index.ts CHANGED
@@ -1,23 +1,39 @@
1
- import { NbackResultsStructure, nback_calculate_results } from './nback';
2
- import { SpatialResultsStructure, spatial_calculate_results } from './spatial-cognition';
3
- import { GonogoResultsStructure, gonogo_calculate_results } from './gonogo';
4
- import { EmotionResultsStructure, emotion_calculate_results } from './emotions';
5
-
6
- const NbackResults = (obj: any[]): NbackResultsStructure => {
7
- return nback_calculate_results(obj);
8
- }
9
-
10
- const SpatialCognitionResults = (obj: any[]): SpatialResultsStructure => {
11
- return spatial_calculate_results(obj);
12
- }
13
-
14
- const GonogoResults = (obj: any[]): GonogoResultsStructure => {
15
- return gonogo_calculate_results(obj);
16
- }
17
-
18
- const EmotionResults = (obj: any[]): EmotionResultsStructure => {
19
- return emotion_calculate_results(obj);
20
- }
21
-
22
- export { NbackResults, SpatialCognitionResults,
23
- EmotionResults, GonogoResults };
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
- // from the passed json extract those objects that have field is_target
2
- const nback_calculate_results = (obj: any[]): NbackResultsStructure => {
3
- const trials = nback_extract_trials(obj);
4
- return nback_results(trials);
5
- }
6
-
7
- const nback_extract_trials = (obj: any[]): any[] => {
8
- const trials = obj.filter((o: any) => 'is_target' in o);
9
- return trials;
10
- }
11
-
12
- interface NbackTrialResultsStructure {
13
- n: number;
14
- avg_rt: number;
15
- avg_acc: number;
16
- }
17
-
18
- interface NbackResultsStructure {
19
- total_trials: NbackTrialResultsStructure;
20
- target_trials: NbackTrialResultsStructure;
21
- non_target_trials: NbackTrialResultsStructure;
22
- performance: number;
23
- }
24
-
25
- const nback_results = (trials: any[]): NbackResultsStructure => {
26
- const target_trials = trials.filter((o: any) => o.is_target);
27
- const non_target_trials = trials.filter((o: any) => !o.is_target);
28
-
29
- let results = {
30
- total_trials: {
31
- n: trials.length,
32
- avg_rt: trials.reduce((a, b) => a + b.rt, 0)/trials.length,
33
- avg_acc: trials.reduce((a, b) => a + b.correct, 0)/trials.length,
34
- },
35
- target_trials: {
36
- n: target_trials.length,
37
- avg_rt: target_trials.reduce((a, b) => a + b.rt, 0)/target_trials.length,
38
- avg_acc: target_trials.reduce((a, b) => a + b.correct, 0)/target_trials.length,
39
- },
40
- non_target_trials: {
41
- n: non_target_trials.length,
42
- avg_rt: non_target_trials.reduce((a, b) => a + b.rt, 0)/non_target_trials.length,
43
- avg_acc: non_target_trials.reduce((a, b) => a + b.correct, 0)/non_target_trials.length,
44
- },
45
- performance: NaN
46
- }
47
- results.performance = results.total_trials.avg_acc;
48
- console.log(results);
49
- return results;
50
- }
51
-
52
- export { nback_extract_trials, nback_results,
53
- nback_calculate_results, NbackResultsStructure };
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 };
@@ -1,55 +1,113 @@
1
- interface SpatialResultsStructure {
2
- n: number;
3
- avg_rt: number;
4
- avg_acc: number;
5
- performance: number;
6
- }
7
-
8
- const spatial_calculate_results = (obj: any[]): SpatialResultsStructure => {
9
- const trials = spatial_extract_trials(obj);
10
- return spatial_results(trials);
11
- }
12
-
13
- const spatial_extract_trials = (obj: any[]) => {
14
- let trials = obj.filter((o: any) => 'stimulus' in o &&
15
- o.stimulus.startsWith("<img src="));
16
- trials = spatial_extract_image_info(trials);
17
- return trials;
18
- }
19
-
20
- const spatial_results = (obj: any[]): SpatialResultsStructure => {
21
- const results = {
22
- n: obj.length,
23
- avg_rt: obj.reduce((a, b) => a + b.rt, 0) / obj.length,
24
- avg_acc: obj.reduce((a, b) => a + b.correct, 0) / obj.length,
25
- performance: NaN
26
- }
27
- const best_score = 1000;
28
- const worst_score = 10000;
29
- // results.avg_acc to the power of 5
30
- //let performance = results.avg_rt / (Math.pow(results.avg_acc, 5) + 0.001);
31
- let performance = results.avg_rt / (results.avg_acc + 0.001);
32
- performance = Math.min(Math.max(performance, best_score), worst_score);
33
- // convert to 0-100 where 100 is the best (200)
34
- results.performance = (1 - ((performance - best_score) / (worst_score - best_score))) * 100;
35
- return results;
36
- }
37
-
38
- const spatial_extract_image_info = (obj: any[]) => {
39
- // from the following field, extract the final image name (map_left_1_90.png), and rotation from the class attribute (rotate_0)
40
- // "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\">",
41
- obj.forEach((o: any) => {
42
- // the image name is map_left_1_90.png
43
- o.image = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[0];
44
- o.direction = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[1];
45
- o.distance = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[2];
46
- o.angle = o.stimulus.match(/map_(left|right)_(\d+)_(\d+)\.png/)?.[3];
47
- o.rotation = o.stimulus.match(/rotate_(\d+)/)?.[1];
48
- o.response = o.response === 1 ? "right" : "left";
49
- o.correct = o.response === o.direction;
50
- });
51
- return obj;
52
- }
53
-
54
- export { spatial_extract_image_info, spatial_extract_trials,
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,
55
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
  }