Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bval): Handle trailing whitespace without newlines #92

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}