File size: 2,172 Bytes
1baf215 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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"
|