Skip to content

Commit aa703e3

Browse files
committed
Loop through passed event files to actually validate HED data
Fixes #967
1 parent 0f0bbdc commit aa703e3

File tree

1 file changed

+47
-65
lines changed
  • bids-validator/validators/events

1 file changed

+47
-65
lines changed

bids-validator/validators/events/hed.js

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,21 @@ const Issue = utils.issues.Issue
44

55
export default function checkHedStrings(events, headers, jsonContents) {
66
let issues = []
7-
// get all headers associated with task data
8-
const taskHeaders = headers.filter(header => {
9-
const file = header[0]
10-
return file.relativePath.includes('_task-')
11-
})
127

138
const hedStrings = []
149

15-
// loop through headers with files that are tasks
16-
taskHeaders.forEach(taskHeader => {
17-
const file = taskHeader[0]
18-
19-
// get the json sidecar dictionary associated with that nifti scan
10+
// loop through event data files
11+
events.forEach(eventFile => {
12+
// get the json sidecar dictionary associated with the event data
2013
const potentialSidecars = utils.files.potentialLocations(
21-
file.relativePath.replace('.gz', '').replace('.nii', '.json'),
14+
eventFile.path.replace('.tsv', '.json'),
2215
)
2316
const mergedDictionary = utils.files.generateMergedSidecarDict(
2417
potentialSidecars,
2518
jsonContents,
2619
)
27-
const sidecarHedTags = {}
2820

21+
const sidecarHedTags = {}
2922
for (const sidecarKey in mergedDictionary) {
3023
const sidecarValue = mergedDictionary[sidecarKey]
3124
if (
@@ -37,66 +30,55 @@ export default function checkHedStrings(events, headers, jsonContents) {
3730
}
3831
}
3932

40-
// get the _events.tsv associated with this task scan
41-
const potentialEvents = utils.files.potentialLocations(
42-
file.relativePath.replace('.gz', '').replace('bold.nii', 'events.tsv'),
43-
)
44-
const associatedEvents = events.filter(
45-
event => potentialEvents.indexOf(event.path) > -1,
46-
)
47-
48-
// loop through all events associated with this task scan
49-
for (const event of associatedEvents) {
50-
// get all non-empty rows
51-
const rows = event.contents
52-
.split('\n')
53-
.filter(row => !(!row || /^\s*$/.test(row)))
33+
// get all non-empty rows
34+
const rows = eventFile.contents
35+
.split('\n')
36+
.filter(row => !(!row || /^\s*$/.test(row)))
5437

55-
const columnHeaders = rows[0].trim().split('\t')
56-
const hedColumnIndex = columnHeaders.indexOf('HED')
57-
const sidecarHedColumnIndices = {}
58-
for (const sidecarHedColumn in sidecarHedTags) {
59-
const sidecarHedColumnHeader = columnHeaders.indexOf(sidecarHedColumn)
60-
if (sidecarHedColumnHeader > -1) {
61-
sidecarHedColumnIndices[sidecarHedColumn] = sidecarHedColumnHeader
62-
}
63-
}
64-
if (hedColumnIndex === -1 && sidecarHedColumnIndices.length === 0) {
65-
continue
38+
const columnHeaders = rows[0].trim().split('\t')
39+
const hedColumnIndex = columnHeaders.indexOf('HED')
40+
const sidecarHedColumnIndices = {}
41+
for (const sidecarHedColumn in sidecarHedTags) {
42+
const sidecarHedColumnHeader = columnHeaders.indexOf(sidecarHedColumn)
43+
if (sidecarHedColumnHeader > -1) {
44+
sidecarHedColumnIndices[sidecarHedColumn] = sidecarHedColumnHeader
6645
}
46+
}
47+
if (hedColumnIndex === -1 && sidecarHedColumnIndices.length === 0) {
48+
return
49+
}
6750

68-
for (const row of rows.slice(1)) {
69-
// get the 'HED' field
70-
const rowCells = row.trim().split('\t')
71-
const hedStringParts = []
72-
if (rowCells[hedColumnIndex]) {
73-
hedStringParts.push(rowCells[hedColumnIndex])
74-
}
75-
for (const sidecarHedColumn in sidecarHedColumnIndices) {
76-
const sidecarHedIndex = sidecarHedColumnIndices[sidecarHedColumn]
77-
const sidecarHedKey = rowCells[sidecarHedIndex]
78-
if (sidecarHedKey) {
79-
const sidecarHedString =
80-
sidecarHedTags[sidecarHedColumn][sidecarHedKey]
81-
if (sidecarHedString !== undefined) {
82-
hedStringParts.push(sidecarHedString)
83-
} else {
84-
issues.push(
85-
new Issue({
86-
code: 112,
87-
file: file,
88-
evidence: sidecarHedKey,
89-
}),
90-
)
91-
}
51+
for (const row of rows.slice(1)) {
52+
// get the 'HED' field
53+
const rowCells = row.trim().split('\t')
54+
const hedStringParts = []
55+
if (rowCells[hedColumnIndex]) {
56+
hedStringParts.push(rowCells[hedColumnIndex])
57+
}
58+
for (const sidecarHedColumn in sidecarHedColumnIndices) {
59+
const sidecarHedIndex = sidecarHedColumnIndices[sidecarHedColumn]
60+
const sidecarHedKey = rowCells[sidecarHedIndex]
61+
if (sidecarHedKey) {
62+
const sidecarHedString =
63+
sidecarHedTags[sidecarHedColumn][sidecarHedKey]
64+
if (sidecarHedString !== undefined) {
65+
hedStringParts.push(sidecarHedString)
66+
} else {
67+
issues.push(
68+
new Issue({
69+
code: 112,
70+
file: eventFile.file,
71+
evidence: sidecarHedKey,
72+
}),
73+
)
9274
}
9375
}
76+
}
9477

95-
if (hedStringParts.length === 0) {
96-
continue
97-
}
98-
hedStrings.push([file, hedStringParts.join(',')])
78+
if (hedStringParts.length === 0) {
79+
continue
9980
}
81+
hedStrings.push([eventFile.file, hedStringParts.join(',')])
10082
}
10183
})
10284

0 commit comments

Comments
 (0)