-
Notifications
You must be signed in to change notification settings - Fork 0
Add features for conversion QC and scalar export to original formats #40
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
mattcieslak
wants to merge
1
commit into
main
Choose a base branch
from
nice-to-haves-0
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 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
|
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. The |
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,34 @@ | ||
| # file generated by setuptools-scm | ||
| # don't change, don't track in version control | ||
|
|
||
| __all__ = [ | ||
| "__version__", | ||
| "__version_tuple__", | ||
| "version", | ||
| "version_tuple", | ||
| "__commit_id__", | ||
| "commit_id", | ||
| ] | ||
|
|
||
| TYPE_CHECKING = False | ||
| if TYPE_CHECKING: | ||
| from typing import Tuple | ||
| from typing import Union | ||
|
|
||
| VERSION_TUPLE = Tuple[Union[int, str], ...] | ||
| COMMIT_ID = Union[str, None] | ||
| else: | ||
| VERSION_TUPLE = object | ||
| COMMIT_ID = object | ||
|
|
||
| version: str | ||
| __version__: str | ||
| __version_tuple__: VERSION_TUPLE | ||
| version_tuple: VERSION_TUPLE | ||
| commit_id: COMMIT_ID | ||
| __commit_id__: COMMIT_ID | ||
|
|
||
| __version__ = version = '0.1.dev115+g7f4c6030e' | ||
| __version_tuple__ = version_tuple = (0, 1, 'dev115', 'g7f4c6030e') | ||
|
|
||
| __commit_id__ = commit_id = None |
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,116 @@ | ||
| """Diagnostic image helpers for conversion commands.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import nibabel as nb | ||
| import numpy as np | ||
|
|
||
| from modelarrayio.utils.cifti import extract_cifti_scalar_data | ||
| from modelarrayio.utils.fixels import nifti2_to_mif | ||
| from modelarrayio.utils.voxels import flattened_image | ||
|
|
||
|
|
||
| def summarize_rows(rows) -> dict[str, np.ndarray]: | ||
| """Compute common diagnostics from a sequence of 1-D subject arrays.""" | ||
| stacked = np.vstack(rows) | ||
| return { | ||
| 'mean': np.nanmean(stacked, axis=0).astype(np.float32), | ||
| 'n_non_nan': np.sum(~np.isnan(stacked), axis=0).astype(np.float32), | ||
| 'element_id': np.arange(stacked.shape[1], dtype=np.float32), | ||
| } | ||
|
|
||
|
|
||
| def verify_nifti_element_mapping(group_mask_img, group_mask_matrix): | ||
| """Verify NIfTI group-mask flattening order matches element indices.""" | ||
| expected = np.arange(int(group_mask_matrix.sum()), dtype=np.float32) | ||
| element_volume = np.zeros(group_mask_matrix.shape, dtype=np.float32) | ||
| element_volume[group_mask_matrix] = expected | ||
| element_img = nb.Nifti1Image( | ||
| element_volume, | ||
| affine=group_mask_img.affine, | ||
| header=group_mask_img.header, | ||
| ) | ||
| extracted = flattened_image(element_img, group_mask_img, group_mask_matrix) | ||
| if not np.array_equal(extracted.astype(np.int64), expected.astype(np.int64)): | ||
| raise ValueError('Element ID mapping check failed for NIfTI group-mask flattening.') | ||
|
|
||
|
|
||
| def write_nifti_diagnostics( | ||
| *, | ||
| maps: list[str], | ||
| scalar_name: str, | ||
| diagnostics: dict[str, np.ndarray], | ||
| group_mask_img, | ||
| group_mask_matrix, | ||
| output_dir: Path, | ||
| ): | ||
| header = group_mask_img.header.copy() | ||
| header.set_data_dtype(np.float32) | ||
| for name in maps: | ||
| out_file = output_dir / f'{scalar_name}_{name}.nii.gz' | ||
| data = np.zeros(group_mask_matrix.shape, dtype=np.float32) | ||
| data[group_mask_matrix] = diagnostics[name] | ||
| nb.Nifti1Image(data, affine=group_mask_img.affine, header=header).to_filename(out_file) | ||
|
|
||
|
|
||
| def verify_cifti_element_mapping(template_cifti, reference_brain_names): | ||
| """Verify CIFTI extraction order matches element indices.""" | ||
| expected = np.arange(reference_brain_names.shape[0], dtype=np.float32) | ||
| test_img = nb.Cifti2Image( | ||
| expected.reshape(1, -1), | ||
| header=template_cifti.header, | ||
| nifti_header=template_cifti.nifti_header, | ||
| ) | ||
| recovered, _ = extract_cifti_scalar_data(test_img, reference_brain_names=reference_brain_names) | ||
| if not np.array_equal(recovered.astype(np.int64), expected.astype(np.int64)): | ||
| raise ValueError('Element ID mapping check failed for CIFTI greyordinate ordering.') | ||
|
|
||
|
|
||
| def write_cifti_diagnostics( | ||
| *, | ||
| maps: list[str], | ||
| scalar_name: str, | ||
| diagnostics: dict[str, np.ndarray], | ||
| template_cifti, | ||
| output_dir: Path, | ||
| ): | ||
| for name in maps: | ||
| out_file = output_dir / f'{scalar_name}_{name}.dscalar.nii' | ||
| nb.Cifti2Image( | ||
| diagnostics[name].reshape(1, -1), | ||
| header=template_cifti.header, | ||
| nifti_header=template_cifti.nifti_header, | ||
| ).to_filename(out_file) | ||
|
|
||
|
|
||
| def verify_mif_element_mapping(template_nifti2, num_elements: int): | ||
| """Verify fixel vector reshape/squeeze mapping remains identity.""" | ||
| expected = np.arange(num_elements, dtype=np.float32) | ||
| test_img = nb.Nifti2Image( | ||
| expected.reshape(-1, 1, 1), | ||
| affine=template_nifti2.affine, | ||
| header=template_nifti2.header, | ||
| ) | ||
| recovered = test_img.get_fdata(dtype=np.float32).squeeze() | ||
| if not np.array_equal(recovered.astype(np.int64), expected.astype(np.int64)): | ||
| raise ValueError('Element ID mapping check failed for MIF fixel vector ordering.') | ||
|
|
||
|
|
||
| def write_mif_diagnostics( | ||
| *, | ||
| maps: list[str], | ||
| scalar_name: str, | ||
| diagnostics: dict[str, np.ndarray], | ||
| template_nifti2, | ||
| output_dir: Path, | ||
| ): | ||
| for name in maps: | ||
| out_file = output_dir / f'{scalar_name}_{name}.mif' | ||
| temp_nifti2 = nb.Nifti2Image( | ||
| diagnostics[name].reshape(-1, 1, 1), | ||
| affine=template_nifti2.affine, | ||
| header=template_nifti2.header, | ||
| ) | ||
| nifti2_to_mif(temp_nifti2, out_file) |
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.
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.