-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubgraph_extractor.py
More file actions
60 lines (53 loc) · 2.54 KB
/
Copy pathsubgraph_extractor.py
File metadata and controls
60 lines (53 loc) · 2.54 KB
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
53
54
55
56
57
58
59
60
from src.dataset.subgraphs import (build_nx_graph, extract_bfs_subgraphs,
extract_community_subgraphs, extract_module_subgraphs,
save_subgraphs)
import ontolearner.ontology as ontology_module
import inspect
import os
from tqdm import tqdm
# configurations
depth = 2
min_nodes = 3
max_nodes = 20
resolution = 1.0
output_dir = "data"
strategies = [
"bfs",
# "community",
# "module"
]
for name, obj in tqdm(inspect.getmembers(ontology_module), desc="Processing ontologies: "):
if inspect.isclass(obj) and name != "BaseOntology":
if hasattr(obj, 'load') and callable(getattr(obj, 'load')) and hasattr(obj, 'ontology_id'):
if obj.ontology_id in ["ChEBI", "GO", "EFO", "NCIt", "OntoCAPE", "PRotein", "YAGO"]:
continue
ontology_name = obj.ontology_id
ontology_dir = os.path.join(output_dir, ontology_name)
if os.path.exists(ontology_dir):
print(f"Skipping {ontology_name}, already processed.")
continue
ontology = obj()
ontology.from_huggingface()
print("Building undirected networkx graph...")
my_nx_graph = build_nx_graph(ontology.rdf_graph)
for strat in strategies:
if strat == "bfs":
sgs = extract_bfs_subgraphs(ontology.rdf_graph,
my_nx_graph,
depth=depth,
min_nodes=min_nodes,
max_nodes=max_nodes)
elif strat == "community":
sgs = extract_community_subgraphs(ontology.rdf_graph,
my_nx_graph,
min_nodes=min_nodes,
max_nodes=max_nodes,
resolution=resolution)
elif strat == "module":
sgs = extract_module_subgraphs(ontology.rdf_graph,
my_nx_graph,
min_nodes=min_nodes,
max_nodes=max_nodes)
if len(sgs) >= 1:
save_subgraphs(sgs, output_dir, ontology_name, strat)
print(f"\nDone. All sub-graphs saved to: {output_dir}/{ontology_name}/")