Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HED error reporting and tests #2174

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions bids-validator/tests/hed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('HED', function () {
]
const jsonDictionary = {
'/sub01/sub01_task-test_events.json': {
myCodes: {
test: {
HED: {
one: 'Duration/5 s',
},
Expand Down Expand Up @@ -55,12 +55,8 @@ describe('HED', function () {
]
const jsonDictionary = {
'/sub01/sub01_task-test_events.json': {
myCodes: {
test: {
HED: {
one: 'Label/#',
},
},
test: {
HED: 'Label/#',
},
},
'/dataset_description.json': { HEDVersion: '8.0.0' },
Expand All @@ -86,11 +82,9 @@ describe('HED', function () {

const jsonDictionary = {
'/sub01/sub01_task-test_events.json': {
myCodes: {
test: {
HED: {
one: 'ts:Sensory-presentation, Label/#',
},
test: {
HED: {
one: 'ts:Sensory-presentation, Train',
},
},
},
Expand Down Expand Up @@ -148,11 +142,9 @@ describe('HED', function () {

const jsonDictionary = {
'/sub01/sub01_task-test_events.json': {
myCodes: {
test: {
HED: {
one: 'ts:Sensory-presentation, Label/#, sc:Sleep-deprivation',
},
test: {
HED: {
one: 'ts:Sensory-presentation, Walk, sc:Sleep-deprivation',
},
},
},
Expand Down Expand Up @@ -260,7 +252,7 @@ describe('HED', function () {
]
const jsonDictionary = {
'/sub01/sub01_task-test_events.json': {
myCodes: {
test: {
HED: {
one: 'Duration/5 s',
},
Expand Down
60 changes: 33 additions & 27 deletions bids-validator/validators/hed.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,24 @@
)
}

const issues = []
for (const [sidecarName, sidecarContents] of Object.entries(jsonContents)) {
try {
const sidecarFile = buildSidecar(sidecarName, sidecarContents, jsonFiles)
issues.push(...validateFile(sidecarFile, hedSchemas))
} catch (e) {
issues.push(new Issue({ code: 109 }))
return issues
}
}
const sidecarIssues = validateFiles(
buildSidecars(jsonContents, jsonFiles),
hedSchemas,
)

if (issues.some((issue) => issue.isError())) {
return issues
if (sidecarIssues.some((issue) => issue.isError() || issue.code === 109)) {
return sidecarIssues
}

for (const tsv of tsvs) {
try {
const tsvFile = buildTsv(tsv, jsonContents)
issues.push(...validateFile(tsvFile, hedSchemas))
} catch (e) {
issues.push(new Issue({ code: 109 }))
return issues
}
}
const tsvIssues = validateFiles(buildTsvs(tsvs, jsonContents), hedSchemas)

return issues
return [...sidecarIssues, ...tsvIssues]
}

function* buildSidecars(jsonContents, jsonFiles) {
for (const [sidecarName, sidecarContents] of Object.entries(jsonContents)) {
yield buildSidecar(sidecarName, sidecarContents, jsonFiles)
}
}

function buildSidecar(sidecarName, sidecarContents, jsonFiles) {
Expand All @@ -63,6 +55,12 @@
return new hedValidator.bids.BidsSidecar(sidecarName, sidecarContents, file)
}

function* buildTsvs(tsvs, jsonContents) {
for (const tsv of tsvs) {
yield buildTsv(tsv, jsonContents)
}
}

function buildTsv(tsv, jsonContents) {
const potentialSidecars = utils.files.potentialLocations(
tsv.file.relativePath.replace('.tsv', '.json'),
Expand All @@ -81,12 +79,20 @@
)
}

function validateFile(file, hedSchemas) {
const issues = file.validate(hedSchemas)
if (issues === null) {
throw new Error()
function validateFiles(fileGenerator, hedSchemas) {
const issues = []
for (const file of fileGenerator) {
try {
const fileIssues = file.validate(hedSchemas)
if (fileIssues === null) {
return [new hedValidator.bids.BidsIssue(109)]
}
issues.push(fileIssues)
} catch (issueError) {
return hedValidator.bids.BidsHedIssue.fromHedIssues(issueError, file.file)

Check warning on line 92 in bids-validator/validators/hed.js

View check run for this annotation

Codecov / codecov/patch

bids-validator/validators/hed.js#L92

Added line #L92 was not covered by tests
}
}
return issues
return issues.flat()
}

function getSidecarFileObject(sidecarName, jsonFiles) {
Expand Down
Loading