Spaces:
Sleeping
Sleeping
Vikram Vasudevan commited on
Commit ·
caa5f7b
1
Parent(s): 96629a3
fixed simulation
Browse files
src/god_sim/analytics/history.py
CHANGED
|
@@ -5,7 +5,19 @@ from datetime import datetime, timezone
|
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Any
|
| 7 |
from uuid import uuid4
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
DEFAULT_HISTORY_PATH = Path("data/run_history.json")
|
| 11 |
|
|
@@ -45,7 +57,7 @@ def append_run_history(sim_output: dict[str, Any], path: Path = DEFAULT_HISTORY_
|
|
| 45 |
runs = _safe_load_json_array(path)
|
| 46 |
record = build_run_record(sim_output)
|
| 47 |
runs.append(record)
|
| 48 |
-
path.write_text(json.dumps(runs, indent=2), encoding="utf-8")
|
| 49 |
return record
|
| 50 |
|
| 51 |
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Any
|
| 7 |
from uuid import uuid4
|
| 8 |
+
import numpy as np
|
| 9 |
|
| 10 |
+
class SimulationEncoder(json.JSONEncoder):
|
| 11 |
+
def default(self, obj):
|
| 12 |
+
if isinstance(obj, np.integer):
|
| 13 |
+
return int(obj)
|
| 14 |
+
if isinstance(obj, np.floating):
|
| 15 |
+
return float(obj)
|
| 16 |
+
if isinstance(obj, np.ndarray):
|
| 17 |
+
return obj.tolist()
|
| 18 |
+
if isinstance(obj, (datetime, Path)):
|
| 19 |
+
return str(obj)
|
| 20 |
+
return super().default(obj)
|
| 21 |
|
| 22 |
DEFAULT_HISTORY_PATH = Path("data/run_history.json")
|
| 23 |
|
|
|
|
| 57 |
runs = _safe_load_json_array(path)
|
| 58 |
record = build_run_record(sim_output)
|
| 59 |
runs.append(record)
|
| 60 |
+
path.write_text(json.dumps(runs, indent=2, cls=SimulationEncoder), encoding="utf-8")
|
| 61 |
return record
|
| 62 |
|
| 63 |
|