Skip to content

Commit ab62558

Browse files
authored
Merge pull request #25 from nipype/fileformats-typing
fixes typing of read_metadata
2 parents c31c861 + af43bb6 commit ab62558

File tree

8 files changed

+40
-31
lines changed

8 files changed

+40
-31
lines changed

.github/workflows/ci-cd.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,19 @@ jobs:
115115
--log-errors
116116
--latest
117117
- name: Upload MRtrix3 install
118-
uses: actions/upload-artifact@v2
118+
uses: actions/upload-artifact@v4
119119
with:
120120
name: MRtrix3
121121
path: ${{ env.MRTRIX_INSTALL}}
122122
- name: Upload auto-gen pydra
123-
uses: actions/upload-artifact@v2
123+
uses: actions/upload-artifact@v4
124124
with:
125125
name: AutoGen
126126
path: pydra/tasks/mrtrix3/${{ env.SUBPKG_NAME }}
127127
- name: Write version file
128128
run: echo $MRTRIX_VERSION > mrtrix3_version.txt
129129
- name: Upload version file
130-
uses: actions/upload-artifact@v2
130+
uses: actions/upload-artifact@v4
131131
with:
132132
name: VersionFile
133133
path: mrtrix3_version.txt
@@ -352,7 +352,7 @@ jobs:
352352
git tag -d ${{ steps.latest_tag.outputs.TAG }};
353353
git tag -a ${{ steps.latest_tag.outputs.TAG }} -m"Tag used to create a pydra-mrtrix3 $MRTRIX_VERSION release";
354354
- name: Set up Python 3.11
355-
uses: actions/setup-python@v2
355+
uses: actions/setup-python@v3
356356
with:
357357
python-version: 3.11
358358
- name: Install build tools
@@ -362,7 +362,7 @@ jobs:
362362
- name: Check distributions
363363
run: twine check dist/*
364364
- name: Upload sdist
365-
uses: actions/upload-artifact@v2
365+
uses: actions/upload-artifact@v4
366366
with:
367367
name: SDist
368368
path: dist/*.tar.gz

conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import typing as ty
23
import pytest
34
from pathlib import Path
45

@@ -22,9 +23,10 @@ def cli_parse_only():
2223
if os.getenv("_PYTEST_RAISE", "0") != "0":
2324

2425
@pytest.hookimpl(tryfirst=True)
25-
def pytest_exception_interact(call):
26-
raise call.excinfo.value
26+
def pytest_exception_interact(call: pytest.CallInfo[ty.Any]) -> None:
27+
if call.excinfo is not None:
28+
raise call.excinfo.value
2729

2830
@pytest.hookimpl(tryfirst=True)
29-
def pytest_internalerror(excinfo):
31+
def pytest_internalerror(excinfo: pytest.ExceptionInfo[BaseException]) -> None:
3032
raise excinfo.value

related-packages/fileformats-extras/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from pathlib import Path
44
import tempfile
5+
import typing as ty
56
import pytest
67
from fileformats.medimage import DicomDir, Nifti
78

@@ -24,11 +25,12 @@
2425
if os.getenv("_PYTEST_RAISE", "0") != "0":
2526

2627
@pytest.hookimpl(tryfirst=True)
27-
def pytest_exception_interact(call):
28-
raise call.excinfo.value
28+
def pytest_exception_interact(call: pytest.CallInfo[ty.Any]) -> None:
29+
if call.excinfo is not None:
30+
raise call.excinfo.value
2931

3032
@pytest.hookimpl(tryfirst=True)
31-
def pytest_internalerror(excinfo):
33+
def pytest_internalerror(excinfo: pytest.ExceptionInfo[BaseException]) -> None:
3234
raise excinfo.value
3335

3436

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import numpy as np
1+
import typing # noqa: F401
2+
import numpy.typing
23
from fileformats.core import extra_implementation
34
from fileformats.medimage import DwiEncoding
5+
from fileformats.medimage.diffusion import EncodingArrayType
46
from fileformats.medimage_mrtrix3 import BFile
57

68

79
@extra_implementation(DwiEncoding.read_array)
8-
def bfile_read_array(bfile: BFile) -> np.ndarray:
9-
return np.asarray(
10+
def bfile_read_array(
11+
bfile: BFile,
12+
) -> EncodingArrayType:
13+
return numpy.asarray(
1014
[[float(x) for x in ln.split()] for ln in bfile.read_contents().splitlines()]
1115
)

related-packages/fileformats/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import logging
3+
import typing as ty
34
from pathlib import Path
45
import tempfile
56
import pytest
@@ -23,11 +24,12 @@
2324
if os.getenv("_PYTEST_RAISE", "0") != "0":
2425

2526
@pytest.hookimpl(tryfirst=True)
26-
def pytest_exception_interact(call):
27-
raise call.excinfo.value
27+
def pytest_exception_interact(call: pytest.CallInfo[ty.Any]) -> None:
28+
if call.excinfo is not None:
29+
raise call.excinfo.value
2830

2931
@pytest.hookimpl(tryfirst=True)
30-
def pytest_internalerror(excinfo):
32+
def pytest_internalerror(excinfo: pytest.ExceptionInfo[BaseException]) -> None:
3133
raise excinfo.value
3234

3335

related-packages/fileformats/fileformats/medimage_mrtrix3/dwi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from fileformats.core import validated_property
12
from fileformats.core.mixin import WithAdjacentFiles
23
from fileformats.medimage import DwiEncoding, Nifti1, NiftiGz, NiftiX, NiftiGzX
34
from .image import ImageFormat, ImageHeader, ImageFormatGz
@@ -11,7 +12,7 @@ class BFile(DwiEncoding):
1112

1213
# NIfTI file format gzipped with BIDS side car
1314
class WithBFile(WithAdjacentFiles):
14-
@property
15+
@validated_property
1516
def encoding(self) -> BFile:
1617
return BFile(self.select_by_ext(BFile))
1718

related-packages/fileformats/fileformats/medimage_mrtrix3/image.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
22
import typing as ty
3-
from fileformats.core import FileSet, extra_implementation
4-
from fileformats.generic import File
3+
from fileformats.core import FileSet, extra_implementation, validated_property
4+
from fileformats.generic import BinaryFile
55
from fileformats.application.archive import BaseGzip
66
from fileformats.core.mixin import WithMagicNumber
77
from fileformats.core.exceptions import FormatMismatchError
@@ -12,12 +12,12 @@ class MultiLineMetadataValue(list):
1212
pass
1313

1414

15-
class BaseMrtrixImage(WithMagicNumber, fileformats.medimage.MedicalImage, File):
15+
class BaseMrtrixImage(WithMagicNumber, fileformats.medimage.MedicalImage, BinaryFile):
1616

1717
magic_number = b"mrtrix image\n"
1818
binary = True
1919

20-
@property
20+
@validated_property
2121
def data_fspath(self):
2222
data_fspath = self.metadata["file"].split()[0]
2323
if data_fspath == ".":
@@ -48,8 +48,8 @@ class ImageFormat(BaseMrtrixImage):
4848
ext = ".mif"
4949
iana_mime = "application/x-mrtrix-image-format"
5050

51-
@property
52-
def check_data_file(self):
51+
@validated_property
52+
def _check_data_file(self):
5353
if self.data_fspath != self.fspath:
5454
raise FormatMismatchError(
5555
f"Data file ('{self.data_fspath}') is not set to the same file as header "
@@ -73,7 +73,7 @@ class ImageHeader(BaseMrtrixImage):
7373
ext = ".mih"
7474
iana_mime = "application/x-mrtrix-image-header"
7575

76-
@property
76+
@validated_property
7777
def data_file(self):
7878
return ImageDataFile(self.data_fspath)
7979

@@ -84,14 +84,14 @@ def __attrs_post_init__(self):
8484
super().__attrs_post_init__()
8585

8686

87-
class ImageDataFile(File):
87+
class ImageDataFile(BinaryFile):
8888

8989
ext = ".dat"
9090

9191

9292
@extra_implementation(FileSet.read_metadata)
9393
def mrtrix_read_metadata(
94-
mif: BaseMrtrixImage, selected_keys: ty.Optional[ty.Collection[str]] = None
94+
mif: BaseMrtrixImage, **kwargs: ty.Any
9595
) -> ty.Mapping[str, ty.Any]:
9696
metadata = {}
9797
with open(mif.fspath, "rb") as f:
@@ -127,6 +127,4 @@ def mrtrix_read_metadata(
127127
else:
128128
metadata[key] = value
129129
line = f.readline().decode("utf-8")
130-
if selected_keys:
131-
metadata = {k: v for k, v in metadata.items() if k in selected_keys}
132130
return metadata

related-packages/fileformats/fileformats/medimage_mrtrix3/track.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from fileformats.generic import File
1+
from fileformats.generic import BinaryFile
22
from fileformats.core.mixin import WithMagicNumber
33

44

5-
class Tracks(WithMagicNumber, File):
5+
class Tracks(WithMagicNumber, BinaryFile):
66

77
ext = ".tck"
88
magic_number = b"mrtrix tracks\n"

0 commit comments

Comments
 (0)