-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbiology.py
More file actions
278 lines (240 loc) · 11.4 KB
/
Copy pathbiology.py
File metadata and controls
278 lines (240 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# coding: utf-8
"""Draw a directed subnetwork of fully proofread MICrONS BC neurons.
This is intentionally the only analysis in this file. It reuses the connectome
loader in ``biology/connectivity_helper.py`` and restricts both ends of every
displayed connection to basket cells (BC) whose axon and dendrite are both
proofread. A deterministic connected core keeps the network legible while every
node and edge shown still comes from the measured connectome.
Run on the compute node with the project environment::
conda activate mpn
python biology.py
The default outputs are ``cartoon_plot/biology_connectivity_network.png`` and
``cartoon_plot/biology_connectivity_network_legend.png``.
"""
import argparse
import sys
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.patches import FancyArrowPatch
import numpy as np
_HELPERS = Path(__file__).resolve().parent / "biology"
if str(_HELPERS) not in sys.path:
sys.path.insert(0, str(_HELPERS))
import connectivity_helper as conn # noqa: E402 (requires path shim above)
OUT_DIR = Path("cartoon_plot")
CELL_TYPE = "BC"
PROOFREAD = "both" # status_axon == "extended" AND full_dendrite == True
DEFAULT_N_NODES = 48
DEFAULT_MIN_SYNAPSES = 1
DEFAULT_LAYOUT_SEED = 7
_NODE_COLOR = "#8f8f8f"
_EDGE_COLOR = "#777777"
plt.rcParams.update({
"font.family": "sans-serif",
"font.size": 9,
"figure.dpi": 150,
"savefig.dpi": 300,
"savefig.bbox": "tight",
})
def _save_connectivity_legend(out_dir):
"""Save the neuron/connectivity key as a standalone transparent figure."""
handles = [
Line2D([], [], marker="o", linestyle="none", markersize=7,
markerfacecolor=_NODE_COLOR, markeredgecolor="white",
markeredgewidth=0.25, label="Neuron"),
Line2D([], [], color=_EDGE_COLOR, linewidth=1.5,
label="Connectivity"),
]
fig = plt.figure(figsize=(2.2, 1.2))
fig.legend(handles=handles, loc="center", ncol=1, frameon=False,
fontsize=11, handlelength=1.6, handletextpad=0.65,
labelspacing=0.5)
legend_path = Path(out_dir) / "biology_connectivity_network_legend.png"
fig.savefig(legend_path, dpi=600, bbox_inches="tight",
pad_inches=0.02, transparent=True)
plt.close(fig)
return legend_path
def _connectivity_core_indices(W, syn_count, n_nodes, min_synapses=1):
"""Select one deterministic, connected and edge-rich induced subgraph.
Selection starts at the cell with largest directed degree and repeatedly adds
the remaining cell with the most observed links to the current set. Synapse
count into the set, whole-graph degree and stable source index break ties.
This defines the subset before plotting and avoids searching many subsets for
the visually most attractive result.
"""
W = np.asarray(W, dtype=bool)
syn_count = np.asarray(syn_count)
if W.ndim != 2 or W.shape[0] != W.shape[1] or syn_count.shape != W.shape:
raise ValueError("W and syn_count must be equal-size square matrices")
n_total = W.shape[0]
n_nodes = int(n_nodes)
min_synapses = int(min_synapses)
if not 3 <= n_nodes <= n_total:
raise ValueError(f"n_nodes must be between 3 and {n_total}; got {n_nodes}")
if min_synapses < 1:
raise ValueError("min_synapses must be >= 1")
directed = W & (syn_count >= min_synapses)
np.fill_diagonal(directed, False)
undirected = directed | directed.T
degree = directed.sum(axis=0) + directed.sum(axis=1)
strength = ((syn_count * directed).sum(axis=0)
+ (syn_count * directed).sum(axis=1))
seed = int(np.lexsort((-np.arange(n_total), strength, degree))[-1])
selected = [seed]
available = np.ones(n_total, dtype=bool)
available[seed] = False
while len(selected) < n_nodes:
candidates = np.flatnonzero(available)
current = np.asarray(selected, dtype=int)
links = undirected[np.ix_(candidates, current)].sum(axis=1)
if int(links.max()) == 0:
raise ValueError(
f"the connected BC component contains only {len(selected)} cells, "
f"fewer than n_nodes={n_nodes}; request fewer nodes or lower "
"min_synapses")
internal_strength = (
syn_count[np.ix_(candidates, current)].sum(axis=1)
+ syn_count[np.ix_(current, candidates)].sum(axis=0))
winner = np.lexsort((-candidates, degree[candidates],
internal_strength, links))[-1]
chosen = int(candidates[winner])
selected.append(chosen)
available[chosen] = False
return np.asarray(selected, dtype=int)
def _spring_layout(adjacency, seed=DEFAULT_LAYOUT_SEED, iterations=450):
"""Deterministic Fruchterman-Reingold layout without a graph dependency."""
adjacency = np.asarray(adjacency, dtype=bool)
n = adjacency.shape[0]
if adjacency.shape != (n, n):
raise ValueError("adjacency must be square")
rng = np.random.RandomState(int(seed))
theta = 2 * np.pi * np.arange(n) / max(n, 1)
pos = np.column_stack((np.cos(theta), np.sin(theta)))
pos += rng.normal(scale=0.08, size=pos.shape)
edge_i, edge_j = np.where(np.triu(adjacency | adjacency.T, 1))
k = 1.35 / np.sqrt(max(n, 1))
for step in range(int(iterations)):
delta = pos[:, None, :] - pos[None, :, :]
distance = np.linalg.norm(delta, axis=-1)
np.fill_diagonal(distance, np.inf)
displacement = ((k * k / np.maximum(distance, 1e-6) ** 2)[..., None]
* delta).sum(axis=1)
for i, j in zip(edge_i, edge_j):
d = pos[i] - pos[j]
r = max(float(np.linalg.norm(d)), 1e-6)
pull = d * (r / k)
displacement[i] -= pull
displacement[j] += pull
length = np.linalg.norm(displacement, axis=1)
temperature = 0.18 * (1.0 - (step + 1) / max(int(iterations), 1))
step_size = np.minimum(length, temperature)
pos += (displacement / np.maximum(length[:, None], 1e-12)
* step_size[:, None])
pos -= pos.mean(axis=0, keepdims=True)
# Fill a square plotting region independently along x and y. The layout has
# no anatomical metric, so this affine rescaling changes no encoded quantity;
# it only avoids a long/flat graph with wasted whitespace.
span = np.maximum(np.ptp(pos, axis=0), 1e-12)
pos /= span[None, :]
return pos
def plot_bc_connectivity_network(out_dir=OUT_DIR, n_nodes=DEFAULT_N_NODES,
min_synapses=DEFAULT_MIN_SYNAPSES,
layout_seed=DEFAULT_LAYOUT_SEED,
data_dir=conn.DATA_DIR, result=None):
"""Plot measured BC→BC connections among fully proofread MICrONS cells.
``result`` may be a previously loaded ``conn.l2it_connectivity`` dictionary.
Otherwise the function loads only cells annotated ``BC`` with proofread mode
``both`` and synapses whose pre- and postsynaptic root IDs both belong to that
set. Arrow direction is presynaptic→postsynaptic. Width and opacity increase
with the number of synapses supporting that directed connection.
"""
if result is None:
result = conn.l2it_connectivity(
cell_type=CELL_TYPE, proofread=PROOFREAD, data_dir=data_dir)
full_W = np.asarray(result["W"], dtype=bool)
full_count = np.asarray(result["syn_count"], dtype=float)
keep = _connectivity_core_indices(
full_W, full_count, n_nodes, min_synapses=min_synapses)
W = full_W[np.ix_(keep, keep)]
syn_count = full_count[np.ix_(keep, keep)]
visible = W & (syn_count >= int(min_synapses))
np.fill_diagonal(visible, False)
positions = _spring_layout(visible, seed=layout_seed)
# Matplotlib scatter `s` is marker AREA. Map total directed degree linearly
# into a deliberately visible area range: a node's degree counts every
# incoming plus every outgoing connection within the displayed BC core.
total_degree = visible.sum(axis=0) + visible.sum(axis=1)
degree_span = int(np.ptp(total_degree))
if degree_span > 0:
degree_fraction = ((total_degree - total_degree.min()) / degree_span)
node_sizes = 24.0 + 66.0 * degree_fraction
else:
node_sizes = np.full(total_degree.shape, 48.0)
fig, ax = plt.subplots(figsize=(3.2, 3.2))
edge_scale = np.log1p(syn_count[visible])
edge_scale /= max(float(edge_scale.max()), 1.0)
for edge_k, (i, j) in enumerate(zip(*np.where(visible))):
scale = float(edge_scale[edge_k])
rad = (0.08 if visible[j, i] and i < j else
-0.08 if visible[j, i] else 0.0)
ax.add_patch(FancyArrowPatch(
positions[i], positions[j], arrowstyle="-|>",
connectionstyle=f"arc3,rad={rad}", color=_EDGE_COLOR,
linewidth=0.30 + 0.75 * scale, alpha=0.18 + 0.38 * scale,
mutation_scale=5.5, shrinkA=4.8, shrinkB=6.0, zorder=1))
ax.scatter(positions[:, 0], positions[:, 1],
s=node_sizes, color=_NODE_COLOR,
edgecolor="white", linewidth=0.25, zorder=3)
limit = 1.06 * max(float(np.abs(positions).max()), 0.5)
ax.set_xlim(-limit, limit)
ax.set_ylim(-limit, limit)
ax.set_aspect("equal", adjustable="box")
ax.axis("off")
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
out_dir = Path(out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
out_path = out_dir / "biology_connectivity_network.png"
fig.savefig(out_path, dpi=300, bbox_inches="tight")
plt.close(fig)
legend_path = _save_connectivity_legend(out_dir)
edges = int(visible.sum())
print(f"Saved: {out_path} ({len(keep)} of {full_W.shape[0]} fully "
f"proofread BC neurons; {edges} directed BC→BC connections with >= "
f"{min_synapses} synapse(s); layout seed {layout_seed})")
print(f"Saved: {legend_path}")
return {
"path": out_path,
"legend_path": legend_path,
"indices": keep,
"root_ids": np.asarray(result["ids"])[keep],
"positions": positions,
"W": visible,
"syn_count": syn_count,
"total_degree": total_degree,
}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--out-dir", default=str(OUT_DIR),
help=f"output directory (default: {OUT_DIR})")
parser.add_argument("--data-dir", default=str(conn.DATA_DIR),
help="directory containing the MICrONS cell and synapse "
"feather tables")
parser.add_argument("--n-nodes", type=int, default=DEFAULT_N_NODES,
help=f"BC neurons in the connected core "
f"(default: {DEFAULT_N_NODES})")
parser.add_argument("--min-synapses", type=int,
default=DEFAULT_MIN_SYNAPSES,
help="minimum synapses required to display an edge "
f"(default: {DEFAULT_MIN_SYNAPSES})")
parser.add_argument("--layout-seed", type=int, default=DEFAULT_LAYOUT_SEED,
help=f"deterministic layout seed "
f"(default: {DEFAULT_LAYOUT_SEED})")
args = parser.parse_args()
plot_bc_connectivity_network(
out_dir=args.out_dir, n_nodes=args.n_nodes,
min_synapses=args.min_synapses, layout_seed=args.layout_seed,
data_dir=args.data_dir)
if __name__ == "__main__":
main()