The BIDS Template File
Introduction
This article describes the BIDS template file. See the sections below to learn more about each attribute.
Instruction Steps
Example BIDS Template
Below is an example of the bids-v1.json
template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
|
Namespace
At the top level of the template JSON file is a namespace
attribute. This is where all of the metadata for that template will be stored. The only valid value for namespace is BIDS.
Definitions
The objects under the definitions object of the template JSON file are JSON schemas that support local referencing of other definitions using the $ref
attribute. Typically, there will be a few property definitions at the top of the definitions, followed by a few templates.
A property definition may look something like this:
The above example is describing the Run
attribute that is commonly used for acquisitions. It is saying that the info.BIDS.Run
attribute must be a string. The pattern
property indicates that any characters must be digits.
A template definition simply describes the properties that are expected to be found in BIDS metadata that matches this template. Note that Acq
and Run
are references to common properties, and that filename
and modality
are required fields for this template. A template definition may look something like this:
NOTE: The auto_update properties are truncated here and will be covered in the next section.
Auto Updates
There are 2 options for auto updates: string or object
String
When the template engine is running in update mode, any field in a template definition that has an auto_update
property will automatically be updated by formatting the auto_update
string using the current context. There are a few special rules to be aware of in the string formatting:
- Normalized Substitution; Variables in angle brackets “<>” will be converted to lower camel case (removing the spaces) before being substituted in the string.
- Literal Substitution: Variables in curly brackets “{}” will not be modified before being substituted in the string.
- Optional Values: Square brackets “[]” indicate that a section is optional. These sections will only be present if the referenced field has a non-empty value.
For example: sub-<subject.code>[/ses-<session.label>]/{file.info.BIDS.Folder}
In the above example, subject.code
is required, and will be converted to lower camel case before being substituted. session.label
is optional, and if present, the section in brackets will be present, and session.label
will be converted to lower camel case. Finally, file.info.BIDS.Folder
is required, and will not be modified before being substituted.
Object
In addition to using a string to auto update certain fields, it is possible to set up more powerful formatting auto update using an object. For example
According to the above example, the Label
field would be updated to the session. label
after being processed as a string according to the rules in the above section, and then formatted to be in lower case. For more information on the format field, check the format section.
Rules
Rules provide logic for matching a context to a template definition, and in some cases, they also provide logic for initializing some of the fields from the context.
An example rule:
- id: The above rule specifies an
id
, which is optional in custom templates but required in predefined templates. The ruleid
can be used to reference the rule in other areas of the template. The template field indicates what template this rule will apply if all of the conditions in thewhere
property are true. - where: The
where
property is a set of conditions that must all be true for the rule to match. The general pattern is that the keys are the names of properties to check, and the values are either the value (e.g. incontainer_type
) or further details about how to match (e.g. the special property$in
infile. type
indicates that the measurements field should be one of the values in the specified array). See the match and initialized conditions below for all possible rule conditions. - Initialize: The initialize property is a list of rules for initializing properties within the BIDS info object. It consists of a set of property names (the keys) and rules for initializing them (the values.) In the above case, we are initializing
info.BIDS.Run
fromacquisition.label
using a regular expression to extract the run number.
The initialized values can further be formatted using regex patterns and replacement strings. The formatting rules are an ordered list that are always executed last, allowing for a procedural execution of $format
commands.
A full list of possible rule conditions:
Match Conditions
In addition to matching an exact value, the following keywords are currently supported in rule matches
**$in: [<values>]**
- Matches if the specified property is an exact match for any value in the list.**$not: <rule or value>**
- Negates the specified rule or value match.$regex: “regex”
- Matches if the specified regular expression is found in the value string. Use the ^ and $ operators to specify whole string matches.
Initialized Conditions
Initialization can be done one of 3 ways:
$regex: “regex”
- Initializes the property using the value group of the regular expression. For example:_run-(?P<value>\\d+)
will match any digit characters after the string“_run-”
**$take: true**
- Initializes the property by taking the value specified.$switch {cases}
: - Allows mapping from one value to another via case statements. For example:
Matches the $on
field, in this case file.info.ImageType
and uses set comparison against each case, in order, taking the first match. If $default
is true, then that case always matches. Note: Value / String comparison is also supported.
Upload Rules
A template may also include a set of rules under the field upload_rules
. These rules will only used when using bids import, which does not support custom templates.
Custom Project Template
Projects can provide a custom template for BIDS curation by uploading their template as project-template.json
as a file attachment to the project. The project-template.json
can either be a completely new template, or it can extend from an existing template. The expected common use case for custom templates is to provide additional matching or initialization rules based on the structure of the project in question.
Here's an example of extending an existing template:
The only differences between this template and a full template are, the extends property, indicating what template to base this template off of, and the exclude_rules
property, that allows the exclusion of rules (by id) from the parent template. Otherwise, new definitions and rules can be specified as in a normal template.
Custom Initializers
As of version 0.3.3 of the curate-bids gear, custom project templates support adding additional initializers to a rule without having to redefine the rule. For example:
The above template will match acquisition labels such as red_green1
and extract a task (removing disallowed characters) and a run number, such that the task would be redgreen
and the run number would be 1
.
Format
Allow a pipeline of string formatting. The format field should be an array of the above operations, the order is preserved and is the order in which the operations are executed:
- Replace:
{“$replace”: {“$pattern”: “[a-z]”, “$replacement”: “Character”}}
- Lower case:
{“$lower”: {“$pattern”: “[a-z]”} or {“$lower”: true}
- Upper case:
{“$upper”: {“$pattern”: “[a-z]”} or {“$upper”: true}
- Camel case:
{“$camelCase”: {“$pattern”: “[a-z]”} or {“$camelCase”: true}
Field map IntendedFor
fields
Within the BIDS specification, field maps should have an IntendedFor field in their sidecar that indicates (via list of local filenames) what MRI scans the field maps are intended to correct. On Flywheel, this is done by setting metadata (also called "Custom Information") on a field map NIfTI file that is a list called file.info.IntendedFor
.
The template processing engine sets what the BIDS specification requires, file.info.IntendedFor
, by using information provided infile.info.BIDS.IntendedFor
(note the "BIDS" part). The latter, a list of lists, acts as a set of filters on files at the session level that the current field map is intended to correct. If the first filter (list 0) is a list of only one item that is "Folder" = " anat" like this:
It means that all files in the BIDS anat
folder will be included in the file.info.IntendedFor
list on the field map. To filter the list of files more narrowly, for instance if you wanted to target a single fMRI run, you could specify something like this:
Here the file.info.BIDS.IntendedFor
list also has "Run" = "1" so only the files in the anat
folder that also have "run-1" in their BIDS name will be included. The next item further filters the list to only the files that also have "task-redgreen" in their BIDS file names. Additional filters (more than one list of lists) can cause the same field map to be applied to multiple BIDS directories (anat, dwi, func).
For projects that have consistent sessions, it is best practice to configure a custom template to initialize the file.info.IntendedFor
field for each field map in the protocol. The custom initializers mentioned above also support conditional matching the same as other rules, so specific initialization can be done based on rule matching. For example:
Resolvers
The information in file.info.BIDS.IntendedFor
is set by rules in the project curation template that recognize and initialize field maps. This information is then used by " resolvers" to determine file.info.IntendedFor
:
Resolvers represent a final additional pass through the context after everything else is done. That is, all of the containers and files have been examined by the rules of the template in the first pass and all of the BIDS metadata fields have been determined by the information in the context. For example, the "Run" attribute has been filled out along with "Task", "Acq", etc. and each NIfTI file's BIDS folder and filename have been determined,and file.info.BIDS.IntendedFor
has been set.
Resolvers are a way to figure out a piece of information that can only be known after the initial pass. Currently, the only example is the "IntendedFor" field as shown above. It says to "update" the metadata file.info.IntendedFor
for anything in the context that is a fieldmap_file
using the "filter" information found in file.info.BIDS.IntendedFor
.
If, for a particular field map filter, file.info.BIDS.IntendedFor
lists "func" as the folder and "2" for the run, the resolver finds all files in the "func" folder that have "Run" = "2" and the result might look like this:
That is, the "format" of the resolver combines the session label, the BIDS folder, and the BIDS filename to produce the relative paths to the files that a particular field map is to modify as required by the BIDS specification.
This is repeated for each of the listed "templates" which are definitions found at the top of the project curation template json file, so any fieldmap_phase_encoded_file
found in the context are resolved after any fieldmap_file
are found.
Just to be clear, note that a field map file.info.BIDS.IntendedFor
is in the "BIDS" name space while the results, file.info.IntendedFor
are in the "file.info" name space similar to DICOM tag values and will therefore end up in the json sidecar for the field map when BIDS formatted data is exported. The "BIDS" name space is only used during project curation template processing and is not exported in the json sidecar.
Template Validation Schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|