Skip to content

Commit

Permalink
fixed up image dims for 4-D nifti files
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed May 29, 2024
1 parent d91a267 commit 9ef5d4d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ jobs:
- name: Install Package
run: python3 -m pip install -e .[test] -e ./extras[test]
- name: Pytest
run: pytest -vvs --cov fileformats --cov-config .coveragerc --cov-report xml .
run: |
REPO=$pwd
cd ~
pytest -vvs --cov fileformats --cov-config .coveragerc --cov-report xml $REPO
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
Expand Down
11 changes: 7 additions & 4 deletions extras/fileformats/extras/medimage/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ def nifti_data_array(nifti: Nifti) -> np.ndarray: # noqa

@MedicalImage.vox_sizes.register
def nifti_vox_sizes(nifti: Nifti) -> ty.Tuple[float, float, float]:
# FIXME: This won't work for 4-D files
return tuple(float(d) for d in nifti.metadata["pixdim"][1:4])
ndims = len(nifti_dims(nifti))
return tuple(float(d) for d in nifti.metadata["pixdim"][1 : ndims + 1])


@MedicalImage.dims.register
def nifti_dims(nifti: Nifti) -> ty.Tuple[int, int, int]:
# FIXME: This won't work for 4-D files
return tuple(int(d) for d in nifti.metadata["dim"][1:4])
dim_array = [int(d) for d in nifti.metadata["dim"]]
for i in range(1, len(dim_array)):
if all(d == 1 for d in dim_array[i:]):
break # Stop when the remaining dimensions are singletons
return tuple(dim_array[1:i])


@FileSet.generate_sample_data.register
Expand Down
4 changes: 2 additions & 2 deletions fileformats/medimage/nifti.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import typing as ty
from fileformats.generic import File
from fileformats.core import hook
from fileformats.core.mixin import (
WithSideCars, WithMagicNumber, WithAdjacentFiles)
from fileformats.core.mixin import WithSideCars, WithMagicNumber, WithAdjacentFiles
from fileformats.application import Json

# from fileformats.text import Tsv
from fileformats.application import Gzip
from .base import MedicalImage
Expand Down

0 comments on commit 9ef5d4d

Please sign in to comment.