| import chromadb |
| import networkx as nx |
| from pyvis.network import Network |
| import random |
|
|
| |
| CHROMA_PATH = "/home/wyomike/topicBuzz/my_mastodon_db" |
| COLLECTION_NAME = "mastodon_posts" |
| OUTPUT_FILE = "user_topic_network.html" |
|
|
| |
| MAX_USERS = 2000 |
| BATCH_SIZE = 5000 |
|
|
| |
| print("Connecting to DB...") |
| client = chromadb.PersistentClient(path=CHROMA_PATH) |
| collection = client.get_collection(COLLECTION_NAME) |
|
|
| total_docs = collection.count() |
| print(f"Database contains {total_docs} documents.") |
|
|
| |
| |
| edges = [] |
| topic_counts = {} |
|
|
| print("Building network connections...") |
|
|
| for offset in range(0, total_docs, BATCH_SIZE): |
| |
| batch = collection.get( |
| limit=BATCH_SIZE, |
| offset=offset, |
| include=["metadatas"] |
| ) |
| |
| for meta in batch["metadatas"]: |
| if not meta: continue |
| |
| user_id = meta.get("author_user_id") |
| topic_id = meta.get("cluster_id") |
| |
| |
| if user_id and topic_id is not None and topic_id != -1: |
| edges.append((user_id, topic_id)) |
| topic_counts[topic_id] = topic_counts.get(topic_id, 0) + 1 |
| |
| |
| if offset % 50000 == 0: |
| print(f" Processed {offset}/{total_docs} docs...") |
|
|
| print(f"Found {len(edges)} total connections.") |
|
|
| |
| |
| unique_users = list(set(uid for uid, tid in edges)) |
|
|
| if len(unique_users) > MAX_USERS: |
| print(f"Sampling {MAX_USERS} users from {len(unique_users)} total...") |
| selected_users = set(random.sample(unique_users, MAX_USERS)) |
| filtered_edges = [(u, t) for u, t in edges if u in selected_users] |
| else: |
| filtered_edges = edges |
|
|
| print(f"Graphing {len(filtered_edges)} connections...") |
|
|
| |
| G = nx.Graph() |
|
|
| for user_id, topic_id in filtered_edges: |
| |
| G.add_node(user_id, label=f"User {user_id}", title=f"User: {user_id}", color="#97C2FC", size=10, group="users") |
| |
| |
| topic_node_id = f"Topic_{topic_id}" |
| |
| |
| |
| size = max(20, min(50, topic_counts.get(topic_id, 10) / 10)) |
| |
| G.add_node(topic_node_id, label=f"Topic {topic_id}", title=f"Topic {topic_id} ({topic_counts.get(topic_id,0)} posts)", color="#FB7E81", size=size, group="topics") |
| |
| |
| G.add_edge(user_id, topic_node_id) |
|
|
| |
| print("Generating interactive HTML...") |
| nt = Network(height="750px", width="100%", bgcolor="#222222", font_color="white", select_menu=True) |
|
|
| |
| nt.from_nx(G) |
|
|
| |
| |
| |
| |
| nt.toggle_physics(False) |
|
|
| |
| nt.save_graph(OUTPUT_FILE) |
| print(f"Done! Open '{OUTPUT_FILE}' in your browser to explore the network.") |