Skip to content

Commit

Permalink
handle single numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
leej3 committed Jun 19, 2024
1 parent 466ea8e commit 475a659
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ignite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ def flatten_to_str(data: Any) -> List[str]:
['tensor: Shape: torch.Size([2, 2])']
"""

formatted_items: List[str] = []

def format_item(item: Any, prefix: str = "") -> Optional[str]:
Expand Down Expand Up @@ -209,6 +208,10 @@ def format_item(item: Any, prefix: str = "") -> Optional[str]:
formatted_items.append(f"{prefix}")
return None

# Directly handle single numeric values
if isinstance(data, numbers.Number):
return [f"{data:.4f}"]

format_item(data)
return formatted_items

Expand Down
1 change: 1 addition & 0 deletions tests/ignite/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_convert_tensor():
@pytest.mark.parametrize(
"input_data,expected",
[
(42, ["42.0000"]),
([{"a": 15, "b": torch.tensor([2.0])}], ["a: 15.0000", "b: [2.0000]"]),
({"a": 10, "b": 2.33333}, ["a: 10.0000", "b: 2.3333"]),
({"x": torch.tensor(0.1234), "y": [1, 2.3567]}, ["x: 0.1234", "y: 1.0000, 2.3567"]),
Expand Down

0 comments on commit 475a659

Please sign in to comment.