Skip to content

Instantly share code, notes, and snippets.

@micabe
Last active November 18, 2020 13:43
Show Gist options
  • Save micabe/27122ac1c8670f95806fecb35b73828f to your computer and use it in GitHub Desktop.
Save micabe/27122ac1c8670f95806fecb35b73828f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import argparse
import json
from glob import glob
import csv
def get_quantification_value(quantDict, roi, side, type):
try:
return quantDict[roi]['total'][side][type]
except:
return -1
def volumes(databasePath, output):
centers = [elt for elt in os.listdir(os.path.join(databasePath))
if os.path.isdir(os.path.join(databasePath, elt))]
cw = csv.writer(open(output, 'w'))
row = [ 'CENTER', 'SUBJECT_NAME', 'SUBJECT_AGE', 'MRITIMEPOINT', 'MRIEXAMDATE',
'WHOLE_BRAIN_ML', 'WHOLE_BRAIN_ICV',
'GREY_MATTER_ML', 'GREY_MATTER_ICV',
'WHITE_MATTER_ML', 'WHITE_MATTER_ICV',
'HIPPOCAMPUS_ML', 'HIPPOCAMPUS_ICV',
'RIGHT_HIPPOCAMPUS_ML', 'RIGHT_HIPPOCAMPUS_ICV',
'LEFT_HIPPOCAMPUS_ML', 'LEFT_HIPPOCAMPUS_ICV',
'AMYGDALA_ML', 'AMYGDALA_ICV',
'RIGHT_AMYGDALA_ML', 'RIGHT_AMYGDALA_ICV',
'LEFT_AMYGDALA_ML', 'LEFT_AMYGDALA_ICV',
'WMH_ML', 'WMH_ICV',
]
cw.writerow(row)
for center in centers:
print('Center', center)
centerPath = os.path.join(databasePath, center)
subjects = [elt for elt in os.listdir(centerPath)
if os.path.isdir(os.path.join(databasePath, center, elt))]
for i, subject in enumerate(subjects):
timepoints = sorted([elt for elt in os.listdir(os.path.join(centerPath, subject, 't1mri'))
if os.path.isdir(os.path.join(databasePath, center, subject, 't1mri', elt))])
for j, acq in enumerate(timepoints):
print('Timepoint', acq)
name = subject
age = None
tp = 'M000' if j == 0 else 'M024'
date = acq
wbml = None
wbicv = None
gmml = None
gmicv = None
wmml = None
wmicv = None
hml = None
hicv = None
rhml = None
rhicv = None
lhml = None
lhicv = None
aml = None
aicv = None
raml = None
raicv = None
laml = None
laicv = None
wmhml = None
wmhicv = None
brainVolumes = \
glob(os.path.join(databasePath, center, subject, 'results', acq, \
'*', 'quantification*json'))
clinical = \
glob(os.path.join(databasePath, center, subject, 'clinical', acq, \
'json', 'clini*json'))
with open(brainVolumes[0], 'r') as fd:
quantJson = json.load(fd)
wbml = get_quantification_value(quantJson, 'Whole Brain', 'Both', 'volume_ml')
wbicv = get_quantification_value(quantJson, 'Whole Brain', 'Both', 'volume_icv')
gmml = get_quantification_value(quantJson, 'Grey Matter', 'Both', 'volume_ml')
gmicv = get_quantification_value(quantJson, 'Grey Matter', 'Both', 'volume_icv')
wmml = get_quantification_value(quantJson, 'White Matter', 'Both', 'volume_ml')
wmicv = get_quantification_value(quantJson, 'White Matter', 'Both', 'volume_icv')
hml = get_quantification_value(quantJson, 'Hippocampus', 'Both', 'volume_ml')
hicv = get_quantification_value(quantJson, 'Hippocampus', 'Both', 'volume_icv')
rhml = get_quantification_value(quantJson, 'Hippocampus', 'Right', 'volume_ml')
rhicv = get_quantification_value(quantJson, 'Hippocampus', 'Right', 'volume_icv')
lhml = get_quantification_value(quantJson, 'Hippocampus', 'Left', 'volume_ml')
lhicv = get_quantification_value(quantJson, 'Hippocampus', 'Left', 'volume_icv')
aml = get_quantification_value(quantJson, 'Amygdala', 'Both', 'volume_ml')
aicv = get_quantification_value(quantJson, 'Amygdala', 'Both', 'volume_icv')
raml = get_quantification_value(quantJson, 'Amygdala', 'Right', 'volume_ml')
raicv = get_quantification_value(quantJson, 'Amygdala', 'Right', 'volume_icv')
laml = get_quantification_value(quantJson, 'Amygdala', 'Left', 'volume_ml')
laicv = get_quantification_value(quantJson, 'Amygdala', 'Left', 'volume_icv')
wmhml = get_quantification_value(quantJson, 'WMH', 'Both', 'volume_ml')
wmhicv = get_quantification_value(quantJson, 'WMH', 'Both', 'volume_icv')
with open(clinical[0], 'r') as pd:
js = json.load(pd)
age = float(js['subject']['subject_age'])
row = [center, name, age, tp, date, \
wbml, wbicv, gmml, gmicv, wmml, wmicv, hml, hicv, \
rhml, rhicv, lhml, lhicv, aml, aicv, raml, raicv, laml, laicv, wmhml, wmhicv]
if not any(x is None for x in row):
cw.writerow(row)
if __name__ == '__main__':
descriptionMsg = \
'''Compute Brain Volumes on subjects of given center of given db.'''
parser = argparse.ArgumentParser(description=descriptionMsg)
parser.add_argument('-d', '--database',
help='database path')
parser.add_argument('-o', '--output',
help='output CSV')
args = parser.parse_args()
databasePath = args.database
outputCsv = args.output
volumes(databasePath, outputCsv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment