Skip to content

Commit d94b0c1

Browse files
committed
feat(cli): Add --max-rows flag
1 parent 487fa3a commit d94b0c1

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/schema/associations.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const associationLookup = {
3636
suffix: 'events',
3737
extensions: ['.tsv'],
3838
inherit: true,
39-
load: async (file: BIDSFile): Promise<Events> => {
40-
const columns = await loadTSV(file)
39+
load: async (file: BIDSFile, options: { maxRows: number }): Promise<Events> => {
40+
const columns = await loadTSV(file, options.maxRows)
4141
.catch((e) => {
4242
return new Map()
4343
})
@@ -53,8 +53,9 @@ const associationLookup = {
5353
inherit: true,
5454
load: async (
5555
file: BIDSFile,
56+
options: { maxRows: number },
5657
): Promise<Aslcontext> => {
57-
const columns = await loadTSV(file)
58+
const columns = await loadTSV(file, options.maxRows)
5859
.catch((e) => {
5960
return new Map()
6061
})
@@ -69,31 +70,31 @@ const associationLookup = {
6970
suffix: 'm0scan',
7071
extensions: ['.nii', '.nii.gz'],
7172
inherit: false,
72-
load: (file: BIDSFile): Promise<M0Scan> => {
73+
load: (file: BIDSFile, options: any): Promise<M0Scan> => {
7374
return Promise.resolve({ path: file.path })
7475
},
7576
},
7677
magnitude: {
7778
suffix: 'magnitude',
7879
extensions: ['.nii', '.nii.gz'],
7980
inherit: false,
80-
load: (file: BIDSFile): Promise<Magnitude> => {
81+
load: (file: BIDSFile, options: any): Promise<Magnitude> => {
8182
return Promise.resolve({ path: file.path })
8283
},
8384
},
8485
magnitude1: {
8586
suffix: 'magnitude1',
8687
extensions: ['.nii', '.nii.gz'],
8788
inherit: false,
88-
load: (file: BIDSFile): Promise<Magnitude1> => {
89+
load: (file: BIDSFile, options: any): Promise<Magnitude1> => {
8990
return Promise.resolve({ path: file.path })
9091
},
9192
},
9293
bval: {
9394
suffix: 'dwi',
9495
extensions: ['.bval'],
9596
inherit: true,
96-
load: async (file: BIDSFile): Promise<Bval> => {
97+
load: async (file: BIDSFile, options: any): Promise<Bval> => {
9798
const contents = await file.text()
9899
const rows = parseBvalBvec(contents)
99100
return {
@@ -109,7 +110,7 @@ const associationLookup = {
109110
suffix: 'dwi',
110111
extensions: ['.bvec'],
111112
inherit: true,
112-
load: async (file: BIDSFile): Promise<Bvec> => {
113+
load: async (file: BIDSFile, options: any): Promise<Bvec> => {
113114
const contents = await file.text()
114115
const rows = parseBvalBvec(contents)
115116

@@ -128,8 +129,8 @@ const associationLookup = {
128129
suffix: 'channels',
129130
extensions: ['.tsv'],
130131
inherit: true,
131-
load: async (file: BIDSFile): Promise<Channels> => {
132-
const columns = await loadTSV(file)
132+
load: async (file: BIDSFile, options: { maxRows: number }): Promise<Channels> => {
133+
const columns = await loadTSV(file, options.maxRows)
133134
.catch((e) => {
134135
return new Map()
135136
})
@@ -145,7 +146,7 @@ const associationLookup = {
145146
suffix: 'coordsystem',
146147
extensions: ['.json'],
147148
inherit: true,
148-
load: (file: BIDSFile): Promise<Coordsystem> => {
149+
load: (file: BIDSFile, options: any): Promise<Coordsystem> => {
149150
return Promise.resolve({ path: file.path })
150151
},
151152
},
@@ -154,6 +155,7 @@ const associationLookup = {
154155
export async function buildAssociations(
155156
source: BIDSFile,
156157
issues: DatasetIssues,
158+
maxRows: number = -1,
157159
): Promise<Associations> {
158160
const associations: Associations = {}
159161

@@ -177,7 +179,7 @@ export async function buildAssociations(
177179

178180
if (file) {
179181
// @ts-expect-error Matching load return value to key is hard
180-
associations[key] = await load(file).catch((error) => {
182+
associations[key] = await load(file, { maxRows }).catch((error) => {
181183
if (error.key) {
182184
issues.add({ code: error.key, location: file.path })
183185
}

src/schema/context.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export class BIDSContext implements Context {
232232
return
233233
}
234234

235-
this.columns = await loadTSV(this.file)
235+
this.columns = await loadTSV(this.file, this.dataset.options?.maxRows)
236236
.catch((error) => {
237237
if (error.key) {
238238
this.dataset.issues.add({ code: error.key, location: this.file.path })
@@ -247,7 +247,11 @@ export class BIDSContext implements Context {
247247
}
248248

249249
async loadAssociations(): Promise<void> {
250-
this.associations = await buildAssociations(this.file, this.dataset.issues)
250+
this.associations = await buildAssociations(
251+
this.file,
252+
this.dataset.issues,
253+
this.dataset.options?.maxRows,
254+
)
251255
return
252256
}
253257

src/setup/options.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Deno.test('options parsing', async (t) => {
1010
json: true,
1111
color: false,
1212
blacklistModalities: [],
13+
maxRows: 1000,
1314
})
1415
})
1516
})

src/setup/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type ValidatorOptions = {
3030
outfile?: string
3131
blacklistModalities: string[]
3232
prune?: boolean
33+
maxRows?: number
3334
}
3435

3536
const modalityType = new EnumType<string>(
@@ -50,6 +51,11 @@ export const validateCommand: Command<void, void, any, string[], void> = new Com
5051
'Specify a schema version to use for validation',
5152
)
5253
.option('-c, --config <file:string>', 'Path to a JSON configuration file')
54+
.option(
55+
'--max-rows <nrows:number>',
56+
'Maximum number of rows to validate in TSVs. Use 0 to validate headers only. Use -1 to validate all.',
57+
{ default: 1000 },
58+
)
5359
.option('-v, --verbose', 'Log more extensive information about issues')
5460
.option('--ignoreWarnings', 'Disregard non-critical issues')
5561
.option(

0 commit comments

Comments
 (0)