Skip to content

Commit

Permalink
Reorganize HED wrapper code
Browse files Browse the repository at this point in the history
  • Loading branch information
happy5214 committed Aug 18, 2023
1 parent 7b02144 commit 86b236c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 56 deletions.
17 changes: 8 additions & 9 deletions bids-validator/validators/bids/fullTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import bval from '../bval'
import bvec from '../bvec'
import microscopy from '../microscopy'
import Events from '../events'
import hed from '../hed'
import { session } from '../session'
import checkAnyDataPresent from '../checkAnyDataPresent'
import headerFields from '../headerFields'
Expand Down Expand Up @@ -215,17 +216,15 @@ const fullTest = (fileList, options, annexed, dir, schema, callback) => {

// Events validation
stimuli.directory = files.stimuli
return Events.validateEvents(
events,
stimuli,
headers,
jsonContentsDict,
jsonFiles,
dir,
self.issues = self.issues.concat(
Events.validateEvents(events, stimuli, headers, jsonContentsDict),
)

// check the HED strings
return hed(tsvs, jsonContentsDict, jsonFiles, dir)
})
.then((eventsIssues) => {
self.issues = self.issues.concat(eventsIssues)
.then((hedIssues) => {
self.issues = self.issues.concat(hedIssues)

// Validate custom fields in all TSVs and add any issues to the list
self.issues = self.issues.concat(
Expand Down
16 changes: 2 additions & 14 deletions bids-validator/validators/events/events.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
/* eslint-disable no-unused-vars */
import hed from './hed'

import utils from '../../utils'
const Issue = utils.issues.Issue

export default function (
events,
stimuli,
headers,
jsonContents,
jsonFiles,
dir,
) {
const issues = []
export default function (events, stimuli, headers, jsonContents) {
// check that all stimuli files present in /stimuli are included in an _events.tsv file
const stimuliIssues = checkStimuli(stimuli)

// check the events file for suspiciously long or short durations
const designIssues = checkDesignLength(events, headers, jsonContents)

// check the HED strings
return hed(events, jsonContents, jsonFiles, dir).then((hedIssues) => {
return issues.concat(stimuliIssues, designIssues, hedIssues)
})
return [].concat(stimuliIssues, designIssues)
}

const checkStimuli = function (stimuli) {
Expand Down
2 changes: 0 additions & 2 deletions bids-validator/validators/events/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import events from './events'
import hed from './hed'
import validate from './validate'

export default {
events: events,
hed: hed,
validateEvents: validate,
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import hedValidator from 'hed-validator'
import cloneDeep from 'lodash/cloneDeep'
import intersection from 'lodash/intersection'
import utils from '../../utils'
import parseTsv from '../tsv/tsvParser'
import utils from '../utils'

const Issue = utils.issues.Issue

export default function checkHedStrings(events, jsonContents, jsonFiles, dir) {
const eventData = constructEventData(events, jsonContents)
const sidecarData = constructSidecarData(events, jsonContents, jsonFiles)
const hedDataExists = detectHed(eventData, sidecarData)
export default function checkHedStrings(tsvs, jsonContents, jsonFiles, dir) {
const tsvData = constructTsvData(tsvs, jsonContents)
const sidecarData = constructSidecarData(tsvData, jsonContents, jsonFiles)
const hedDataExists = detectHed(tsvData, sidecarData)
if (!hedDataExists) {
return Promise.resolve([])
}

const datasetDescription = jsonContents['/dataset_description.json']
const datasetDescriptionData = new hedValidator.validator.BidsJsonFile(
'/dataset_description.json',
datasetDescription,
cloneDeep(datasetDescription),
getSidecarFileObject('/dataset_description.json', jsonFiles),
)
const dataset = new hedValidator.validator.BidsDataset(
eventData,
tsvData,
sidecarData,
datasetDescriptionData,
dir,
Expand All @@ -43,61 +43,65 @@ export default function checkHedStrings(events, jsonContents, jsonFiles, dir) {
}
}

function constructEventData(events, jsonContents) {
return events.map((eventFile) => {
function constructTsvData(tsvFiles, jsonContents) {
return tsvFiles.map((tsvFile) => {
const potentialSidecars = utils.files.potentialLocations(
eventFile.path.replace('.tsv', '.json'),
tsvFile.file.relativePath.replace('.tsv', '.json'),
)
const mergedDictionary = utils.files.generateMergedSidecarDict(
potentialSidecars,
jsonContents,
)
const parsedTsv = parseTsv(eventFile.contents)
const file = eventFile.file
return new hedValidator.validator.BidsEventFile(
eventFile.path,
let TsvFileClass
if (tsvFile.file.relativePath.endsWith('_events.tsv')) {
TsvFileClass = hedValidator.bids.BidsEventFile
} else {
TsvFileClass = hedValidator.bids.BidsTabularFile
}
return new TsvFileClass(
tsvFile.path,
potentialSidecars,
mergedDictionary,
parsedTsv,
file,
tsvFile.contents,
tsvFile.file,
)
})
}

function constructSidecarData(eventData, jsonContents, jsonFiles) {
function constructSidecarData(tsvData, jsonContents, jsonFiles) {
const actualSidecarNames = Object.keys(jsonContents)
let potentialEventSidecars = []
for (const eventFileData of eventData) {
potentialEventSidecars = potentialEventSidecars.concat(
eventFileData.potentialSidecars,
)
const potentialSidecars = []
for (const tsvFileData of tsvData) {
potentialSidecars.push(...tsvFileData.potentialSidecars)
}
const actualEventSidecars = intersection(
actualSidecarNames,
potentialEventSidecars,
potentialSidecars,
)
return actualEventSidecars.map((sidecarName) => {
return new hedValidator.validator.BidsSidecar(
sidecarName,
jsonContents[sidecarName],
cloneDeep(jsonContents[sidecarName]),
getSidecarFileObject(sidecarName, jsonFiles),
)
})
}

function getSidecarFileObject(sidecarName, jsonFiles) {
return jsonFiles.filter((file) => {
return file.relativePath === sidecarName
})[0]
return cloneDeep(
jsonFiles.filter((file) => {
return file.relativePath === sidecarName
})[0],
)
}

function detectHed(eventData, sidecarData) {
function detectHed(tsvData, sidecarData) {
return (
sidecarData.some((sidecarFileData) => {
return Object.values(sidecarFileData.sidecarData).some(sidecarValueHasHed)
}) ||
eventData.some((eventFileData) => {
return eventFileData.parsedTsv.headers.indexOf('HED') !== -1
tsvData.some((tsvFileData) => {
return tsvFileData.parsedTsv.headers.indexOf('HED') !== -1
})
)
}
Expand Down

0 comments on commit 86b236c

Please sign in to comment.