Skip to content
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

Hcp make wm vent mask #1279

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions xcp_d/ingression/hcpya.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
copy_files_in_dict,
plot_bbreg,
write_json,
make_masks,
)
from xcp_d.utils.filemanip import ensure_list

Expand Down Expand Up @@ -133,6 +134,8 @@
sub-<sub_id>
└── files
└── MNINonLinear
├── ROIs
├── wmparc.2.nii.gz # 2 is the roi resolution 2 mm, this will be used for making the subject-specific masks
├── Results
│ ├── *_<TASK_ID><RUN_ID>_<DIR_ID>
│ │ ├── SBRef_dc.nii.gz
Expand Down Expand Up @@ -162,6 +165,7 @@
├── aparc+aseg.nii.gz
├── brainmask_fs.nii.gz
└── ribbon.nii.gz

"""
assert isinstance(in_dir, str)
assert os.path.isdir(in_dir), f"Folder DNE: {in_dir}"
Expand All @@ -179,6 +183,7 @@

anat_dir_orig = os.path.join(in_dir, sub_id, "MNINonLinear")
func_dir_orig = os.path.join(anat_dir_orig, "Results")
ROI_dir_orig = os.path.join(anat_dir_orig,"ROIs")

Check warning on line 186 in xcp_d/ingression/hcpya.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/hcpya.py#L186

Added line #L186 was not covered by tests
subject_dir_bids = os.path.join(out_dir, sub_ent)
anat_dir_bids = os.path.join(subject_dir_bids, "anat")
func_dir_bids = os.path.join(subject_dir_bids, "func")
Expand All @@ -194,9 +199,12 @@
os.makedirs(func_dir_bids, exist_ok=True)
os.makedirs(work_dir, exist_ok=True)

segmentation = os.path.join(anat_dir_orig, "wmparc.2.nii.gz")

Check warning on line 202 in xcp_d/ingression/hcpya.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/hcpya.py#L202

Added line #L202 was not covered by tests

make_masks(segmentation,os.path.join(anat_dir_bids,f"wm_2mm_{sub_id}_mask_eroded.nii.gz"),os.path.join(anat_dir_bids,f"vent_2mm_{sub_id}_mask_eroded.nii.gz"),fmri_res=2,roi_res=2)

Check warning on line 204 in xcp_d/ingression/hcpya.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/hcpya.py#L204

Added line #L204 was not covered by tests
# Get masks to be used to extract confounds
csf_mask = str(load_data(f"masks/{volspace_ent}_{RES_ENT}_label-CSF_mask.nii.gz"))
wm_mask = str(load_data(f"masks/{volspace_ent}_{RES_ENT}_label-WM_mask.nii.gz"))
csf_mask = str(load_data(os.path.join(anat_dir_bids,f"vent_2mm_{sub_id}_mask_eroded.nii.gz")))
wm_mask = str(load_data(os.path.join(anat_dir_bids,f"wm_2mm_{sub_id}_mask_eroded.nii.gz")))

Check warning on line 207 in xcp_d/ingression/hcpya.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/hcpya.py#L206-L207

Added lines #L206 - L207 were not covered by tests

# A dictionary of mappings from HCP derivatives to fMRIPrep derivatives.
# Values will be lists, to allow one-to-many mappings.
Expand Down
64 changes: 64 additions & 0 deletions xcp_d/ingression/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,67 @@
json.dump(data, f, sort_keys=True, indent=4)

return outfile

def make_masks(segmentation, wm_mask_out, vent_mask_out, **kwargs):

"""
# make eroded white matter and ventricular masks copied from DCAN-bold-processing
generates ventricular and white matter masks from a Desikan/FreeSurfer
segmentation file. label constraints may be overridden.
:param segmentation: Desikan/FreeSurfer spec segmentation nifti file. [e.g. 'wmparc.%g.nii.gz' % roi_res]
Does not need to be a cifti but must have labels according to FS lookup
table, including cortical parcellations.
:param wm_mask_out: binary white matter mask. [e.g. ‘wm_%gmm_%s_mask_eroded.nii.gz’%(fmri_res, subject)]
:param vent_mask_out: binary ventricular mask. [e.g. ‘vent_%gmm_%s_mask_eroded.nii.gz’%(fmri_res, subject)]
:param kwargs: dictionary of label value overrides. You may override
default label number bounds for white matter and ventricle masks in the
segmentation file.
:return: None
"""

wd = os.path.dirname(wm_mask_out)

Check warning on line 417 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L417

Added line #L417 was not covered by tests
# set parameter defaults
defaults = dict(wm_lt_R=2950, wm_ut_R=3050, wm_lt_L=3950, wm_ut_L=4050,

Check warning on line 419 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L419

Added line #L419 was not covered by tests
vent_lt_R=43, vent_ut_R=43, vent_lt_L=4, vent_ut_L=4,
roi_res=2)
# set temporary filenames
tempfiles = {

Check warning on line 423 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L423

Added line #L423 was not covered by tests
'wm_mask_L': os.path.join(wd, 'tmp_left_wm.nii.gz'),
'wm_mask_R': os.path.join(wd, 'tmp_right_wm.nii.gz'),
'vent_mask_L': os.path.join(wd, 'tmp_left_vent.nii.gz'),
'vent_mask_R': os.path.join(wd, 'tmp_right_vent.nii.gz'),
'wm_mask': os.path.join(wd, 'tmp_wm.nii.gz'),
'vent_mask': os.path.join(wd, 'tmp_vent.nii.gz')
}
# inputs and outputs
iofiles = {

Check warning on line 432 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L432

Added line #L432 was not covered by tests
'segmentation': segmentation,
'wm_mask_out': wm_mask_out,
'vent_mask_out': vent_mask_out
}
# command pipeline
cmdlist = [

Check warning on line 438 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L438

Added line #L438 was not covered by tests
'fslmaths {segmentation} -thr {wm_lt_R} -uthr {wm_ut_R} {wm_mask_R}',
'fslmaths {segmentation} -thr {wm_lt_L} -uthr {wm_ut_L} {wm_mask_L}',
'fslmaths {wm_mask_R} -add {wm_mask_L} -bin {wm_mask}',
'fslmaths {wm_mask} -kernel gauss {roi_res:g} -ero {wm_mask_out}',
'fslmaths {segmentation} -thr {vent_lt_R} -uthr {vent_ut_R} '
'{vent_mask_R}',
'fslmaths {segmentation} -thr {vent_lt_L} -uthr {vent_ut_L} '
'{vent_mask_L}',
'fslmaths {vent_mask_R} -add {vent_mask_L} -bin {vent_mask}',
'fslmaths {vent_mask} -kernel gauss {roi_res:g} -ero {vent_mask_out}'
]

# get params
defaults.update(kwargs)
kwargs.update(defaults)
kwargs.update(iofiles)
kwargs.update(tempfiles)

Check warning on line 455 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L452-L455

Added lines #L452 - L455 were not covered by tests
# format and run commands
for cmdfmt in cmdlist:
cmd = cmdfmt.format(**kwargs)
subprocess.call(cmd.split())

Check warning on line 459 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L458-L459

Added lines #L458 - L459 were not covered by tests
# cleanup
for key in tempfiles.keys():
os.remove(tempfiles[key])

Check warning on line 462 in xcp_d/ingression/utils.py

View check run for this annotation

Codecov / codecov/patch

xcp_d/ingression/utils.py#L462

Added line #L462 was not covered by tests
Loading