-
Notifications
You must be signed in to change notification settings - Fork 9
[FIX/ENH] VOF HBN changes #202
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
5d83ef2
WIP/ENH add t1w/logb0 metric
36000 1842bdd
set max numba n threads
36000 dd16772
better bound logb0
36000 08ba023
Debug gaussian weights error
36000 7861b40
try float64
36000 bd3b11e
fix brainchop
36000 129414a
return float64 weights
36000 5a26f3f
combination model
36000 631c794
add nn test
36000 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
Some comments aren't visible on the classic Files Changed page.
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
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,21 @@ | ||
| import numpy as np | ||
|
|
||
| from AFQ.nn.utils import merge_PVEs | ||
|
|
||
|
|
||
| def test_wm_is_elementwise_maximum(): | ||
| a = np.array([[0.5, 0.3, 0.2], [0.1, 0.1, 0.8]]) | ||
| b = np.array([[0.1, 0.2, 0.7], [0.4, 0.3, 0.3]]) | ||
| np.testing.assert_allclose(merge_PVEs(a, b)[..., 2], [0.7, 0.8]) | ||
|
|
||
|
|
||
| def test_larger_gm_source_wins(): | ||
| a = np.array([[0.6, 0.2, 0.2]]) | ||
| b = np.array([[0.2, 0.5, 0.3]]) | ||
| np.testing.assert_allclose(merge_PVEs(a, b), b) | ||
|
|
||
|
|
||
| def test_tie_prefers_a_and_renormalizes(): | ||
| a = np.array([[0.6, 0.2, 0.2]]) | ||
| b = np.array([[0.4, 0.2, 0.4]]) | ||
| np.testing.assert_allclose(merge_PVEs(a, b), [[0.45, 0.15, 0.4]]) |
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 |
|---|---|---|
|
|
@@ -23,14 +23,14 @@ def crop_to_nonzero(img): | |
| return nib.Nifti1Image(cropped_data, new_affine) | ||
|
|
||
|
|
||
| def prepare_t1_for_nn(t1_img): | ||
| def prepare_t1_for_nn(t1_img, orientation="RAS"): | ||
| t1_img_cropped = crop_to_nonzero(t1_img) | ||
|
|
||
| t1_img_conformed = nbp.conform( | ||
| t1_img_cropped, | ||
| out_shape=(256, 256, 256), | ||
| voxel_size=(1.0, 1.0, 1.0), | ||
| orientation="RAS", | ||
| orientation=orientation, | ||
| order=1, | ||
| ) | ||
|
|
||
|
|
@@ -49,3 +49,38 @@ def resample_output(output, conformed_affine, t1_img): | |
| t1_img, | ||
| order=0, | ||
| ) | ||
|
|
||
|
|
||
| def merge_PVEs(PVE_a, PVE_b): | ||
|
36000 marked this conversation as resolved.
Member
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. Any chance to add a test of this?
Collaborator
Author
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. Yes, I can do that |
||
| """ | ||
| Merge two PVE (Partial Volume Estimation) images, PVE_a and PVE_b, | ||
| into a single PVE image. The merging is done by taking the maximum | ||
| WM estimate from both images, then the maximum GM estimate, and | ||
| finally normalizing. | ||
| """ | ||
|
|
||
| csf_a, gm_a, wm_a = PVE_a[..., 0], PVE_a[..., 1], PVE_a[..., 2] | ||
| csf_b, gm_b, wm_b = PVE_b[..., 0], PVE_b[..., 1], PVE_b[..., 2] | ||
|
|
||
| new_wm = np.maximum(wm_a, wm_b) | ||
|
|
||
| # 2. Choose the source (a or b) with the larger GM estimate, per voxel | ||
| use_a = gm_a >= gm_b | ||
| chosen_csf = np.where(use_a, csf_a, csf_b) | ||
| chosen_gm = np.where(use_a, gm_a, gm_b) | ||
|
|
||
| # 3. Normalize chosen csf+gm to fill the remaining mass (1 - new_wm) | ||
| remaining = 1.0 - new_wm | ||
| chosen_sum = chosen_csf + chosen_gm | ||
|
|
||
| # avoid divide-by-zero where chosen_sum is 0 (e.g. pure-WM voxel) | ||
| scale = np.divide( | ||
| remaining, chosen_sum, out=np.zeros_like(chosen_sum), where=chosen_sum > 0 | ||
| ) | ||
|
|
||
| new_csf = chosen_csf * scale | ||
| new_gm = chosen_gm * scale | ||
|
|
||
| PVE = np.stack([new_csf, new_gm, new_wm], axis=-1) | ||
|
|
||
| return PVE | ||
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
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
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.