transition-foundation-model / tests /test_external.py
maxffarrell's picture
Release experimental Transition Foundation Model v0.1 with reproducible source and validation
1baf215 verified
Raw History Blame Contribute Delete
2.17 kB
from types import SimpleNamespace
import numpy as np
import pytest
from tfm.data.external import collect_citylearn, collect_minari
class Space:
shape = (1,)
dtype = np.float32
low = np.array([-1], dtype=np.float32)
high = np.array([1], dtype=np.float32)
def contains(self, value):
return value.shape == (1,) and np.all(value >= self.low) and np.all(value <= self.high)
class City:
action_names = [["electrical_storage"]]
observation_names = [["indoor_dry_bulb_temperature"]]
action_space = [Space()]
seconds_per_time_step = 900
def reset(self, seed):
self.t = 0
return [[22.0]], {}
def step(self, actions):
self.t += 1
return [[22.0]], 0, self.t == 2, False, {}
def test_citylearn_native_enabled_actions_preserved(tmp_path):
seen = []
def policy(observed, names, spaces):
seen.extend(names[0])
return [[0.5]]
metadata = collect_citylearn(City(), policy, tmp_path / "city.npz", system_id="building-1")
assert set(seen) == {"electrical_storage"}
assert metadata["schema_mapping_validated"] is False
with np.load(tmp_path / "city.npz", allow_pickle=False) as arrays:
assert arrays["observations_0"].shape == (3, 1)
assert arrays["actions_0"].shape == (2, 1)
with pytest.raises(ValueError, match="Invalid native"):
collect_citylearn(City(), lambda *args: [[2]], tmp_path / "invalid.npz", system_id="building-1")
def test_minari_requires_instance_grouping(tmp_path):
episode = SimpleNamespace(
id=1,
observations=np.zeros((3, 2)),
actions=np.zeros((2, 1)),
terminations=np.array([False, True]),
truncations=np.array([False, False]),
)
dataset = SimpleNamespace(
spec=SimpleNamespace(dataset_id="test-v0"), iterate_episodes=lambda: iter([episode])
)
with pytest.raises(ValueError, match="grouping"):
collect_minari(dataset, tmp_path, system_id_for_episode=lambda ep: None)
metadata = collect_minari(dataset, tmp_path, system_id_for_episode=lambda ep: "physical-instance")
assert metadata[0]["system_id"] == "physical-instance"