Skip to content

Commit 28a9639

Browse files
committed
FIX: Decode
1 parent 2bb57b3 commit 28a9639

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

nibabel/casting.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,3 @@ def ulp(val=np.float64(1.0)):
796796
fl2 = info['minexp']
797797
# 'nmant' value does not include implicit first bit
798798
return 2 ** (fl2 - info['nmant'])
799-
800-
801-
# Ported from np.compat
802-
def asstr(s):
803-
if isinstance(s, bytes):
804-
return s.decode('latin1')
805-
return str(s)

nibabel/nicom/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""Utilities for working with DICOM datasets
22
"""
33

4-
from nibabel.casting import asstr
5-
64

75
def find_private_section(dcm_data, group_no, creator):
86
"""Return start element in group `group_no` given creator name `creator`
@@ -19,10 +17,10 @@ def find_private_section(dcm_data, group_no, creator):
1917
``tag``, ``VR``, ``value``
2018
group_no : int
2119
Group number in which to search
22-
creator : str or bytes or regex
23-
Name of section - e.g. 'SIEMENS CSA HEADER' - or regex to search for
20+
creator : bytes or regex
21+
Name of section - e.g. b'SIEMENS CSA HEADER' - or regex to search for
2422
section name. Regex used via ``creator.search(element_value)`` where
25-
``element_value`` is the value of the data element.
23+
``element_value`` is the decoded value of the data element.
2624
2725
Returns
2826
-------
@@ -31,15 +29,17 @@ def find_private_section(dcm_data, group_no, creator):
3129
"""
3230
if hasattr(creator, 'search'):
3331
match_func = creator.search
34-
else: # assume string / bytes
35-
match_func = asstr(creator).__eq__
32+
else: # assume bytes
33+
creator = creator.decode('latin-1')
34+
match_func = creator.__eq__
3635
# Group elements assumed ordered by tag (groupno, elno)
3736
for element in dcm_data.group_dataset(group_no):
3837
elno = element.tag.elem
3938
if elno > 0xFF:
4039
break
4140
if element.VR not in ('LO', 'OB'):
4241
continue
43-
if match_func(asstr(element.value)):
42+
val = element.value.decode('latin-1')
43+
if match_func(val):
4444
return elno * 0x100
4545
return None

nibabel/nifti1.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from . import analyze # module import
2222
from .arrayproxy import get_obj_dtype
2323
from .batteryrunners import Report
24-
from .casting import have_binary128, asstr
24+
from .casting import have_binary128
2525
from .deprecated import alert_future_error
2626
from .filebasedimages import ImageFileError, SerializableImage
2727
from .optpkg import optional_package
@@ -1404,7 +1404,7 @@ def get_intent(self, code_repr='label'):
14041404
raise TypeError('repr can be "label" or "code"')
14051405
n_params = len(recoder.parameters[code]) if known_intent else 0
14061406
params = (float(hdr['intent_p%d' % (i + 1)]) for i in range(n_params))
1407-
name = asstr(hdr['intent_name'].item())
1407+
name = hdr['intent_name'].item().decode('latin-1')
14081408
return label, tuple(params), name
14091409

14101410
def set_intent(self, code, params=(), name='', allow_unknown=False):
@@ -1740,7 +1740,8 @@ def _chk_magic(hdr, fix=False):
17401740
magic = hdr['magic'].item()
17411741
if magic in (hdr.pair_magic, hdr.single_magic):
17421742
return hdr, rep
1743-
rep.problem_msg = f'magic string "{asstr(magic)}" is not valid'
1743+
magic = magic.decode('latin-1')
1744+
rep.problem_msg = f'magic string "{magic}" is not valid'
17441745
rep.problem_level = 45
17451746
if fix:
17461747
rep.fix_msg = 'leaving as is, but future errors are likely'

nibabel/streamlines/trk.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import numpy as np
1010

1111
import nibabel as nib
12-
from nibabel.casting import asstr
1312
from nibabel.openers import Opener
1413
from nibabel.orientations import aff2axcodes, axcodes2ornt
1514
from nibabel.volumeutils import endian_codes, native_code, swapped_code
@@ -180,7 +179,7 @@ def decode_value_from_name(encoded_name):
180179
value : int
181180
Value decoded from the name.
182181
"""
183-
encoded_name = asstr(encoded_name)
182+
encoded_name = encoded_name.decode('latin1')
184183
if len(encoded_name) == 0:
185184
return encoded_name, 0
186185

@@ -740,14 +739,25 @@ def __str__(self):
740739
vars[attr] = vars[hdr_field]
741740

742741
nb_scalars = self.header[Field.NB_SCALARS_PER_POINT]
743-
scalar_names = [asstr(s) for s in vars['scalar_name'][:nb_scalars] if len(s) > 0]
742+
scalar_names = [
743+
s.decode('latin-1')
744+
for s in vars['scalar_name'][:nb_scalars]
745+
if len(s) > 0
746+
]
744747
vars['scalar_names'] = '\n '.join(scalar_names)
745748
nb_properties = self.header[Field.NB_PROPERTIES_PER_STREAMLINE]
746-
property_names = [asstr(s) for s in vars['property_name'][:nb_properties] if len(s) > 0]
749+
property_names = [
750+
s.decode('latin-1')
751+
for s in vars['property_name'][:nb_properties]
752+
if len(s) > 0
753+
]
747754
vars['property_names'] = '\n '.join(property_names)
748755
# Make all byte strings into strings
749756
# Fixes recursion error on Python 3.3
750-
vars = {k: asstr(v) if hasattr(v, 'decode') else v for k, v in vars.items()}
757+
vars = {
758+
k: v.decode('latin-1') if hasattr(v, 'decode') else v
759+
for k, v in vars.items()
760+
}
751761
return """\
752762
MAGIC NUMBER: {MAGIC_NUMBER}
753763
v.{version}

nibabel/tests/test_openers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import pytest
2020
from packaging.version import Version
2121

22-
from ..casting import asstr
2322
from ..deprecator import ExpiredDeprecationError
2423
from ..openers import HAVE_INDEXED_GZIP, BZ2File, DeterministicGzipFile, ImageOpener, Opener
2524
from ..optpkg import optional_package
@@ -345,7 +344,7 @@ def test_iter():
345344
fobj.write(bytes(line + os.linesep, 'ascii'))
346345
with Opener(input, 'rb') as fobj:
347346
for back_line, line in zip(fobj, lines):
348-
assert asstr(back_line).rstrip() == line
347+
assert back_line.decode('latin-1').rstrip() == line
349348
if not does_t:
350349
continue
351350
with Opener(input, 'rt') as fobj:

0 commit comments

Comments
 (0)