WIP: diff graph with sub graph, before and after model correction

This commit is contained in:
2026-03-17 11:03:52 +01:00
parent 2ef0343338
commit b323c724c9
2 changed files with 79 additions and 6 deletions

View File

@@ -78,6 +78,22 @@ def spatial_graph(adata):
weight[e] = math.sqrt(sum(map(abs, pos[e.source()].a - pos[e.target()].a)))**2 weight[e] = math.sqrt(sum(map(abs, pos[e.source()].a - pos[e.target()].a)))**2
return g, weight return g, weight
def plot_graph_diff(G, c, fig, ax, name):
pos = G.vp["pos"]
x = []
y = []
for v in G.vertices():
ver = pos[v]
if ver[0] > 0.33 and ver[0] <= 0.66 and ver[1] > 0.33 and ver[1] <= 0.66:
x.append(ver[0])
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
ax.set_title(name)
fig.colorbar(sc, ax=ax)
def apply(g, seed, weight, convex_hull, ax, method, method_name): def apply(g, seed, weight, convex_hull, ax, method, method_name):
# calculate centrality values # calculate centrality values
vp = None vp = None
@@ -207,9 +223,66 @@ fig.savefig(f"Diff_scores.svg", format='svg')
print(f"Closeness: {vp_closeness}") print(f"Closeness: {vp_closeness}")
print(f"Closeness corrected: {vp_closeness_corrected}") print(f"Closeness corrected: {vp_closeness_corrected}")
keys = iter(vp_closeness_corrected.a) sub_keys = iter(g_sub.vertices())
keys = iter(g.vertices())
for key in keys: scores = []
# NOTE I think that the key's are not referencing the exact same point between the two centrality values! sub_scores = []
delta = vp_closeness[key] - vp_closeness_corrected[key]
print(f"original: {vp_closeness[key]} | corrected: {vp_closeness_corrected[key]} | delta: {delta}") for sub_key in sub_keys:
key = next(keys)
position = g.vp["pos"][key]
while not (position[0] > 0.33 and position[0] <= 0.66 and position[1] > 0.33 and position[1] <= 0.66):
key = next(keys)
position = g.vp["pos"][key]
# NOTE print corresponding position (which are identical)
# position = g.vp["pos"][key]
# sub_position = g_sub.vp["pos"][sub_key]
# print(f"position: {position} | sub_position: {sub_position}")
value = vp_closeness[key]
sub_value = vp_closeness_corrected[sub_key]
scores.append(value)
sub_scores.append(sub_value)
# print(f"value: {value} | sub_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_sub_score = np.median(sub_scores)
print(f"median score: {median_score}")
print(f"median sub_score: {median_sub_score}")
print(f"median delta: {(median_score - median_sub_score)}")
print("")
max_value_score = np.max(scores)
max_value_sub_score = np.max(sub_scores)
print(f"max value score: {max_value_score}")
print(f"max value sub_score: {max_value_sub_score}")
print(f"max value delta: {(max_value_score - max_value_sub_score)}")
print("")
min_value_score = np.min(scores)
min_value_sub_score = np.min(sub_scores)
print(f"min value score: {min_value_score}")
print(f"min value sub_score: {min_value_sub_score}")
print(f"min value delta: {(min_value_score - min_value_sub_score)}")
fig = plt.figure(figsize=(35, 10))
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, sub_scores, fig, plot_sub_graph_ax, "Sub Graph (extracted region of original graph) with correction")
vp = closeness(g_sub, weight=weight_sub)
vp.a = np.nan_to_num(vp.a) # correct floating point values
# normalization
min_val, max_val = vp.a.min(), vp.a.max()
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")
fig.savefig(f"Diff_graph_scatter.svg", format='svg')

View File

@@ -56,7 +56,7 @@ def correct(G, centrality, m_opt, c0_opt, b_opt):
x.append(ver[0]) x.append(ver[0])
y.append(ver[1]) y.append(ver[1])
keys = iter(centrality.a) keys = iter(G.vertices())
hull = convex_hull(G) hull = convex_hull(G)
points = np.stack((np.array(x), np.array(y)), axis=-1) points = np.stack((np.array(x), np.array(y)), axis=-1)