diff --git a/nibabel/__init__.py b/nibabel/__init__.py index c08890ac37..09be1d2792 100644 --- a/nibabel/__init__.py +++ b/nibabel/__init__.py @@ -39,7 +39,7 @@ # module imports from . import analyze as ana -from . import ecat, imagestats, mriutils +from . import ecat, imagestats, mriutils, orientations from . import nifti1 as ni1 from . import spm2analyze as spm2 from . import spm99analyze as spm99 diff --git a/nibabel/nicom/utils.py b/nibabel/nicom/utils.py index f62bc72c5a..617ff2a28a 100644 --- a/nibabel/nicom/utils.py +++ b/nibabel/nicom/utils.py @@ -1,8 +1,6 @@ """Utilities for working with DICOM datasets """ -from numpy.compat.py3k import asstr - def find_private_section(dcm_data, group_no, creator): """Return start element in group `group_no` given creator name `creator` @@ -31,8 +29,10 @@ def find_private_section(dcm_data, group_no, creator): """ if hasattr(creator, 'search'): match_func = creator.search - else: # assume string / bytes - match_func = asstr(creator).__eq__ + else: + if isinstance(creator, bytes): + creator = creator.decode('latin-1') + match_func = creator.__eq__ # Group elements assumed ordered by tag (groupno, elno) for element in dcm_data.group_dataset(group_no): elno = element.tag.elem @@ -40,6 +40,9 @@ def find_private_section(dcm_data, group_no, creator): break if element.VR not in ('LO', 'OB'): continue - if match_func(asstr(element.value)): + val = element.value + if isinstance(val, bytes): + val = val.decode('latin-1') + if match_func(val): return elno * 0x100 return None diff --git a/nibabel/nifti1.py b/nibabel/nifti1.py index 07fb177736..c1b0124ebb 100644 --- a/nibabel/nifti1.py +++ b/nibabel/nifti1.py @@ -17,7 +17,6 @@ import numpy as np import numpy.linalg as npl -from numpy.compat.py3k import asstr from . import analyze # module import from .arrayproxy import get_obj_dtype @@ -1405,7 +1404,7 @@ def get_intent(self, code_repr='label'): raise TypeError('repr can be "label" or "code"') n_params = len(recoder.parameters[code]) if known_intent else 0 params = (float(hdr['intent_p%d' % (i + 1)]) for i in range(n_params)) - name = asstr(hdr['intent_name'].item()) + name = hdr['intent_name'].item().decode('latin-1') return label, tuple(params), name def set_intent(self, code, params=(), name='', allow_unknown=False): @@ -1741,7 +1740,7 @@ def _chk_magic(hdr, fix=False): magic = hdr['magic'].item() if magic in (hdr.pair_magic, hdr.single_magic): return hdr, rep - rep.problem_msg = f'magic string "{asstr(magic)}" is not valid' + rep.problem_msg = f'magic string {magic.decode("latin1")!r} is not valid' rep.problem_level = 45 if fix: rep.fix_msg = 'leaving as is, but future errors are likely' diff --git a/nibabel/streamlines/trk.py b/nibabel/streamlines/trk.py index 4f570a2803..04ac56a51d 100644 --- a/nibabel/streamlines/trk.py +++ b/nibabel/streamlines/trk.py @@ -7,7 +7,6 @@ import warnings import numpy as np -from numpy.compat.py3k import asstr import nibabel as nib from nibabel.openers import Opener @@ -180,7 +179,7 @@ def decode_value_from_name(encoded_name): value : int Value decoded from the name. """ - encoded_name = asstr(encoded_name) + encoded_name = encoded_name.decode('latin1') if len(encoded_name) == 0: return encoded_name, 0 @@ -740,14 +739,18 @@ def __str__(self): vars[attr] = vars[hdr_field] nb_scalars = self.header[Field.NB_SCALARS_PER_POINT] - scalar_names = [asstr(s) for s in vars['scalar_name'][:nb_scalars] if len(s) > 0] + scalar_names = [ + s.decode('latin-1') for s in vars['scalar_name'][:nb_scalars] if len(s) > 0 + ] vars['scalar_names'] = '\n '.join(scalar_names) nb_properties = self.header[Field.NB_PROPERTIES_PER_STREAMLINE] - property_names = [asstr(s) for s in vars['property_name'][:nb_properties] if len(s) > 0] + property_names = [ + s.decode('latin-1') for s in vars['property_name'][:nb_properties] if len(s) > 0 + ] vars['property_names'] = '\n '.join(property_names) # Make all byte strings into strings # Fixes recursion error on Python 3.3 - vars = {k: asstr(v) if hasattr(v, 'decode') else v for k, v in vars.items()} + vars = {k: v.decode('latin-1') if hasattr(v, 'decode') else v for k, v in vars.items()} return """\ MAGIC NUMBER: {MAGIC_NUMBER} v.{version} diff --git a/nibabel/tests/test_nifti1.py b/nibabel/tests/test_nifti1.py index 7b7f44fe0b..1031c6c1aa 100644 --- a/nibabel/tests/test_nifti1.py +++ b/nibabel/tests/test_nifti1.py @@ -251,7 +251,7 @@ def test_magic_offset_checks(self): fhdr, message, raiser = self.log_chk(hdr, 45) assert fhdr['magic'] == b'ooh' assert ( - message == 'magic string "ooh" is not valid; ' + message == "magic string 'ooh' is not valid; " 'leaving as is, but future errors are likely' ) # For pairs, any offset is OK, but should be divisible by 16 diff --git a/nibabel/tests/test_openers.py b/nibabel/tests/test_openers.py index 893c5f4f88..0d150a145c 100644 --- a/nibabel/tests/test_openers.py +++ b/nibabel/tests/test_openers.py @@ -17,7 +17,6 @@ from unittest import mock import pytest -from numpy.compat.py3k import asbytes, asstr from packaging.version import Version from ..deprecator import ExpiredDeprecationError @@ -342,10 +341,10 @@ def test_iter(): for input, does_t in files_to_test: with Opener(input, 'wb') as fobj: for line in lines: - fobj.write(asbytes(line + os.linesep)) + fobj.write(str.encode(line + os.linesep)) with Opener(input, 'rb') as fobj: for back_line, line in zip(fobj, lines): - assert asstr(back_line).rstrip() == line + assert back_line.decode().rstrip() == line if not does_t: continue with Opener(input, 'rt') as fobj: diff --git a/nibabel/tests/test_scripts.py b/nibabel/tests/test_scripts.py index e875065c8d..cc4bb468ad 100644 --- a/nibabel/tests/test_scripts.py +++ b/nibabel/tests/test_scripts.py @@ -228,7 +228,7 @@ def test_nib_nifti_dx(): expected = f"""Picky header check output for "{dirty_hdr}" pixdim[0] (qfac) should be 1 (default) or -1 -magic string "" is not valid +magic string '' is not valid sform_code 11776 not valid""" # Split strings to remove line endings assert stdout == expected