Tianshou/examples/mujoco/gen_json.py
Daniel Plop 8a0629ded6
Fix mypy issues in tests and examples (#1077)
Closes #952 

- `SamplingConfig` supports `batch_size=None`. #1077
- tests and examples are covered by `mypy`. #1077
- `NetBase` is more used, stricter typing by making it generic. #1077
- `utils.net.common.Recurrent` now receives and returns a
`RecurrentStateBatch` instead of a dict. #1077

---------

Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2024-04-03 18:07:51 +02:00

38 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import csv
import json
import os
import sys
from os import PathLike
def merge(rootdir: str | PathLike[str]) -> None:
"""format: $rootdir/$algo/*.csv."""
result = []
for path, _, filenames in os.walk(rootdir):
filtered_filenames = [f for f in filenames if f.endswith(".csv")]
if len(filtered_filenames) == 0:
continue
if len(filtered_filenames) != 1:
print(f"More than 1 csv found in {path}!")
continue
algo = os.path.relpath(path, rootdir).upper()
with open(os.path.join(path, filtered_filenames[0])) as f:
reader = csv.DictReader(f)
for row in reader:
result.append(
{
"env_step": int(row["env_step"]),
"rew": float(row["reward"]),
"rew_std": float(row["reward:shaded"]),
"Agent": algo,
},
)
with open(os.path.join(rootdir, "result.json"), "w") as f:
f.write(json.dumps(result))
if __name__ == "__main__":
merge(sys.argv[-1])