-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphBuilder.py
More file actions
58 lines (45 loc) · 1.66 KB
/
Copy pathGraphBuilder.py
File metadata and controls
58 lines (45 loc) · 1.66 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
import networkx as nx
import pickle
def builtEdge(graph, lines, without_attribute_relation):
for line in lines:
parts = line.split(" ")
# parts = line.split(";")
active = parts[2]
focus_concept = parts[4]
attribute_value = parts[5]
relation_type = parts[7]
if without_attribute_relation:
b = (active == '1' and relation_type == '116680003')
else:
b = (active == '1')
if b:
if not graph.has_edge(focus_concept, attribute_value):
graph.add_edge(focus_concept, attribute_value, label=relation_type)
return graph
def builtNode(graph, lines):
for line in lines:
parts = line.split(" ")
node = parts[0]
active = parts[2]
if active == '1':
graph.add_node(node)
return graph
def createGraph(name, without_attribute_relation):
file0 = open('data/sct2_Concept_Snapshot_INT_20230430.txt', 'r')
Lines0 = file0.readlines()
file1 = open('data/sct2_Relationship_Snapshot_INT_20230430.txt', 'r')
Lines1 = file1.readlines()
#file2 = open('data/sct2_RelationshipConcreteValues_Snapshot_INT_20230430.txt', 'r')
#Lines2 = file2.readlines()
graph = nx.DiGraph()
graph = builtNode(graph, Lines0)
graph = builtEdge(graph, Lines1, without_attribute_relation)
#graph = builtEdge(graph, Lines2, without_attribute_relation)
with open(name + ".pkl", "wb") as f:
pickle.dump(graph, f)
print(graph)
if __name__ == '__main__':
print("--GRAPH BUILDER--")
createGraph('snomed-20230430_dag_is-a', True)
createGraph('snomed-20230430_dag_rel', False)
print("--FINISH--")