-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dwi preprocessing using metadata #15
base: master
Are you sure you want to change the base?
Changes from 15 commits
d97f843
ce1f242
92cde71
d7823d6
6037f36
31b460b
6c256db
86926c2
15ed837
0b41059
6aa43ed
db56702
0eae829
00c3891
b79f6a8
2317eef
4f6f47c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,10 +52,10 @@ ENV PATH /opt/freesurfer/bin:/opt/freesurfer/fsfast/bin:/opt/freesurfer/tktools: | |
# Install FSL 5.0.9 | ||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends curl && \ | ||
curl -sSL http://neuro.debian.net/lists/trusty.us-ca.full >> /etc/apt/sources.list.d/neurodebian.sources.list && \ | ||
curl -sSL http://neuro.debian.net/lists/xenial.us-ca.full >> /etc/apt/sources.list.d/neurodebian.sources.list && \ | ||
apt-key adv --recv-keys --keyserver hkp://pgp.mit.edu:80 0xA5D32F012649A5A9 && \ | ||
apt-get update && \ | ||
apt-get install -y fsl-core=5.0.9-3~nd14.04+1 && \ | ||
apt-get install -y fsl-core=5.0.9-1~nd+1+nd16.04+1 && \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are those changes still necessary applicable to this PR? (applies to |
||
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* | ||
|
||
# Configure environment | ||
|
@@ -72,7 +72,7 @@ ENV FSLOUTPUTTYPE=NIFTI_GZ | |
RUN echo "cHJpbnRmICJrcnp5c3p0b2YuZ29yZ29sZXdza2lAZ21haWwuY29tXG41MTcyXG4gKkN2dW12RVYzelRmZ1xuRlM1Si8yYzFhZ2c0RVxuIiA+IC9vcHQvZnJlZXN1cmZlci9saWNlbnNlLnR4dAo=" | base64 -d | sh | ||
|
||
# Install Connectome Workbench | ||
RUN apt-get update && apt-get -y install connectome-workbench=1.2.3-1~nd14.04+1 | ||
RUN apt-get update && apt-get -y install connectome-workbench=1.2.3-1~nd16.04+1 | ||
|
||
ENV CARET7DIR=/usr/bin | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,7 @@ def run_diffusion_processsing(**args): | |
'--echospacing="{echospacing}" '+ \ | ||
'--PEdir={PEdir} ' + \ | ||
'--gdcoeffs="NONE" ' + \ | ||
'--dwiname="{dwiname}" ' + \ | ||
'--printcom=""' | ||
cmd = cmd.format(**args) | ||
run(cmd, cwd=args["path"], env={"OMP_NUM_THREADS": str(args["n_cpus"])}) | ||
|
@@ -193,7 +194,7 @@ def run_diffusion_processsing(**args): | |
'fMRISurface', 'DiffusionPreprocessing'], | ||
default=['PreFreeSurfer', 'FreeSurfer', 'PostFreeSurfer', | ||
'fMRIVolume', 'fMRISurface', | ||
'DiffusionPreprocessing']) | ||
'DiffusionPreprocessing', 'TaskfMRIAnalysis']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover from another PR? This one should be just about DWI right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes. |
||
parser.add_argument('--license_key', help='FreeSurfer license key - letters and numbers after "*" in the email you received after registration. To register (for free) visit https://surfer.nmr.mgh.harvard.edu/registration.html', | ||
required=True) | ||
parser.add_argument('-v', '--version', action='version', | ||
|
@@ -343,14 +344,15 @@ def run_diffusion_processsing(**args): | |
type='bold', | ||
extensions=["nii.gz", "nii"])] | ||
for fmritcs in bolds: | ||
fmriname = "_".join(fmritcs.split("sub-")[-1].split("_")[1:]).split(".")[0] | ||
fmriname = "_".join(fmritcs.split("sub-")[-1].split("_")[1:-1]).split(".")[0] | ||
assert fmriname | ||
|
||
fmriscout = fmritcs.replace("_bold", "_sbref") | ||
if not os.path.exists(fmriscout): | ||
fmriscout = "NONE" | ||
|
||
fieldmap_set = layout.get_fieldmap(fmritcs) | ||
print(fieldmap_set) | ||
if fieldmap_set and fieldmap_set["type"] == "epi": | ||
SEPhaseNeg = None | ||
SEPhasePos = None | ||
|
@@ -409,31 +411,80 @@ def run_diffusion_processsing(**args): | |
if stage in args.stages: | ||
stage_func() | ||
|
||
dwis = layout.get(subject=subject_label, type='dwi', | ||
extensions=["nii.gz", "nii"]) | ||
|
||
# print(dwis) | ||
# acqs = set(layout.get(target='acquisition', return_type='id', | ||
# subject=subject_label, type='dwi', | ||
# extensions=["nii.gz", "nii"])) | ||
# print(acqs) | ||
# posData = [] | ||
# negData = [] | ||
# for acq in acqs: | ||
# pos = "EMPTY" | ||
# neg = "EMPTY" | ||
# dwis = layout.get(subject=subject_label, | ||
# type='dwi', acquisition=acq, | ||
# extensions=["nii.gz", "nii"]) | ||
# assert len(dwis) <= 2 | ||
# for dwi in dwis: | ||
# dwi = dwi.filename | ||
# if "-" in layout.get_metadata(dwi)["PhaseEncodingDirection"]: | ||
# neg = dwi | ||
# else: | ||
# pos = dwi | ||
# posData.append(pos) | ||
# negData.append(neg) | ||
# | ||
# print(negData) | ||
# print(posData) | ||
posData = [] | ||
negData = [] | ||
PEdir = "None" | ||
dwiname = "Diffusion" | ||
dirnums = [] | ||
onerun = False | ||
|
||
numruns = set(layout.get(target='run', return_type='id', | ||
subject=subject_label, type='dwi', | ||
extensions=["nii.gz", "nii"])) | ||
# accounts for multiple runs, number of directions, and phase encoding directions | ||
if not numruns: | ||
onerun= True | ||
numruns = {'run-01'} | ||
if numruns: | ||
for session in numruns: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run is a function - I believe. Would you like me to rename the variable session? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would make more sense since you are iterating over runs not sessions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could change it to |
||
if not onerun: | ||
bvals = [f.filename for f in layout.get(subject=subject_label, | ||
type='dwi', run=session, | ||
extensions=["bval"])] | ||
else: | ||
bvals = [f.filename for f in layout.get(subject=subject_label, | ||
type='dwi', extensions=["bval"])] | ||
dwi_dict = {'bvalFile':[], 'bval':[], 'dwiFile':[], 'direction':[]} | ||
for bvalfile in bvals: | ||
with open(bvalfile) as f: # get number of directions | ||
bvalues = [bvalue for line in f for bvalue in line.split()] | ||
dwi_dict['bvalFile'].append(bvalfile) | ||
dwi_dict['bval'].append(len(bvalues) - 1) | ||
dwiFile = glob(os.path.join(os.path.dirname(bvalfile),'{0}.nii*'.format(os.path.basename(bvalfile).split('.')[0]))) # ensures bval file has same name as dwi file | ||
assert len(dwiFile) == 1 | ||
dwi_dict['dwiFile'].append(dwiFile[0]) | ||
dwi_dict['direction'].append(layout.get_metadata(dwiFile[0])["PhaseEncodingDirection"][0]) | ||
|
||
# check if length of lists in dictionary are the same | ||
n = len(dwi_dict['bvalFile']) | ||
assert all(len(dwi_dict[k]) for k,v in dwi_dict.items()) | ||
|
||
for dirnum in set(dwi_dict['bval']): | ||
idxs = { i for k,v in dwi_dict.iteritems() for i in range(0,len(dwi_dict['bval'])) if v[i] == dirnum } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment explaining what this line does? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 450: I made a mistake - it's supposed to be Line 453: extracts the index values in dwi_dict['bval'] if the value matches "dirnum", which is the number of directions (i.e. 98 or 99). These index values gets used to find the PE directions, dwi files names, etc. in the dictionary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you could add this comment to the code it would be easier for people in the future to understand this code. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course! |
||
PEdirNums = set([dwi_dict['direction'][i] for i in idxs]) | ||
for PEdirNum in PEdirNums: | ||
dwis = [ dwi_dict['dwiFile'][i] for i in idxs if dwi_dict['direction'][i] == PEdirNum ] | ||
assert len(dwis) <= 2 | ||
dwiname = "Diffusion" + "_dir-" + str(dirnum) + "_" + session + "_corr_" + str(PEdirNum) | ||
if "j" in PEdirNum: | ||
PEdir = 2 | ||
elif "i" in PEdirNum: | ||
PEdir = 1 | ||
else: | ||
RuntimeError("Phase encoding direction not specified for diffusion data.") | ||
pos = "EMPTY" | ||
neg = "EMPTY" | ||
gdcoeffs = "None" | ||
for dwi in dwis: | ||
if "-" in layout.get_metadata(dwi)["PhaseEncodingDirection"]: | ||
neg = dwi | ||
else: | ||
pos = dwi | ||
try: | ||
echospacing = layout.get_metadata(pos)["EffectiveEchoSpacing"] * 1000 | ||
dwi_stage_dict = OrderedDict([("DiffusionPreprocessing", partial(run_diffusion_processsing, | ||
posData=pos, | ||
negData=neg, | ||
path=args.output_dir, | ||
subject="sub-%s" % subject_label, | ||
echospacing=echospacing, | ||
PEdir=PEdir, | ||
gdcoeffs="NONE", | ||
dwiname=dwiname, | ||
n_cpus=args.n_cpus))]) | ||
for stage, stage_func in dwi_stage_dict.iteritems(): | ||
if stage in args.stages: | ||
stage_func() | ||
except: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please replace this with a more specific catch (narrower in the amount of code it encomapses and types of exceptions it is suppose to catch). Right now it will catch all exceptions making debugging really hard. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it! |
||
print("You may have missing diffusion data in the positive phase encoding direction.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tra/except statement still encompases a huge chunk of code that could produce "NameError" that does not necessarily mean that there is missing diffusion data in the positive phase encoding direction. This should be made more specific. Furthermore we should exit with a non zero code to indicate that something did not work correctly. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you need to change the base Ubuntu distribution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. I actually wasn't aware of this change. There wasn't any need - sorry about that.