Skip to content

Commit e9188e8

Browse files
committed
TEST: Adjust existing tests/benchmarks to import zstd from _compression,
rather than calling optional_package again. Use "zstd" instead of "pyzstd"
1 parent d537d33 commit e9188e8

File tree

6 files changed

+13
-17
lines changed

6 files changed

+13
-17
lines changed

nibabel/benchmarks/bench_fileslice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
from ..optpkg import optional_package
2020
from ..rstutils import rst_table
2121
from ..tmpdirs import InTemporaryDirectory
22+
from .._compression import HAVE_ZSTD
2223

2324
SHAPE = (64, 64, 32, 100)
2425
ROW_NAMES = [f'axis {i}, len {dim}' for i, dim in enumerate(SHAPE)]
2526
COL_NAMES = ['mid int', 'step 1', 'half step 1', 'step mid int']
26-
HAVE_ZSTD = optional_package('pyzstd')[1]
2727

2828

2929
def _slices_for_len(L):

nibabel/tests/test_analyze.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@
3838
suppress_warnings,
3939
)
4040
from ..tmpdirs import InTemporaryDirectory
41+
from .._compression import HAVE_ZSTD
4142
from . import test_spatialimages as tsi
4243
from . import test_wrapstruct as tws
4344

44-
HAVE_ZSTD = optional_package('pyzstd')[1]
45-
4645
header_file = os.path.join(data_path, 'analyze.hdr')
4746

4847
PIXDIM0_MSG = 'pixdim[1,2,3] should be non-zero; setting 0 dims to 1'

nibabel/tests/test_loadsave.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
from ..optpkg import optional_package
2323
from ..testing import deprecated_to, expires
2424
from ..tmpdirs import InTemporaryDirectory
25+
from .._compression import HAVE_ZSTD
2526

2627
_, have_scipy, _ = optional_package('scipy')
27-
_, have_pyzstd, _ = optional_package('pyzstd')
2828

2929
import pytest
3030
from numpy.testing import assert_almost_equal, assert_array_equal
@@ -84,7 +84,7 @@ def test_load_empty_image():
8484

8585
@pytest.mark.parametrize('extension', ['.gz', '.bz2', '.zst'])
8686
def test_load_bad_compressed_extension(tmp_path, extension):
87-
if extension == '.zst' and not have_pyzstd:
87+
if extension == '.zst' and not HAVE_ZSTD:
8888
pytest.skip()
8989
file_path = tmp_path / f'img.nii{extension}'
9090
file_path.write_bytes(b'bad')
@@ -94,7 +94,7 @@ def test_load_bad_compressed_extension(tmp_path, extension):
9494

9595
@pytest.mark.parametrize('extension', ['.gz', '.bz2', '.zst'])
9696
def test_load_good_extension_with_bad_data(tmp_path, extension):
97-
if extension == '.zst' and not have_pyzstd:
97+
if extension == '.zst' and not HAVE_ZSTD:
9898
pytest.skip()
9999
file_path = tmp_path / f'img.nii{extension}'
100100
with Opener(file_path, 'wb') as fobj:

nibabel/tests/test_minc1.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
from ..tmpdirs import InTemporaryDirectory
2525
from . import test_spatialimages as tsi
2626
from .test_fileslice import slicer_samples
27-
28-
pyzstd, HAVE_ZSTD, _ = optional_package('pyzstd')
27+
from .._compression import zstd, HAVE_ZSTD
2928

3029
EG_FNAME = pjoin(data_path, 'tiny.mnc')
3130

@@ -175,7 +174,7 @@ def test_compressed(self):
175174
content = open(tp['fname'], 'rb').read()
176175
openers_exts = [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]
177176
if HAVE_ZSTD: # add .zst to test if installed
178-
openers_exts += [(pyzstd.ZstdFile, '.zst')]
177+
openers_exts += [(zstd.ZstdFile, '.zst')]
179178
with InTemporaryDirectory():
180179
for opener, ext in openers_exts:
181180
fname = 'test.mnc' + ext

nibabel/tests/test_openers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
from ..openers import HAVE_INDEXED_GZIP, BZ2File, DeterministicGzipFile, ImageOpener, Opener
2424
from ..optpkg import optional_package
2525
from ..tmpdirs import InTemporaryDirectory
26-
27-
pyzstd, HAVE_ZSTD, _ = optional_package('pyzstd')
26+
from .._compression import zstd, HAVE_ZSTD
2827

2928

3029
class Lunk:
@@ -271,7 +270,7 @@ class StrictOpener(Opener):
271270
IndexedGzipFile = GzipFile
272271
assert isinstance(fobj.fobj, (GzipFile, IndexedGzipFile))
273272
elif lext == 'zst':
274-
assert isinstance(fobj.fobj, pyzstd.ZstdFile)
273+
assert isinstance(fobj.fobj, zstd.ZstdFile)
275274
else:
276275
assert isinstance(fobj.fobj, BZ2File)
277276

nibabel/tests/test_volumeutils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@
5656
working_type,
5757
write_zeros,
5858
)
59-
60-
pyzstd, HAVE_ZSTD, _ = optional_package('pyzstd')
59+
from .._compression import zstd, HAVE_ZSTD
6160

6261
# convenience variables for numpy types
6362
FLOAT_TYPES = sctypes['float']
@@ -81,7 +80,7 @@ def test__is_compressed_fobj():
8180
with InTemporaryDirectory():
8281
file_openers = [('', open, False), ('.gz', gzip.open, True), ('.bz2', BZ2File, True)]
8382
if HAVE_ZSTD:
84-
file_openers += [('.zst', pyzstd.ZstdFile, True)]
83+
file_openers += [('.zst', zstd.ZstdFile, True)]
8584
for ext, opener, compressed in file_openers:
8685
fname = 'test.bin' + ext
8786
for mode in ('wb', 'rb'):
@@ -105,7 +104,7 @@ def make_array(n, bytes):
105104
with InTemporaryDirectory():
106105
openers = [open, gzip.open, BZ2File]
107106
if HAVE_ZSTD:
108-
openers += [pyzstd.ZstdFile]
107+
openers += [zstd.ZstdFile]
109108
for n, opener in itertools.product((256, 1024, 2560, 25600), openers):
110109
in_arr = np.arange(n, dtype=dtype)
111110
# Write array to file
@@ -262,7 +261,7 @@ def test_array_from_file_reread():
262261
with InTemporaryDirectory():
263262
openers = [open, gzip.open, bz2.BZ2File, BytesIO]
264263
if HAVE_ZSTD:
265-
openers += [pyzstd.ZstdFile]
264+
openers += [zstd.ZstdFile]
266265
for shape, opener, dtt, order in itertools.product(
267266
((64,), (64, 65), (64, 65, 66)), openers, (np.int16, np.float32), ('F', 'C')
268267
):

0 commit comments

Comments
 (0)