Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf14770

Browse files
committed
ENH: Add type annotations to pacify mypy
1 parentbea81f3 commitbf14770

22 files changed

+93
-81
lines changed

‎nibabel/analyze.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@
8181
can be loaded with and without a default flip, so the saved zoom will not
8282
constrain the affine.
8383
"""
84+
from __future__importannotations
85+
86+
fromtypingimportType
8487

8588
importnumpyasnp
8689

8790
from .arrayproxyimportArrayProxy
8891
from .arraywritersimportArrayWriter,WriterError,get_slope_inter,make_array_writer
8992
from .batteryrunnersimportReport
9093
from .fileholdersimportcopy_file_map
91-
from .spatialimagesimportHeaderDataError,HeaderTypeError,SpatialImage
94+
from .spatialimagesimportHeaderDataError,HeaderTypeError,SpatialHeader,SpatialImage
9295
from .volumeutilsimport (
9396
apply_read_scaling,
9497
array_from_file,
@@ -131,7 +134,7 @@
131134
('glmax','i4'),
132135
('glmin','i4'),
133136
]
134-
data_history_dtd= [
137+
data_history_dtd:list[tuple[str,str]|tuple[str,str,tuple[int, ...]]]= [
135138
('descrip','S80'),
136139
('aux_file','S24'),
137140
('orient','S1'),
@@ -172,7 +175,7 @@
172175
data_type_codes=make_dt_codes(_dtdefs)
173176

174177

175-
classAnalyzeHeader(LabeledWrapStruct):
178+
classAnalyzeHeader(LabeledWrapStruct,SpatialHeader):
176179
"""Class for basic analyze header
177180
178181
Implements zoom-only setting of affine transform, and no image
@@ -892,11 +895,11 @@ def may_contain_header(klass, binaryblock):
892895
classAnalyzeImage(SpatialImage):
893896
"""Class for basic Analyze format image"""
894897

895-
header_class=AnalyzeHeader
898+
header_class:Type[AnalyzeHeader]=AnalyzeHeader
896899
_meta_sniff_len=header_class.sizeof_hdr
897-
files_types= (('image','.img'), ('header','.hdr'))
898-
valid_exts= ('.img','.hdr')
899-
_compressed_suffixes= ('.gz','.bz2','.zst')
900+
files_types:tuple[tuple[str,str], ...]= (('image','.img'), ('header','.hdr'))
901+
valid_exts:tuple[str, ...]= ('.img','.hdr')
902+
_compressed_suffixes:tuple[str, ...]= ('.gz','.bz2','.zst')
900903

901904
makeable=True
902905
rw=True

‎nibabel/benchmarks/bench_arrayproxy_slicing.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
# if memory_profiler is installed, we get memory usage results
2828
try:
29-
frommemory_profilerimportmemory_usage
29+
frommemory_profilerimportmemory_usage# type: ignore
3030
exceptImportError:
3131
memory_usage=None
3232

‎nibabel/brikhead.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
am aware) always be >= 1. This permits sub-brick indexing common in AFNI
2828
programs (e.g., example4d+orig'[0]').
2929
"""
30+
from __future__importannotations
3031

3132
importos
3233
importre
@@ -476,9 +477,9 @@ class AFNIImage(SpatialImage):
476477
True
477478
"""
478479

479-
header_class=AFNIHeader
480-
valid_exts= ('.brik','.head')
481-
files_types= (('image','.brik'), ('header','.head'))
480+
header_class:type=AFNIHeader
481+
valid_exts:tuple[str, ...]= ('.brik','.head')
482+
files_types:tuple[tuple[str,str], ...]= (('image','.brik'), ('header','.head'))
482483
_compressed_suffixes= ('.gz','.bz2','.Z','.zst')
483484
makeable=False
484485
rw=False

‎nibabel/casting.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Most routines work round some numpy oddities in floating point precision and
44
casting. Others work round numpy casting to and from python ints
55
"""
6+
from __future__importannotations
67

78
importwarnings
89
fromnumbersimportIntegral
@@ -110,7 +111,7 @@ def float_to_int(arr, int_type, nan2zero=True, infmax=False):
110111

111112

112113
# Cache range values
113-
_SHARED_RANGES= {}
114+
_SHARED_RANGES:dict[tuple[type,type],tuple[np.number,np.number]]= {}
114115

115116

116117
defshared_range(flt_type,int_type):

‎nibabel/cmdline/dicomfs.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class dummy_fuse:
2525

2626

2727
try:
28-
importfuse
28+
importfuse# type: ignore
2929

3030
uid=os.getuid()
3131
gid=os.getgid()

‎nibabel/ecat.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
from .arraywritersimportmake_array_writer
5252
from .filesliceimportcanonical_slicers,predict_shape,slice2outax
53-
from .spatialimagesimportSpatialImage
53+
from .spatialimagesimportSpatialHeader,SpatialImage
5454
from .volumeutilsimportarray_from_file,make_dt_codes,native_code,swapped_code
5555
from .wrapstructimportWrapStruct
5656

@@ -243,7 +243,7 @@
243243
patient_orient_neurological= [1,3,5,7]
244244

245245

246-
classEcatHeader(WrapStruct):
246+
classEcatHeader(WrapStruct,SpatialHeader):
247247
"""Class for basic Ecat PET header
248248
249249
Sub-parts of standard Ecat File

‎nibabel/externals/netcdf.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ def __setattr__(self, attr, value):
871871
pass
872872
self.__dict__[attr]=value
873873

