-
Notifications
You must be signed in to change notification settings - Fork 34
Prototype cli #218
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
Open
arnaudbore
wants to merge
4
commits into
ANTsX:master
Choose a base branch
from
arnaudbore:prototype_cli
base: master
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.
+154
−0
Open
Prototype cli #218
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Empty file.
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,82 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| import argparse | ||
|
|
||
| import ants | ||
| import antspynet | ||
|
|
||
|
|
||
| def build_parser(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Run mouse brain extraction with ANTsPyNet." | ||
| ) | ||
| parser.add_argument( | ||
| "input_image", | ||
| type=str, | ||
| help="Path to the input mouse image.", | ||
| ) | ||
| parser.add_argument( | ||
| "output_image", | ||
| type=str, | ||
| help="Path to write the extracted probability image.", | ||
| ) | ||
| parser.add_argument( | ||
| "--modality", | ||
| default="t2", | ||
| choices=["t2", "ex5coronal", "ex5sagittal"], | ||
| help='Mouse image modality passed to mouse_brain_extraction (default: "t2").', | ||
| ) | ||
| parser.add_argument( | ||
| "--isotropic-output", | ||
| action="store_true", | ||
| help="Return the probability image in isotropic space before writing it.", | ||
| ) | ||
| parser.add_argument( | ||
| "--axis", | ||
| type=int, | ||
| default=2, | ||
| choices=[0, 1, 2], | ||
| help="Axis index used for ex5 modalities (default: 2).", | ||
| ) | ||
| parser.add_argument( | ||
| "--verbose", | ||
| action="store_true", | ||
| help="Print progress while running the model.", | ||
| ) | ||
| return parser | ||
|
|
||
|
|
||
| def run(input_image, output_image, modality="t2", isotropic_output=False, axis=2, verbose=False): | ||
| image = ants.image_read(input_image) | ||
| probability_image = antspynet.mouse_brain_extraction( | ||
| image, | ||
| modality=modality, | ||
| return_isotropic_output=isotropic_output, | ||
| which_axis=axis, | ||
| verbose=verbose, | ||
| ) | ||
| ants.image_write(probability_image, output_image) | ||
| return output_image | ||
|
|
||
|
|
||
| def main(argv=None): | ||
| parser = build_parser() | ||
| args = parser.parse_args(argv) | ||
|
|
||
| try: | ||
| run( | ||
| args.input_image, | ||
| args.output_image, | ||
| modality=args.modality, | ||
| isotropic_output=args.isotropic_output, | ||
| axis=args.axis, | ||
| verbose=args.verbose, | ||
| ) | ||
| except Exception as error: | ||
| parser.exit(1, f"Error: {error}\n") | ||
|
|
||
|
Comment on lines
+62
to
+77
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(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
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,69 @@ | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import ants | ||
| import numpy as np | ||
|
|
||
| from antspynet.cli.mouse_brain_extraction import main | ||
|
|
||
|
|
||
| class TestMouseBrainExtractionCli(unittest.TestCase): | ||
| def test_main_routes_to_mouse_brain_extraction(self): | ||
| input_image = ants.from_numpy(np.ones((4, 4, 4))) | ||
| output_image = ants.from_numpy(np.full((4, 4, 4), 0.25)) | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| input_path = os.path.join(tmpdir, "input.nii.gz") | ||
| output_path = os.path.join(tmpdir, "output.nii.gz") | ||
|
|
||
| calls = {} | ||
|
|
||
| def fake_image_read(path): | ||
| calls["image_read"] = path | ||
| return input_image | ||
|
|
||
| def fake_mouse_brain_extraction( | ||
| image, | ||
| modality, | ||
| return_isotropic_output, | ||
| which_axis, | ||
| verbose, | ||
| ): | ||
| calls["mouse_brain_extraction"] = { | ||
| "image": image, | ||
| "modality": modality, | ||
| "return_isotropic_output": return_isotropic_output, | ||
| "which_axis": which_axis, | ||
| "verbose": verbose, | ||
| } | ||
| return output_image | ||
|
|
||
| def fake_image_write(image, path): | ||
| calls["image_write"] = {"image": image, "path": path} | ||
|
|
||
| with patch("antspynet.cli.mouse_brain_extraction.ants.image_read", side_effect=fake_image_read), \ | ||
| patch("antspynet.cli.mouse_brain_extraction.antspynet.mouse_brain_extraction", side_effect=fake_mouse_brain_extraction), \ | ||
| patch("antspynet.cli.mouse_brain_extraction.ants.image_write", side_effect=fake_image_write): | ||
| exit_code = main([ | ||
| input_path, | ||
| output_path, | ||
| "--modality", "t2", | ||
| "--isotropic-output", | ||
| "--axis", "1", | ||
| "--verbose", | ||
| ]) | ||
|
|
||
| self.assertEqual(exit_code, 0) | ||
| self.assertEqual(calls["image_read"], input_path) | ||
| self.assertEqual(calls["image_write"]["path"], output_path) | ||
| self.assertIs(calls["image_write"]["image"], output_image) | ||
| self.assertEqual(calls["mouse_brain_extraction"]["modality"], "t2") | ||
| self.assertTrue(calls["mouse_brain_extraction"]["return_isotropic_output"]) | ||
| self.assertEqual(calls["mouse_brain_extraction"]["which_axis"], 1) | ||
| self.assertTrue(calls["mouse_brain_extraction"]["verbose"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
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.
I think this is important. The documentation needs to be complete and explanatory, which does introduce some duplication with function documentation, but I don't see how it can be avoided.