mrsi_segment 2.65 KB
Newer Older
1
2
#!/usr/bin/env python

William Clarke's avatar
William Clarke committed
3
# mrsi_segment - use fsl to segment a T1 and register it to an mrsi scan
4
5
6
7
#
# Author: Saad Jbabdi <saad@fmrib.ox.ac.uk>
#         William Clarke <william.clarke@ndcn.ox.ac.uk>
#
William Clarke's avatar
William Clarke committed
8
# Copyright (C) 2020 University of Oxford
9
10
11
12
13
14
# SHBASECOPYRIGHT

# Quick imports
import argparse
import os.path as op
from os import remove
William Clarke's avatar
William Clarke committed
15
from fsl.wrappers import fsl_anat
William Clarke's avatar
William Clarke committed
16
17
18
from fsl.wrappers.fnirt import applywarp
import numpy as np
from fsl.data.image import Image
William Clarke's avatar
William Clarke committed
19

20
21
22

def main():
    # Parse command-line arguments
William Clarke's avatar
William Clarke committed
23
24
25
    parser = argparse.ArgumentParser(
            description="FSL Magnetic Resonance Spectroscopy"
                        " - register fast segmentation to mrsi.")
26

William Clarke's avatar
William Clarke committed
27
28
    parser.add_argument('mrsi', type=str, metavar='MRSI',
                        help='MRSI nifti file')
29
    group = parser.add_mutually_exclusive_group(required=True)
William Clarke's avatar
William Clarke committed
30
31
32
33
34
35
36
37
    group.add_argument('-t', '--t1', type=str, metavar='T1',
                       help='T1 nifti file')
    group.add_argument('-a', '--anat', type=str,
                       help='fsl_anat output directory.')
    parser.add_argument('-o', '--output', type=str,
                        help='Output directory', default='.')
    parser.add_argument('-f', '--filename', type=str,
                        help='Output file name', default='mrsi_seg')
38
39
    args = parser.parse_args()

William Clarke's avatar
William Clarke committed
40
    # If not prevented run fsl_anat for fast segmentation
41
    if (args.anat is None) and (not args.mask_only):
William Clarke's avatar
William Clarke committed
42
43
        anat = op.join(args.output, 'fsl_anat')
        fsl_anat(args.t1, out=anat, nosubcortseg=True)
44
45
46
47
48
        anat += '.anat'
    else:
        anat = args.anat

    # Make dummy nifti as nothing works with complex data
William Clarke's avatar
William Clarke committed
49
50
51
52
    mrsi_in = Image(args.mrsi)
    tmp_img = np.zeros(mrsi_in.shape[0:3])
    tmp_img = Image(tmp_img, xform=mrsi_in.voxToWorldMat)
    tmp_img.save(op.join(args.output, 'tmp.nii.gz'))
53
54

    # Register the pvseg to the MRSI data using flirt
William Clarke's avatar
William Clarke committed
55
56
57
58
59
60
61
    def applywarp_func(i, o):
        applywarp(i,
                  op.join(args.output, 'tmp.nii.gz'),
                  o,
                  usesqform=True,
                  super=True,
                  superlevel='a')
William Clarke's avatar
William Clarke committed
62
63
64

    # T1_fast_pve_0, T1_fast_pve_1, T1_fast_pve_2
    # partial volume segmentations (CSF, GM, WM respectively)
William Clarke's avatar
William Clarke committed
65
66
67
68
69
70
    applywarp_func(op.join(anat, 'T1_fast_pve_0.nii.gz'),
                   op.join(args.output, args.filename+'_csf.nii.gz'))
    applywarp_func(op.join(anat, 'T1_fast_pve_1.nii.gz'),
                   op.join(args.output, args.filename+'_gm.nii.gz'))
    applywarp_func(op.join(anat, 'T1_fast_pve_2.nii.gz'),
                   op.join(args.output, args.filename+'_wm.nii.gz'))
71

William Clarke's avatar
William Clarke committed
72
    remove(op.join(args.output, 'tmp.nii.gz'))
73
74
75


if __name__ == '__main__':
William Clarke's avatar
William Clarke committed
76
    main()