Commit 7cf9b239 authored by Jerome Wuerf's avatar Jerome Wuerf
Browse files

Add graph analyzation

parent 7582599c
# %% # %%
import pandas as pd import pandas as pd
import pickle import pickle
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import networkx as nx import networkx as nx
...@@ -15,43 +17,64 @@ argument_edges = pd.Series( ...@@ -15,43 +17,64 @@ argument_edges = pd.Series(
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [4, 1]}, figsize=(10, 5)) fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [4, 1]}, figsize=(10, 5))
fig.suptitle('Argument Edges') fig.suptitle('Argument Edges')
ax1.bar(argument_edges.index, argument_edges) ax1.bar(argument_edges.index, argument_edges)
ax1.set_xlabel('Topic number')
ax1.set_ylabel('# Edges')
ax2.boxplot(argument_edges) ax2.boxplot(argument_edges)
ax2.yaxis.set_ticklabels([]) ax2.yaxis.set_ticklabels([])
ax2.xaxis.set_ticklabels([])
plt.savefig('argument_edges.png')
# %% # %%
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [4, 1]}, figsize=(10, 5)) fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [4, 1]}, figsize=(10, 5))
fig.suptitle('Argument Nodes') fig.suptitle('Argument Nodes')
ax1.bar(argument_nodes.index, argument_nodes) ax1.bar(argument_nodes.index, argument_nodes)
ax2.boxplot(argument_nodes) ax2.boxplot(argument_nodes)
ax1.set_xlabel('Topic number')
ax1.set_ylabel('# Nodes')
ax2.yaxis.set_ticklabels([]) ax2.yaxis.set_ticklabels([])
ax2.xaxis.set_ticklabels([])
plt.savefig('argument_nodes.png')
# %% # %%
fig, axis = plt.subplots(5, 10, figsize=(15, 10)) fig, axis = plt.subplots(1, 5, figsize=(13, 5))
for ax, elem in zip(axis.flatten(), graphs.items()): fig.suptitle('Argument Graphs per Topic')
half_graphs = list(graphs.items())[:5]
for ax, elem in zip(axis.flatten(), half_graphs):
topic_nrb = elem[0] topic_nrb = elem[0]
g = elem[1] ax.set_title(topic_nrb)
f = nx.Graph() g = elem[1].to_undirected()
fedges = filter(lambda x: g.degree()[x[0]] > 0 and g.degree()[x[1]] > 0, g.edges()) Gcc = g.subgraph(sorted(nx.connected_components(g), key=len, reverse=True)[0])
f.add_edges_from(fedges) pos = nx.spring_layout(Gcc, seed=10396953)
nx.draw(f, node_size=10, ax=ax) nx.draw_networkx_nodes(Gcc, pos, ax=ax, node_size=20)
nx.draw_networkx_edges(Gcc, pos, ax=ax, alpha=0.4)
plt.savefig('argument_graphs.png')
# %% # %%
len(graphs)
# %%
ax
# %% fig, axis = plt.subplots(5, 10, figsize=(20, 15))
# %%
fig, axis = plt.subplots(5, 10, figsize=(20, 10))
for ax, elem in zip(axis.flatten(), graphs.items()): for ax, elem in zip(axis.flatten(), graphs.items()):
topic_nrb = elem[0] topic_nrb = elem[0]
ax.set_title(topic_nrb)
g = elem[1] g = elem[1]
f = nx.Graph() f = nx.Graph()
fedges = filter(lambda x: g.degree()[x[0]] > 1 and g.degree()[x[1]] > 1, g.edges()) fedges = filter(lambda x: g.degree()[x[0]] > 1 and g.degree()[x[1]] > 1, g.edges())
f.add_edges_from(fedges) f.add_edges_from(fedges)
ax.hist(nx.pagerank(f).values()) ax.hist(nx.pagerank(f).values())
# %%
degree_list = [g.degree() for g in graphs.values()]
degree_list = [item[1] for sublist in degree_list for item in sublist]
fig, ax = plt.subplots()
ax.set_title('Degrees Histogramm of Argument Graphs (all Topics)')
ax.set_yscale('log')
ax.set_xlabel("Degree")
ax.set_ylabel("# of Nodes")
ax.hist(degree_list)
plt.savefig('degree_histogramm.png')
# %% # %%
...@@ -35,6 +35,8 @@ class ArgumentGraph: ...@@ -35,6 +35,8 @@ class ArgumentGraph:
premise_argument_id = premise["_id"].split('_')[0] premise_argument_id = premise["_id"].split('_')[0]
if premise_argument_id not in graph: if premise_argument_id not in graph:
graph.add_node(premise_argument_id) graph.add_node(premise_argument_id)
else:
graph.nodes[premise_argument_id]['premises']
for conc_idx in range(len(premises_per_conclusions)): for conc_idx in range(len(premises_per_conclusions)):
sim = sim_matrices[conc_idx][prem_idx] sim = sim_matrices[conc_idx][prem_idx]
if sim >= self.threshold: if sim >= self.threshold:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment