Skip to content

Commit c6cddec

Browse files
authored
Merge pull request #73 from yhuang43/master
fix read-only data array
2 parents 5a168aa + a94931c commit c6cddec

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

surfa/io/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ def read_bytes(file, dtype, count=1):
8686
The read dtype array.
8787
"""
8888
dtype = np.dtype(dtype)
89-
value = np.frombuffer(file.read(dtype.itemsize * count), dtype=dtype)
89+
90+
# numpy.frombuffer() creates a read-only array if the input buffer is immutable
91+
# to get a writable array
92+
# 1. create a mutable buffer (bytearray) of a specific size
93+
buffer_size = dtype.itemsize * count
94+
buf = bytearray(buffer_size)
95+
file.readinto(buf)
96+
97+
# 2. pass the mutable buffer to frombuffer
98+
value = np.frombuffer(buf, dtype=dtype)
9099
if count == 1:
91100
return value[0]
92101
return value

0 commit comments

Comments
 (0)