Skip to content

Bugfix/header deepcopy #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/python/pose_format/pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def get_components(self, components: List[str], points: Union[Dict[str, List[str


def copy(self):
return self.__class__(self.header, self.body.copy())
return self.__class__(self.header.copy(), self.body.copy())

def bbox(self):
"""
Expand Down
21 changes: 19 additions & 2 deletions src/python/pose_format/pose_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,15 @@ def __init__(self, name: str, points: List[str], limbs: List[Tuple[int, int]], c

self.relative_limbs = self.get_relative_limbs()

def copy(self) -> 'PoseHeaderComponent':
return PoseHeaderComponent(name = self.name,
points = self.points,
limbs= self.limbs,
colors=self.colors,
point_format = self.format)

@staticmethod
def read(version: float, reader: BufferReader):
def read(version: float, reader: BufferReader) -> 'PoseHeaderComponent':
"""
Reads pose header dimensions from reader (BufferReader).

Expand Down Expand Up @@ -182,8 +189,11 @@ def __init__(self, width: int, height: int, depth: int = 0, *args):
self.height = math.ceil(height)
self.depth = math.ceil(depth)

def copy(self) -> 'PoseHeaderDimensions':
return self.__class__(self.width, self.height, self.depth)

@staticmethod
def read(version: float, reader: BufferReader):
def read(version: float, reader: BufferReader) -> 'PoseHeaderDimensions':
"""
Reads and returns a PoseHeaderDimensions object from a buffer reader.

Expand Down Expand Up @@ -293,6 +303,13 @@ def __init__(self,
self.components = components
self.is_bbox = is_bbox

def copy(self) -> 'PoseHeader':
return PoseHeader(version=self.version,
dimensions=self.dimensions.copy(),
components=[c.copy() for c in self.components],
is_bbox=self.is_bbox
)

@staticmethod
def read(reader: BufferReader) -> 'PoseHeader':
"""
Expand Down
16 changes: 12 additions & 4 deletions src/python/tests/pose_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def create_pose_and_frame_dropout_uniform(example: tf.Tensor) -> tf.Tensor:

def test_pose_tf_posebody_copy_creates_deepcopy(self):
pose = _get_random_pose_object_with_tf_posebody(num_keypoints=5)
self.assertIsInstance(pose.body, TensorflowPoseBody)
self.assertIsInstance(pose.body, TensorflowPoseBody)
self.assertIsInstance(pose.body.data, TensorflowMaskedTensor)

pose_copy = pose.copy()
Expand All @@ -488,7 +488,9 @@ def test_pose_tf_posebody_copy_creates_deepcopy(self):

# Check that pose and pose_copy are not the same object
self.assertNotEqual(pose, pose_copy, "Copy of pose should not be 'equal' to original")

self.assertNotEqual(pose.header, pose_copy.header, "headers should be new objects as well")
self.assertNotEqual(pose.header.components, pose_copy.header.components, "components should be new objects as well")

# Ensure the data tensors are equal but independent
self.assertTrue(tf.reduce_all(pose.body.data == pose_copy.body.data), "Copy's data should match original")

Expand All @@ -499,6 +501,9 @@ def test_pose_tf_posebody_copy_creates_deepcopy(self):

# Create another copy and ensure it matches the first copy
pose = pose_copy.copy()
self.assertNotEqual(pose, pose_copy, "Copy of pose should not be 'equal' to original")
self.assertNotEqual(pose.header, pose_copy.header, "headers should be new objects as well")
self.assertNotEqual(pose.header.components, pose_copy.header.components, "Components should be new objects as well")

self.assertTrue(tf.reduce_all(pose.body.data == pose_copy.body.data), "Copy's data should match original again")

Expand Down Expand Up @@ -560,8 +565,9 @@ def test_pose_numpy_posebody_copy_creates_deepcopy(self):
pose = _get_random_pose_object_with_numpy_posebody(num_keypoints=5, frames_min=3)

pose_copy = pose.copy()

self.assertNotEqual(pose, pose_copy, "Copy of pose should not be 'equal' to original")
self.assertNotEqual(pose.header, pose_copy.header, "headers should be new objects as well")
self.assertNotEqual(pose.header.components, pose_copy.header.components, "components should be new objects as well")

self.assertTrue(np.array_equal(pose.body.data, pose_copy.body.data), "Copy's data should match original")

Expand Down Expand Up @@ -599,7 +605,9 @@ def test_pose_torch_posebody_copy_creates_deepcopy(self):
self.assertIsInstance(pose_copy.body, TorchPoseBody)
self.assertIsInstance(pose_copy.body.data, TorchMaskedTensor)

self.assertNotEqual(pose, pose_copy, "Copy of pose should not be 'equal' to original")
self.assertNotEqual(pose, pose_copy, "Copy of pose should not be 'equal' to original")
self.assertNotEqual(pose.header, pose_copy.header, "headers should be new objects as well")
self.assertNotEqual(pose.header.components, pose_copy.header.components, "components should be new objects as well")
self.assertTrue(pose.body.data.tensor.equal(pose_copy.body.data.tensor), "Copy's data should match original")
self.assertTrue(pose.body.data.mask.equal(pose_copy.body.data.mask), "Copy's mask should match original")

Expand Down