-
Notifications
You must be signed in to change notification settings - Fork 0
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
add AMICO noddi fitting step #20
Open
ebrahimebrahim
wants to merge
1
commit into
main
Choose a base branch
from
14-improve-noddi-step-in-the-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from pathlib import Path | ||
import shutil | ||
import argparse | ||
import amico | ||
from common import get_unique_file_with_extension | ||
|
||
|
||
|
||
# === Parse args === | ||
|
||
parser = argparse.ArgumentParser(description='fits NODDI model to ABCD DWI images using AMICO') | ||
parser.add_argument('extracted_images_path', type=str, help='path to folder in which downloaded ABCD images were extracted') | ||
parser.add_argument('masks_path', type=str, help='path to folder containing brain masks. the mask of x.nii.gz is expected to be named x_mask.nii.gz') | ||
parser.add_argument('output_dir', type=str, help='path to folder in which to save the fitted NODDI results') | ||
args = parser.parse_args() | ||
extracted_images_path = Path(args.extracted_images_path) | ||
masks_path = Path(args.masks_path) | ||
output_dir = Path(args.output_dir) | ||
|
||
# === Set up AMICO === | ||
# following https://github.com/daducci/AMICO/wiki/NODDI | ||
|
||
amico.setup() | ||
kernels_generated = False | ||
|
||
|
||
# === Iterate through images, performing NODDI fit and saving results === | ||
|
||
for dwi_nii_directory in extracted_images_path.glob('*/*/*/dwi/'): | ||
|
||
nii_path = get_unique_file_with_extension(dwi_nii_directory, 'nii.gz') | ||
# (Note that getting a unique file like this wouldn't work in general on an ABCD download if someone extracted everything to the same | ||
# target folder instead of creating one folder for each archive as I did.) | ||
basename = nii_path.name.split('.')[0] | ||
|
||
subject_output_dir = output_dir/basename | ||
if subject_output_dir.exists(): | ||
print(f"Skipping {basename} and assuming it was already processed since the following output directory exists:\n{subject_output_dir}") | ||
continue | ||
subject_output_dir.mkdir() | ||
|
||
try: | ||
amico_workspace = subject_output_dir/'amico_workspace' | ||
amico_workspace.mkdir(exist_ok=True) | ||
|
||
bval_path = get_unique_file_with_extension(dwi_nii_directory, 'bval') | ||
bvec_path = get_unique_file_with_extension(dwi_nii_directory, 'bvec') | ||
|
||
scheme_path = amico_workspace/f'{basename}.scheme' | ||
amico.util.fsl2scheme(bval_path, bvec_path, schemeFilename=scheme_path) | ||
|
||
mask_path = masks_path/(basename + '_mask.nii.gz') | ||
|
||
ae = amico.Evaluation() | ||
ae.load_data(nii_path, scheme_path, mask_path, replace_bad_voxels=0) | ||
ae.set_model('NODDI') | ||
ae.generate_kernels(regenerate=(not kernels_generated)) # We only need to generate kernels once because the b-values are the same for all of ABCD. | ||
kernels_generated = True | ||
ae.load_kernels() | ||
ae.fit() | ||
ae.save_results(path_suffix=basename) | ||
amico_results_dir = Path('.')/'AMICO'/f'NODDI_{basename}' | ||
|
||
for amico_output_file in amico_results_dir.iterdir(): | ||
filename = amico_output_file.name | ||
filename = filename.replace('fit', basename) | ||
shutil.move(amico_output_file, subject_output_dir/filename) | ||
shutil.rmtree(Path('.')/'AMICO') | ||
except Exception as e: | ||
shutil.rmtree(subject_output_dir) # Remove subject output dir so that upon re-run it doesn't think this was already processed successfully. | ||
raise e |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
still not sure about
replace_bad_voxels=0
i need to look at minimally processed ABCD images and why there are "bad voxels" in the first place