From 9ef5d4d17ec273d7a25d4a9e79030d880a76e3b8 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Thu, 30 May 2024 00:39:30 +0930 Subject: [PATCH] fixed up image dims for 4-D nifti files --- .github/workflows/ci-cd.yml | 5 ++++- extras/fileformats/extras/medimage/nifti.py | 11 +++++++---- fileformats/medimage/nifti.py | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index ecb6e9e..8ae5bc0 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -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: diff --git a/extras/fileformats/extras/medimage/nifti.py b/extras/fileformats/extras/medimage/nifti.py index 2fef5af..40cf74a 100644 --- a/extras/fileformats/extras/medimage/nifti.py +++ b/extras/fileformats/extras/medimage/nifti.py @@ -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 diff --git a/fileformats/medimage/nifti.py b/fileformats/medimage/nifti.py index 015780b..41f6cb2 100644 --- a/fileformats/medimage/nifti.py +++ b/fileformats/medimage/nifti.py @@ -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