mod update corresponding examples

This commit is contained in:
2026-04-09 08:48:07 +02:00
parent be101411cd
commit a89c6d4833
6 changed files with 155 additions and 111 deletions
+18 -17
View File
@@ -1,6 +1,7 @@
import math
import matplotlib.pyplot as plt
from matplotlib.colors import TwoSlopeNorm
import numpy as np
from graph_tool.all import *
@@ -87,7 +88,7 @@ def plot_graph_diff(G, c, fig, ax, name, cmap=plt.cm.plasma):
x.append(ver[0])
y.append(ver[1])
sc = ax.scatter(x, y, s=1, cmap=cmap, c=c) # map closeness values as color mapping on the verticies
sc = ax.scatter(x, y, s=1, cmap=cmap, norm=TwoSlopeNorm(0), c=c) # map closeness values as color mapping on the verticies
ax.set_title(name)
fig.colorbar(sc, ax=ax)
@@ -95,8 +96,8 @@ def plot_graph_diff(G, c, fig, ax, name, cmap=plt.cm.plasma):
def apply(g, seed, weight, convex_hull, ax, method, method_name):
# calculate centrality values
vp = None
if method_name == "Betweenness":
vp, ep = method(g, weight=weight)
if method_name == "Closeness":
vp = method(g, weight=weight)
elif method_name == "Eigenvector":
ep, vp = method(g, weight=weight)
elif method_name == "Hits":
@@ -136,8 +137,8 @@ def apply_corrected(g, seed, weight, convex_hull, ax, method, method_name):
# calculate centrality values
vp = None
ep = None
if method_name == "Betweenness":
vp, ep = method(g, weight=weight)
if method_name == "Closeness":
vp = method(g, weight=weight)
elif method_name == "Eigenvector":
ep, vp = method(g, weight=weight)
elif method_name == "Hits":
@@ -183,7 +184,7 @@ def apply_corrected(g, seed, weight, convex_hull, ax, method, method_name):
# - apply centrality measure to the next axis
# - Draw the corresponding resulting models into a grid
#
points, seed = random_graph(n=5000)
points, seed = random_graph(n=5000, seed=303437129487698362622376224319354280305)
g, weight = spatial_graph(points)
g = GraphView(g)
@@ -193,15 +194,15 @@ convex_hull = centrality.convex_hull(g)
# plot graph with convex_hull
fig_graph, ax_graph = plt.subplots(figsize=(15, 12))
# draw without any centrality measure `vp`
vp, ep = betweenness(g, weight=weight)
vp = closeness(g, weight=weight)
plot.graph_plot(fig_graph, ax_graph, g, vp, convex_hull, f"Pointcloud (seed: {seed})")
fig_graph.savefig("model_prediction_graph_original_betweenness_5000.svg", format='svg')
fig_graph.savefig("model_prediction_graph_original_closeness_5000.svg", format='svg')
# normalization
min_val, max_val = vp.a.min(), vp.a.max()
vp.a = (vp.a - min_val) / (max_val - min_val)
vp_betweenness_original = vp
vp_closeness_original = vp
for percentage in np.arange(0.1, 1, 0.1, dtype=float):
print(f"Percentage: {percentage:.0%}")
@@ -211,15 +212,15 @@ for percentage in np.arange(0.1, 1, 0.1, dtype=float):
# draw subgraph
fig_sub = plt.figure(figsize=(25, 12))
ax1, ax2 = fig_sub.subplots(1, 2)
vp, ep = betweenness(g_sub, weight=weight_sub)
vp = closeness(g_sub, weight=weight_sub)
plot.graph_plot(fig_sub, ax1, g_sub, vp, convex_hull, f"{percentage:.0%} of Pointcloud (seed: {seed})")
min_val, max_val = vp.a.min(), vp.a.max()
vp.a = (vp.a - min_val) / (max_val - min_val)
vp_betweenness_corrected = apply_corrected(g_sub, seed, weight_sub, convex_hull, None, betweenness, "Betweenness")
plot.graph_plot(fig_sub, ax2, g_sub, vp_betweenness_corrected, convex_hull, f"{percentage:.0%} of Pointcloud with applied prediction")
fig_sub.savefig(f"model_prediction_subgraph_betweenness_5000_{percentage * 100:.0f}_percent.svg", format='svg')
vp_closeness_corrected = apply_corrected(g_sub, seed, weight_sub, convex_hull, None, closeness, "Closeness")
plot.graph_plot(fig_sub, ax2, g_sub, vp_closeness_corrected, convex_hull, f"{percentage:.0%} of Pointcloud with applied prediction")
fig_sub.savefig(f"model_prediction_subgraph_closeness_5000_{percentage * 100:.0f}_percent.svg", format='svg')
distance_of_center = 0.5 * percentage
@@ -243,10 +244,10 @@ for percentage in np.arange(0.1, 1, 0.1, dtype=float):
# sub_position = g_sub.vp["pos"][sub_key]
# print(f"position: {position} | sub_position: {sub_position}")
# calculate for betweenness
value = vp_betweenness_original[key]
# calculate for closeness
value = vp_closeness_original[key]
pre_prediction = vp[sub_key]
sub_value = vp_betweenness_corrected[sub_key]
sub_value = vp_closeness_corrected[sub_key]
scores.append(value)
raw_sub_scores.append(pre_prediction)
@@ -291,4 +292,4 @@ for percentage in np.arange(0.1, 1, 0.1, dtype=float):
plot_graph_diff(g, diff_scores, fig, plot_sub_graph_ax, "Differences after correction of sub graph compared to original graph", plt.cm.seismic)
plot_graph_diff(g, vp.a, fig, plot_sub_graph_before_ax, "Sub Graph (extracted region of original graph) without correction")
fig.savefig(f"model_prediction_subgraph_betweenness_5000_{percentage * 100:.0f}_percentage_diff.svg", format='svg')
fig.savefig(f"model_prediction_subgraph_closeness_5000_{percentage * 100:.0f}_percentage_diff.svg", format='svg')