Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

Browse Source

Preliminary state of ROI script and configuration

Michael Hanke 8 years ago
parent
commit
cf027223e1
2 changed files with 260 additions and 0 deletions
  1. 189 0
      code/stats2rois
  2. 71 0
      code/stats2rois.cfg

+ 189 - 0
code/stats2rois

@@ -0,0 +1,189 @@
+#!/usr/bin/python3
+
+import sys
+from tempfile import mkdtemp
+from os.path import join as _opj
+from os.path import abspath
+from shutil import rmtree
+from configparser import SafeConfigParser
+from subprocess import check_call
+import nibabel as nb
+from scipy.ndimage import label
+import numpy as np
+
+cfg = SafeConfigParser()
+cfg.read(_opj('code', 'stats2rois.cfg'))
+
+subj = int(sys.argv[1])
+subj_code = 'sub-%.2i' % subj
+mode = None
+rois = None
+if len(sys.argv) > 2:
+    mode = 'indi'
+    rois = sys.argv[2:]
+
+
+# encode ROI IDs as powers of 2
+roi_ids = {
+    'rFFA':  int('0000000000001', base=2),  # 1
+    'rOFA':  int('0000000000010', base=2),  # 2
+    'rpSTS': int('0000000000100', base=2),  # 4
+    'rPPA':  int('0000000001000', base=2),  # 8
+    'rEBA':  int('0000000010000', base=2),  # 16
+    'rLOC':  int('0000000100000', base=2),  # 32
+    'lFFA':  int('0000001000000', base=2),  # 64
+    'lOFA':  int('0000010000000', base=2),  # 128
+    'lpSTS': int('0000100000000', base=2),  # 256
+    'lPPA':  int('0001000000000', base=2),  # 512
+    'lEBA':  int('0010000000000', base=2),  # 1024
+    'lLOC':  int('0100000000000', base=2),  # 2048
+    'VIS':   int('1000000000000', base=2),  # 4096
+}
+
+#mask_fname = abspath(_opj(
+#    # TODO: switch back before release
+#    #'src',
+#    '..',
+#    'tnt',
+#    subj_code,
+#    'bold3Tp2',
+#    'brain_mask.nii.gz'))
+
+tmplwarp_fname = abspath(_opj(
+    # TODO: switch back before release
+    #'src',
+    '..',
+    'tnt',
+    subj_code,
+    'bold3Tp2',
+    'in_grpbold3Tp2',
+    'subj2tmpl_warp.nii.gz'))
+
+tmpl_fname = abspath(_opj(
+    # TODO: switch back before release
+    #'src',
+    '..',
+    'tnt',
+    'templates',
+    'grpbold3Tp2',
+    'brain.nii.gz'))
+
+roisummary_vol = None
+roisummary_hdr = None
+
+if rois is None:
+    rois = cfg.options(subj_code)
+for roi in rois:
+    # doesn't do casing properly
+    roi = roi[:-3] + roi[-3:].upper()
+    print(roi)
+    #wdir = mkdtemp(suffix='_stats2roi_%s' % subj_code)
+    wdir = '/tmp/stats2rois'
+    print(wdir)
+    # get particular setting for this ROI
+    info = cfg.get(subj_code, roi).split()
+    cope = int(info[0])
+    zthresh = float(info[1])
+    clabels = info[2:]
+    cluster_idx = [int(i) for i in info[2:]]
+
+    zstats_fname = abspath(_opj(
+        subj_code,
+        '2ndlvl_z164.gfeat',
+        'cope%i.feat' % cope,
+        'thresh_zstat1.nii.gz'))
+    grp_zstats_fname = _opj(wdir, 'grpzstats.nii.gz')
+
+    # find clusters
+    zstat_img = nb.load(zstats_fname)
+    clusters, nclusters = label(zstat_img.get_data() > zthresh)
+    print('Cluster sizes:', {i: np.sum(clusters == i) for i in range(1, nclusters + 1)})
+
+    # make ROI mask
+    roi_fname = _opj(wdir, '%s.nii.gz' % roi)
+    roi_vol = np.zeros(clusters.shape, dtype=np.bool)
+    for cli in cluster_idx:
+        roi_vol += clusters == cli
+    if roisummary_vol is None:
+        roisummary_vol = np.zeros(roi_vol.shape, dtype='int16')
+        roisummary_hdr = zstat_img.get_header()
+    roisummary_vol += roi_vol.astype(int) * roi_ids[roi]
+
+    nb.save(nb.Nifti1Image(roi_vol.astype('int16'), zstat_img.get_affine()),
+            roi_fname)
+
+    native_clusters_fname = _opj(wdir, 'native_clusters.nii.gz')
+    nb.save(zstat_img, _opj(wdir, 'stats.nii.gz'))
+    grp_roi_fname = _opj(wdir, 'grp_roi.nii.gz')
+    grp_clusters_fname = _opj(wdir, 'grp_clusters.nii.gz')
+    nb.save(nb.Nifti1Image(clusters, zstat_img.get_affine()),
+            native_clusters_fname)
+
+
+    print(nclusters)
+    #check_call(
+    #    ['easythresh', zstats_fname, mask_fname, '%s' % zthresh, '%s' % pthresh,
+    #     mask_fname, 'stats2rois'],
+    #    cwd=wdir)
+
+    if mode == 'indi':
+        check_call([
+            'applywarp',
+            '--in=%s' % roi_fname,
+            '--ref=%s' % tmpl_fname,
+            '--warp=%s' % tmplwarp_fname,
+            '--interp=nn',
+            '--out=%s' % grp_roi_fname])
+        check_call([
+            'applywarp',
+            '--in=%s' % native_clusters_fname,
+            '--ref=%s' % tmpl_fname,
+            '--warp=%s' % tmplwarp_fname,
+            '--interp=nn',
+            '--out=%s' % grp_clusters_fname])
+        check_call([
+            'applywarp',
+            '--in=%s' % zstats_fname,
+            '--ref=%s' % tmpl_fname,
+            '--warp=%s' % tmplwarp_fname,
+            '--interp=trilinear',
+            '--out=%s' % grp_zstats_fname])
+
+        check_call([
+            'fslview',
+            '../tnt/templates/grpbold3Tp2/from_mni/MNI152_T1_1mm.nii.gz',
+            grp_zstats_fname,
+            '-l', 'Red-Yellow',
+            grp_clusters_fname,
+            '-l',
+            'Random-Rainbow',
+            grp_roi_fname,
+            '-l',
+            'Greyscale',
+        ])
+
+    #rmtree(wdir)
+
+if mode == 'indi':
+    sys.exit(0)
+
+roisummary_fname = 'roisummary.nii.gz'
+grp_roisummary_fname = 'grp_roisummary.nii.gz'
+nb.save(nb.Nifti1Image(roisummary_vol, roisummary_hdr.get_best_affine()),
+        roisummary_fname)
+check_call([
+    'applywarp',
+    '--in=%s' % roisummary_fname,
+    '--ref=%s' % tmpl_fname,
+    '--warp=%s' % tmplwarp_fname,
+    '--interp=nn',
+    '--out=%s' % grp_roisummary_fname])
+check_call([
+    'fslview',
+    '../tnt/templates/grpbold3Tp2/from_mni/MNI152_T1_1mm.nii.gz',
+    grp_roisummary_fname,
+    '-l',
+    'Random-Rainbow',
+])
+
+

+ 71 - 0
code/stats2rois.cfg

@@ -0,0 +1,71 @@
+[sub-01]
+# copeID; cluster-forming threshold; cluster ID
+rFFA = 7 2.3 3 5
+rPPA = 3 2.3 2
+lPPA = 8 2.3 5
+rEBA = 4 3.1 1
+lEBA = 4 3.1 5
+rLOC = 10 2.3 2
+lLOC = 10 2.3 8
+[sub-02]
+rFFA = 7 3.1 3
+lFFA = 7 3.1 9
+rPPA = 8 2.6 3
+lPPA = 8 2.6 26
+rEBA = 9 3.3 1
+lEBA = 9 2.8 12
+rLOC = 10 2.3 1
+lLOC = 10 2.3 20
+[sub-03]
+rFFA = 7 2.0 10 12
+rLOC = 10 2.5 1 3
+lLOC = 10 2.5 9
+[sub-04]
+rPPA = 8 2.7 5
+rEBA = 9 2.7 1
+rLOC = 10 2.3 1
+lLOC = 10 2.3 7
+[sub-05]
+rFFA = 7 1.9 14
+rPPA = 8 2.7 5
+rEBA = 9 2.7 2
+lEBA = 9 2.8 11
+rLOC = 10 2.3 3
+[sub-06]
+rFFA = 7 2.6 5 6
+lFFA = 7 2.6 9
+rPPA = 8 2.4 5
+lPPA = 8 2.4 25
+rEBA = 9 2.6 1
+rLOC = 10 2.3 2
+lLOC = 10 2.3 97
+[sub-09]
+rFFA = 7 2.6 7
+lFFA = 7 2.6 13
+rPPA = 8 2.6 3
+lPPA = 8 2.6 14
+rEBA = 9 3.1 2
+lEBA = 9 3.1 13
+rLOC = 10 2.3 4
+lLOC = 10 2.3 14
+[sub-10]
+rFFA = 7 2.3 4
+rOFA = 7 2.3 1
+lFFA = 7 2.3 10
+lOFA = 7 2.3 8
+rPPA = 8 2.5 2
+lPPA = 8 2.5 6
+rEBA = 9 2.9 1
+lEBA = 9 2.9 4
+rLOC = 10 2.3 1
+lLOC = 10 2.3 21
+[sub-14]
+rFFA = 7 2.3 7
+rOFA = 7 2.3 5
+lOFA = 7 2.3 54
+rPPA = 8 3.3 4
+lPPA = 8 3.3 7
+rEBA = 9 3.1 1
+lEBA = 9 3.1 3
+rLOC = 10 2.4 1
+lLOC = 10 2.4 13