Skip to content

Commit 2f851a2

Browse files
author
Ben Cipollini
committed
Use Opener (which contains .mgz) instead of BinOpener (which is no longer needed).
1 parent 9875e1c commit 2f851a2

File tree

10 files changed

+43
-45
lines changed

10 files changed

+43
-45
lines changed

bin/nib-nifti-dx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def main():
2525
(opts, files) = parser.parse_args()
2626

2727
for fname in files:
28-
with nib.volumeutils.BinOpener(fname) as fobj:
29-
hdr = fobj.read(nib.nifti1.header_dtype.itemsize)
28+
with nib.openers.Opener(fname) as fobj:
29+
hdr = fobj.read(nib.nifti1.header_dtype.itemsize)
3030
result = nib.Nifti1Header.diagnose_binaryblock(hdr)
3131
if len(result):
3232
print('Picky header check output for "%s"\n' % fname)

nibabel/arrayproxy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
"""
2828
import warnings
2929

30-
from .volumeutils import BinOpener, array_from_file, apply_read_scaling
30+
from .volumeutils import array_from_file, apply_read_scaling
3131
from .fileslice import fileslice
3232
from .keywordonly import kw_only_meth
33+
from .openers import Opener
3334

3435

3536
class ArrayProxy(object):
@@ -130,7 +131,7 @@ def get_unscaled(self):
130131
131132
This is an optional part of the proxy API
132133
'''
133-
with BinOpener(self.file_like) as fileobj:
134+
with Opener(self.file_like) as fileobj:
134135
raw_data = array_from_file(self._shape,
135136
self._dtype,
136137
fileobj,
@@ -145,7 +146,7 @@ def __array__(self):
145146
return apply_read_scaling(raw_data, self._slope, self._inter)
146147

147148
def __getitem__(self, slicer):
148-
with BinOpener(self.file_like) as fileobj:
149+
with Opener(self.file_like) as fileobj:
149150
raw_data = fileslice(fileobj,
150151
slicer,
151152
self._shape,

nibabel/fileholders.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from copy import copy
1212

13-
from .volumeutils import BinOpener
13+
from .openers import Opener
1414

1515

1616
class FileHolderError(Exception):
@@ -63,10 +63,10 @@ def get_prepare_fileobj(self, *args, **kwargs):
6363
``self.pos``
6464
'''
6565
if self.fileobj is not None:
66-
obj = BinOpener(self.fileobj) # for context manager
66+
obj = Opener(self.fileobj) # for context manager
6767
obj.seek(self.pos)
6868
elif self.filename is not None:
69-
obj = BinOpener(self.filename, *args, **kwargs)
69+
obj = Opener(self.filename, *args, **kwargs)
7070
if self.pos != 0:
7171
obj.seek(self.pos)
7272
else:

nibabel/loadsave.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import numpy as np
1313

1414
from .filename_parser import types_filenames, splitext_addext
15-
from .volumeutils import BinOpener, Opener
15+
from .openers import Opener
1616
from .analyze import AnalyzeImage
1717
from .spm2analyze import Spm2AnalyzeImage
1818
from .nifti1 import Nifti1Image, Nifti1Pair, header_dtype as ni1_hdr_dtype
@@ -72,14 +72,14 @@ def guessed_image_type(filename):
7272
signature = fobj.read(4)
7373
klass = Minc2Image if signature == b'\211HDF' else Minc1Image
7474
elif lext == '.nii':
75-
with BinOpener(filename) as fobj:
75+
with Opener(filename) as fobj:
7676
binaryblock = fobj.read(348)
7777
ft = which_analyze_type(binaryblock)
7878
klass = Nifti2Image if ft == 'nifti2' else Nifti1Image
7979
else: # might be nifti 1 or 2 pair or analyze of some sort
8080
files_types = (('image', '.img'), ('header', '.hdr'))
8181
filenames = types_filenames(filename, files_types)
82-
with BinOpener(filenames['header']) as fobj:
82+
with Opener(filenames['header']) as fobj:
8383
binaryblock = fobj.read(348)
8484
ft = which_analyze_type(binaryblock)
8585
if ft == 'nifti2':
@@ -208,7 +208,7 @@ def read_img_data(img, prefer='scaled'):
208208
hdr.set_data_offset(dao.offset)
209209
if default_scaling and (dao.slope, dao.inter) != (1, 0):
210210
hdr.set_slope_inter(dao.slope, dao.inter)
211-
with BinOpener(img_file_like) as fileobj:
211+
with Opener(img_file_like) as fileobj:
212212
if prefer == 'scaled':
213213
return hdr.data_from_fileobj(fileobj)
214214
return hdr.raw_data_from_fileobj(fileobj)

nibabel/nicom/dicomwrappers.py

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

1919
from . import csareader as csar
2020
from .dwiparams import B2q, nearest_pos_semi_def, q2bg
21-
from ..volumeutils import BinOpener
21+
from ..openers import Opener
2222
from ..onetime import setattr_on_read as one_time
2323

2424

@@ -51,7 +51,7 @@ def wrapper_from_file(file_like, *args, **kwargs):
5151
"""
5252
import dicom
5353

