WIP different small python scripts to generate corresponding images

The final API will be derived from these scripts into a different
repository, which then only holds the corresponding functions that
provide the corresponding functionalities described in the associated
master thesis.
This commit is contained in:
2026-03-28 15:04:38 +01:00
parent ead3d70c35
commit 7581966c88
8 changed files with 469 additions and 198 deletions

View File

@@ -2,7 +2,7 @@ import math
import matplotlib.pyplot as plt
import numpy as np
import squidpy as sq
# import squidpy as sq
from graph_tool.all import *
from src import centrality
@@ -29,6 +29,7 @@ def mibitof():
adata = sq.datasets.mibitof()
return adata
def random_graph(n=5000, seed=None):
"""
Uniformly random point cloud generation.
@@ -96,207 +97,60 @@ def spatial_graph(adata):
weight[e] = math.sqrt(sum(map(abs, pos[e.source()].a - pos[e.target()].a)))**2
return g, weight
def merfish_example():
# generate spatial graph from a given dataset
g, weight = spatial_graph(merfish().obsm['spatial'])
g = GraphView(g)
x_spatial = []
for v in g.vertices():
x_spatial.append(g.vp["pos"][v][0])
points, seed = random_graph()
g, weight = spatial_graph(points)
g = GraphView(g)
# calculate centrality values
vp = closeness(g, weight=weight)
vp.a = np.nan_to_num(vp.a) # correct floating point values
# calculate centrality values
vp = closeness(g, weight=weight)
vp.a = np.nan_to_num(vp.a) # correct floating point values
# ep.a = np.nan_to_num(ep.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)
# normalization
# min_val, max_val = vp.a.min(), vp.a.max()
# vp.a = (vp.a - min_val) / (max_val - min_val)
# calculate convex hull
convex_hull = centrality.convex_hull(g)
# calculate convex hull
convex_hull = centrality.convex_hull(g)
# plot graph with convex_hull
fig = plt.figure(figsize=(15, 5))
ax0, ax1 = fig.subplots(1, 2)
plot.graph_plot(fig, ax0, g, vp, convex_hull, f"Merfish\nCloseness")
# plot graph with convex_hull
fig = plt.figure(figsize=(15, 5))
ax0, ax1 = fig.subplots(1, 2)
plot.graph_plot(fig, ax0, g, vp, convex_hull, f"Random Graph (seed: {seed})")
# generate model based on convex hull and associated centrality values
quantification = plot.quantification_data(g, vp, convex_hull)
# generate model based on convex hull and associated centrality values
quantification = plot.quantification_data(g, vp, convex_hull)
# optimize model's piece-wise linear function
d = quantification[:, 0]
C = quantification[:, 1]
m_opt, c0_opt, b_opt, aic_opt = fitting.fit_piece_wise_linear(d, C)
# optimize model's piece-wise linear function
d = quantification[:, 0]
C = quantification[:, 1]
m_opt, c0_opt, b_opt, aic_opt = fitting.fit_piece_wise_linear(d, C)
# TODO
# should this be part of the plotting function itself, it should not be necessary for me to do this
d_curve = np.linspace(min(d), max(d), 500)
C_curve = np.piecewise(
d_curve,
[d_curve <= b_opt, d_curve > b_opt],
[lambda x: m_opt * x + c0_opt, lambda x: m_opt * b_opt + c0_opt]
)
# plot model containing modeled piece-wise linear function
plot.quantification_plot(ax1, quantification, d_curve, C_curve, 'Models', aic_opt)
# TODO
# should this be part of the plotting function itself, it should not be necessary for me to do this
d_curve = np.linspace(min(d), max(d), 500)
C_curve = np.piecewise(
d_curve,
[d_curve <= b_opt, d_curve > b_opt],
[lambda x: m_opt * x + c0_opt, lambda x: m_opt * b_opt + c0_opt]
)
# plot model containing modeled piece-wise linear function
plot.quantification_plot(ax1, quantification, d_curve, C_curve, 'Closeness', aic_opt)
# linear regression model
m_reg, c_reg, aic_reg = fitting.fit_linear_regression(d, C)
# # linear regression model
# m_reg, c0_reg, b_reg, aic_reg = fitting.fit_cut(d, C)
# print(f"m_reg = {m_reg}")
x = np.linspace(min(d), max(d), 500)
y = m_reg * x + c_reg
ax1.plot(x, y, color='k', linewidth=1, label=f"Simple Linear Regression | AIC: {aic_reg}")
ax1.legend()
# # TODO
# # should this be part of the plotting function itself, it should not be necessary for me to do this
# d_curve = np.linspace(min(d), max(d), 500)
# C_curve = np.piecewise(
# d_curve,
# [d_curve <= b_reg, d_curve > b_reg],
# [lambda x: m_reg * x + c0_reg, lambda x: m_reg * b_reg + c0_reg]
# )
# ax1.plot(d_curve, C_curve, color='k', linewidth=1, label=f"Top Cut | AIC: {aic_reg}")
# ax1.legend()
fig.savefig(f"Merfish_closeness.svg", format='svg')
for i in range(1, 6):
points, seed = random_graph()
g, weight = spatial_graph(points)
g = GraphView(g)
x_spatial = []
for v in g.vertices():
x_spatial.append(g.vp["pos"][v][0])
# calculate centrality values
vp = closeness(g, weight=weight)
vp.a = np.nan_to_num(vp.a) # correct floating point values
# ep.a = np.nan_to_num(ep.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)
# calculate convex hull
convex_hull = centrality.convex_hull(g)
# plot graph with convex_hull
fig = plt.figure(figsize=(15, 5))
ax0, ax1 = fig.subplots(1, 2)
plot.graph_plot(fig, ax0, g, vp, convex_hull, f"Random Graph (seed: {seed})\nCloseness")
# generate model based on convex hull and associated centrality values
quantification = plot.quantification_data(g, vp, convex_hull)
# optimize model's piece-wise linear function
d = quantification[:, 0]
C = quantification[:, 1]
m_opt, c0_opt, b_opt, aic_opt = fitting.fit_piece_wise_linear(d, C)
# TODO
# should this be part of the plotting function itself, it should not be necessary for me to do this
d_curve = np.linspace(min(d), max(d), 500)
C_curve = np.piecewise(
d_curve,
[d_curve <= b_opt, d_curve > b_opt],
[lambda x: m_opt * x + c0_opt, lambda x: m_opt * b_opt + c0_opt]
)
# plot model containing modeled piece-wise linear function
plot.quantification_plot(ax1, quantification, d_curve, C_curve, 'Models', aic_opt)
# linear regression model
m_reg, c_reg, aic_reg = fitting.fit_linear_regression(d, C)
x = np.linspace(min(d), max(d), 500)
y = m_reg * x + c_reg
ax1.plot(x, y, color='k', linewidth=1, label=f"Simple Linear Regression | AIC: {aic_reg}")
ax1.legend()
fig.savefig(f"uniform_random_point_clouds/{i}_closeness.svg", format='svg')
# ---------------------------------------------------------------------------------------------
# calculate centrality values
vp, ep = betweenness(g, weight=weight)
vp.a = np.nan_to_num(vp.a) # correct floating point values
# ep.a = np.nan_to_num(ep.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)
# calculate convex hull
convex_hull = centrality.convex_hull(g)
# plot graph with convex_hull
fig = plt.figure(figsize=(15, 5))
ax0, ax1 = fig.subplots(1, 2)
plot.graph_plot(fig, ax0, g, vp, convex_hull, f"Random Graph (seed: {seed})\nBetweenness")
# generate model based on convex hull and associated centrality values
quantification = plot.quantification_data(g, vp, convex_hull)
# optimize model's piece-wise linear function
d = quantification[:, 0]
C = quantification[:, 1]
m_opt, c0_opt, b_opt, aic_opt = fitting.fit_piece_wise_linear(d, C)
# TODO
# should this be part of the plotting function itself, it should not be necessary for me to do this
d_curve = np.linspace(min(d), max(d), 500)
C_curve = np.piecewise(
d_curve,
[d_curve <= b_opt, d_curve > b_opt],
[lambda x: m_opt * x + c0_opt, lambda x: m_opt * b_opt + c0_opt]
)
# plot model containing modeled piece-wise linear function
plot.quantification_plot(ax1, quantification, d_curve, C_curve, 'Models', aic_opt)
# linear regression model
m_reg, c_reg, aic_reg = fitting.fit_linear_regression(d, C)
x = np.linspace(min(d), max(d), 500)
y = m_reg * x + c_reg
ax1.plot(x, y, color='k', linewidth=1, label=f"Simple Linear Regression | AIC: {aic_reg}")
ax1.legend()
fig.savefig(f"uniform_random_point_clouds/{i}_betweenness.svg", format='svg')
# ---------------------------------------------------------------------------------------------
# calculate centrality values
vp = pagerank(g, weight=weight)
vp.a = np.nan_to_num(vp.a) # correct floating point values
# ep.a = np.nan_to_num(ep.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)
# calculate convex hull
convex_hull = centrality.convex_hull(g)
# plot graph with convex_hull
fig = plt.figure(figsize=(15, 5))
ax0, ax1 = fig.subplots(1, 2)
plot.graph_plot(fig, ax0, g, vp, convex_hull, f"Random Graph (seed: {seed})\nPageRank")
# generate model based on convex hull and associated centrality values
quantification = plot.quantification_data(g, vp, convex_hull)
# optimize model's piece-wise linear function
d = quantification[:, 0]
C = quantification[:, 1]
m_opt, c0_opt, b_opt, aic_opt = fitting.fit_piece_wise_linear(d, C)
# TODO
# should this be part of the plotting function itself, it should not be necessary for me to do this
d_curve = np.linspace(min(d), max(d), 500)
C_curve = np.piecewise(
d_curve,
[d_curve <= b_opt, d_curve > b_opt],
[lambda x: m_opt * x + c0_opt, lambda x: m_opt * b_opt + c0_opt]
)
# plot model containing modeled piece-wise linear function
plot.quantification_plot(ax1, quantification, d_curve, C_curve, 'Models', aic_opt)
# linear regression model
m_reg, c_reg, aic_reg = fitting.fit_linear_regression(d, C)
x = np.linspace(min(d), max(d), 500)
y = m_reg * x + c_reg
ax1.plot(x, y, color='k', linewidth=1, label=f"Simple Linear Regression | AIC: {aic_reg}")
ax1.legend()
fig.savefig(f"uniform_random_point_clouds/{i}_pagerank.svg", format='svg')
fig.savefig(f"model_closeness_5000_fitted.svg", format='svg')