Skip to content

Commit 752bc44

Browse files
committed
remove old lookup entries in associations.ts
1 parent 1afcdac commit 752bc44

File tree

1 file changed

+75
-120
lines changed

1 file changed

+75
-120
lines changed

src/schema/associations.ts

Lines changed: 75 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import type {
1010
Magnitude,
1111
Magnitude1,
1212
} from '@bids/schema/context'
13-
import type {
14-
Schema as MetaSchema
15-
} from '@bids/schema/metaschema'
13+
import type { Schema as MetaSchema } from '@bids/schema/metaschema'
1614

1715
import type { BIDSFile, FileTree } from '../types/filetree.ts'
1816
import type { BIDSContext } from './context.ts'
@@ -38,128 +36,83 @@ import { expressionFunctions } from './expressionLanguage.ts'
3836
* returning promises for now.
3937
*/
4038
const associationLookup = {
41-
events: {
42-
suffix: 'events',
43-
extensions: ['.tsv'],
44-
inherit: true,
45-
load: async (file: BIDSFile, options: { maxRows: number }): Promise<Events> => {
46-
const columns = await loadTSV(file, options.maxRows)
47-
.catch((e) => {
48-
return new Map()
49-
})
50-
return {
51-
path: file.path,
52-
onset: columns.get('onset') || [],
53-
}
54-
},
39+
events: async (file: BIDSFile, options: { maxRows: number }): Promise<Events> => {
40+
const columns = await loadTSV(file, options.maxRows)
41+
.catch((e) => {
42+
return new Map()
43+
})
44+
return {
45+
path: file.path,
46+
onset: columns.get('onset') || [],
47+
}
5548
},
56-
aslcontext: {
57-
suffix: 'aslcontext',
58-
extensions: ['.tsv'],
59-
inherit: true,
60-
load: async (
61-
file: BIDSFile,
62-
options: { maxRows: number },
63-
): Promise<Aslcontext> => {
64-
const columns = await loadTSV(file, options.maxRows)
65-
.catch((e) => {
66-
return new Map()
67-
})
68-
return {
69-
path: file.path,
70-
n_rows: columns.get('volume_type')?.length || 0,
71-
volume_type: columns.get('volume_type') || [],
72-
}
73-
},
49+
aslcontext: async (
50+
file: BIDSFile,
51+
options: { maxRows: number },
52+
): Promise<Aslcontext> => {
53+
const columns = await loadTSV(file, options.maxRows)
54+
.catch((e) => {
55+
return new Map()
56+
})
57+
return {
58+
path: file.path,
59+
n_rows: columns.get('volume_type')?.length || 0,
60+
volume_type: columns.get('volume_type') || [],
61+
}
7462
},
75-
m0scan: {
76-
suffix: 'm0scan',
77-
extensions: ['.nii', '.nii.gz'],
78-
inherit: false,
79-
load: (file: BIDSFile, options: any): Promise<M0Scan> => {
80-
return Promise.resolve({ path: file.path })
81-
},
63+
m0scan: (file: BIDSFile, options: any): Promise<M0Scan> => {
64+
return Promise.resolve({ path: file.path })
8265
},
83-
magnitude: {
84-
suffix: 'magnitude',
85-
extensions: ['.nii', '.nii.gz'],
86-
inherit: false,
87-
load: (file: BIDSFile, options: any): Promise<Magnitude> => {
88-
return Promise.resolve({ path: file.path })
89-
},
66+
magnitude: (file: BIDSFile, options: any): Promise<Magnitude> => {
67+
return Promise.resolve({ path: file.path })
9068
},
91-
magnitude1: {
92-
suffix: 'magnitude1',
93-
extensions: ['.nii', '.nii.gz'],
94-
inherit: false,
95-
load: (file: BIDSFile, options: any): Promise<Magnitude1> => {
96-
return Promise.resolve({ path: file.path })
97-
},
69+
magnitude1: (file: BIDSFile, options: any): Promise<Magnitude1> => {
70+
return Promise.resolve({ path: file.path })
9871
},
99-
bval: {
100-
suffix: 'dwi',
101-
extensions: ['.bval'],
102-
inherit: true,
103-
load: async (file: BIDSFile, options: any): Promise<Bval> => {
104-
const contents = await file.text()
105-
const rows = parseBvalBvec(contents)
106-
return {
107-
path: file.path,
108-
n_cols: rows ? rows[0].length : 0,
109-
n_rows: rows ? rows.length : 0,
110-
// @ts-expect-error values is expected to be a number[], coerce lazily
111-
values: rows[0],
112-
}
113-
},
72+
bval: async (file: BIDSFile, options: any): Promise<Bval> => {
73+
const contents = await file.text()
74+
const rows = parseBvalBvec(contents)
75+
return {
76+
path: file.path,
77+
n_cols: rows ? rows[0].length : 0,
78+
n_rows: rows ? rows.length : 0,
79+
// @ts-expect-error values is expected to be a number[], coerce lazily
80+
values: rows[0],
81+
}
11482
},
115-
bvec: {
116-
suffix: 'dwi',
117-
extensions: ['.bvec'],
118-
inherit: true,
119-
load: async (file: BIDSFile, options: any): Promise<Bvec> => {
120-
const contents = await file.text()
121-
const rows = parseBvalBvec(contents)
83+
bvec: async (file: BIDSFile, options: any): Promise<Bvec> => {
84+
const contents = await file.text()
85+
const rows = parseBvalBvec(contents)
12286

123-
if (rows.some((row) => row.length !== rows[0].length)) {
124-
throw { key: 'BVEC_ROW_LENGTH' }
125-
}
87+
if (rows.some((row) => row.length !== rows[0].length)) {
88+
throw { key: 'BVEC_ROW_LENGTH' }
89+
}
12690

127-
return {
128-
path: file.path,
129-
n_cols: rows ? rows[0].length : 0,
130-
n_rows: rows ? rows.length : 0,
131-
}
132-
},
91+
return {
92+
path: file.path,
93+
n_cols: rows ? rows[0].length : 0,
94+
n_rows: rows ? rows.length : 0,
95+
}
13396
},
134-
channels: {
135-
suffix: 'channels',
136-
extensions: ['.tsv'],
137-
inherit: true,
138-
load: async (file: BIDSFile, options: { maxRows: number }): Promise<Channels> => {
139-
const columns = await loadTSV(file, options.maxRows)
140-
.catch((e) => {
141-
return new Map()
142-
})
143-
return {
144-
path: file.path,
145-
type: columns.get('type'),
146-
short_channel: columns.get('short_channel'),
147-
sampling_frequency: columns.get('sampling_frequency'),
148-
}
149-
},
97+
channels: async (file: BIDSFile, options: { maxRows: number }): Promise<Channels> => {
98+
const columns = await loadTSV(file, options.maxRows)
99+
.catch((e) => {
100+
return new Map()
101+
})
102+
return {
103+
path: file.path,
104+
type: columns.get('type'),
105+
short_channel: columns.get('short_channel'),
106+
sampling_frequency: columns.get('sampling_frequency'),
107+
}
150108
},
151-
coordsystem: {
152-
suffix: 'coordsystem',
153-
extensions: ['.json'],
154-
inherit: true,
155-
load: (file: BIDSFile, options: any): Promise<Coordsystem> => {
156-
return Promise.resolve({ path: file.path })
157-
},
109+
coordsystem: (file: BIDSFile, options: any): Promise<Coordsystem> => {
110+
return Promise.resolve({ path: file.path })
158111
},
159112
}
160113

161114
export async function buildAssociations(
162-
context: BIDSContext
115+
context: BIDSContext,
163116
): Promise<Associations> {
164117
const associations: Associations = {}
165118

@@ -175,8 +128,8 @@ export async function buildAssociations(
175128
}
176129
let file
177130
let extension: string[] = []
178-
if (typeof rule.target.extension === "string") {
179-
extension = [ rule.target.extension ]
131+
if (typeof rule.target.extension === 'string') {
132+
extension = [rule.target.extension]
180133
} else if (Array.isArray(rule.target.extension)) {
181134
extension = rule.target.extension
182135
}
@@ -197,13 +150,15 @@ export async function buildAssociations(
197150

198151
if (file) {
199152
// @ts-expect-error
200-
const load = associationLookup[key].load
201-
// @ts-expect-error Matching load return value to key is hard
202-
associations[key] = await load(file, { maxRows: context.dataset.options?.maxRows }).catch((error) => {
203-
if (error.key) {
204-
context.dataset.issues.add({ code: error.key, location: file.path })
205-
}
206-
})
153+
const load = associationLookup[key]
154+
// @ts-expect-error
155+
associations[key] = await load(file, { maxRows: context.dataset.options?.maxRows }).catch(
156+
(error: any) => {
157+
if (key in error) {
158+
context.dataset.issues.add({ code: error.key, location: file.path })
159+
}
160+
},
161+
)
207162
}
208163
}
209164
return Promise.resolve(associations)

0 commit comments

Comments
 (0)