Skip to content

Commit

Permalink
Merge pull request bids-standard#2054 from effigies/feat/file-relative
Browse files Browse the repository at this point in the history
feat: Add `exists(..., 'file')` for file-relative paths
  • Loading branch information
rwblair authored Aug 5, 2024
2 parents 3a7335d + 5ece19e commit d778802
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 9 additions & 4 deletions bids-validator/src/schema/expressionLanguage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ Deno.test('test expression functions', async (t) => {
assert(count(['a', 'b', 'a', 'b'], 'a') === 2)
assert(count(['a', 'b', 'a', 'b'], 'c') === 0)
})

const exists = expressionFunctions.exists.bind(context)
await t.step('exists(..., "dataset") function', () => {
const exists = expressionFunctions.exists.bind(context)
assert(exists([], 'dataset') === 0)
assert(
exists(['sub-01/ses-01/anat/sub-01_ses-01_T1w.nii.gz'], 'dataset') === 1,
Expand All @@ -105,7 +106,6 @@ Deno.test('test expression functions', async (t) => {
)
})
await t.step('exists(..., "subject") function', () => {
const exists = expressionFunctions.exists.bind(context)
assert(exists([], 'subject') === 0)
assert(exists(['ses-01/anat/sub-01_ses-01_T1w.nii.gz'], 'subject') === 1)
assert(
Expand All @@ -115,15 +115,19 @@ Deno.test('test expression functions', async (t) => {
) === 1,
)
})
await t.step('exists(..., "file") function', () => {
assert(exists([], 'file') === 0)
assert(exists(['sub-01_ses-01_T1w.nii.gz'], 'file') === 1)
assert(exists(['sub-01_ses-01_T1w.nii.gz', 'sub-01_ses-01_T1w.json'], 'file') === 2)
assert(exists(['sub-01_ses-01_T1w.nii.gz', 'ses-01_T1w.json'], 'file') === 1)
})
await t.step('exists(..., "stimuli") function', () => {
const exists = expressionFunctions.exists.bind(context)
assert(exists([], 'stimuli') === 0)
assert(exists(['stimfile1.png'], 'stimuli') === 1)
assert(exists(['stimfile1.png', 'stimfile2.png'], 'stimuli') === 2)
assert(exists(['X.png', 'Y.png'], 'stimuli') === 0)
})
await t.step('exists(..., "bids-uri") function', () => {
const exists = expressionFunctions.exists.bind(context)
assert(exists([], 'subject') === 0)
assert(
exists(
Expand All @@ -140,6 +144,7 @@ Deno.test('test expression functions', async (t) => {
// Not yet implemented; currently returns length of array
// assert(exists(['bids::sub-01/ses-01/anat/sub-01_ses-01_T1w.nii.gz', 'bids::T2w.json'], 'bids-uri') === 1)
})

await t.step('substr function', () => {
const substr = expressionFunctions.substr
assert(substr('abc', 0, 1) === 'a')
Expand Down
9 changes: 5 additions & 4 deletions bids-validator/src/schema/expressionLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
function exists(list: string[], rule: string = 'dataset'): number {
import { BIDSContext } from './context.ts'

function exists(this: BIDSContext, list: string[], rule: string = 'dataset'): number {
if (list == null) {
return 0
}

const prefix: string[] = []
const fileTree = rule == 'file' ? this.file.parent : this.fileTree

// Stimuli and subject-relative paths get prefixes
if (rule == 'stimuli') {
prefix.push('stimuli')
} else if (rule == 'subject') {
// @ts-expect-error
prefix.push('sub-' + this.entities.sub)
}

Expand All @@ -28,8 +30,7 @@ function exists(list: string[], rule: string = 'dataset'): number {
// dataset, subject and stimuli
return list.filter((x) => {
const parts = prefix.concat(x.split('/'))
// @ts-expect-error
return this.fileTree.contains(parts)
return fileTree.contains(parts)
}).length
}
}
Expand Down

0 comments on commit d778802

Please sign in to comment.