874+
@property
874875
defisrec(self):
875876
"""Returns whether the variable has a record dimension or not.
876877
@@ -881,16 +882,15 @@ def isrec(self):
881882
882883
"""
883884
returnbool(self.data.shape)andnotself._shape[0]
884-
isrec=property(isrec)
885885

886+
@property
886887
defshape(self):
887888
"""Returns the shape tuple of the data variable.
888889
889890
This is a read-only attribute and can not be modified in the
890891
same manner of other numpy arrays.
891892
"""
892893
returnself.data.shape
893-
shape=property(shape)
894894

895895
defgetValue(self):
896896
"""

‎nibabel/filebasedimages.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Common interface for any image format--volume or surface, binary or xml."""
10+
from __future__importannotations
1011

1112
importio
1213
fromcopyimportdeepcopy
14+
fromtypingimportType
1315
fromurllibimportrequest
1416

1517
from .fileholdersimportFileHolder
@@ -144,14 +146,14 @@ class FileBasedImage:
144146
work.
145147
"""
146148

147-
header_class=FileBasedHeader
148-
_meta_sniff_len=0
149-
files_types= (('image',None),)
150-
valid_exts= ()
151-
_compressed_suffixes= ()
149+
header_class:Type[FileBasedHeader]=FileBasedHeader
150+
_meta_sniff_len:int=0
151+
files_types:tuple[tuple[str,str|None], ...]= (('image',None),)
152+
valid_exts:tuple[str, ...]= ()
153+
_compressed_suffixes:tuple[str, ...]= ()
152154

153-
makeable=True# Used in test code
154-
rw=True# Used in test code
155+
makeable:bool=True# Used in test code
156+
rw:bool=True# Used in test code
155157

156158
def__init__(self,header=None,extra=None,file_map=None):
157159
"""Initialize image

‎nibabel/freesurfer/mghformat.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
1111
Author: Krish Subramaniam
1212
"""
13+
from __future__importannotations
14+
1315
fromos.pathimportsplitext
16+
fromtypingimportType
1417

1518
importnumpyasnp
1619

@@ -21,7 +24,7 @@
2124
from ..fileholdersimportFileHolder
2225
from ..filename_parserimport_stringify_path
2326
from ..openersimportImageOpener
24-
from ..spatialimagesimportHeaderDataError,SpatialImage
27+
from ..spatialimagesimportHeaderDataError,SpatialHeader,SpatialImage
2528
from ..volumeutilsimportRecoder,array_from_file,array_to_file,endian_codes
2629
from ..wrapstructimportLabeledWrapStruct
2730

@@ -87,7 +90,7 @@ class MGHError(Exception):
8790
"""
8891

8992

90-
classMGHHeader(LabeledWrapStruct):
93+
classMGHHeader(LabeledWrapStruct,SpatialHeader):
9194
"""Class for MGH format header
9295
9396
The header also consists of the footer data which MGH places after the data
@@ -461,7 +464,7 @@ def diagnose_binaryblock(klass, binaryblock, endianness=None):
461464
classMGHImage(SpatialImage,SerializableImage):
462465
"""Class for MGH format image"""
463466

464-
header_class=MGHHeader
467+
header_class:Type[MGHHeader]=MGHHeader
465468
valid_exts= ('.mgh','.mgz')
466469
# Register that .mgz extension signals gzip compression
467470
ImageOpener.compress_ext_map['.mgz']=ImageOpener.gz_def

‎nibabel/gifti/gifti.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
The Gifti specification was (at time of writing) available as a PDF download
1212
from http://www.nitrc.org/projects/gifti/
1313
"""
14+
from __future__importannotations
1415

1516
importbase64
1617
importsys
1718
importwarnings
19+
fromtypingimportType
1820

1921
importnumpyasnp
2022

@@ -577,7 +579,7 @@ class GiftiImage(xml.XmlSerializable, SerializableImage):
577579
# The parser will in due course be a GiftiImageParser, but we can't set
578580
# that now, because it would result in a circular import. We set it after
579581
# the class has been defined, at the end of the class definition.
580-
parser=None
582+
parser:Type[xml.XmlParser]
581583

582584
def__init__(
583585
self,
@@ -832,17 +834,18 @@ def _to_xml_element(self):
832834
GIFTI.append(dar._to_xml_element())
833835
returnGIFTI
834836

835-
defto_xml(self,enc='utf-8'):
837+
defto_xml(self,enc='utf-8')->bytes:
836838
"""Return XML corresponding to image content"""
837839
header=b"""<?xml version="1.0" encoding="UTF-8"?>
838840
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
839841
"""
840842
returnheader+super().to_xml(enc)
841843

842844
# Avoid the indirection of going through to_file_map
843-
to_bytes=to_xml
845+
defto_bytes(self,enc='utf-8'):
846+
returnself.to_xml(enc=enc)
844847

845-
defto_file_map(self,file_map=None):
848+
defto_file_map(self,file_map=None,enc='utf-8'):
846849
"""Save the current image to the specified file_map
847850
848851
Parameters
@@ -858,7 +861,7 @@ def to_file_map(self, file_map=None):
858861
iffile_mapisNone:
859862
file_map=self.file_map
860863
withfile_map['image'].get_prepare_fileobj('wb')asf:
861-
f.write(self.to_xml())
864+
f.write(self.to_xml(enc=enc))
862865

863866
@classmethod
864867
deffrom_file_map(klass,file_map,buffer_size=35000000,mmap=True):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp