Skip to content

Commit

Permalink
Merge pull request #2131 from rwblair/feat/output_to_file
Browse files Browse the repository at this point in the history
Add option to write validation results to file.
  • Loading branch information
effigies committed Sep 10, 2024
2 parents 53fc889 + 2381a3a commit b174bb1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
7 changes: 5 additions & 2 deletions bids-validator/src/files/nifti.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isCompressed, readHeader } from '@mango/nifti'
import type { BIDSFile } from '../types/filetree.ts'
import type { logger } from '../utils/logger.ts'
import { logger } from '../utils/logger.ts'
import type { NiftiHeader } from '@bids/schema/context'

async function extract(buffer: Uint8Array, nbytes: number): Promise<Uint8Array> {
Expand Down Expand Up @@ -29,9 +29,12 @@ async function extract(buffer: Uint8Array, nbytes: number): Promise<Uint8Array>

function readHeaderQuiet(buf: ArrayBuffer) {
const console_error = console.error
console.error = () => {}
const console_log = console.log
console.error = (msg: string) => { logger.info(msg)}
console.log = (msg: string) => { logger.info(msg)}
const header = readHeader(buf)
console.error = console_error
console.log = console_log
return header
}

Expand Down
36 changes: 17 additions & 19 deletions bids-validator/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { readFileTree } from './files/deno.ts'
import { fileListToTree } from './files/browser.ts'
import { resolve } from '@std/path'
import { validate } from './validators/bids.ts'
import { consoleFormat } from './utils/output.ts'
import { consoleFormat, resultToJSONStr } from './utils/output.ts'
import { setupLogging } from './utils/logger.ts'
import type { ValidationResult } from './types/validation-result.ts'

Expand All @@ -27,26 +27,24 @@ export async function main(): Promise<ValidationResult> {
// Run the schema based validator
const schemaResult = await validate(tree, options, config)

let output_string = ""
if (options.json) {
console.log(
JSON.stringify(schemaResult, (key, value) => {
if (value?.parent) {
// Remove parent reference to avoid circular references
value.parent = undefined
}
if (value instanceof Map) {
return Object.fromEntries(value)
} else {
return value
}
}),
)
output_string = resultToJSONStr(schemaResult)
} else {
console.log(
consoleFormat(schemaResult, {
verbose: options.verbose ? options.verbose : false,
}),
)
output_string = consoleFormat(schemaResult, {
verbose: options.verbose ? options.verbose : false,
})
}

if (options.outfile) {
if (globalThis.Deno) {
Deno.writeTextFileSync(options.outfile, output_string)
} else {
console.error("Output to file only supported in Deno runtime")
console.log(output_string)
}
} else {
console.log(output_string)
}

return schemaResult
Expand Down
7 changes: 6 additions & 1 deletion bids-validator/src/setup/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ValidatorOptions = {
debug: LevelName
color?: boolean
recursive?: boolean
outfile?: string
blacklistModalities: string[]
}

Expand All @@ -34,7 +35,7 @@ const modalityType = new EnumType<string>(
)

/** Extendable Cliffy Command with built in BIDS validator options */
export const validateCommand: Command<void, void, any, string[]> = new Command()
export const validateCommand: Command<void, void, any, string[], void> = new Command()
.name('bids-validator')
.type('debugLevel', new EnumType(LogLevelNames))
.description(
Expand Down Expand Up @@ -70,6 +71,10 @@ export const validateCommand: Command<void, void, any, string[]> = new Command()
'-r, --recursive',
'Validate datasets found in derivatives directories in addition to root dataset',
)
.option(
'-o, --outfile <file:string>',
'File to write validation results to.',
)

// Disabling color output is only available in Deno
if (typeof Deno !== 'undefined') {
Expand Down
14 changes: 14 additions & 0 deletions bids-validator/src/utils/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,17 @@ function helpUrl(code: string): string {
// Provide a link to NeuroStars
return `https://neurostars.org/search?q=${code}`
}

export function resultToJSONStr(result: ValidationResult) {
return JSON.stringify(result, (key, value) => {
if (value?.parent) {
// Remove parent reference to avoid circular references
value.parent = undefined
}
if (value instanceof Map) {
return Object.fromEntries(value)
} else {
return value
}
})
}

0 comments on commit b174bb1

Please sign in to comment.