canns 0.14.3__py3-none-any.whl → 0.15.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- canns/analyzer/data/asa/__init__.py +77 -21
- canns/analyzer/data/asa/coho.py +97 -0
- canns/analyzer/data/asa/cohomap.py +408 -0
- canns/analyzer/data/asa/cohomap_scatter.py +10 -0
- canns/analyzer/data/asa/cohomap_vectors.py +311 -0
- canns/analyzer/data/asa/cohospace.py +173 -1153
- canns/analyzer/data/asa/cohospace_phase_centers.py +137 -0
- canns/analyzer/data/asa/cohospace_scatter.py +1220 -0
- canns/analyzer/data/asa/embedding.py +3 -4
- canns/analyzer/data/asa/plotting.py +4 -4
- canns/analyzer/data/cell_classification/__init__.py +10 -0
- canns/analyzer/data/cell_classification/core/__init__.py +4 -0
- canns/analyzer/data/cell_classification/core/btn.py +272 -0
- canns/analyzer/data/cell_classification/visualization/__init__.py +3 -0
- canns/analyzer/data/cell_classification/visualization/btn_plots.py +258 -0
- canns/analyzer/visualization/__init__.py +2 -0
- canns/analyzer/visualization/core/config.py +20 -0
- canns/analyzer/visualization/theta_sweep_plots.py +142 -0
- canns/pipeline/asa/runner.py +19 -19
- canns/pipeline/asa_gui/__init__.py +5 -3
- canns/pipeline/asa_gui/analysis_modes/pathcompare_mode.py +3 -1
- canns/pipeline/asa_gui/core/runner.py +23 -23
- canns/pipeline/asa_gui/views/pages/preprocess_page.py +7 -12
- {canns-0.14.3.dist-info → canns-0.15.0.dist-info}/METADATA +1 -1
- {canns-0.14.3.dist-info → canns-0.15.0.dist-info}/RECORD +28 -20
- {canns-0.14.3.dist-info → canns-0.15.0.dist-info}/WHEEL +0 -0
- {canns-0.14.3.dist-info → canns-0.15.0.dist-info}/entry_points.txt +0 -0
- {canns-0.14.3.dist-info → canns-0.15.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
"""Stripe vectors and diagnostics for CohoMap."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
import numpy as np
|
|
10
|
+
from scipy.ndimage import rotate
|
|
11
|
+
|
|
12
|
+
from ...visualization.core import PlotConfig, finalize_figure
|
|
13
|
+
from .cohomap import fit_cohomap_stripes
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _ensure_plot_config(
|
|
17
|
+
config: PlotConfig | None,
|
|
18
|
+
factory,
|
|
19
|
+
*args,
|
|
20
|
+
**defaults,
|
|
21
|
+
) -> PlotConfig:
|
|
22
|
+
if config is None:
|
|
23
|
+
return factory(*args, **defaults)
|
|
24
|
+
return config
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _ensure_parent_dir(save_path: str | None) -> None:
|
|
28
|
+
if save_path:
|
|
29
|
+
parent = os.path.dirname(save_path)
|
|
30
|
+
if parent:
|
|
31
|
+
os.makedirs(parent, exist_ok=True)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _rot_para(params1: np.ndarray, params2: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
|
35
|
+
if abs(np.cos(params1[0])) < abs(np.cos(params2[0])):
|
|
36
|
+
y = params1.copy()
|
|
37
|
+
x = params2.copy()
|
|
38
|
+
else:
|
|
39
|
+
x = params1.copy()
|
|
40
|
+
y = params2.copy()
|
|
41
|
+
|
|
42
|
+
alpha = y[0] - x[0]
|
|
43
|
+
if (alpha < 0) and (abs(alpha) > np.pi / 2):
|
|
44
|
+
x[0] += np.pi
|
|
45
|
+
x[0] = x[0] % (2 * np.pi)
|
|
46
|
+
elif (alpha < 0) and (abs(alpha) < np.pi / 2):
|
|
47
|
+
x[0] += np.pi * 4 / 3
|
|
48
|
+
x[0] = x[0] % (2 * np.pi)
|
|
49
|
+
elif abs(alpha) > np.pi / 2:
|
|
50
|
+
y[0] -= np.pi / 3
|
|
51
|
+
y[0] = y[0] % (2 * np.pi)
|
|
52
|
+
|
|
53
|
+
if y[0] > np.pi / 2:
|
|
54
|
+
y[0] -= np.pi / 6
|
|
55
|
+
x[0] -= np.pi / 6
|
|
56
|
+
x[0] = x[0] % (2 * np.pi)
|
|
57
|
+
y[0] = y[0] % (2 * np.pi)
|
|
58
|
+
if x[0] > np.pi / 2:
|
|
59
|
+
y[0] += np.pi / 6
|
|
60
|
+
x[0] += np.pi / 6
|
|
61
|
+
x[0] = x[0] % (2 * np.pi)
|
|
62
|
+
y[0] = y[0] % (2 * np.pi)
|
|
63
|
+
|
|
64
|
+
return x, y
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def cohomap_vectors(
|
|
68
|
+
cohomap_result: dict[str, Any],
|
|
69
|
+
*,
|
|
70
|
+
grid_size: int | None = 151,
|
|
71
|
+
trim: int = 25,
|
|
72
|
+
angle_grid: int = 10,
|
|
73
|
+
phase_grid: int = 10,
|
|
74
|
+
spacing_grid: int = 10,
|
|
75
|
+
spacing_range: tuple[float, float] = (1.0, 6.0),
|
|
76
|
+
) -> dict[str, Any]:
|
|
77
|
+
"""
|
|
78
|
+
Fit CohoMap stripe parameters and compute parallelogram vectors (v, w).
|
|
79
|
+
|
|
80
|
+
Returns a dict containing the stripe fit, rotated parameters, vector components,
|
|
81
|
+
and angle (deg) following GridCellTorus conventions.
|
|
82
|
+
"""
|
|
83
|
+
phase_map1 = np.asarray(cohomap_result["phase_map1"])
|
|
84
|
+
phase_map2 = np.asarray(cohomap_result["phase_map2"])
|
|
85
|
+
x_edge = np.asarray(cohomap_result["x_edge"])
|
|
86
|
+
y_edge = np.asarray(cohomap_result["y_edge"])
|
|
87
|
+
|
|
88
|
+
p1, f1 = fit_cohomap_stripes(
|
|
89
|
+
phase_map1,
|
|
90
|
+
grid_size=grid_size,
|
|
91
|
+
trim=trim,
|
|
92
|
+
angle_grid=angle_grid,
|
|
93
|
+
phase_grid=phase_grid,
|
|
94
|
+
spacing_grid=spacing_grid,
|
|
95
|
+
spacing_range=spacing_range,
|
|
96
|
+
)
|
|
97
|
+
p2, f2 = fit_cohomap_stripes(
|
|
98
|
+
phase_map2,
|
|
99
|
+
grid_size=grid_size,
|
|
100
|
+
trim=trim,
|
|
101
|
+
angle_grid=angle_grid,
|
|
102
|
+
phase_grid=phase_grid,
|
|
103
|
+
spacing_grid=spacing_grid,
|
|
104
|
+
spacing_range=spacing_range,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
x_params, y_params = _rot_para(p1, p2)
|
|
108
|
+
|
|
109
|
+
x_edge_shift = x_edge - float(x_edge.min())
|
|
110
|
+
y_edge_shift = y_edge - float(y_edge.min())
|
|
111
|
+
xmax = float(x_edge_shift.max())
|
|
112
|
+
ymax = float(y_edge_shift.max())
|
|
113
|
+
|
|
114
|
+
v = np.array(
|
|
115
|
+
[
|
|
116
|
+
(1 / x_params[2]) * np.cos(x_params[0]) * xmax,
|
|
117
|
+
(1 / x_params[2]) * np.sin(x_params[0]) * xmax,
|
|
118
|
+
],
|
|
119
|
+
dtype=float,
|
|
120
|
+
)
|
|
121
|
+
w = np.array(
|
|
122
|
+
[
|
|
123
|
+
(1 / y_params[2]) * np.cos(y_params[0]) * ymax,
|
|
124
|
+
(1 / y_params[2]) * np.sin(y_params[0]) * ymax,
|
|
125
|
+
],
|
|
126
|
+
dtype=float,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
angle_deg = float(((y_params[0] - x_params[0]) / (2 * np.pi) * 360.0) % 360.0)
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
"params1": p1,
|
|
133
|
+
"params2": p2,
|
|
134
|
+
"fit_error1": float(f1),
|
|
135
|
+
"fit_error2": float(f2),
|
|
136
|
+
"x_params": x_params,
|
|
137
|
+
"y_params": y_params,
|
|
138
|
+
"x_edge": x_edge,
|
|
139
|
+
"y_edge": y_edge,
|
|
140
|
+
"x_range": xmax,
|
|
141
|
+
"y_range": ymax,
|
|
142
|
+
"v": v,
|
|
143
|
+
"w": w,
|
|
144
|
+
"len_v": float(1 / x_params[2] * xmax),
|
|
145
|
+
"len_w": float(1 / y_params[2] * ymax),
|
|
146
|
+
"angle_deg": angle_deg,
|
|
147
|
+
"grid_size": grid_size,
|
|
148
|
+
"trim": trim,
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def plot_cohomap_vectors(
|
|
153
|
+
cohomap_vectors_result: dict[str, Any],
|
|
154
|
+
*,
|
|
155
|
+
config: PlotConfig | None = None,
|
|
156
|
+
save_path: str | None = None,
|
|
157
|
+
show: bool = False,
|
|
158
|
+
figsize: tuple[int, int] = (5, 5),
|
|
159
|
+
color: str = "#f28e2b",
|
|
160
|
+
) -> plt.Figure:
|
|
161
|
+
"""
|
|
162
|
+
Plot v/w vectors and the parallelogram in spatial coordinates.
|
|
163
|
+
"""
|
|
164
|
+
config = _ensure_plot_config(
|
|
165
|
+
config,
|
|
166
|
+
PlotConfig.for_static_plot,
|
|
167
|
+
title="CohoMap Vectors",
|
|
168
|
+
xlabel="",
|
|
169
|
+
ylabel="",
|
|
170
|
+
figsize=figsize,
|
|
171
|
+
save_path=save_path,
|
|
172
|
+
show=show,
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
vmax = float(cohomap_vectors_result["x_range"])
|
|
176
|
+
wmax = float(cohomap_vectors_result["y_range"])
|
|
177
|
+
v = np.asarray(cohomap_vectors_result["v"], dtype=float)
|
|
178
|
+
w = np.asarray(cohomap_vectors_result["w"], dtype=float)
|
|
179
|
+
|
|
180
|
+
fig, ax = plt.subplots(1, 1, figsize=config.figsize)
|
|
181
|
+
|
|
182
|
+
ax.plot([0, 0], [0, wmax], "--", color="0.6", lw=1)
|
|
183
|
+
ax.plot([vmax, vmax], [0, wmax], "--", color="0.6", lw=1)
|
|
184
|
+
ax.plot([0, vmax], [0, 0], "--", color="0.6", lw=1)
|
|
185
|
+
ax.plot([0, vmax], [wmax, wmax], "--", color="0.6", lw=1)
|
|
186
|
+
|
|
187
|
+
ax.plot([0, v[0]], [0, v[1]], color=color, lw=3)
|
|
188
|
+
ax.plot([0, w[0]], [0, w[1]], color=color, lw=3)
|
|
189
|
+
ax.plot([v[0], v[0] + w[0]], [v[1], v[1] + w[1]], color=color, lw=3)
|
|
190
|
+
ax.plot([w[0], v[0] + w[0]], [w[1], v[1] + w[1]], color=color, lw=3)
|
|
191
|
+
|
|
192
|
+
pad_x = 0.05 * vmax if vmax > 0 else 1.0
|
|
193
|
+
pad_y = 0.05 * wmax if wmax > 0 else 1.0
|
|
194
|
+
ax.set_xlim(-pad_x, vmax + pad_x)
|
|
195
|
+
ax.set_ylim(-pad_y, wmax + pad_y)
|
|
196
|
+
ax.set_aspect("equal", "box")
|
|
197
|
+
ax.set_xticks([])
|
|
198
|
+
ax.set_yticks([])
|
|
199
|
+
|
|
200
|
+
fig.tight_layout()
|
|
201
|
+
_ensure_parent_dir(config.save_path)
|
|
202
|
+
finalize_figure(fig, config)
|
|
203
|
+
return fig
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _resolve_grid_size(phase_map: np.ndarray, grid_size: int | None, trim: int) -> int:
|
|
207
|
+
if grid_size is None:
|
|
208
|
+
return int(phase_map.shape[0]) + 2 * trim + 1
|
|
209
|
+
return int(grid_size)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _stripe_fit_map(params: np.ndarray, grid_size: int, trim: int) -> np.ndarray:
|
|
213
|
+
numangsint = grid_size
|
|
214
|
+
x, _ = np.meshgrid(
|
|
215
|
+
np.linspace(0, 3 * np.pi, numangsint - 1),
|
|
216
|
+
np.linspace(0, 3 * np.pi, numangsint - 1),
|
|
217
|
+
)
|
|
218
|
+
x_rot = rotate(x, params[0] * 360.0 / (2 * np.pi), reshape=False)
|
|
219
|
+
return np.cos(params[2] * x_rot[trim:-trim, trim:-trim] + params[1])
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def plot_cohomap_stripes(
|
|
223
|
+
cohomap_result: dict[str, Any],
|
|
224
|
+
*,
|
|
225
|
+
cohomap_vectors_result: dict[str, Any] | None = None,
|
|
226
|
+
grid_size: int | None = 151,
|
|
227
|
+
trim: int = 25,
|
|
228
|
+
angle_grid: int = 10,
|
|
229
|
+
phase_grid: int = 10,
|
|
230
|
+
spacing_grid: int = 10,
|
|
231
|
+
spacing_range: tuple[float, float] = (1.0, 6.0),
|
|
232
|
+
config: PlotConfig | None = None,
|
|
233
|
+
save_path: str | None = None,
|
|
234
|
+
show: bool = False,
|
|
235
|
+
figsize: tuple[int, int] = (10, 6),
|
|
236
|
+
cmap: str = "viridis",
|
|
237
|
+
) -> plt.Figure:
|
|
238
|
+
"""
|
|
239
|
+
Plot stripe fit diagnostics for CohoMap (observed vs fitted stripes).
|
|
240
|
+
"""
|
|
241
|
+
if cohomap_vectors_result is None:
|
|
242
|
+
cohomap_vectors_result = cohomap_vectors(
|
|
243
|
+
cohomap_result,
|
|
244
|
+
grid_size=grid_size,
|
|
245
|
+
trim=trim,
|
|
246
|
+
angle_grid=angle_grid,
|
|
247
|
+
phase_grid=phase_grid,
|
|
248
|
+
spacing_grid=spacing_grid,
|
|
249
|
+
spacing_range=spacing_range,
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
if "grid_size" in cohomap_vectors_result:
|
|
253
|
+
grid_size = cohomap_vectors_result["grid_size"]
|
|
254
|
+
if "trim" in cohomap_vectors_result:
|
|
255
|
+
trim = cohomap_vectors_result["trim"]
|
|
256
|
+
|
|
257
|
+
config = _ensure_plot_config(
|
|
258
|
+
config,
|
|
259
|
+
PlotConfig.for_static_plot,
|
|
260
|
+
title="CohoMap Stripe Fit",
|
|
261
|
+
xlabel="",
|
|
262
|
+
ylabel="",
|
|
263
|
+
figsize=figsize,
|
|
264
|
+
save_path=save_path,
|
|
265
|
+
show=show,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
phase_map1 = np.asarray(cohomap_result["phase_map1"])
|
|
269
|
+
phase_map2 = np.asarray(cohomap_result["phase_map2"])
|
|
270
|
+
x_edge = np.asarray(cohomap_result["x_edge"])
|
|
271
|
+
y_edge = np.asarray(cohomap_result["y_edge"])
|
|
272
|
+
|
|
273
|
+
grid_size = _resolve_grid_size(phase_map1, grid_size, trim)
|
|
274
|
+
expected = grid_size - 1 - 2 * trim
|
|
275
|
+
if phase_map1.shape[0] != expected or phase_map2.shape[0] != expected:
|
|
276
|
+
raise ValueError("phase_map shape does not match grid_size/trim")
|
|
277
|
+
|
|
278
|
+
p1 = np.asarray(cohomap_vectors_result["params1"])
|
|
279
|
+
p2 = np.asarray(cohomap_vectors_result["params2"])
|
|
280
|
+
fit1 = _stripe_fit_map(p1, grid_size, trim)
|
|
281
|
+
fit2 = _stripe_fit_map(p2, grid_size, trim)
|
|
282
|
+
|
|
283
|
+
obs1 = np.cos(phase_map1)
|
|
284
|
+
obs2 = np.cos(phase_map2)
|
|
285
|
+
|
|
286
|
+
fig, axes = plt.subplots(2, 2, figsize=config.figsize)
|
|
287
|
+
panels = [
|
|
288
|
+
(obs1, "Phase Map 1 (cos)"),
|
|
289
|
+
(fit1, "Stripe Fit 1"),
|
|
290
|
+
(obs2, "Phase Map 2 (cos)"),
|
|
291
|
+
(fit2, "Stripe Fit 2"),
|
|
292
|
+
]
|
|
293
|
+
|
|
294
|
+
for ax, (img, title) in zip(axes.flat, panels, strict=True):
|
|
295
|
+
ax.imshow(
|
|
296
|
+
img,
|
|
297
|
+
origin="lower",
|
|
298
|
+
extent=[x_edge[0], x_edge[-1], y_edge[0], y_edge[-1]],
|
|
299
|
+
cmap=cmap,
|
|
300
|
+
vmin=-1.0,
|
|
301
|
+
vmax=1.0,
|
|
302
|
+
)
|
|
303
|
+
ax.set_title(title, fontsize=10)
|
|
304
|
+
ax.set_aspect("equal", "box")
|
|
305
|
+
ax.set_xticks([])
|
|
306
|
+
ax.set_yticks([])
|
|
307
|
+
|
|
308
|
+
fig.tight_layout()
|
|
309
|
+
_ensure_parent_dir(config.save_path)
|
|
310
|
+
finalize_figure(fig, config)
|
|
311
|
+
return fig
|