Skip to content

MAINT: Deprecations #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nibabel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions nibabel/nicom/utils.py
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -31,15 +29,20 @@ 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')
Comment on lines +33 to +34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has both bytes and str tests in nicom/tests/test_utils.py so I assume it's necessary

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
if elno > 0xFF:
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
5 changes: 2 additions & 3 deletions nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'
Expand Down
13 changes: 8 additions & 5 deletions nibabel/streamlines/trk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions nibabel/tests/test_openers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down