Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit 47ff4e2

Browse files
colahludwigschubert
authored andcommitted
@colah's graph_analysis: nicer json parsed graph structure
1 parent db40dec commit 47ff4e2

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

lucid/misc/graph_analysis/parse_overlay.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,35 @@ def parse_structure(node):
4949
structure = node.sub_structure
5050

5151
if structure is None:
52-
return node.name
52+
return {
53+
"type" : "Node",
54+
"name": node.name
55+
}
5356
elif structure.structure_type == "Sequence":
54-
return {"Sequence" : [parse_structure(n) for n in structure.structure["sequence"]]}
57+
return {
58+
"type" : "Sequence",
59+
"children": [parse_structure(n) for n in structure.structure["sequence"]]
60+
}
5561
elif structure.structure_type == "HeadBranch":
56-
return {"Sequence" : [
57-
{"Branch" : [parse_structure(n) for n in structure.structure["branches"]] },
58-
parse_structure(structure.structure["head"])
59-
]}
62+
return {
63+
"type" : "Sequence",
64+
"children": [{
65+
"type": "Branch",
66+
"children": [parse_structure(n) for n in structure.structure["branches"]]
67+
},
68+
parse_structure(structure.structure["head"])]
69+
}
6070
elif structure.structure_type == "TailBranch":
61-
return {"Sequence" : [
71+
return {
72+
"type" : "Sequence",
73+
"children": [
6274
parse_structure(structure.structure["tail"]),
63-
{"Branch" : [parse_structure(n) for n in structure.structure["branches"]] },
64-
]}
75+
{
76+
"type": "Branch",
77+
"subtype": "AuxilliaryHeadBranch",
78+
"children": [parse_structure(n) for n in structure.structure["branches"]]
79+
}]
80+
}
6581
else:
6682
data = {}
6783
for k in structure.structure:
@@ -70,26 +86,27 @@ def parse_structure(node):
7086
else:
7187
data[k] = parse_structure(structure.structure[k])
7288

73-
return {structure.structure_type : data}
89+
data["type"] = structure.structure_type
90+
return data
7491

7592

7693
def flatten_sequences(structure):
7794
"""Flatten nested sequences into a single sequence."""
78-
if isinstance(structure, str) or structure is None:
95+
if isinstance(structure, str) or (isinstance(structure, dict) and structure["type"] == "Node") or structure is None:
7996
return structure
8097
else:
8198
structure = structure.copy()
82-
for k in structure:
83-
structure[k] = [flatten_sequences(sub) for sub in structure[k]]
99+
if "children" in structure:
100+
structure["children"] = [flatten_sequences(sub) for sub in structure["children"]]
84101

85-
if "Sequence" in structure:
102+
if structure["type"] == "Sequence":
86103
new_seq = []
87-
for sub in structure["Sequence"]:
88-
if isinstance(sub, dict) and "Sequence" in sub:
89-
new_seq += sub["Sequence"]
104+
for sub in structure["children"]:
105+
if isinstance(sub, dict) and sub["type"] == "Sequence":
106+
new_seq += sub["children"]
90107
else:
91108
new_seq.append(sub)
92-
structure["Sequence"] = new_seq
109+
structure["children"] = new_seq
93110
return structure
94111

95112

0 commit comments

Comments
 (0)