2
0
mirror of https://github.com/lnbook/lnbook synced 2024-11-16 21:27:59 +00:00
lnbook/code/lngraph/lngraph.py

72 lines
1.7 KiB
Python
Raw Normal View History

2021-07-27 07:11:13 +00:00
#!/usr/bin/env python3
2021-07-25 07:48:32 +00:00
# coding: utf-8
import networkx as nx
import matplotlib.pyplot as plt
import sys
import ujson as json
get_ipython().run_line_magic('matplotlib', 'inline')
filename = 'lngraph0721'
# Load channel graph exported from LND
2021-07-27 07:11:13 +00:00
# To create the JSON file, use the following command
# on an LND node that is fully synced:
2021-07-25 07:48:32 +00:00
# lndcli describegraph > filename.json
describegraph = open(filename+'.json', 'r')
lngraph = json.load(describegraph)
# Construct network graph
graph = nx.Graph()
# Add edges and nodes
for edge in lngraph['edges']:
# Name the nodes by last 4 characters of node ID
node1 = edge['node1_pub'][-4:]
node2 = edge['node2_pub'][-4:]
2021-07-27 07:11:13 +00:00
2021-07-25 07:48:32 +00:00
graph.add_node(node1)
graph.add_node(node2)
graph.add_edge(node1, node2)
# Show graph info before reduction
print(nx.info(graph))
# Remove nodes with fewer than 3 channels to make graph cleaner
remove = [node for node,degree in dict(graph.degree()).items() if degree < 3]
graph.remove_nodes_from(remove)
2021-07-27 07:11:13 +00:00
2021-07-25 07:48:32 +00:00
# Show graph info after reduction
print(nx.info(graph))
2021-07-27 07:11:13 +00:00
2021-07-25 07:48:32 +00:00
# Set figure/diagram options (thin grey lines for channels, black dots for nodes)
options = {
"node_color": "black",
"node_size": 2,
"edge_color" : "grey",
"linewidths": 0,
"width": 0.01,
}
# 16:9 image ratio
fig = plt.figure(figsize=(16,9))
2021-07-27 07:11:13 +00:00
# Spring layout arranges nodes automatically.
2021-07-25 07:48:32 +00:00
# Channels cause "attraction" (spring), nodes cause "repulsion" (opposite)
# k controls distance between nodes. Spread them out to make graph less dense
# Seed for reproducible layout
2021-07-27 07:11:13 +00:00
pos = nx.spring_layout(graph,k=0.4, iterations=10, seed=721)
2021-07-25 07:48:32 +00:00
nx.draw(graph, pos, **options)
# Save PNG image
plt.savefig(filename+'.png', format="png", dpi=600)