Skip to content

Commit

Permalink
RF: Parse bval/bvec files uniformly, add n_rows attribute to associat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
effigies committed Sep 28, 2023
1 parent 6789e4d commit 0f273ff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
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

0 comments on commit 0f273ff

Please sign in to comment.