Skip to content

Commit

Permalink
Merge pull request #634 from DaNish808/master
Browse files Browse the repository at this point in the history
validate IntendedFor filetype
  • Loading branch information
nellh authored Oct 23, 2018
2 parents cbcb0d3 + 2c23a3d commit a9844e7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
40 changes: 39 additions & 1 deletion tests/nii.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,44 @@ describe('NIFTI', function() {
})
})

it('should generate warning if files listed in IntendedFor of fieldmap json are not of type .nii or .nii.gz', function() {
var file = {
name: 'sub-09_ses-test_run-01_fieldmap.nii.gz',
path:
'/ds114/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.nii.gz',
relativePath:
'/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.nii.gz',
}

var jsonContentsDict = {
'/sub-09/ses-test/fmap/sub-09_ses-test_run-01_fieldmap.json': {
TaskName: 'Mixed Event Related Probe',
IntendedFor: [
'func/sub-15_task-mixedeventrelatedprobe_run-05_bold.json',
'func/sub-15_task-mixedeventrelatedprobe_run-02_bold.nii.gz',
],
},
}
var fileList = []
fileList.push({
name: 'sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz',
path: 'sub-15/func/sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz',
relativePath:
'/func/sub-15_task-mixedeventrelatedprobe_run-01_bold.nii.gz',
})
validate.NIFTI(null, file, jsonContentsDict, {}, [], [], function(issues) {
assert(
issues.some(
issue =>
issue.reason ===
'Invalid filetype: IntendedFor should point to the .nii[.gz] files.' &&
issue.evidence ===
'func/sub-15_task-mixedeventrelatedprobe_run-05_bold.json',
),
)
})
})

it('should generate warning if files listed in IntendedFor of fieldmap json do not exist', function() {
var file = {
name: 'sub-09_ses-test_run-01_fieldmap.nii.gz',
Expand Down Expand Up @@ -152,7 +190,7 @@ describe('NIFTI', function() {
})
validate.NIFTI(null, file, jsonContentsDict, {}, [], [], function(issues) {
assert(
(issues.length = 2 && issues[0].code == 17 && issues[1].code == 37),
issues.length === 3 && issues[0].code == 17 && issues[1].code == 37,
)
})
})
Expand Down
24 changes: 21 additions & 3 deletions validators/nifti/nii.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ module.exports = function NIFTI(
for (let key = 0; key < intendedFor.length; key++) {
const intendedForFile = intendedFor[key]
checkIfIntendedExists(intendedForFile, fileList, issues, file)
checkIfValidFiletype(intendedForFile, issues, file)
}
}
}
Expand Down Expand Up @@ -485,9 +486,11 @@ function checkIfIntendedExists(intendedForFile, fileList, issues, file) {
let onTheList = false

for (let key2 in fileList) {
const filePath = fileList[key2].relativePath
if (filePath === intendedForFileFull) {
onTheList = true
if (key2) {
const filePath = fileList[key2].relativePath
if (filePath === intendedForFileFull) {
onTheList = true
}
}
}
if (!onTheList) {
Expand All @@ -509,3 +512,18 @@ function checkIfIntendedExists(intendedForFile, fileList, issues, file) {
)
}
}

function checkIfValidFiletype(intendedForFile, issues, file) {
const validFiletype = new RegExp('.nii(.gz)?$')
const isValidFiletype = validFiletype.test(intendedForFile)
if (!isValidFiletype) {
issues.push(
new Issue({
file: file,
code: 37,
reason: `Invalid filetype: IntendedFor should point to the .nii[.gz] files.`,
evidence: intendedForFile,
}),
)
}
}

0 comments on commit a9844e7

Please sign in to comment.