Page MenuHomec4science

plot_participant_graph.py
No OneTemporary

File Metadata

Created
Wed, Sep 25, 08:02

plot_participant_graph.py

import pandas as pd
import matplotlib.pyplot as plt
import json
import networkx as nx
def plot(path):
LABEL_IDX = 0
NAME_IDX = 1
AFFILIATION_IDX = 2
CATEGORY_IDX = 3
f = open(path, "r", encoding="utf-8")
text = f.read()
names = json.loads(text)
country_file = open("../data/dictionaries/valid_countries.txt", "r")
countries = country_file.readlines()
countries = [c.replace("\n", "") for c in countries]
"""G = nx.MultiGraph()
G.add_node("Germany")
G.add_node("Switzerland")
G.add_node("France")
G.add_edge("Germany", "Switzerland", weight=0.001)
G.add_edge("Germany", "France", weight=1)"""
G = nx.DiGraph()
G.clear()
affiliations = set()
for name, list in names.items():
previous_affiliation = ""
current_affiliation = ""
for participation in list:
if participation[AFFILIATION_IDX] in countries:
previous_affiliation = current_affiliation
current_affiliation = participation[AFFILIATION_IDX]
if current_affiliation not in affiliations:
print(current_affiliation)
G.add_node(current_affiliation)
affiliations.add(current_affiliation)
if previous_affiliation != "" and previous_affiliation != current_affiliation:
if (previous_affiliation, current_affiliation) in G.edges:
# increase weight
G[previous_affiliation][current_affiliation]["weight"] += 1
else:
G.add_edge(previous_affiliation, current_affiliation, weight=1)
# find the largest weight for the resizing of the egdes
sorted_edges = sorted(G.edges(data=True), key=lambda x: x[2]['weight'], reverse=True)
max_weight = sorted_edges[0][2]["weight"]
print(max_weight)
pos = nx.circular_layout(G)
nx.draw_networkx(G, pos)
for edge in G.edges(data='weight'):
nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2]/max_weight*10)
"""plt.subplot(122)
nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')"""
"""print(G.nodes())
nx.draw(G)"""
plt.show()

Event Timeline