Create MRI Data Acquisition Methods Writeup

  1. Import BIDS dataset
  2. Loop through sessions (given that different sessions may have different scans/parameters).
  3. Loop through jsons found for each subject within each session.
  4. Select appropriate function in compile_methods for the json's scan type.
  5. Combine all strings returned by functions into one writeup per subject.
  6. Output each unique string, along with count for that string.
    • This allows for different parameters per subject (unlikely), but also for unplanned acquisition deviations (e.g., quitting early) for individual subjects.

TODO: Improve how we loop through sessions/subjects/scans.


In [1]:
import json
import nibabel as nib
from collections import Counter
from bids.grabbids import BIDSLayout
from os.path import join, basename, abspath, splitext

import sys
sys.path.append('/home/data/nbc/misc-bids/')
import compile_methods as cm

In [2]:
# Study-specific dictionary of fMRI task names.
task_converter = {'stopsignal': 'stop signal task',
                  'emotionalnback': 'emotional n-back task',
                  'monetaryincentive': 'monetary incentive task',
                  'rest': 'resting-state'}

In [3]:
# Read in BIDS dataset
bids_dir = '/scratch/tsalo006/abcd_out/'
layout = BIDSLayout(bids_dir)
sessions = layout.get_sessions()

In [4]:
strings = []
for sess in sessions:
    print('For session {0}'.format(sess))
    subjs = layout.get_subjects(session=sess)
    for sid in subjs:
        # Remove potential trailing slash with abspath
        subj_dir = abspath(join(bids_dir, sid))

        # Get json files for field maps
        jsons = layout.get(subject=sid, session=sess, extensions='json')

        l = []
        for J in jsons:
            jf = J.filename
            nf = splitext(jf)[0] + '.nii.gz'
            with open(jf, 'r') as fi:
                jd = json.load(fi)
            nd = nib.load(nf)

            # Assume all data were acquired the same way.
            if not l:
                l.append(cm.general_acquisition_info(jd))

            if J.modality == 'func':
                task = task_converter[J.task]
                n_runs = len(layout.get(subject=sid, session=sess,
                                        extensions='json', task=J.task))
                l.append(cm.functional(task, n_runs, jd, nd))
            elif J.modality == 'anat':
                type_ = J.type[:-1]
                l.append(cm.structural(type_, jd, nd))
            elif J.modality == 'dwi':
                bval_file = splitext(jf)[0]+'.bval'
                l.append(cm.dwi(bval_file, jd, nd))
            elif J.modality == 'fmap':
                l.append(cm.fieldmap(jd, nd, task_converter, subj_dir))

        # Assume all data were converted the same way.
        l.append(cm.final_paragraph(jd))
        l = cm.rem_dupes(l)
        string = '\n\n'.join(l)
        strings.append(string)

    counter = Counter(strings)
    n_total = sum(counter.values())
    print('\tNumber of patterns detected: {0}'.format(len(counter.keys())))
    print('\tOnly showing most common pattern.')
    tup = counter.most_common(1)
    print('\tThe following pattern was detected {0} times '
          'out of {1}:\n\n'.format(tup[0][1], n_total))
    print(tup[0][0])


For session 1
	Number of patterns detected: 2
	Only showing most common pattern.
	The following pattern was detected 4 times out of 5:


MR data were acquired using a 3-Tesla Siemens Prisma MRI scanner.

T1-weighted segmented k-space, spoiled, and MAG prepared gradient recalled and inversion recovery (GR/IR) structural MRI data were collected (256 slices; repetition time, TR=2500ms; echo time, TE=2.9ms; flip angle, FA=8<deg>; field of view, FOV=176x256mm; matrix size=176x256; voxel size=1x1x1mm).

T2-weighted segmented k-space and spoiled spin echo (SE) structural MRI data were collected (256 slices; repetition time, TR=3200ms; echo time, TE=565ms; flip angle, FA=120<deg>; field of view, FOV=176x256mm; matrix size=176x256; voxel size=1x1x1mm).

Segmented k-space and spoiled echo planar (EP) diffusion-weighted (dMRI) data were collected (81 slices in interleaved ascending order; repetition time, TR=4200ms; echo time, TE=89ms; flip angle, FA=90<deg>; field of view, FOV=240x240mm; matrix size=140x140; voxel size=1.71x1.71x1.7mm; b-values of 0, 500, 1000, 2000, and 3000 acquired; 103 diffusion directions; MB factor=3).

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: anterior to posterior; 82 slices; repetition time, TR=12600ms; echo time, TE=89ms; flip angle, FA=90<deg>; field of view, FOV=240x240mm; matrix size=140x140; voxel size=1.71x1.71x1.7mm) was acquired for the first run of the DWI scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: posterior to anterior; 82 slices; repetition time, TR=12600ms; echo time, TE=89ms; flip angle, FA=90<deg>; field of view, FOV=240x240mm; matrix size=140x140; voxel size=1.71x1.71x1.7mm) was acquired for the first run of the DWI scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: anterior to posterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the emotional n-back task BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: posterior to anterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the emotional n-back task BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: anterior to posterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the monetary incentive task BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: posterior to anterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the monetary incentive task BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: anterior to posterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the resting-state BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: anterior to posterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the third and fourth runs of the resting-state BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: posterior to anterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the resting-state BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: posterior to anterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the third and fourth runs of the resting-state BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: anterior to posterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the stop signal task BOLD scan.

A segmented k-space and spoiled echo planar (EP) field map (Phase encoding: posterior to anterior; 60 slices; repetition time, TR=7030ms; echo time, TE=80ms; flip angle, FA=90<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm) was acquired for the first and second runs of the stop signal task BOLD scan.

Two runs of emotional n-back task segmented k-space echo planar (EP) fMRI data were collected (60 slices in interleaved ascending order; repetition time, TR=800ms; echo time, TE=30ms; flip angle, FA=52<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm; MB factor=6). Each run was 4:56 minutes in length, during which 370 functional volumes were acquired.

Two runs of monetary incentive task segmented k-space echo planar (EP) fMRI data were collected (60 slices in interleaved ascending order; repetition time, TR=800ms; echo time, TE=30ms; flip angle, FA=52<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm; MB factor=6). Each run was 5:29 minutes in length, during which 411 functional volumes were acquired.

Four runs of resting-state segmented k-space echo planar (EP) fMRI data were collected (60 slices in interleaved ascending order; repetition time, TR=800ms; echo time, TE=30ms; flip angle, FA=52<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm; MB factor=6). Each run was 5:07 minutes in length, during which 383 functional volumes were acquired.

Two runs of stop signal task segmented k-space echo planar (EP) fMRI data were collected (60 slices in interleaved ascending order; repetition time, TR=800ms; echo time, TE=30ms; flip angle, FA=52<deg>; field of view, FOV=216x216mm; matrix size=90x90; voxel size=2.4x2.4x2.4mm; MB factor=6). Each run was 5:56 minutes in length, during which 445 functional volumes were acquired.

Dicoms were converted to NIfTI-1 format using dcm2niix (v1.0.20170314). This section was (in part) generated automatically using Bids2Methods (0.1).