@@ -5,16 +5,26 @@ import type { BIDSFile } from '../types/filetree.ts'
55import type { BIDSContext } from './context.ts'
66import { loadTSV } from '../files/tsv.ts'
77import { parseBvalBvec } from '../files/dwi.ts'
8- import { walkBack } from '../files/inheritance.ts'
8+ import { readSidecars , walkBack } from '../files/inheritance.ts'
99import { evalCheck } from './applyRules.ts'
1010import { expressionFunctions } from './expressionLanguage.ts'
1111
1212import { readText } from '../files/access.ts'
1313
14+ interface WithSidecar {
15+ sidecar : Record < string , unknown >
16+ }
17+
1418function defaultAssociation ( file : BIDSFile , _options : any ) : Promise < { path : string } > {
1519 return Promise . resolve ( { path : file . path } )
1620}
1721
22+ async function constructSidecar ( file : BIDSFile ) : Promise < Record < string , unknown > > {
23+ const sidecars = await readSidecars ( file )
24+ // Note ordering here gives precedence to the more specific sidecar
25+ return sidecars . values ( ) . reduce ( ( acc , json ) => ( { ...json , ...acc } ) , { } )
26+ }
27+
1828/**
1929 * This object describes lookup functions for files associated to data files in a bids dataset.
2030 * For any given data file we iterate over the associations defined schema.meta.associations.
@@ -24,14 +34,15 @@ function defaultAssociation(file: BIDSFile, _options: any): Promise<{ path: stri
2434 * Many associations only consist of a path; this object is for more complex associations.
2535 */
2636const associationLookup = {
27- events : async ( file : BIDSFile , options : { maxRows : number } ) : Promise < Events > => {
37+ events : async ( file : BIDSFile , options : { maxRows : number } ) : Promise < Events & WithSidecar > => {
2838 const columns = await loadTSV ( file , options . maxRows )
2939 . catch ( ( e ) => {
3040 return new Map ( )
3141 } )
3242 return {
3343 path : file . path ,
3444 onset : columns . get ( 'onset' ) || [ ] ,
45+ sidecar : await constructSidecar ( file ) ,
3546 }
3647 } ,
3748 aslcontext : async (
@@ -85,6 +96,12 @@ const associationLookup = {
8596 sampling_frequency : columns . get ( 'sampling_frequency' ) ,
8697 }
8798 } ,
99+ physio : async ( file : BIDSFile , options : any ) : Promise < { path : string } & WithSidecar > => {
100+ return {
101+ path : file . path ,
102+ sidecar : await constructSidecar ( file ) ,
103+ }
104+ } ,
88105}
89106
90107export async function buildAssociations (
@@ -93,18 +110,12 @@ export async function buildAssociations(
93110 const associations : Associations = { }
94111
95112 const schema : MetaSchema = context . dataset . schema as MetaSchema
96- // Augment rule type with an entities field that should be present in BIDS 1.10.1+
97- type ruleType = MetaSchema [ 'meta' ] [ 'associations' ] [ keyof MetaSchema [ 'meta' ] [ 'associations' ] ]
98- type AugmentedRuleType = ruleType & {
99- target : ruleType [ 'target' ] & { entities ?: string [ ] }
100- }
101113
102114 Object . assign ( context , expressionFunctions )
103115 // @ts -expect-error
104116 context . exists . bind ( context )
105117
106- for ( const key of Object . keys ( schema . meta . associations ) ) {
107- const rule = schema . meta . associations [ key ] as AugmentedRuleType
118+ for ( const [ key , rule ] of Object . entries ( schema . meta . associations ) ) {
108119 if ( ! rule . selectors ! . every ( ( x ) => evalCheck ( x , context ) ) ) {
109120 continue
110121 }
0 commit comments