Skip to content

Commit 3ebad63

Browse files
committed
RF: Use optional_package in tests/benchmark
1 parent 58cba21 commit 3ebad63

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

nibabel/benchmarks/bench_fileslice.py

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

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

2223
SHAPE = (64, 64, 32, 100)
2324
ROW_NAMES = [f'axis {i}, len {dim}' for i, dim in enumerate(SHAPE)]
2425
COL_NAMES = ['mid int',
2526
'step 1',
2627
'half step 1',
2728
'step mid int']
29+
HAVE_ZSTD = optional_package("pyzstd")[1]
2830

2931

3032
def _slices_for_len(L):
@@ -104,11 +106,10 @@ def my_table(title, times, base):
104106
my_table('bz2 slice - raw (ratio)',
105107
np.dstack((bz2_times, bz2_times / bz2_base)),
106108
bz2_base)
107-
if HAVE_ZSTD:
108-
if zst:
109-
with InTemporaryDirectory():
110-
zst_times, zst_base = run_slices('data.zst', repeat)
111-
my_table('zst slice - raw (ratio)',
112-
np.dstack((zst_times, zst_times / zst_base)),
113-
zst_base)
109+
if zst and HAVE_ZSTD:
110+
with InTemporaryDirectory():
111+
zst_times, zst_base = run_slices('data.zst', repeat)
112+
my_table('zst slice - raw (ratio)',
113+
np.dstack((zst_times, zst_times / zst_base)),
114+
zst_base)
114115
sys.stdout.flush()

nibabel/tests/test_analyze.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from ..casting import as_int
3131
from ..tmpdirs import InTemporaryDirectory
3232
from ..arraywriters import WriterError
33-
from ..openers import HAVE_ZSTD
33+
from ..optpkg import optional_package
3434

3535
import pytest
3636
from numpy.testing import (assert_array_equal, assert_array_almost_equal)
@@ -41,6 +41,8 @@
4141
from .test_wrapstruct import _TestLabeledWrapStruct
4242
from . import test_spatialimages as tsi
4343

44+
HAVE_ZSTD = optional_package("pyzstd")[1]
45+
4446
header_file = os.path.join(data_path, 'analyze.hdr')
4547

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

nibabel/tests/test_minc1.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ..deprecated import ModuleProxy
2323
from .. import minc1
2424
from ..minc1 import Minc1File, Minc1Image, MincHeader
25-
from ..openers import HAVE_ZSTD
25+
from ..optpkg import optional_package
2626

2727
from ..tmpdirs import InTemporaryDirectory
2828
from ..deprecator import ExpiredDeprecationError
@@ -33,9 +33,7 @@
3333
from . import test_spatialimages as tsi
3434
from .test_fileslice import slicer_samples
3535

36-
# only import ZstdFile, if installed
37-
if HAVE_ZSTD:
38-
from ..openers import ZstdFile
36+
pyzstd, HAVE_ZSTD, _ = optional_package("pyzstd")
3937

4038
EG_FNAME = pjoin(data_path, 'tiny.mnc')
4139

@@ -178,7 +176,7 @@ def test_compressed(self):
178176
openers_exts = [(gzip.open, '.gz'),
179177
(bz2.BZ2File, '.bz2')]
180178
if HAVE_ZSTD: # add .zst to test if installed
181-
openers_exts += [(ZstdFile, '.zst')]
179+
openers_exts += [(pyzstd.ZstdFile, '.zst')]
182180
with InTemporaryDirectory():
183181
for opener, ext in openers_exts:
184182
fname = 'test.mnc' + ext

nibabel/tests/test_openers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
ImageOpener,
1919
HAVE_INDEXED_GZIP,
2020
BZ2File,
21-
HAVE_ZSTD)
21+
)
2222
from ..tmpdirs import InTemporaryDirectory
2323
from ..volumeutils import BinOpener
24+
from ..optpkg import optional_package
2425

2526
import unittest
2627
from unittest import mock
2728
import pytest
2829
from ..testing import error_warnings
2930

30-
if HAVE_ZSTD:
31-
from ..openers import ZstdFile
31+
pyzstd, HAVE_ZSTD, _ = optional_package("pyzstd")
3232

3333

3434
class Lunk(object):
@@ -277,7 +277,7 @@ class StrictOpener(Opener):
277277
IndexedGzipFile = GzipFile
278278
assert isinstance(fobj.fobj, (GzipFile, IndexedGzipFile))
279279
elif lext == 'zst':
280-
assert isinstance(fobj.fobj, ZstdFile)
280+
assert isinstance(fobj.fobj, pyzstd.ZstdFile)
281281
else:
282282
assert isinstance(fobj.fobj, BZ2File)
283283

nibabel/tests/test_volumeutils.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,19 @@
4545
_write_data,
4646
_ftype4scaled_finite,
4747
)
48-
from ..openers import Opener, BZ2File, HAVE_ZSTD
48+
from ..openers import Opener, BZ2File
4949
from ..casting import (floor_log2, type_info, OK_FLOATS, shared_range)
5050

5151
from ..deprecator import ExpiredDeprecationError
52+
from ..optpkg import optional_package
5253

5354
from numpy.testing import (assert_array_almost_equal,
5455
assert_array_equal)
5556
import pytest
5657

5758
from nibabel.testing import nullcontext, assert_dt_equal, assert_allclose_safely, suppress_warnings
5859

59-
# only import ZstdFile, if installed
60-
if HAVE_ZSTD:
61-
from ..openers import ZstdFile
60+
pyzstd, HAVE_ZSTD, _ = optional_package("pyzstd")
6261

6362
#: convenience variables for numpy types
6463
FLOAT_TYPES = np.sctypes['float']
@@ -76,7 +75,7 @@ def test__is_compressed_fobj():
7675
('.gz', gzip.open, True),
7776
('.bz2', BZ2File, True)]
7877
if HAVE_ZSTD:
79-
file_openers += [('.zst', ZstdFile, True)]
78+
file_openers += [('.zst', pyzstd.ZstdFile, True)]
8079
for ext, opener, compressed in file_openers:
8180
fname = 'test.bin' + ext
8281
for mode in ('wb', 'rb'):
@@ -100,7 +99,7 @@ def make_array(n, bytes):
10099
with InTemporaryDirectory():
101100
openers = [open, gzip.open, BZ2File]
102101
if HAVE_ZSTD:
103-
openers += [ZstdFile]
102+
openers += [pyzstd.ZstdFile]
104103
for n, opener in itertools.product(
105104
(256, 1024, 2560, 25600),
106105
openers):
@@ -266,7 +265,7 @@ def test_array_from_file_reread():
266265
with InTemporaryDirectory():
267266
openers = [open, gzip.open, bz2.BZ2File, BytesIO]
268267
if HAVE_ZSTD:
269-
openers += [ZstdFile]
268+
openers += [pyzstd.ZstdFile]
270269
for shape, opener, dtt, order in itertools.product(
271270
((64,), (64, 65), (64, 65, 66)),
272271
openers,

0 commit comments

Comments
 (0)