Skip to content

Commit

Permalink
Merge pull request #92 from effigies/fix/bval-bvec
Browse files Browse the repository at this point in the history
fix(bval): Handle trailing whitespace without newlines
  • Loading branch information
rwblair authored Nov 7, 2024
2 parents 4223800 + f2ca92b commit dd8a5b3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/files/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export async function readFileTree(rootPath: string): Promise<FileTree> {
)
ignore.add(await readBidsIgnore(ignoreFile))
} catch (err) {
if (err && typeof err === 'object' && !('code' in err && err.code !== 'ENOENT')) {
if (err && typeof err === 'object' && !('code' in err && err.code === 'ENOENT')) {
logger.error(`Failed to read '.bidsignore' file with the following error:\n${err}`)
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/files/dwi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { assertEquals } from '@std/assert'

import { parseBvalBvec } from './dwi.ts'

Deno.test('Test bval/bvec parsing', async (t) => {
await t.step('Load 3 bvals', async () => {
const bvals = parseBvalBvec('0 1 2 \n') // Typically ends with " \n"
assertEquals(bvals, [['0', '1', '2']])
})
await t.step('Load 3 bvals - missing newline', async () => {
const bvals = parseBvalBvec('0 1 2 ')
assertEquals(bvals, [['0', '1', '2']])
})
await t.step('Load 3 bvals - no spaces', async () => {
const bvals = parseBvalBvec('0 1 2')
assertEquals(bvals, [['0', '1', '2']])
})
await t.step('Load 3 bvecs', async () => {
const bvecs = parseBvalBvec('0 1 2 \n0 1 2 \n0 1 2 \n')
assertEquals(bvecs, [['0', '1', '2'], ['0', '1', '2'], ['0', '1', '2']])
})
await t.step('Load 3 bvals - missing newline', async () => {
const bvecs = parseBvalBvec('0 1 2 \n0 1 2 \n0 1 2 ')
assertEquals(bvecs, [['0', '1', '2'], ['0', '1', '2'], ['0', '1', '2']])
})
await t.step('Load 3 bvals - no spaces', async () => {
const bvecs = parseBvalBvec('0 1 2\n0 1 2\n0 1 2\n')
assertEquals(bvecs, [['0', '1', '2'], ['0', '1', '2'], ['0', '1', '2']])
})
await t.step('Load 3 bvals - no spaces, missing newline', async () => {
const bvecs = parseBvalBvec('0 1 2\n0 1 2\n0 1 2')
assertEquals(bvecs, [['0', '1', '2'], ['0', '1', '2'], ['0', '1', '2']])
})
})

4 changes: 2 additions & 2 deletions src/files/dwi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function parseBvalBvec(contents: string): string[][] {
// BVAL files are a single row of numbers, and may contain
// trailing whitespace
return normalizeEOL(contents)
.split(/\s*\n/) // Split on newlines, ignoring trailing whitespace
.split(/\n/) // Split on newlines
.filter((x) => x.match(/\S/)) // Remove empty lines
.map((row) => row.split(/\s+/))
.map((row) => row.trimEnd().split(/\s+/)) // Split on whitespace, ignoring trailing
}

0 comments on commit dd8a5b3

Please sign in to comment.