Skip to content
Open
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
6 changes: 3 additions & 3 deletions aisteer360/evaluation/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def to_jsonable(obj: Any) -> Any:
- Path: str(path)
- mappings: recurse, stringify keys
- sequences: recurse on elements
- numpy scalars/arrays: convert to Python / list
- numpy scalars/arrays: convert to Python / list, then recurse
- everything else: repr(obj)
"""
from pathlib import Path as _Path
Expand All @@ -28,7 +28,7 @@ def to_jsonable(obj: Any) -> Any:
return obj.item()

if isinstance(obj, np.ndarray):
return obj.tolist()
return to_jsonable(obj.tolist())

if isinstance(obj, Mapping):
return {str(k): to_jsonable(v) for k, v in obj.items()}
Expand All @@ -38,7 +38,7 @@ def to_jsonable(obj: Any) -> Any:

if callable(obj):
return f"callable:{getattr(obj, '__qualname__', type(obj).__name__)}"

return repr(obj)


Expand Down
30 changes: 28 additions & 2 deletions tests/core/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
test are the package implementations.
"""
import json
from pathlib import Path
from unittest.mock import MagicMock

import numpy as np
import pytest

from aisteer360.algorithms.core.specs import ControlSpec
Expand Down Expand Up @@ -118,8 +120,6 @@ def test_num_trials_coerced_to_int(self, sample_evaluation_data):
assert isinstance(benchmark.num_trials, int)

def test_save_dir_coerced_to_path(self, sample_evaluation_data, tmp_path):
from pathlib import Path

benchmark = Benchmark(
use_case=_make_use_case(sample_evaluation_data),
base_model_name_or_path="test-model",
Expand Down Expand Up @@ -439,6 +439,32 @@ def test_mixed_spec_and_concrete_raises(self, sample_evaluation_data, mock_base_
class TestBenchmarkCheckpointing:
"""Tests for incremental checkpointing and resume."""

def test_checkpoint_converts_nested_numpy_objects(self, sample_evaluation_data, tmp_path):
benchmark = Benchmark(
use_case=_make_use_case(sample_evaluation_data),
base_model_name_or_path="test-model",
steering_pipelines={"baseline": []},
save_dir=tmp_path,
)
profiles = {
"baseline": [
{
"params": np.array(
[Path("models/base"), {"layers": {1, 2}}],
dtype=object,
)
}
]
}

benchmark._save_checkpoint(profiles)

with (tmp_path / "checkpoint.json").open(encoding="utf-8") as handle:
saved = json.load(handle)
saved_params = saved["baseline"][0]["params"]
assert saved_params[0] == "models/base"
assert sorted(saved_params[1]["layers"]) == [1, 2]

def test_checkpoint_written(self, sample_evaluation_data, mock_base_model, tmp_path):
use_case = _make_use_case(sample_evaluation_data)
benchmark = Benchmark(
Expand Down
19 changes: 17 additions & 2 deletions tests/core/test_evaluation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- Visualization utilities (plot functions)
"""

import json
from pathlib import Path

import numpy as np
import pandas as pd
import pytest
Expand Down Expand Up @@ -149,8 +152,6 @@ def test_primitive_passthrough(self):

def test_path_to_string(self):
"""Test that Path objects become strings."""
from pathlib import Path

result = to_jsonable(Path("/some/path"))
assert result == "/some/path"
assert isinstance(result, str)
Expand All @@ -168,6 +169,20 @@ def test_numpy_array(self):
assert result == [1, 2, 3]
assert isinstance(result, list)

def test_numpy_object_array_recurses(self):
"""Test nested objects in numpy arrays become JSON-safe."""
arr = np.array(
[Path("models/base"), {"layers": {1, 2}}],
dtype=object,
)

result = to_jsonable(arr)
encoded = json.dumps(result)

assert result[0] == "models/base"
assert sorted(result[1]["layers"]) == [1, 2]
assert json.loads(encoded) == result

def test_nested_dict(self):
"""Test nested dict conversion."""
data = {"a": np.float64(1.0), "b": {"c": np.array([1, 2])}}
Expand Down