Tianshou/examples/mujoco/gen_json.py
Michael Panchenko 600f4bbd55
Python 3.9, black + ruff formatting (#921)
Preparation for #914 and #920

Changes formatting to ruff and black. Remove python 3.8

## Additional Changes

- Removed flake8 dependencies
- Adjusted pre-commit. Now CI and Make use pre-commit, reducing the
duplication of linting calls
- Removed check-docstyle option (ruff is doing that)
- Merged format and lint. In CI the format-lint step fails if any
changes are done, so it fulfills the lint functionality.

---------

Co-authored-by: Jiayi Weng <jiayi@openai.com>
2023-08-25 14:40:56 -07:00

37 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import csv
import json
import os
import sys
def merge(rootdir):
"""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])