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

RF: Parse bval/bvec files uniformly, add n_rows attribute to associations #1813

Merged
merged 1 commit into from
Sep 28, 2023
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
13 changes: 3 additions & 10 deletions bids-validator/src/files/dwi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@
const normalizeEOL = (str: string): string =>
str.replace(/\r\n/g, '\n').replace(/\r/g, '\n')

export function parseBval(contents: string): number[][] {
// BVAL files are a single row of numbers, and may contain
// trailing whitespace
return [contents
.split(/\s+/)
.filter((x) => x !== '')
.map((x) => Number(x))]
}

export function parseBvec(contents: string): number[][] {
export function parseBvalBvec(contents: string): number[][] {
// BVEC files are a matrix of numbers, with each row being
// a different axis
// BVAL files are a single row of numbers, and may contain
// trailing whitespace
return normalizeEOL(contents)
.split(/\s*\n/)
.filter((x) => x !== '')
Expand Down
8 changes: 5 additions & 3 deletions bids-validator/src/schema/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FileTree } from '../types/filetree.ts'
import { BIDSContext } from './context.ts'
import { readEntities } from './entities.ts'
import { parseTSV } from '../files/tsv.ts'
import { parseBval, parseBvec } from '../files/dwi.ts'
import { parseBvalBvec } from '../files/dwi.ts'

// type AssociationsLookup = Record<keyof ContextAssociations, { extensions: string[], inherit: boolean, load: ... }

Expand Down Expand Up @@ -82,10 +82,11 @@ const associationLookup = {
inherit: true,
load: async (file: BIDSFile): Promise<ContextAssociations['bval']> => {
const contents = await file.text()
const columns = parseBval(contents)
const columns = parseBvalBvec(contents)
return {
path: file.path,
n_cols: columns ? columns[0].length : 0,
n_rows: columns ? columns.length : 0,
}
},
},
Expand All @@ -95,10 +96,11 @@ const associationLookup = {
inherit: true,
load: async (file: BIDSFile): Promise<ContextAssociations['bvec']> => {
const contents = await file.text()
const columns = parseBvec(contents)
const columns = parseBvalBvec(contents)
return {
path: file.path,
n_cols: columns ? columns[0].length : 0,
n_rows: columns ? columns.length : 0,
}
},
},
Expand Down
2 changes: 2 additions & 0 deletions bids-validator/src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ export interface ContextAssociationsMagnitude1 {
export interface ContextAssociationsBval {
path: string
n_cols: number
n_rows: number
}
export interface ContextAssociationsBvec {
path: string
n_cols: number
n_rows: number
}
export interface ContextAssociationsChannels {
path?: string
Expand Down
Loading