Skip to content

Commit 80487b7

Browse files
authored
Merge pull request #899 from effigies/rf/tobytes
MNT: Drop deprecated ndarray.tostring for tobytes
2 parents ff7c276 + d63a4a0 commit 80487b7

28 files changed

+82
-83
lines changed

nibabel/analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def data_to_fileobj(self, data, fileobj, rescale=True):
509509
>>> str_io = BytesIO()
510510
>>> data = np.arange(6).reshape(1,2,3)
511511
>>> hdr.data_to_fileobj(data, str_io)
512-
>>> data.astype(np.float64).tostring('F') == str_io.getvalue()
512+
>>> data.astype(np.float64).tobytes('F') == str_io.getvalue()
513513
True
514514
'''
515515
data = np.asanyarray(data)

nibabel/benchmarks/bench_fileslice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def run_slices(file_like, repeat=3, offset=0, order='F'):
4848
times_arr = np.zeros((n_dim, n_slicers))
4949
with ImageOpener(file_like, 'wb') as fobj:
5050
fobj.write(b'\0' * offset)
51-
fobj.write(arr.tostring(order=order))
51+
fobj.write(arr.tobytes(order=order))
5252
with ImageOpener(file_like, 'rb') as fobj:
5353
for i, L in enumerate(SHAPE):
5454
for j, slicer in enumerate(_slices_for_len(L)):

nibabel/dft.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ def as_png(self, index=None, scale_to_slice=True):
144144
max = data.max()
145145
data = data * 255 / (max - min)
146146
data = data.astype(numpy.uint8)
147-
im = frombytes('L', (self.rows, self.columns),
148-
data.tostring())
147+
im = frombytes('L', (self.rows, self.columns), data.tobytes())
149148

150149
s = BytesIO()
151150
im.save(s, 'PNG')
@@ -213,7 +212,7 @@ def as_nifti(self):
213212
s = BytesIO()
214213
hdr.write_to(s)
215214

216-
return s.getvalue() + data.tostring()
215+
return s.getvalue() + data.tobytes()
217216

218217
def nifti_size(self):
219218
return 352 + 2 * len(self.storage_instances) * self.columns * self.rows

nibabel/ecat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ def to_file_map(self, file_map=None):
981981

982982
# Write subheader
983983
subhdr = subheaders.subheaders[index]
984-
imgf.write(subhdr.tostring())
984+
imgf.write(subhdr.tobytes())
985985

986986
# Seek to the next image block
987987
pos = imgf.tell()

nibabel/externals/netcdf.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def flush(self):
421421
def _write(self):
422422
self.fp.seek(0)
423423
self.fp.write(b'CDF')
424-
self.fp.write(array(self.version_byte, '>b').tostring())
424+
self.fp.write(array(self.version_byte, '>b').tobytes())
425425

426426
# Write headers and data.
427427
self._write_numrecs()
@@ -531,7 +531,7 @@ def _write_var_data(self, name):
531531

532532
# Write data.
533533
if not var.isrec:
534-
self.fp.write(var.data.tostring())
534+
self.fp.write(var.data.tobytes())
535535
count = var.data.size * var.data.itemsize
536536
self._write_var_padding(var, var._vsize - count)
537537
else: # record variable
@@ -553,7 +553,7 @@ def _write_var_data(self, name):
553553
if not rec.shape and (rec.dtype.byteorder == '<' or
554554
(rec.dtype.byteorder == '=' and LITTLE_ENDIAN)):
555555
rec = rec.byteswap()
556-
self.fp.write(rec.tostring())
556+
self.fp.write(rec.tobytes())
557557
# Padding
558558
count = rec.size * rec.itemsize
559559
self._write_var_padding(var, var._vsize - count)
@@ -606,7 +606,7 @@ def _write_att_values(self, values):
606606
if not values.shape and (values.dtype.byteorder == '<' or
607607
(values.dtype.byteorder == '=' and LITTLE_ENDIAN)):
608608
values = values.byteswap()
609-
self.fp.write(values.tostring())
609+
self.fp.write(values.tobytes())
610610
count = values.size * values.itemsize
611611
self.fp.write(b'\x00' * (-count % 4)) # pad
612612

@@ -791,15 +791,15 @@ def _pack_begin(self, begin):
791791
self._pack_int64(begin)
792792

793793
def _pack_int(self, value):
794-
self.fp.write(array(value, '>i').tostring())
794+
self.fp.write(array(value, '>i').tobytes())
795795
_pack_int32 = _pack_int
796796

797797
def _unpack_int(self):
798798
return int(frombuffer(self.fp.read(4), '>i')[0])
799799
_unpack_int32 = _unpack_int
800800

801801
def _pack_int64(self, value):
802-
self.fp.write(array(value, '>q').tostring())
802+
self.fp.write(array(value, '>q').tobytes())
803803

804804
def _unpack_int64(self):
805805
return frombuffer(self.fp.read(8), '>q')[0]
@@ -1045,7 +1045,7 @@ def _get_encoded_fill_value(self):
10451045
"""
10461046
if '_FillValue' in self._attributes:
10471047
fill_value = np.array(self._attributes['_FillValue'],
1048-
dtype=self.data.dtype).tostring()
1048+
dtype=self.data.dtype).tobytes()
10491049
if len(fill_value) == self.itemsize():
10501050
return fill_value
10511051
else:

nibabel/freesurfer/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def _serialize_volume_info(volume_info):
613613
if not (np.array_equal(volume_info[key], [20]) or np.array_equal(
614614
volume_info[key], [2, 0, 20])):
615615
warnings.warn("Unknown extension code.")
616-
strings.append(np.array(volume_info[key], dtype='>i4').tostring())
616+
strings.append(np.array(volume_info[key], dtype='>i4').tobytes())
617617
elif key in ('valid', 'filename'):
618618
val = volume_info[key]
619619
strings.append('{0} = {1}\n'.format(key, val).encode('utf-8'))

nibabel/freesurfer/mghformat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def writehdr_to(self, fileobj):
407407
buffer=self.binaryblock)
408408
# goto the very beginning of the file-like obj
409409
fileobj.seek(0)
410-
fileobj.write(hdr_nofooter.tostring())
410+
fileobj.write(hdr_nofooter.tobytes())
411411

412412
def writeftr_to(self, fileobj):
413413
''' Write footer to fileobj
@@ -427,7 +427,7 @@ def writeftr_to(self, fileobj):
427427
ftr_nd = np.ndarray((), dtype=self._ftrdtype,
428428
buffer=self.binaryblock, offset=ftr_loc_in_hdr)
429429
fileobj.seek(self.get_footer_offset())
430-
fileobj.write(ftr_nd.tostring())
430+
fileobj.write(ftr_nd.tobytes())
431431

