JdeRobot / JdeRobot/PerceptionMetrics

Bug in utils/torch.py base-case handling and lack of unit test coverage

Open
#518 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
112
Forks
109
PR merge metrics
No merged PRs in 30d

Description

**Description**
While implementing a new test suite for the utility modules, I identified a bug in `perceptionmetrics/utils/torch.py` specifically within the `get_data_shape()` function.

Currently, this function recursively traverse nested lists and tuples but assume that any non-sequence base case is a PyTorch tensor. This causes an `AttributeError` when the data structure contains metadata such as strings (filenames), integers (labels), or dictionaries.

**Bug Details**
In `get_data_shape()`:
```python
# Current implementation
elif torch.is_tensor(data):
return tuple(data.shape)
else:
return tuple(data.shape) # Raises AttributeError if data is a string/int
```
The `else` block should implement a "passthrough" logic (matching `data_to_device()`) to return the non-tensor data as-is, allowing for mixed-type data structures.

**Proposed Changes**
1. **Fix `torch.py`:** Update the `else` block in `get_data_shape()` to `return data`.
2. **Add Unit Tests:** The utility modules (`torch.py` and `image.py`) currently lack rigorous testing. We should implement a formal unit testing suite using `pytest` to validate recursive operations and visualization helpers to prevent future regressions.

**Impact**
Fixing this will enable the evaluation pipeline to handle datasets that bundle metadata (like image paths or class names) alongside tensors without crashing during shape validation.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.