Skip to content

Commit ae41633

Browse files
authored
Merge pull request #24 from bricksdont/read_only_option
fix(reader): make array writeable by default
2 parents 5697a1b + 88a7326 commit ae41633

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

pose_format/utils/reader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ def unpack_f(self, s_format: str):
3333
return self.unpack(getattr(ConstStructs, s_format))
3434

3535
def unpack_numpy(self, s: struct.Struct, shape: Tuple):
36-
arr = np.ndarray(shape, s.format, self.buffer, self.read_offset)
36+
arr = np.ndarray(shape, s.format, self.buffer, self.read_offset).copy()
3737
self.advance(s, int(np.prod(shape)))
3838
return arr
3939

4040
def unpack_torch(self, s: struct.Struct, shape: Tuple):
4141
import torch
4242

43-
arr = self.unpack_numpy(s, shape) # Array is not writable
44-
return torch.from_numpy(np.array(arr))
43+
arr = self.unpack_numpy(s, shape)
44+
return torch.from_numpy(arr)
4545

4646
def unpack_tensorflow(self, s: struct.Struct, shape: Tuple):
4747
import tensorflow as tf

pose_format/utils/reader_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ def test_unpack_numpy(self):
5151
res = np.array([[1., 2.5], [3.5, 4.5]])
5252
self.assertTrue(np.all(arr == res), msg="Numpy unpacked array is not equal to expected array")
5353

54+
def test_unpack_numpy_writeable(self):
55+
buffer = struct.pack("<ffff", 1., 2.5, 3.5, 4.5)
56+
reader = BufferReader(buffer)
57+
58+
arr = reader.unpack_numpy(ConstStructs.float, (2, 2))
59+
60+
# if array is read-only, this will raise a ValueError
61+
62+
arr -= 0.1
63+
5464
def test_unpack_torch(self):
5565
buffer = struct.pack("<ffff", 1., 2.5, 3.5, 4.5)
5666
reader = BufferReader(buffer)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
setup(
1212
name='pose_format',
1313
packages=packages,
14-
version='0.1.0',
14+
version='0.1.1',
1515
description='Library for viewing, augmenting, and handling .pose files',
1616
author='Amit Moryossef',
1717
author_email='[email protected]',

0 commit comments

Comments
 (0)