Skip to content

Commit

Permalink
Merge pull request #24 from bricksdont/read_only_option
Browse files Browse the repository at this point in the history
fix(reader): make array writeable by default
  • Loading branch information
AmitMY authored Aug 23, 2022
2 parents 5697a1b + 88a7326 commit ae41633
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pose_format/utils/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def unpack_f(self, s_format: str):
return self.unpack(getattr(ConstStructs, s_format))

def unpack_numpy(self, s: struct.Struct, shape: Tuple):
arr = np.ndarray(shape, s.format, self.buffer, self.read_offset)
arr = np.ndarray(shape, s.format, self.buffer, self.read_offset).copy()
self.advance(s, int(np.prod(shape)))
return arr

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

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

def unpack_tensorflow(self, s: struct.Struct, shape: Tuple):
import tensorflow as tf
Expand Down
10 changes: 10 additions & 0 deletions pose_format/utils/reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def test_unpack_numpy(self):
res = np.array([[1., 2.5], [3.5, 4.5]])
self.assertTrue(np.all(arr == res), msg="Numpy unpacked array is not equal to expected array")

def test_unpack_numpy_writeable(self):
buffer = struct.pack("<ffff", 1., 2.5, 3.5, 4.5)
reader = BufferReader(buffer)

arr = reader.unpack_numpy(ConstStructs.float, (2, 2))

# if array is read-only, this will raise a ValueError

arr -= 0.1

def test_unpack_torch(self):
buffer = struct.pack("<ffff", 1., 2.5, 3.5, 4.5)
reader = BufferReader(buffer)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
setup(
name='pose_format',
packages=packages,
version='0.1.0',
version='0.1.1',
description='Library for viewing, augmenting, and handling .pose files',
author='Amit Moryossef',
author_email='amitmoryossef@gmail.com',
Expand Down

0 comments on commit ae41633

Please sign in to comment.