lebedov / msgpack-numpy

Serialize numpy arrays using msgpack
Other
197 stars 33 forks source link

Decoded arrays are read only? #40

Closed goodboy closed 4 years ago

goodboy commented 4 years ago

I'm not entirely sure of the details of this because I haven't ever really encountered a read-only array before, but the this script should explain the issue:

import numpy as np

import msgpack
import msgpack_numpy
from msgpack_numpy import patch
patch()

x = np.array([(1, 2, b'a', [1.0, 2.0])],
             np.dtype([('arg0', np.uint32),
                       ('arg1', np.uint32),
                       ('arg2', 'S1'),
                       ('arg3', np.float32, (2,))]))

x['arg0'] = [2]
x['arg0'] == [2]

x_enc = msgpack.packb(x)
x2 = msgpack.unpackb(x_enc)

# ValueError here
try:
    x2['arg0'] = [1]
except ValueError:
    # I guess this is expected?
    x3 = np.array(x2)
    x3['arg0'] = [1]

Does this mean the user is expected to make a copy of any decoded array they wish to modify?

lebedov commented 4 years ago

Yes. The numpy.frombuffer() function used to recreate the encoded array returns a read-only array. I just added a note to the README regarding this for the edification of other users.

goodboy commented 4 years ago

@lebedov thanks! Very gtk.