-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate_annotations.py
43 lines (40 loc) · 1.35 KB
/
generate_annotations.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
"""
Utility to generate primitives .json annotations
"""
import os
import json
import importlib
import glob
ignore = [
"PrimitiveBase",
"PrimitiveNotFittedError",
"UnsupervisedLearnerPrimitiveBase",
"SupervisedLearnerPrimitiveBase",
"TransformerPrimitiveBase",
"PrimitiveStep",
]
# List all the primitives
for p in glob.glob("kf_d3m_primitives/*/*/*.py"):
if p in glob.glob("kf_d3m_primitives/*/utils/*") or p in glob.glob(
"kf_d3m_primitives/*/*/__.init__.py"
):
continue
f = p.replace("/", ".").replace(".py", "")
module = importlib.import_module(f)
for c in dir(module):
if "Primitive" in c and c not in ignore:
primitive = getattr(module, c)
primitive_json = primitive.metadata.to_json_structure()
primitive_name = primitive_json["python_path"]
os.chdir("/annotations")
if not os.path.isdir(primitive_name):
os.mkdir(primitive_name)
os.chdir(primitive_name)
version = getattr(module, "__version__")
if not os.path.isdir(version):
os.mkdir(version)
os.chdir(version)
with open("primitive.json", "w") as f:
f.write(json.dumps(primitive_json, indent=4))
f.write("\n")
print(f"Generated json annotation for {c}")