432432
def copy(self):
433433
''' Return copy of structure '''

nibabel/freesurfer/tests/test_io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def gen_old_annot_file(fpath, nverts, labels, rgba, names):
309309
# number of vertices
310310
fbytes += struct.pack(dt, nverts)
311311
# vertices + annotation values
312-
fbytes += bytes(vdata.astype(dt).tostring())
312+
fbytes += vdata.astype(dt).tobytes()
313313
# is there a colour table?
314314
fbytes += struct.pack(dt, 1)
315315
# number of entries in colour table
@@ -321,7 +321,7 @@ def gen_old_annot_file(fpath, nverts, labels, rgba, names):
321321
# length of entry name (+1 for terminating byte)
322322
fbytes += struct.pack(dt, len(names[i]) + 1)
323323
fbytes += names[i].encode('ascii') + b'\x00'
324-
fbytes += bytes(rgba[i, :].astype(dt).tostring())
324+
fbytes += rgba[i, :].astype(dt).tobytes()
325325
with open(fpath, 'wb') as f:
326326
f.write(fbytes)
327327
with InTemporaryDirectory():

nibabel/gifti/gifti.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def _data_tag_element(dataarray, encoding, dtype, ordering):
281281
# XXX Accommodating data_tag API - don't try to fix dtype
282282
if isinstance(dtype, str):
283283
dtype = dataarray.dtype
284-
out = np.asanyarray(dataarray, dtype).tostring(order)
284+
out = np.asanyarray(dataarray, dtype).tobytes(order)
285285
if enclabel == 'B64GZ':
286286
out = zlib.compress(out)
287287
da = base64.b64encode(out).decode()

nibabel/nicom/tests/test_dicomwrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def test_data_real(self):
618618
# data hash depends on the endianness
619619
if endian_codes[data.dtype.byteorder] == '>':
620620
data = data.byteswap()
621-
dat_str = data.tostring()
621+
dat_str = data.tobytes()
622622
assert_equal(sha1(dat_str).hexdigest(),
623623
'149323269b0af92baa7508e19ca315240f77fa8c')
624624

0 commit comments

Comments
 (0)