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
+35 -53
View File
@@ -2,6 +2,7 @@ import math
import matplotlib.pyplot as plt
import numpy as np
import squidpy as sq
from graph_tool.all import *
from src import centrality
@@ -9,6 +10,23 @@ from src import plot
from src import fitting
def merfish():
"""
Merfish dataset from `squidpy`.
"""
adata = sq.datasets.merfish()
adata = adata[adata.obs.Bregma == -9].copy()
return adata
def mibitof():
"""
Mibitof dataset from `squidpy`.
"""
adata = sq.datasets.mibitof()
return adata
def degree(g, weight):
# VertexPropertyMap
vp = g.new_vertex_property("double")
@@ -25,6 +43,8 @@ def leverage(g, weight):
li = 0.0
neighbours = g.get_all_neighbours(v)
ki = len(neighbours)
if ki == 0:
continue
# sum
for nv in neighbours:
other_neighbours = g.get_all_neighbours(nv)
@@ -48,46 +68,6 @@ def random_graph(n=5000, seed=None):
return rng.random((n, 2)), seed
def random_graph_favor_border(n=3000, seed = None):
if seed is None:
import secrets
seed = secrets.randbits(128)
rng = np.random.default_rng(seed=seed)
vps = np.zeros((n, 2))
for i in range(0, n):
r_x = rng.random()
if rng.random() > 0.5:
while (r_x > 0.3 and r_x < 0.7):
r_x = rng.random()
r_y = rng.random()
if rng.random() > 0.5:
while (r_y > 0.3 and r_y < 0.7):
r_y = rng.random()
vps[i][0] = r_x
vps[i][1] = r_y
return vps, seed
def random_graph_favor_center(n=3000, seed = None):
if seed is None:
import secrets
seed = secrets.randbits(128)
rng = np.random.default_rng(seed=seed)
vps = np.zeros((n, 2))
for i in range(0, n):
r_x = rng.random()
if rng.random() > 0.7:
while (r_x < 0.4 or r_x > 0.6):
r_x = rng.random()
r_y = rng.random()
if rng.random() > 0.7:
while (r_y < 0.4 or r_y > 0.6):
r_y = rng.random()
vps[i][0] = r_x
vps[i][1] = r_y
return vps, seed
def spatial_graph(adata):
"""
Generate the spatial graph using delaunay for the given `adata`.
@@ -102,6 +82,7 @@ def spatial_graph(adata):
weight[e] = math.sqrt(sum(map(abs, pos[e.source()].a - pos[e.target()].a)))**2
return g, weight
def apply(g, seed, weight, convex_hull, ax, method, method_name):
# calculate centrality values
vp = None
@@ -147,8 +128,9 @@ def apply(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)
g, weight = spatial_graph(points)
# points, seed = random_graph(n=5000)
adata = mibitof()
g, weight = spatial_graph(adata.obsm['spatial'])
g = GraphView(g)
# calculate convex hull
convex_hull = centrality.convex_hull(g)
@@ -157,23 +139,23 @@ convex_hull = centrality.convex_hull(g)
fig_graph, ax_graph = plt.subplots(figsize=(15, 12))
# draw without any centrality measure `vp`
vp = g.new_vertex_property("double")
plot.graph_plot(fig_graph, ax_graph, g, vp, convex_hull, f"Pointcloud (seed: {seed})")
fig_graph.savefig("Pointcloud_graph.svg", format='svg')
plot.graph_plot(fig_graph, ax_graph, g, vp, convex_hull, f"mibitof")
fig_graph.savefig(f"mibitof_graph.svg", format='svg')
fig = plt.figure(figsize=(15, 12))
row1, row2 = fig.subplots(2, 4)
ax1, ax2, ax3, ax4 = row1
# TODO select corresponding centrality measure method
apply(g, seed, weight, convex_hull, ax1, closeness, "Closeness")
apply(g, seed, weight, convex_hull, ax2, pagerank, "PageRank")
apply(g, seed, weight, convex_hull, ax3, betweenness, "Betweeness")
apply(g, seed, weight, convex_hull, ax4, eigenvector, "Eigenvector")
apply(g, None, weight, convex_hull, ax1, closeness, "Closeness")
apply(g, None, weight, convex_hull, ax2, pagerank, "PageRank")
apply(g, None, weight, convex_hull, ax3, betweenness, "Betweeness")
apply(g, None, weight, convex_hull, ax4, eigenvector, "Eigenvector")
ax1, ax2, ax3, ax4 = row2
apply(g, seed, weight, convex_hull, ax1, katz, "Katz")
apply(g, seed, weight, convex_hull, ax2, hits, "Hits")
apply(g, seed, weight, convex_hull, ax3, leverage, "Leverage")
apply(g, seed, weight, convex_hull, ax4, degree, "Degree")
apply(g, None, weight, convex_hull, ax1, katz, "Katz")
apply(g, None, weight, convex_hull, ax2, hits, "Hits")
apply(g, None, weight, convex_hull, ax3, leverage, "Leverage")
apply(g, None, weight, convex_hull, ax4, degree, "Degree")
fig.savefig(f"Comparison_Pointcloud.svg", format='svg')
fig.savefig(f"Comparison_node_centralities_mibitof_.svg", format='svg')