Skip to content

Create a Template

Introduction

The template is a set of instructions for how to map your dataset to a Flywheel project.

All projects in Flywheel are made up of subjects, sessions, and acquisitions. Using the template in the config file, leverage the folder and file structure on your local machine to map your dataset to subjects, sessions, and acquisitions. With the template , you can also use folder or filenames as additional metadata fields.

Instruction Steps

Mapping your Dataset Folder Structure to Flywheel

To successfully upload your dataset, you must define the labels for subjects, sessions, and acquisitions. Below is an example of the most basic template for uploading DICOM files.

The steps below explain how to build your own template for labeling subject, sessions, and acquisitions.

1
2
3
4
5
6
7
8
# Template and Group/Project Settings
#####

template:
  - pattern: "{subject}"
  - pattern: "{session}"
  - pattern: "{acquisition}"
      scan: dicom
  1. To begin, open a plain text editor such as TextEdit, Notepad, Sublime, etc.
  2. Copy and paste the first step of your template:

    template:
      - pattern:
    

    This first pattern field corresponds to the top folder of your dataset.

  3. Configure how your first folder and any data inside it will map to the Flywheel project. Below is an overview of the available options. See the reference section at the end of this article for more details.

    • Use the {subject}, {session}, or {acquisition} variables to create labels: You can use all or part of the folder name for the label. To use all of the folder name for the subject label:

      template:
        - pattern: "{subject}"
      

      For example if you had the following folder name it would become a subject label:

      Example folder name Subject label
      sub-01 sub-01

      To use only part of the folder name, replace a portion of the folder with the "{subject}" variable. For example:

      template:
        - pattern: "{subject}-subject-Nov-2021"
      
      Example folder name Subject label
      001-subject-Nov-2021 001
      002- subject-Nov-2021 002

      See all of the default variables available for the template.

    • Add custom metadata: Metadata have many useful applications in Flywheel including being used to properly categorize data, create collections, or curate a dataview. CThe template allows you to use folder or file names as metadata fields. For example:

      template:
        - pattern: "001-subject-{session.info.dataset}"
      

      For example

      Example folder name Metadata
      001-subject-Nov-2021 dataset: Nov-2021

      Note: this metadata field is added to the sessions of your project

    • Skip this folder: Use regex to skip this folder level and move on to the next:

      template:
        - pattern: .*
      
    • Use filenames for labels in Flywheel: Oftentimes the filename has useful information. To leverage the filename for a label, add a scan step for filenames.

      The filename scan allows for regex pattern matching, so you can pull out only the relevant information in each filename. For example:

      1
      2
      3
      4
      5
      template:
        - pattern: "{subject}"
            scan:
            name: filename
            pattern: "(?P<acquisition>)[^.]*"
      

      The above template would take the filename without the extension and make it the acquisition label. For example:

      Example filename Acquisition label
      SAG T1.dicom SAG T1

      Tip

      Use angled brackets <> instead of curly brackets {} when using regex to assign variables. This is a requirement of Python regex.

  4. Add a second -pattern: to the template. This corresponds to the first subfolder of your dataset. The teYou can use any of the above mapping methods. For example:

    1
    2
    3
    template:
      - pattern: "{subject}"
      - pattern: .*
    
  5. Continue for each new subfolder level in your dataset until you reach the last folder you wish to upload. To package files for upload, use packfile_type or scan:

    • Compress all files into a packfile: If you don't have DICOM data, you should still compress all of your images for upload using packfile_type: <filetype>. This creates a single zip file for upload. For example:

      1
      2
      3
      4
      5
      template:
        - pattern: "{subject}"
        - pattern: "{session}"
        - pattern: "{acquisition}"
            packfile_type: jpg 
      

      or

      1
      2
      3
      4
      5
      6
      template:
        - pattern: "{subject}-subject-Nov-2021"
        - pattern: .*
        - pattern: "amyg_s*_amyg_{session}_{subject.info.pcol}"
        - pattern: "{acquisition}"
            packfile_type: dicom
      
    • Organize DICOM files based on metadata: If you have DICOM data and you would like to organize data using the seriesinstanceuid tag, use the dicom scan step:

      1
      2
      3
      template:
        - pattern: "{acquisition}"
            scan: dicom
      

      When dicom scan is enabled, Flywheel parses all files with the .dcm extension in that folder. Flywheel pulls out relevant metadata from the DICOM files to use as Flywheel metadata and compresses all data into a zip file for upload.

      If the file is not a valid DICOM file, the file is not uploaded and the import stops by default. To determine if a file is valid DICOM, we look for a DICM string at byte 128. However, you can use the --ignore-scan flag in your CLI command to set it so that Flywheel only ignores the invalid DICOM file and continues to upload valid files.

    • "Select" folders for upload: When there are multiple folders at the same level, you can use select: to set different rules for each folder:

      1
      2
      3
      4
      5
      6
      template:
        - pattern: "{subject}-{session}-{acquisition}"
        - select:
          - pattern: "*.dcm"
          packfile_type: dicom
          - pattern: .*
      

      This example only scans files that end in the common DICOM file extension, .dcm.

  6. Review your template. Verify that you have added each of these variables once:

    • {subject}
    • {session}
    • {acquisition}
  7. Verify that your template is valid YAML using an online tool such as YAML lint.

Reference

For more information about the various options available when creating an Ingest Template, see the Ingest Template Reference documentation.