-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_nb_vocab_file.py
executable file
·52 lines (39 loc) · 1.31 KB
/
generate_nb_vocab_file.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
47
48
49
50
51
52
import inspect
from pathlib import Path
import pydantic
from bagel import mappings, models
EXCLUDE_CLASSES = ["Bagel", "BaseModel"]
PREAMBLE = "\n".join(
[
"@prefix nb: <http://neurobagel.org/vocab/>.",
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.",
]
)
OUTPUT_PATH = Path.cwd() / "nb_vocab.ttl"
def generate_vocab_string_from_models():
all_classes = {
name: obj
for name, obj in inspect.getmembers(models)
if inspect.isclass(obj)
and issubclass(obj, pydantic.BaseModel)
and name not in EXCLUDE_CLASSES
}
class_entries = []
for name, obj in sorted(all_classes.items()):
class_entry = f"{mappings.NB.pf}:{name} a rdfs:Class"
is_subclass = False
for other_name, other_obj in all_classes.items():
if other_obj in obj.__bases__:
class_entry += (
f";\n rdfs:subClassOf {mappings.NB.pf}:{other_name}."
)
is_subclass = True
break
if not is_subclass:
class_entry += "."
class_entries.append(class_entry)
nb_vocab = "\n\n".join([PREAMBLE] + class_entries) + "\n"
with OUTPUT_PATH.open("w") as f:
f.write(nb_vocab)
if __name__ == "__main__":
generate_vocab_string_from_models()