-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies_to_json.py
46 lines (34 loc) · 1.33 KB
/
policies_to_json.py
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
import json
from pathlib import Path
import re
def collect_policies(path: Path):
policy_list = []
for agent_type_path in path.iterdir():
type_entry = {}
type_entry["type"] = agent_type_path.name
type_entry["morphologies"] = [get_morphology(morphology_path) for morphology_path in agent_type_path.iterdir()]
policy_list.append(type_entry)
return policy_list
def get_morphology(morphology_path: Path):
morphology_entry = {}
morphology_entry["morphology"] = morphology_path.name
seeds = [get_seed(seed_path) for seed_path in morphology_path.iterdir()]
morphology_entry["seeds"] = sorted(seeds, key = lambda x: x["seed"])
return morphology_entry
def get_seed(seed_path: Path):
seed_entry = {}
seed_entry["seed"] = re.findall('_s(\d+)$', seed_path.name)[0]
seed_entry["path"] = str(seed_path)
seed_entry["name"] = get_seed_name(seed_path)
return seed_entry
def get_seed_name(seed_path: Path):
name_file = seed_path / 'name.txt'
if name_file.exists():
with name_file.open() as f:
name = f.readline()
return name.strip('\n')
if __name__ == "__main__":
policy_list = collect_policies(Path('policy_models'))
print(policy_list)
with open("web_demo/policies.json", "w") as write_file:
json.dump(policy_list, write_file)