Skip to content

Commit cea40b2

Browse files
committed
Add zstd to all; Add tests for zstd
1 parent f231e5c commit cea40b2

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

nibabel/tests/test_analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ def test_big_offset_exts(self):
787787
arr = np.arange(24, dtype=np.int16).reshape((2, 3, 4))
788788
aff = np.eye(4)
789789
img_ext = img_klass.files_types[0][1]
790-
compressed_exts = ['', '.gz', '.bz2']
790+
compressed_exts = ['', '.gz', '.bz2', '.zst']
791791
with InTemporaryDirectory():
792792
for offset in (0, 2048):
793793
# Set offset in in-memory image

nibabel/tests/test_minc1.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ..deprecated import ModuleProxy
2323
from .. import minc1
2424
from ..minc1 import Minc1File, Minc1Image, MincHeader
25+
from ..openers import ZstdFile
2526

2627
from ..tmpdirs import InTemporaryDirectory
2728
from ..deprecator import ExpiredDeprecationError
@@ -170,7 +171,9 @@ def test_compressed(self):
170171
# Not so for MINC2; hence this small sub-class
171172
for tp in self.test_files:
172173
content = open(tp['fname'], 'rb').read()
173-
openers_exts = ((gzip.open, '.gz'), (bz2.BZ2File, '.bz2'))
174+
openers_exts = ((gzip.open, '.gz'),
175+
(bz2.BZ2File, '.bz2'),
176+
(ZstdFile, '.zst'))
174177
with InTemporaryDirectory():
175178
for opener, ext in openers_exts:
176179
fname = 'test.mnc' + ext

nibabel/tests/test_openers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_Opener_various():
7474
for input in ('test.txt',
7575
'test.txt.gz',
7676
'test.txt.bz2',
77+
'test.txt.zst',
7778
sobj):
7879
with Opener(input, 'wb') as fobj:
7980
fobj.write(message)
@@ -276,6 +277,7 @@ def test_name():
276277
for input in ('test.txt',
277278
'test.txt.gz',
278279
'test.txt.bz2',
280+
'test.txt.zst',
279281
sobj,
280282
lunk):
281283
exp_name = input if type(input) == type('') else None
@@ -332,6 +334,7 @@ def test_iter():
332334
for input, does_t in (('test.txt', True),
333335
('test.txt.gz', False),
334336
('test.txt.bz2', False),
337+
('test.txt.zst', False),
335338
(sobj, True)):
336339
with Opener(input, 'wb') as fobj:
337340
for line in lines:

nibabel/tests/test_volumeutils.py

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

5151
from ..deprecator import ExpiredDeprecationError
@@ -70,7 +70,8 @@ def test__is_compressed_fobj():
7070
with InTemporaryDirectory():
7171
for ext, opener, compressed in (('', open, False),
7272
('.gz', gzip.open, True),
73-
('.bz2', BZ2File, True)):
73+
('.bz2', BZ2File, True),
74+
('.zst', ZstdFile, True)):
7475
fname = 'test.bin' + ext
7576
for mode in ('wb', 'rb'):
7677
fobj = opener(fname, mode)
@@ -88,12 +89,12 @@ def make_array(n, bytes):
8889
arr.flags.writeable = True
8990
return arr
9091

91-
# Check whether file, gzip file, bz2 file reread memory from cache
92+
# Check whether file, gzip file, bz2, zst file reread memory from cache
9293
fname = 'test.bin'
9394
with InTemporaryDirectory():
9495
for n, opener in itertools.product(
9596
(256, 1024, 2560, 25600),
96-
(open, gzip.open, BZ2File)):
97+
(open, gzip.open, BZ2File, ZstdFile)):
9798
in_arr = np.arange(n, dtype=dtype)
9899
# Write array to file
99100
fobj_w = opener(fname, 'wb')
@@ -230,7 +231,7 @@ def test_array_from_file_openers():
230231
dtype = np.dtype(np.float32)
231232
in_arr = np.arange(24, dtype=dtype).reshape(shape)
232233
with InTemporaryDirectory():
233-
for ext, offset in itertools.product(('', '.gz', '.bz2'),
234+
for ext, offset in itertools.product(('', '.gz', '.bz2', '.zst'),
234235
(0, 5, 10)):
235236
fname = 'test.bin' + ext
236237
with Opener(fname, 'wb') as out_buf:
@@ -253,7 +254,7 @@ def test_array_from_file_reread():
253254
with InTemporaryDirectory():
254255
for shape, opener, dtt, order in itertools.product(
255256
((64,), (64, 65), (64, 65, 66)),
256-
(open, gzip.open, bz2.BZ2File, BytesIO),
257+
(open, gzip.open, bz2.BZ2File, ZstdFile, BytesIO),
257258
(np.int16, np.float32),
258259
('F', 'C')):
259260
n_els = np.prod(shape)
@@ -901,7 +902,7 @@ def test_write_zeros():
901902
def test_seek_tell():
902903
# Test seek tell routine
903904
bio = BytesIO()
904-
in_files = bio, 'test.bin', 'test.gz', 'test.bz2'
905+
in_files = bio, 'test.bin', 'test.gz', 'test.bz2', 'test.zst'
905906
start = 10
906907
end = 100
907908
diff = end - start
@@ -920,9 +921,12 @@ def test_seek_tell():
920921
fobj.write(b'\x01' * start)
921922
assert fobj.tell() == start
922923
# Files other than BZ2Files can seek forward on write, leaving
923-
# zeros in their wake. BZ2Files can't seek when writing, unless
924-
# we enable the write0 flag to seek_tell
925-
if not write0 and in_file == 'test.bz2': # Can't seek write in bz2
924+
# zeros in their wake. BZ2Files can't seek when writing,
925+
# unless we enable the write0 flag to seek_tell
926+
# ZstdFiles also does not support seek forward on write
927+
if (not write0 and
928+
(in_file == 'test.bz2' or
929+
in_file == 'test.zst')): # Can't seek write in bz2, zst
926930
# write the zeros by hand for the read test below
927931
fobj.write(b'\x00' * diff)
928932
else:
@@ -946,7 +950,7 @@ def test_seek_tell():
946950
# Check we have the expected written output
947951
with ImageOpener(in_file, 'rb') as fobj:
948952
assert fobj.read() == b'\x01' * start + b'\x00' * diff + b'\x02' * tail
949-
for in_file in ('test2.gz', 'test2.bz2'):
953+
for in_file in ('test2.gz', 'test2.bz2', 'test2.zst'):
950954
# Check failure of write seek backwards
951955
with ImageOpener(in_file, 'wb') as fobj:
952956
fobj.write(b'g' * 10)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ all =
7070
%(spm)s
7171
%(style)s
7272
%(test)s
73+
%(zstd)s
7374

7475
[options.entry_points]
7576
console_scripts =

0 commit comments

Comments
 (0)