54-
with BinOpener(file_like) as fobj:
54+
with Opener(file_like) as fobj:
5555
dcm_data = dicom.read_file(fobj, *args, **kwargs)
5656
return wrapper_from_data(dcm_data)
5757

nibabel/parrec.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@
9999
from .keywordonly import kw_only_meth
100100
from .spatialimages import SpatialImage, Header
101101
from .eulerangles import euler2mat
102-
from .volumeutils import Recoder, array_from_file, BinOpener
102+
from .volumeutils import Recoder, array_from_file
103103
from .affines import from_matvec, dot_reduce, apply_affine
104104
from .nifti1 import unit_codes
105105
from .fileslice import fileslice, strided_scalar
106+
from .openers import Opener
106107

107108
# PSL to RAS affine
108109
PSL_TO_RAS = np.array([[0, 0, -1, 0], # L -> R
@@ -581,13 +582,13 @@ def is_proxy(self):
581582
return True
582583

583584
def get_unscaled(self):
584-
with BinOpener(self.file_like) as fileobj:
585+
with Opener(self.file_like) as fileobj:
585586
return _data_from_rec(fileobj, self._rec_shape, self._dtype,
586587
self._slice_indices, self._shape,
587588
mmap=self._mmap)
588589

589590
def __array__(self):
590-
with BinOpener(self.file_like) as fileobj:
591+
with Opener(self.file_like) as fileobj:
591592
return _data_from_rec(fileobj,
592593
self._rec_shape,
593594
self._dtype,
@@ -603,7 +604,7 @@ def __getitem__(self, slicer):
603604
return np.asanyarray(self)[slicer]
604605
# Slices all sequential from zero, can use fileslice
605606
# This gives more efficient volume by volume loading, for example
606-
with BinOpener(self.file_like) as fileobj:
607+
with Opener(self.file_like) as fileobj:
607608
raw_data = fileslice(fileobj, slicer, self._shape, self._dtype, 0,
608609
'F')
609610
# Broadcast scaling to shape of original data

nibabel/tests/test_openers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def test_Opener():
5757
# mode is gently ignored
5858
fobj = Opener(obj, mode='r')
5959

60-
6160
def test_Opener_various():
6261
# Check we can do all sorts of files here
6362
message = b"Oh what a giveaway"
@@ -85,6 +84,13 @@ def test_Opener_various():
8584
# Just check there is a fileno
8685
assert_not_equal(fobj.fileno(), 0)
8786

87+
def test_OpenerAgain():
88+
# Test that ImageOpener does add '.mgz' as gzipped file type
89+
with InTemporaryDirectory():
90+
with ImageOpener('test.gz', 'w') as fobj:
91+
assert_true(hasattr(fobj.fobj, 'compress'))
92+
with ImageOpener('test.mgz', 'w') as fobj:
93+
assert_true(hasattr(fobj.fobj, 'compress'))
8894

8995
def test_file_like_wrapper():
9096
# Test wrapper using BytesIO (full API)

nibabel/tests/test_utils.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
import numpy as np
2424

2525
from ..tmpdirs import InTemporaryDirectory
26-
26+
from ..openers import ImageOpener
2727
from ..volumeutils import (array_from_file,
2828
_is_compressed_fobj,
2929
array_to_file,
30-
allopen, # for backwards compatibility
31-
BinOpener,
30+
allopen, # for backwards compatibility
3231
fname_ext_ul_case,
3332
calculate_scale,
3433
can_cast,
@@ -928,7 +927,7 @@ def test_seek_tell():
928927
st = functools.partial(seek_tell, write0=write0)
929928
bio.seek(0)
930929
# First write the file
931-
with BinOpener(in_file, 'wb') as fobj:
930+
with ImageOpener(in_file, 'wb') as fobj:
932931
assert_equal(fobj.tell(), 0)
933932
# already at position - OK
934933
st(fobj, 0)
@@ -949,7 +948,7 @@ def test_seek_tell():
949948
fobj.write(b'\x02' * tail)
950949
bio.seek(0)
951950
# Now read back the file testing seek_tell in reading mode
952-
with BinOpener(in_file, 'rb') as fobj:
951+
with ImageOpener(in_file, 'rb') as fobj:
953952
assert_equal(fobj.tell(), 0)
954953
st(fobj, 0)
955954
assert_equal(fobj.tell(), 0)
@@ -961,22 +960,22 @@ def test_seek_tell():
961960
st(fobj, 0)
962961
bio.seek(0)
963962
# Check we have the expected written output
964-
with BinOpener(in_file, 'rb') as fobj:
963+
with ImageOpener(in_file, 'rb') as fobj:
965964
assert_equal(fobj.read(),
966965
b'\x01' * start + b'\x00' * diff + b'\x02' * tail)
967966
for in_file in ('test2.gz', 'test2.bz2'):
968967
# Check failure of write seek backwards
969-
with BinOpener(in_file, 'wb') as fobj:
968+
with ImageOpener(in_file, 'wb') as fobj:
970969
fobj.write(b'g' * 10)
971970
assert_equal(fobj.tell(), 10)
972971
seek_tell(fobj, 10)
973972
assert_equal(fobj.tell(), 10)
974973
assert_raises(IOError, seek_tell, fobj, 5)
975974
# Make sure read seeks don't affect file
976-
with BinOpener(in_file, 'rb') as fobj:
975+
with ImageOpener(in_file, 'rb') as fobj:
977976
seek_tell(fobj, 10)
978977
seek_tell(fobj, 0)
979-
with BinOpener(in_file, 'rb') as fobj:
978+
with ImageOpener(in_file, 'rb') as fobj:
980979
assert_equal(fobj.read(), b'g' * 10)
981980

982981

@@ -1004,15 +1003,6 @@ def seek(self, *args):
10041003
assert_equal(bio.getvalue(), ZEROB * 20)
10051004

10061005

1007-
def test_BinOpener():
1008-
# Test that BinOpener does add '.mgz' as gzipped file type
1009-
with InTemporaryDirectory():
1010-
with BinOpener('test.gz', 'w') as fobj:
1011-
assert_true(hasattr(fobj.fobj, 'compress'))
1012-
with BinOpener('test.mgz', 'w') as fobj:
1013-
assert_true(hasattr(fobj.fobj, 'compress'))
1014-
1015-
10161006
def test_fname_ext_ul_case():
10171007
# Get filename ignoring the case of the filename extension
10181008
with InTemporaryDirectory():

nibabel/trackvis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from .py3k import asstr
1212
from .volumeutils import (native_code, swapped_code, endian_codes, rec2dict)
13-
from .volumeutils import BinOpener
13+
from .openers import Opener
1414
from .orientations import aff2axcodes
1515
from .affines import apply_affine
1616

@@ -143,7 +143,7 @@ def read(fileobj, as_generator=False, points_space=None):
143143
coordinate along the first image axis, multiplied by the voxel size for
144144
that axis.
145145
'''
146-
fileobj = BinOpener(fileobj)
146+
fileobj = Opener(fileobj)
147147
hdr_str = fileobj.read(header_2_dtype.itemsize)
148148
# try defaulting to version 2 format
149149
hdr = np.ndarray(shape=(),
@@ -334,7 +334,7 @@ def write(fileobj, streamlines, hdr_mapping=None, endianness=None,
334334
except StopIteration: # empty sequence or iterable
335335
# write header without streams
336336
hdr = _hdr_from_mapping(None, hdr_mapping, endianness)
337-
with BinOpener(fileobj, 'wb') as fileobj:
337+
with Opener(fileobj, 'wb') as fileobj:
338338
fileobj.write(hdr.tostring())
339339
return
340340
if endianness is None:
@@ -375,7 +375,7 @@ def write(fileobj, streamlines, hdr_mapping=None, endianness=None,
375375
mm2vx = npl.inv(affine)
376376
mm2tv = np.dot(vx2tv, mm2vx).astype('f4')
377377
# write header
378-
fileobj = BinOpener(fileobj, mode='wb')
378+
fileobj = Opener(fileobj, mode='wb')
379379
fileobj.write(hdr.tostring())
380380
# track preliminaries
381381
f4dt = np.dtype(endianness + 'f4')

nibabel/volumeutils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,16 +1584,16 @@ def fname_ext_ul_case(fname):
15841584
def allopen(fileish, *args, **kwargs):
15851585
""" Compatibility wrapper for old ``allopen`` function
15861586
1587-
Wraps creation of ``BinOpener`` instance, while picking up module global
1587+
Wraps creation of ``Opener`` instance, while picking up module global
15881588
``default_compresslevel``.
15891589
1590-
Please see docstring for ``BinOpener`` and ``Opener`` for details.
1590+
Please see docstring for ``Opener`` for details.
15911591
"""
1592-
warnings.warn("Please use BinOpener class instead of this function",
1592+
warnings.warn("Please use Opener class instead of this function",
15931593
DeprecationWarning,
15941594
stacklevel=2)
15951595

1596-
class MyOpener(BinOpener):
1596+
class MyOpener(Opener):
15971597
default_compresslevel = default_compresslevel
15981598

15991599
return MyOpener(fileish, *args, **kwargs)

0 commit comments

Comments
 (0)