-
Notifications
You must be signed in to change notification settings - Fork 75
Adding SF amplitude thresholding as a way of create masks #1337
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
164922d
Add global SF thresholding and advanced tracking features (Independen…
frheault 11a7ed4
Fix undefined variables and PEP8 cleanup for Branch B
frheault 496d68f
Merge branch 'master' of https://github.com/scilus/scilpy into split_…
frheault 1172d7f
arg parser fix
frheault cd77e54
Merge after first split was merged
frheault f33ee61
Fix Copilot initial review and extracted redundant code
frheault 172265a
Improved descriptions, flake8 and utils harmo
frheault 5f87a6b
Fix Charles comments
frheault 31323ad
Switch 0.05 to a parameter
frheault 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 hidden or 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 hidden or 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,101 @@ | ||
| #!/usr/bin/env python3 | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| """ | ||
| Compute a binary mask based on a global SF threshold. | ||
| The script masks voxels where the maximum SF amplitude is below | ||
| either a relative factor or an absolute threshold (or both). | ||
|
|
||
| When fODFs are evaluated on a sphere (SF), the amplitude of the lobes | ||
| corresponds to the strength of the diffusion signal in those directions. | ||
| Thresholding these amplitudes is a common practice to filter out spurious | ||
| peaks arising from noise or the deconvolution process (e.g., ringing effects). | ||
|
|
||
| The absolute threshold can be estimated from the mean/median maximum fODF | ||
| in the ventricles, computed with scil_fodf_max_in_ventricles. | ||
|
|
||
| If both --relative and --absolute are provided, the final threshold is the | ||
| maximum of the two resulting values. | ||
| """ | ||
|
|
||
| import argparse | ||
| import logging | ||
|
|
||
| import nibabel as nib | ||
| import numpy as np | ||
|
|
||
| from scilpy.io.utils import (add_sh_basis_args, add_sphere_arg, | ||
| add_verbose_arg, add_overwrite_arg, | ||
| assert_inputs_exist, assert_outputs_exist, | ||
| parse_sh_basis_arg) | ||
| from scilpy.tracking.utils import compute_sf_threshold_mask | ||
| from scilpy.version import version_string | ||
|
|
||
|
|
||
| def _build_arg_parser(): | ||
| p = argparse.ArgumentParser(description=__doc__, | ||
| formatter_class=argparse.RawTextHelpFormatter, | ||
| epilog=version_string) | ||
|
|
||
| p.add_argument('in_odf', | ||
| help='Input ODF file (SH or Peaks) (.nii.gz).') | ||
| p.add_argument('out_mask', | ||
| help='Output binary mask (.nii.gz).') | ||
|
|
||
| thr_g = p.add_argument_group('Threshold options') | ||
| thr_g.add_argument('--relative', type=float, | ||
| help='Global SF threshold relative factor (0-1).') | ||
| thr_g.add_argument('--absolute', type=float, | ||
| help='Global SF absolute threshold.') | ||
| add_sh_basis_args(p) | ||
| add_sphere_arg(p) | ||
| add_overwrite_arg(p) | ||
| add_verbose_arg(p) | ||
|
|
||
| return p | ||
|
|
||
|
|
||
| def main(): | ||
| parser = _build_arg_parser() | ||
| args = parser.parse_args() | ||
| logging.getLogger().setLevel(logging.getLevelName(args.verbose)) | ||
|
|
||
|
frheault marked this conversation as resolved.
|
||
| if args.relative is None and args.absolute is None: | ||
| parser.error("At least one of --relative or --absolute must be " | ||
| "provided.") | ||
|
|
||
| assert_inputs_exist(parser, args.in_odf) | ||
| assert_outputs_exist(parser, args, args.out_mask) | ||
|
|
||
| sh_basis, is_legacy = parse_sh_basis_arg(args) | ||
|
|
||
| logging.info("Loading ODF data.") | ||
| img = nib.load(args.in_odf) | ||
| data = img.get_fdata(dtype=np.float32) | ||
|
|
||
| logging.info("Computing global SF threshold mask.") | ||
| mask, global_max, threshold = compute_sf_threshold_mask( | ||
| data, sphere_name=args.sphere, relative_factor=args.relative, | ||
| absolute_threshold=args.absolute, sh_basis=sh_basis, | ||
| is_legacy=is_legacy) | ||
|
|
||
| logging.info("Global max SF amplitude: {:.4f}".format(global_max)) | ||
| if args.relative is not None and args.absolute is not None: | ||
| logging.info("Both relative and absolute thresholds used. " | ||
| "Final threshold: {:.4f}".format(threshold)) | ||
| elif args.relative is not None: | ||
| logging.info("Relative threshold: {:.4f} (Factor: {})" | ||
| .format(threshold, args.relative)) | ||
| else: | ||
| logging.info("Absolute threshold used: {:.4f}".format(args.absolute)) | ||
|
|
||
| logging.info("Number of voxels in mask: {}".format(np.sum(mask))) | ||
|
|
||
| # Save mask | ||
| mask_img = nib.Nifti1Image(mask.astype(np.uint8), img.affine, | ||
| img.header) | ||
| nib.save(mask_img, args.out_mask) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.