WIP diff centrality scores

Check whether model correction is reliable in predicting the
"expected" outcome.
This commit is contained in:
2026-03-21 21:16:18 +01:00
parent b323c724c9
commit ead3d70c35

View File

@@ -79,7 +79,7 @@ def spatial_graph(adata):
return g, weight return g, weight
def plot_graph_diff(G, c, fig, ax, name): def plot_graph_diff(G, c, fig, ax, name, cmap=plt.cm.plasma):
pos = G.vp["pos"] pos = G.vp["pos"]
x = [] x = []
y = [] y = []
@@ -89,7 +89,7 @@ def plot_graph_diff(G, c, fig, ax, name):
x.append(ver[0]) x.append(ver[0])
y.append(ver[1]) y.append(ver[1])
sc = ax.scatter(x, y, s=1, cmap=plt.cm.plasma, c=c) # map closeness values as color mapping on the verticies sc = ax.scatter(x, y, s=1, cmap=cmap, c=c) # map closeness values as color mapping on the verticies
ax.set_title(name) ax.set_title(name)
fig.colorbar(sc, ax=ax) fig.colorbar(sc, ax=ax)
@@ -200,7 +200,7 @@ row1, row2 = fig.subplots(2, 2)
ax1, ax2 = row1 ax1, ax2 = row1
# TODO select corresponding centrality measure method # TODO select corresponding centrality measure method
vp_closeness = apply(g, seed, weight, convex_hull, ax1, closeness, "Closeness") vp_closeness = apply(g, seed, weight, convex_hull, ax1, closeness, "Closeness")
# vp_betweenness = apply(g, seed, weight, convex_hull, ax2, betweenness, "Betweeness") vp_betweenness = apply(g, seed, weight, convex_hull, ax2, betweenness, "Betweeness")
# calculate convex hull # calculate convex hull
convex_hull = centrality.convex_hull(g_sub) convex_hull = centrality.convex_hull(g_sub)
@@ -214,20 +214,19 @@ fig_graph.savefig("Diff_subgraph.svg", format='svg')
ax1, ax2 = row2 ax1, ax2 = row2
vp_closeness_corrected = apply_corrected(g_sub, seed, weight_sub, convex_hull, ax1, closeness, "Closeness") vp_closeness_corrected = apply_corrected(g_sub, seed, weight_sub, convex_hull, ax1, closeness, "Closeness")
# vp_betweeness_corrected = apply_corrected(g_sub, seed, weight_sub, convex_hull, ax2, betweenness, "Betweeness") vp_betweeness_corrected = apply_corrected(g_sub, seed, weight_sub, convex_hull, ax2, betweenness, "Betweeness")
fig.savefig(f"Diff_scores.svg", format='svg') fig.savefig(f"Diff_scores.svg", format='svg')
# TODO how can I match the two vp's such that I can actually create a diff? for type in ['closeness', 'betweenness']:
# print(type)
print(f"Closeness: {vp_closeness}")
print(f"Closeness corrected: {vp_closeness_corrected}")
sub_keys = iter(g_sub.vertices()) sub_keys = iter(g_sub.vertices())
keys = iter(g.vertices()) keys = iter(g.vertices())
scores = [] scores = []
sub_scores = [] sub_scores = []
diff_scores = []
for sub_key in sub_keys: for sub_key in sub_keys:
key = next(keys) key = next(keys)
@@ -240,49 +239,57 @@ for sub_key in sub_keys:
# sub_position = g_sub.vp["pos"][sub_key] # sub_position = g_sub.vp["pos"][sub_key]
# print(f"position: {position} | sub_position: {sub_position}") # print(f"position: {position} | sub_position: {sub_position}")
value = 0.0
sub_value = 0.0
if type == 'closeness':
value = vp_closeness[key] value = vp_closeness[key]
sub_value = vp_closeness_corrected[sub_key] sub_value = vp_closeness_corrected[sub_key]
else:
value = vp_betweenness[key]
sub_value = vp_betweeness_corrected[sub_key]
scores.append(value) scores.append(value)
sub_scores.append(sub_value) sub_scores.append(sub_value)
# print(f"value: {value} | sub_value: {sub_value}") diff_scores.append(value - sub_value)
# TODO what do I want to know?
# - median score comparison?
# - max delta's between scores
# - improvement compared to with and without correction?
# TODO can I create the scatter graph with the points with their corresponding values?
median_score = np.median(scores) median_score = np.median(scores)
median_sub_score = np.median(sub_scores) median_sub_score = np.median(sub_scores)
print(f"median score: {median_score}") print(f"\tmedian score: {median_score}")
print(f"median sub_score: {median_sub_score}") print(f"\tmedian sub_score: {median_sub_score}")
print(f"median delta: {(median_score - median_sub_score)}") print(f"\tmedian delta: {(median_score - median_sub_score)}")
print("") print("")
max_value_score = np.max(scores) max_value_score = np.max(scores)
max_value_sub_score = np.max(sub_scores) max_value_sub_score = np.max(sub_scores)
print(f"max value score: {max_value_score}") print(f"\tmax value score: {max_value_score}")
print(f"max value sub_score: {max_value_sub_score}") print(f"\tmax value sub_score: {max_value_sub_score}")
print(f"max value delta: {(max_value_score - max_value_sub_score)}") print(f"\tmax value delta: {(max_value_score - max_value_sub_score)}")
print("") print("")
min_value_score = np.min(scores) min_value_score = np.min(scores)
min_value_sub_score = np.min(sub_scores) min_value_sub_score = np.min(sub_scores)
print(f"min value score: {min_value_score}") print(f"\tmin value score: {min_value_score}")
print(f"min value sub_score: {min_value_sub_score}") print(f"\tmin value sub_score: {min_value_sub_score}")
print(f"min value delta: {(min_value_score - min_value_sub_score)}") print(f"\tmin value delta: {(min_value_score - min_value_sub_score)}")
print("")
fig = plt.figure(figsize=(35, 10)) fig = plt.figure(figsize=(35, 10))
plot_graph_ax, plot_sub_graph_ax, plot_sub_graph_before_ax = fig.subplots(1, 3) plot_graph_ax, plot_sub_graph_ax, plot_sub_graph_before_ax = fig.subplots(1, 3)
plot_graph_diff(g, scores, fig, plot_graph_ax, "Original Graph (region of sub graph)") plot_graph_diff(g, scores, fig, plot_graph_ax, "Original Graph (region of sub graph)")
plot_graph_diff(g, sub_scores, fig, plot_sub_graph_ax, "Sub Graph (extracted region of original graph) with correction") plot_graph_diff(g, diff_scores, fig, plot_sub_graph_ax, "Differences after correction of sub graph compared to original graph", plt.cm.seismic)
vp = None
ep = None
if type == 'closeness':
vp = closeness(g_sub, weight=weight_sub) vp = closeness(g_sub, weight=weight_sub)
vp.a = np.nan_to_num(vp.a) # correct floating point values vp.a = np.nan_to_num(vp.a) # correct floating point values
else:
vp, ep = betweenness(g_sub, weight=weight_sub)
vp.a = np.nan_to_num(vp.a) # correct floating point values
# normalization # normalization
min_val, max_val = vp.a.min(), vp.a.max() # min_val, max_val = vp.a.min(), vp.a.max()
vp.a = (vp.a - min_val) / (max_val - min_val) # vp.a = (vp.a - min_val) / (max_val - min_val)
plot_graph_diff(g, vp.a, fig, plot_sub_graph_before_ax, "Sub Graph (extracted region of original graph) without correction") plot_graph_diff(g, vp.a, fig, plot_sub_graph_before_ax, "Sub Graph (extracted region of original graph) without correction")
fig.savefig(f"Diff_graph_scatter.svg", format='svg') fig.savefig(f"Diff_graph_scatter_{type}.svg", format='svg')