Skip to content

Commit 2c0a610

Browse files
authored
Merge pull request #843 from bids-standard/revise-subjectmetadata
change subjectMetadata to array
2 parents f552be1 + 4ca3000 commit 2c0a610

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

bids-validator/tests/bids.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ describe('BIDS example datasets ', function() {
117117
var warnings = issues.warnings
118118
assert(summary.sessions.length === 0)
119119
assert(summary.subjects.length === 1)
120-
assert.deepEqual(summary.subjectMetadata, {
121-
'01': {
122-
sex: 'M',
120+
assert.deepEqual(summary.subjectMetadata, [
121+
{
122+
participantId: '01',
123123
age: 25,
124+
sex: 'M',
124125
},
125-
})
126+
])
126127
assert.deepEqual(summary.tasks, ['rhyme judgment'])
127128
assert.isFalse(summary.dataProcessed)
128129
assert(summary.modalities.includes('T1w'))

bids-validator/utils/__tests__/collectSubjectMetadata.spec.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ sub-01 34 F
88
sub-02 38 M
99
`
1010
const subjectMetadata = collectSubjectMetadata(tsvFile)
11-
assert.deepEqual(subjectMetadata, {
12-
'01': {
13-
age: 34,
14-
sex: 'F',
15-
},
16-
'02': {
17-
age: 38,
18-
sex: 'M',
19-
},
11+
assert.lengthOf(subjectMetadata, 2)
12+
assert.deepEqual(subjectMetadata[0], {
13+
participantId: '01',
14+
age: 34,
15+
sex: 'F',
2016
})
2117
})
2218
it('extracts tsv string to subjectMetadata object', () => {

bids-validator/utils/summary/collectSubjectMetadata.js

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
const PARTICIPANT_ID = 'participantId'
2+
const AGE = 'age'
13
/**
2-
* Go from tsv format string with participant_id as a required header to object of form
3-
* {
4-
* participant_id_1: {
4+
* Go from tsv format string with participant_id as a required header to array of form
5+
* [
6+
* {
7+
* participantId: 'participant_id_1'
58
* foo: 'x',
69
* ...
710
* },
8-
* participant_id_2: {
11+
* {
12+
* participantId: 'participant_id_2'
913
* foo: 'y',
1014
* ...
1115
* }
1216
* ...
13-
* }
17+
* ]
1418
*
1519
* returns null if participant_id is not a header or file contents do not exist
1620
* @param {string} participantsTsvContent
@@ -21,29 +25,44 @@ const collectSubjectMetadata = participantsTsvContent => {
2125
.split('\n')
2226
.filter(row => row !== '')
2327
.map(row => row.split('\t'))
24-
const [headers, ...subjectData] = contentTable
25-
const participant_idIndex = headers.findIndex(
26-
header => header === 'participant_id',
28+
const [snakeCaseHeaders, ...subjectData] = contentTable
29+
const headers = snakeCaseHeaders.map(header =>
30+
header === 'participant_id' ? PARTICIPANT_ID : header,
2731
)
28-
if (participant_idIndex === -1) return null
32+
const targetKeys = [PARTICIPANT_ID, 'age', 'sex', 'group']
33+
.map(key => ({
34+
key,
35+
index: headers.findIndex(targetKey => targetKey === key),
36+
}))
37+
.filter(({ index }) => index !== -1)
38+
const participantIdKey = targetKeys.find(
39+
({ key }) => key === PARTICIPANT_ID,
40+
)
41+
const ageKey = targetKeys.find(({ key }) => key === AGE)
42+
if (participantIdKey === undefined) return null
2943
else
30-
return subjectData.reduce(
31-
(subjectMetadata, data) => ({
32-
...subjectMetadata,
33-
[data[participant_idIndex].replace(/^sub-/, '')]: data.reduce(
34-
(subjectMetadata, datum, i) =>
35-
i === participant_idIndex
36-
? subjectMetadata
37-
: {
38-
...subjectMetadata,
39-
[headers[i]]:
40-
headers[i] === 'age' ? parseInt(datum) : datum,
41-
},
44+
return subjectData
45+
.map(data => {
46+
// this first map is for transforming any data coming out of participants.tsv:
47+
// strip subject ids to match metadata.subjects: 'sub-01' -> '01'
48+
data[participantIdKey.index] = data[participantIdKey.index].replace(
49+
/^sub-/,
50+
'',
51+
)
52+
// make age an integer
53+
if (ageKey) data[ageKey.index] = parseInt(data[ageKey.index])
54+
return data
55+
})
56+
.map(data =>
57+
//extract all target metadata for each subject
58+
targetKeys.reduce(
59+
(subject, { key, index }) => ({
60+
...subject,
61+
[key]: data[index],
62+
}),
4263
{},
4364
),
44-
}),
45-
{},
46-
)
65+
)
4766
}
4867
}
4968

0 commit comments

Comments
 (0)