Skip to content

Commit 03b08ed

Browse files
committed
Formatting and code fixes
- Fix code formatting issues - Remove unused imports - Fix undefined reference to ZstdFile when pyzstd missing
1 parent bd35af7 commit 03b08ed

File tree

6 files changed

+18
-30
lines changed

6 files changed

+18
-30
lines changed

nibabel/analyze.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
from .fileholders import copy_file_map
9696
from .batteryrunners import Report
9797
from .arrayproxy import ArrayProxy
98+
from .openers import HAVE_ZSTD
9899

99100
# Sub-parts of standard analyze header from
100101
# Mayo dbh.h file
@@ -907,11 +908,8 @@ class AnalyzeImage(SpatialImage):
907908
files_types = (('image', '.img'), ('header', '.hdr'))
908909
valid_exts = ('.img', '.hdr')
909910
_compressed_suffixes = ('.gz', '.bz2')
910-
try: # If pyzstd installed., add .zst suffix
911-
import pyzstd
911+
if HAVE_ZSTD: # If pyzstd installed., add .zst suffix
912912
_compressed_suffixes = (*_compressed_suffixes, '.zst')
913-
except ImportError:
914-
pass
915913

916914
makeable = True
917915
rw = True

nibabel/benchmarks/bench_fileslice.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,11 @@
1414
import numpy as np
1515

1616
from io import BytesIO
17-
from ..openers import ImageOpener
17+
from ..openers import ImageOpener, HAVE_ZSTD
1818
from ..fileslice import fileslice
1919
from ..rstutils import rst_table
2020
from ..tmpdirs import InTemporaryDirectory
2121

22-
try:
23-
import pyzstd
24-
ZSTD_INSTALLED = True
25-
except ImportError:
26-
ZSTD_INSTALLED = False
27-
2822
SHAPE = (64, 64, 32, 100)
2923
ROW_NAMES = [f'axis {i}, len {dim}' for i, dim in enumerate(SHAPE)]
3024
COL_NAMES = ['mid int',
@@ -110,7 +104,7 @@ def my_table(title, times, base):
110104
my_table('bz2 slice - raw (ratio)',
111105
np.dstack((bz2_times, bz2_times / bz2_base)),
112106
bz2_base)
113-
if ZSTD_INSTALLED:
107+
if HAVE_ZSTD:
114108
if zst:
115109
with InTemporaryDirectory():
116110
zst_times, zst_base = run_slices('data.zst', repeat)

nibabel/brikhead.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
ImageDataError
4444
)
4545
from .volumeutils import Recoder
46+
from .openers import HAVE_ZSTD
4647

4748
# used for doc-tests
4849
filepath = os.path.dirname(os.path.realpath(__file__))
@@ -491,14 +492,11 @@ class AFNIImage(SpatialImage):
491492
valid_exts = ('.brik', '.head')
492493
files_types = (('image', '.brik'), ('header', '.head'))
493494
_compressed_suffixes = ('.gz', '.bz2', '.Z')
495+
if HAVE_ZSTD: # If pyzstd installed., add .zst suffix
496+
_compressed_suffixes = (*_compressed_suffixes, '.zst')
494497
makeable = False
495498
rw = False
496499
ImageArrayProxy = AFNIArrayProxy
497-
try: # If pyzstd installed., add .zst suffix
498-
import pyzstd
499-
_compressed_suffixes = (*_compressed_suffixes, '.zst')
500-
except ImportError:
501-
pass
502500

503501
@classmethod
504502
def from_file_map(klass, file_map, *, mmap=True, keep_file_open=None):

nibabel/loadsave.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
import numpy as np
1414

1515
from .filename_parser import splitext_addext, _stringify_path
16-
from .openers import ImageOpener
16+
from .openers import ImageOpener, HAVE_ZSTD
1717
from .filebasedimages import ImageFileError
1818
from .imageclasses import all_image_classes
1919
from .arrayproxy import is_proxy
2020
from .deprecated import deprecate_with_version
2121

2222
_compressed_suffixes = ('.gz', '.bz2')
23-
try: # If pyzstd installed., add .zst suffix
24-
import pyzstd
23+
if HAVE_ZSTD: # If pyzstd installed., add .zst suffix
2524
_compressed_suffixes = (*_compressed_suffixes, '.zst')
26-
except ImportError:
27-
pass
2825

2926
def load(filename, **kwargs):
3027
r""" Load file given filename, guessing at file type

nibabel/minc1.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from .spatialimages import SpatialHeader, SpatialImage
1818
from .fileslice import canonical_slicers
19+
from .openers import HAVE_ZSTD
1920

2021
_dt_dict = {
2122
('b', 'unsigned'): np.uint8,
@@ -317,11 +318,8 @@ class Minc1Image(SpatialImage):
317318
valid_exts = ('.mnc',)
318319
files_types = (('image', '.mnc'),)
319320
_compressed_suffixes = ('.gz', '.bz2')
320-
try: # If pyzstd installed., add .zst suffix
321-
import pyzstd
321+
if HAVE_ZSTD: # If pyzstd installed., add .zst suffix
322322
_compressed_suffixes = (*_compressed_suffixes, '.zst')
323-
except ImportError:
324-
pass
325323

326324
makeable = True
327325
rw = False

nibabel/openers.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
# Enable .zst support if pyzstd installed.
4343
try:
4444
from pyzstd import ZstdFile
45+
HAVE_ZSTD = True
4546
except ImportError:
46-
pass
47+
HAVE_ZSTD = False
4748

4849
def _gzip_open(filename, mode='rb', compresslevel=9, keep_open=False):
4950

@@ -82,19 +83,21 @@ class Opener(object):
8283
"""
8384
gz_def = (_gzip_open, ('mode', 'compresslevel', 'keep_open'))
8485
bz2_def = (BZ2File, ('mode', 'buffering', 'compresslevel'))
85-
zstd_def = (ZstdFile, ('mode', 'level_or_option'))
8686
compress_ext_map = {
8787
'.gz': gz_def,
8888
'.bz2': bz2_def,
89-
'.zst': zstd_def,
9089
None: (open, ('mode', 'buffering')) # default
9190
}
91+
if HAVE_ZSTD: # add zst to ext map, if library exists
92+
zstd_def = (ZstdFile, ('mode', 'level_or_option'))
93+
compress_ext_map['.zst'] = zstd_def
9294
#: default compression level when writing gz and bz2 files
9395
default_compresslevel = 1
9496
#: default option for zst files
9597
default_zst_compresslevel = 3
9698
default_level_or_option = {"rb": None, "r": None,
97-
"wb": default_zst_compresslevel, "w": default_zst_compresslevel}
99+
"wb": default_zst_compresslevel,
100+
"w": default_zst_compresslevel}
98101
#: whether to ignore case looking for compression extensions
99102
compress_ext_icase = True
100103

0 commit comments

Comments
 (0)