Get graph's island centers
Applicable to
Personal Library Notes index links
Extract
graph_path = '/home/kenkeiras/repos/codigoparallevar/_gen/notes/graph.json'
ignored_nodes = (
# Index
'ea48ec1d-f9d4-4fb7-b39a-faa7b6e2ba95',
'096ddc7b-e98e-40bf-b0d0-0b447d54265c',
# This note
'6eed20c1-d778-4b23-a04e-fc245ad8eccd',
)
import matplotlib.pyplot as plt, json, networkx
data = json.load(open(graph_path))
graph = networkx.DiGraph()
for k, v in data.items():
for l in v['links']:
graph.add_edge(k, l['target'].split(':')[-1])
for node_id in ignored_nodes:
graph.remove_node(node_id)
subgraphs = list(networkx.connected_components(graph.to_undirected()))
centers = []
for subgraph in subgraphs:
sg = networkx.DiGraph()
any_found = False
for k in subgraph:
if k not in data:
continue
v = data[k]
for l in v['links']:
sg.add_edge(k, l['target'].split(':')[-1])
any_found = True
if not any_found:
continue
sg_centers = networkx.center(sg.to_undirected())
if data.get(sg_centers[0], {}).get('title'):
centers.append(f'[[id:{sg_centers[0]}][{data.get(sg_centers[0], {})["title"]}]]')
return '\n'.join(centers)