Skip to content

Commit 05a0b2f

Browse files
committed
ENH: Fully annotate nibabel.dataobj_images
1 parent 8eee7f5 commit 05a0b2f

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

nibabel/dataobj_images.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,31 @@
77
* returns an array from ``numpy.asanyarray(obj)``;
88
* has an attribute or property ``shape``.
99
"""
10+
from __future__ import annotations
11+
12+
import typing as ty
1013

1114
import numpy as np
15+
import numpy.typing as npt
1216

17+
from .arrayproxy import ArrayLike
1318
from .deprecated import deprecate_with_version
14-
from .filebasedimages import FileBasedImage
19+
from .filebasedimages import FileBasedHeader, FileBasedImage, FileMap, FileSpec
1520

1621

1722
class DataobjImage(FileBasedImage):
1823
"""Template class for images that have dataobj data stores"""
1924

20-
def __init__(self, dataobj, header=None, extra=None, file_map=None):
25+
_data_cache: np.ndarray | None
26+
_fdata_cache: np.ndarray | None
27+
28+
def __init__(
29+
self,
30+
dataobj: ArrayLike,
31+
header: FileBasedHeader | ty.Mapping | None = None,
32+
extra: ty.Mapping | None = None,
33+
file_map: FileMap | None = None,
34+
):
2135
"""Initialize dataobj image
2236
2337
The datobj image is a combination of (dataobj, header), with optional
@@ -40,11 +54,11 @@ def __init__(self, dataobj, header=None, extra=None, file_map=None):
4054
"""
4155
super().__init__(header=header, extra=extra, file_map=file_map)
4256
self._dataobj = dataobj
43-
self._fdata_cache = None
4457
self._data_cache = None
58+
self._fdata_cache = None
4559

4660
@property
47-
def dataobj(self):
61+
def dataobj(self) -> ArrayLike:
4862
return self._dataobj
4963

5064
@deprecate_with_version(
@@ -202,7 +216,11 @@ def get_data(self, caching='fill'):
202216
self._data_cache = data
203217
return data
204218

205-
def get_fdata(self, caching='fill', dtype=np.float64):
219+
def get_fdata(
220+
self,
221+
caching: ty.Literal['fill', 'unchanged'] = 'fill',
222+
dtype: npt.DTypeLike = np.float64,
223+
) -> np.ndarray:
206224
"""Return floating point image data with necessary scaling applied
207225
208226
The image ``dataobj`` property can be an array proxy or an array. An
@@ -351,7 +369,7 @@ def get_fdata(self, caching='fill', dtype=np.float64):
351369
return data
352370

353371
@property
354-
def in_memory(self):
372+
def in_memory(self) -> bool:
355373
"""True when any array data is in memory cache
356374
357375
There are separate caches for `get_data` reads and `get_fdata` reads.
@@ -363,7 +381,7 @@ def in_memory(self):
363381
or self._data_cache is not None
364382
)
365383

366-
def uncache(self):
384+
def uncache(self) -> None:
367385
"""Delete any cached read of data from proxied data
368386
369387
Remember there are two types of images:
@@ -392,15 +410,21 @@ def uncache(self):
392410
self._data_cache = None
393411

394412
@property
395-
def shape(self):
413+
def shape(self) -> tuple[int, ...]:
396414
return self._dataobj.shape
397415

398416
@property
399-
def ndim(self):
417+
def ndim(self) -> int:
400418
return self._dataobj.ndim
401419

402420
@classmethod
403-
def from_file_map(klass, file_map, *, mmap=True, keep_file_open=None):
421+
def from_file_map(
422+
klass,
423+
file_map: FileMap,
424+
*,
425+
mmap: bool | ty.Literal['c', 'r'] = True,
426+
keep_file_open: bool | None = None,
427+
):
404428
"""Class method to create image from mapping in ``file_map``
405429
406430
Parameters
@@ -433,7 +457,13 @@ def from_file_map(klass, file_map, *, mmap=True, keep_file_open=None):
433457
raise NotImplementedError
434458

435459
@classmethod
436-
def from_filename(klass, filename, *, mmap=True, keep_file_open=None):
460+
def from_filename(
461+
klass,
462+
filename: FileSpec,
463+
*,
464+
mmap: bool | ty.Literal['c', 'r'] = True,
465+
keep_file_open: bool | None = None,
466+
):
437467
"""Class method to create image from filename `filename`
438468
439469
Parameters

0 commit comments

Comments
 (0)