Community detection in graphs but with my actual data.
Python code via.
import networkx as nx
import sqlite3
import re
DG = nx.DiGraph()
datey = re.compile('.*\d{4}-\d{2}-\d{2}.*')
def exclude_datey(itr, k):
for x in itr:
if datey.match(x[k]) is None:
yield x
def dict_factory(cursor, row):
"sqlite rows turn into dicts"
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
conn = sqlite3.connect('/home/abrahms/.emacs.d/org-roam.db')
conn.row_factory = dict_factory
cursor = conn.cursor()
res = cursor.execute("select title from nodes;")
val = res.fetchall()
for e in exclude_datey(val, 'title'):
DG.add_node(e['title'])
res = cursor.execute("""
select n1.title as 'source', n2.title as 'dest'
from links l
left join nodes n1
on l.source=n1.id
left join nodes n2
on l.dest = n2.id
where type = '"id"';
""")
for e in exclude_datey(exclude_datey(res, 'dest'), 'source'):
if e['source'] is None or e['dest'] is None:
print(e)
DG.add_edge(e['source'], e['dest'])
# # communities_generator = nx.community.girvan_newman(DG)
# # communities = nx.community.greedy_modularity_communities(DG)
# # communities = nx.community.louvain_communities(DG, resolution=0.000001)
# # communities = nx.community.asyn_fluidc(DG, 20)
# # communities = nx.community.k_clique_communities(DG, 20)
# communities = nx.community.asyn_lpa_communities(DG)
# print("Communities:")
# # top_level = next(communities_generator)
# # next_level = next(communities_generator)
# # print("Next level communities:")
# for i in communities:
# if len(i) == 1:
# # boring
# continue
# print(f"- {i}\n\n")
import matplotlib.pyplot as plt
nx.draw_networkx_nodes(DG, pos=nx.spring_layout(DG))
nx.draw_networkx_labels(DG, pos=nx.spring_layout(DG))
plt.show()
Thus far, the above code hasn’t yielded particularly interesting clusters.