-
Notifications
You must be signed in to change notification settings - Fork 107
/
summary.ts
167 lines (155 loc) · 4.43 KB
/
summary.ts
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
import { collectSubjectMetadata } from './collectSubjectMetadata.ts'
import { SummaryOutput, SubjectMetadata } from '../types/validation-result.ts'
import { BIDSContext } from '../schema/context.ts'
export const modalityPrettyLookup: Record<string, string> = {
mri: 'MRI',
pet: 'PET',
meg: 'MEG',
eeg: 'EEG',
ieeg: 'iEEG',
micro: 'Microscopy',
}
const secondaryLookup: Record<string, string> = {
dwi: 'MRI_Diffusion',
anat: 'MRI_Structural',
func: 'MRI_Functional',
perf: 'MRI_Perfusion',
}
export function computeModalities(
modalities: Record<string, number>,
): string[] {
// Order by matching file count
const nonZero = Object.keys(modalities).filter((a) => modalities[a] !== 0)
if (nonZero.length === 0) {
return []
}
const sortedModalities = nonZero.sort((a, b) => {
if (modalities[b] === modalities[a]) {
// On a tie, hand it to the non-MRI modality
if (b === 'mri') {
return -1
} else {
return 0
}
}
return modalities[b] - modalities[a]
})
return sortedModalities.map((mod) =>
mod in modalityPrettyLookup ? modalityPrettyLookup[mod] : mod,
)
}
export function computeSecondaryModalities(
secondary: Record<string, number>,
): string[] {
const nonZeroSecondary = Object.keys(secondary).filter(
(a) => secondary[a] !== 0,
)
const sortedSecondary = nonZeroSecondary.sort(
(a, b) => secondary[b] - secondary[a],
)
return sortedSecondary
}
export class Summary {
sessions: Set<string>
subjects: Set<string>
subjectMetadata: SubjectMetadata[]
tasks: Set<string>
totalFiles: number
size: number
dataProcessed: boolean
pet: Record<string, any>
modalitiesCount: Record<string, number>
secondaryModalitiesCount: Record<string, number>
dataTypes: Set<string>
schemaVersion: string
constructor() {
this.dataProcessed = false
this.totalFiles = 0
this.size = 0
this.sessions = new Set()
this.subjects = new Set()
this.subjectMetadata = []
this.tasks = new Set()
this.pet = {}
this.dataTypes = new Set()
this.modalitiesCount = {
mri: 0,
pet: 0,
meg: 0,
eeg: 0,
ieeg: 0,
microscopy: 0,
}
this.secondaryModalitiesCount = {
MRI_Diffusion: 0,
MRI_Structural: 0,
MRI_Functional: 0,
MRI_Perfusion: 0,
PET_Static: 0,
PET_Dynamic: 0,
iEEG_ECoG: 0,
iEEG_SEEG: 0,
}
this.schemaVersion = ''
}
get modalities() {
return computeModalities(this.modalitiesCount)
}
get secondaryModalities() {
return computeSecondaryModalities(this.secondaryModalitiesCount)
}
async update(context: BIDSContext): Promise<void> {
if (context.file.path.startsWith('/derivatives') && !this.dataProcessed) {
return
}
this.totalFiles++
this.size += await context.file.size
if ('sub' in context.entities) {
this.subjects.add(context.entities.sub)
}
if ('ses' in context.entities) {
this.sessions.add(context.entities.ses)
}
if (context.datatype.length) {
this.dataTypes.add(context.datatype)
}
if (context.extension === '.json') {
if ('TaskName' in context.json) {
this.tasks.add(context.json.TaskName as string)
}
}
if (context.modality) {
this.modalitiesCount[context.modality]++
}
if (context.datatype in secondaryLookup) {
const key = secondaryLookup[context.datatype]
this.secondaryModalitiesCount[key]++
} else if (context.datatype === 'pet' && 'rec' in context.entities) {
if (['acstat', 'nacstat'].includes(context.entities.rec)) {
this.secondaryModalitiesCount.PET_Static++
} else if (['acdyn', 'nacdyn'].includes(context.entities.rec)) {
this.secondaryModalitiesCount.PET_Dynamic++
}
}
if (context.file.path.endsWith('participants.tsv')) {
const tsvContents = await context.file.text()
this.subjectMetadata = collectSubjectMetadata(tsvContents)
}
}
formatOutput(): SummaryOutput {
return {
sessions: Array.from(this.sessions),
subjects: Array.from(this.subjects),
subjectMetadata: this.subjectMetadata,
tasks: Array.from(this.tasks),
modalities: this.modalities,
secondaryModalities: this.secondaryModalities,
totalFiles: this.totalFiles,
size: this.size,
dataProcessed: this.dataProcessed,
pet: this.pet,
dataTypes: Array.from(this.dataTypes),
schemaVersion: this.schemaVersion,
}
}
}