Skip to content

Commit

Permalink
Merge pull request #156 from VisLab/hed-schemas-once
Browse files Browse the repository at this point in the history
Updated the hed validation to only load schemas once
  • Loading branch information
effigies authored Feb 9, 2025
2 parents 587842a + 3b64628 commit 55a00e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/schema/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class BIDSContextDataset implements Dataset {
schema: Schema
pseudofileExtensions: Set<string>

// Opaque object for HED validator
hedSchemas: object | undefined | null = undefined

constructor(
args: Partial<BIDSContextDataset>,
) {
Expand Down
25 changes: 12 additions & 13 deletions src/validators/hed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ function sidecarValueHasHed(sidecarValue: unknown) {
)
}

let hedSchemas: object | undefined | null = undefined

async function setHedSchemas(datasetDescriptionJson = {}) {
async function setHedSchemas(dataset: BIDSContextDataset) {
if (dataset.hedSchemas !== undefined) {
return [] as HedIssue[]
}
const datasetDescriptionData = new hedValidator.bids.BidsJsonFile(
'/dataset_description.json',
datasetDescriptionJson,
dataset.dataset_description,
null,
)
try {
hedSchemas = await hedValidator.bids.buildBidsSchemas(
dataset.hedSchemas = await hedValidator.bids.buildBidsSchemas(
datasetDescriptionData,
null,
)
return [] as HedIssue[]
} catch (issueError) {
hedSchemas = null
dataset.hedSchemas = null
return hedValidator.bids.BidsHedIssue.fromHedIssues(
issueError,
datasetDescriptionData.file,
Expand All @@ -59,22 +60,20 @@ export async function hedValidate(
let hedValidationIssues = [] as HedIssue[]

try {
if (context.extension == '.tsv' && context.columns) {
if (context.extension === '.tsv' && context.columns) {
if (!('HED' in context.columns) && !sidecarHasHed(context.sidecar)) {
return
}
hedValidationIssues = await setHedSchemas(context.dataset.dataset_description)
hedValidationIssues = await setHedSchemas(context.dataset)

file = await buildHedTsvFile(context)
} else if (context.extension == '.json' && sidecarHasHed(context.json)) {
hedValidationIssues = hedValidationIssues = await setHedSchemas(
context.dataset.dataset_description,
)
} else if (context.extension === '.json' && sidecarHasHed(context.json)) {
hedValidationIssues = hedValidationIssues = await setHedSchemas(context.dataset)
file = buildHedSidecarFile(context)
}

if (file) {
hedValidationIssues.push(...file.validate(hedSchemas))
hedValidationIssues.push(...file.validate(context.dataset.hedSchemas))
}
} catch (error) {
context.dataset.issues.add({
Expand Down

0 comments on commit 55a00e3

Please sign in to comment.