-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
431 lines (350 loc) · 15.1 KB
/
Copy path__init__.py
File metadata and controls
431 lines (350 loc) · 15.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""Graph data structures and graph-level algorithms.
Top-level surface:
- Graph structures: :class:`UndirectedGraph`, :class:`GridGraph2D`,
:class:`GridGraph3D`, :class:`RegionAdjacencyGraph`.
- Constructors: :func:`undirected_graph`, :func:`grid_graph`,
:func:`region_adjacency_graph`.
- Algorithms: :func:`connected_components`, :func:`breadth_first_search`,
:func:`edge_weighted_watershed`, :func:`project_node_labels_to_pixels`.
Algorithm domains live in dedicated submodules:
- :mod:`bioimage_cpp.graph.multicut` — multicut objective and solvers,
proposal generators, multicut problem loaders.
- :mod:`bioimage_cpp.graph.lifted_multicut` — lifted multicut objective and
solvers, lifted problem loaders.
- :mod:`bioimage_cpp.graph.mutex_watershed` — mutex watershed clustering
(with and without semantic constraints).
- :mod:`bioimage_cpp.graph.features` — edge-feature accumulation on RAGs and
grid graphs.
Thread safety
-------------
All graph types (:class:`UndirectedGraph`, :class:`GridGraph2D`,
:class:`GridGraph3D`, :class:`RegionAdjacencyGraph`) build their internal
adjacency representation *lazily*, on the first call that reads it. The
built-in multi-threaded algorithms freeze the graph internally before fanning
out, so passing a graph straight into them is safe and needs no extra step.
If you build a graph yourself and then share it across **your own** threads
(reading adjacency, running a BFS, etc. concurrently), call ``graph.freeze()``
once on the construction thread first: the lazy build is not thread-safe, and
racing the first read across threads corrupts the adjacency. ``freeze()`` is a
no-op on an already-built graph.
"""
from __future__ import annotations
import numpy as np
from .. import _core
from ._shared import (
_as_1d_array,
_as_coordinate_array,
_as_node_array,
_as_offset_array,
_as_serialization_array,
_as_shape,
_as_uv_array,
_normalize_labels,
_normalize_number_of_threads,
)
class UndirectedGraph(_core.UndirectedGraph):
"""Undirected graph with consecutive node and edge ids.
Nodes are fixed at construction and addressed by ids
``0 .. number_of_nodes - 1``. Edges are inserted lazily and receive
consecutive ids in insertion order. Re-inserting an existing undirected edge
returns the existing edge id.
The adjacency representation is built lazily on first use. Before sharing a
freshly built graph across threads of your own, call :meth:`freeze` once on
the construction thread — see the module-level "Thread safety" note. The
built-in multi-threaded algorithms already freeze internally.
"""
def insert_edges(self, uvs):
return super().insert_edges(_as_uv_array(uvs, "uvs"))
def find_edges(self, uvs):
return super().find_edges(_as_uv_array(uvs, "uvs"))
def insertEdges(self, uvs):
return self.insert_edges(uvs)
def findEdges(self, uvs):
return self.find_edges(uvs)
def extract_subgraph_from_nodes(self, nodes):
return super().extract_subgraph_from_nodes(_as_node_array(nodes, "nodes"))
def edges_from_node_list(self, nodes):
return super().edges_from_node_list(_as_node_array(nodes, "nodes"))
def extractSubgraphFromNodes(self, nodes):
return self.extract_subgraph_from_nodes(nodes)
def edgesFromNodeList(self, nodes):
return self.edges_from_node_list(nodes)
@classmethod
def from_edges(cls, number_of_nodes: int, uvs):
graph = cls(number_of_nodes)
graph.insert_edges(uvs)
return graph
@classmethod
def deserialize(cls, serialization):
serialization = _as_serialization_array(serialization)
number_of_nodes = int(serialization[0])
number_of_edges = int(serialization[1])
uvs = serialization[2:].reshape(number_of_edges, 2)
return cls.from_edges(number_of_nodes, uvs)
class GridGraph2D(_core.GridGraph2D):
"""Regular 2D nearest-neighbor grid graph.
Nodes use C-order ids for ``shape=(y, x)``. Edge ids are deterministic:
all y-axis edges first, then all x-axis edges.
"""
def __init__(self, shape):
super().__init__(_as_shape(shape, 2))
def node_id(self, coordinate):
return super().node_id(_as_coordinate_array(coordinate, 2, "coordinate"))
def nodeId(self, coordinate):
return self.node_id(coordinate)
def offset_target(self, node: int, offset):
return super().offset_target(int(node), _as_offset_array(offset, 2, "offset"))
def offsetTarget(self, node: int, offset):
return self.offset_target(node, offset)
class GridGraph3D(_core.GridGraph3D):
"""Regular 3D nearest-neighbor grid graph.
Nodes use C-order ids for ``shape=(z, y, x)``. Edge ids are deterministic:
all z-axis edges first, then y-axis edges, then x-axis edges.
"""
def __init__(self, shape):
super().__init__(_as_shape(shape, 3))
def node_id(self, coordinate):
return super().node_id(_as_coordinate_array(coordinate, 3, "coordinate"))
def nodeId(self, coordinate):
return self.node_id(coordinate)
def offset_target(self, node: int, offset):
return super().offset_target(int(node), _as_offset_array(offset, 3, "offset"))
def offsetTarget(self, node: int, offset):
return self.offset_target(node, offset)
RegionAdjacencyGraph = _core.RegionAdjacencyGraph
def undirected_graph(number_of_nodes: int) -> UndirectedGraph:
"""Create an :class:`UndirectedGraph`."""
return UndirectedGraph(number_of_nodes)
def grid_graph(shape):
"""Create a regular 2D or 3D nearest-neighbor grid graph."""
ndim = np.asarray(shape).ndim
if ndim != 1:
raise ValueError("shape must be a 1D sequence")
n_axes = len(shape)
if n_axes == 2:
return GridGraph2D(shape)
if n_axes == 3:
return GridGraph3D(shape)
raise ValueError(f"shape must have length 2 or 3, got length={n_axes}")
_EDGE_WEIGHTED_WATERSHED_BY_DTYPE = {
(np.dtype("float32"), np.dtype("uint32")): _core._edge_weighted_watershed_float32_uint32,
(np.dtype("float32"), np.dtype("uint64")): _core._edge_weighted_watershed_float32_uint64,
(np.dtype("float32"), np.dtype("int32")): _core._edge_weighted_watershed_float32_int32,
(np.dtype("float32"), np.dtype("int64")): _core._edge_weighted_watershed_float32_int64,
(np.dtype("float64"), np.dtype("uint32")): _core._edge_weighted_watershed_float64_uint32,
(np.dtype("float64"), np.dtype("uint64")): _core._edge_weighted_watershed_float64_uint64,
(np.dtype("float64"), np.dtype("int32")): _core._edge_weighted_watershed_float64_int32,
(np.dtype("float64"), np.dtype("int64")): _core._edge_weighted_watershed_float64_int64,
}
_REGION_ADJACENCY_GRAPH_BY_DTYPE = {
np.dtype("uint32"): _core._region_adjacency_graph_uint32,
np.dtype("uint64"): _core._region_adjacency_graph_uint64,
np.dtype("int32"): _core._region_adjacency_graph_int32,
np.dtype("int64"): _core._region_adjacency_graph_int64,
}
_PROJECT_NODE_LABELS_TO_PIXELS_BY_DTYPE = {
np.dtype("uint32"): _core._project_node_labels_to_pixels_uint32,
np.dtype("uint64"): _core._project_node_labels_to_pixels_uint64,
np.dtype("int32"): _core._project_node_labels_to_pixels_int32,
np.dtype("int64"): _core._project_node_labels_to_pixels_int64,
}
def connected_components(
graph,
edge_mask: np.ndarray | None = None,
) -> np.ndarray:
"""Compute dense connected-component labels for graph nodes.
If ``edge_mask`` is given, only edges with a true mask value contribute to
the connected components.
"""
if edge_mask is None:
return _core._connected_components(graph)
mask = np.asarray(edge_mask)
if mask.dtype != np.dtype("bool"):
raise TypeError(f"edge_mask must have dtype bool, got dtype={mask.dtype}")
if mask.ndim != 1:
raise ValueError("edge_mask must be a 1D array")
if mask.shape[0] != graph.number_of_edges:
raise ValueError("edge_mask length must match graph number_of_edges")
return _core._connected_components_masked(
graph, np.ascontiguousarray(mask.astype(np.uint8, copy=False))
)
def breadth_first_search(
graph,
source: int,
*,
max_distance: int | None = None,
include_source: bool = True,
) -> tuple[np.ndarray, np.ndarray]:
"""Breadth-first search from ``source`` on ``graph``.
Returns ``(nodes, distances)`` — two 1D ``uint64`` arrays of equal length,
listing every reachable node within ``max_distance`` hops (inclusive) in
BFS order along with its hop distance from the source.
Parameters
----------
graph:
:class:`UndirectedGraph` or :class:`RegionAdjacencyGraph`.
source:
Source node id.
max_distance:
Maximum hop distance from ``source`` to report. ``None`` (default)
means no limit — the search expands until the entire connected
component of ``source`` is visited.
include_source:
If ``True`` (default), the source itself is reported with distance 0.
Set to ``False`` for "nodes within k hops, excluding self" queries.
"""
if int(source) < 0 or int(source) >= int(graph.number_of_nodes):
raise ValueError(
f"source must be in [0, number_of_nodes), got source={source}, "
f"number_of_nodes={int(graph.number_of_nodes)}"
)
if max_distance is None:
limit = (1 << 64) - 1
else:
if int(max_distance) < 0:
raise ValueError("max_distance must be non-negative")
limit = int(max_distance)
return _core._breadth_first_search(
graph, int(source), limit, bool(include_source)
)
def edge_weighted_watershed(
graph,
edge_weights,
seeds,
) -> np.ndarray:
"""Kruskal-style edge-weighted seeded watershed on an undirected graph.
Edges are visited in ascending weight order. Two distinct components are
merged iff at least one of them is unlabeled (seed label ``0``); the
non-zero seed label then propagates. Two distinct already-labeled
components are never merged, so seed boundaries are preserved.
Parameters
----------
graph:
:class:`UndirectedGraph` or :class:`RegionAdjacencyGraph`.
edge_weights:
1D array of length ``graph.number_of_edges``. Supported dtypes are
``float32`` and ``float64``. Other floating dtypes are cast to
``float32`` (matches nifty); other dtypes raise ``TypeError``.
seeds:
1D array of length ``graph.number_of_nodes``. Supported dtypes are
``uint32``, ``uint64``, ``int32``, ``int64``. ``0`` marks unlabeled
nodes; positive ids are seed labels and propagate along low-weight
paths. Signed seed arrays must not contain negative values.
Returns
-------
np.ndarray
1D array of length ``graph.number_of_nodes`` with the same dtype as
``seeds``. Nodes reachable from a seed receive that seed's label;
unreachable nodes remain ``0``. Seed label values are preserved (no
dense relabeling).
"""
weight_array = np.asarray(edge_weights)
if weight_array.dtype not in (np.dtype("float32"), np.dtype("float64")):
if np.issubdtype(weight_array.dtype, np.floating):
weight_array = weight_array.astype(np.float32, copy=False)
else:
raise TypeError(
"edge_weights must have dtype float32 or float64, got "
f"dtype={weight_array.dtype}"
)
seed_array = np.asarray(seeds)
if seed_array.dtype not in (
np.dtype("uint32"),
np.dtype("uint64"),
np.dtype("int32"),
np.dtype("int64"),
):
raise TypeError(
"seeds must have dtype uint32, uint64, int32, or int64, got "
f"dtype={seed_array.dtype}"
)
weight_array = _as_1d_array(
weight_array, weight_array.dtype, "edge_weights", int(graph.number_of_edges)
)
seed_array = _as_1d_array(
seed_array, seed_array.dtype, "seeds", int(graph.number_of_nodes)
)
run = _EDGE_WEIGHTED_WATERSHED_BY_DTYPE[(weight_array.dtype, seed_array.dtype)]
return run(graph, weight_array, seed_array)
def region_adjacency_graph(
labels: np.ndarray,
*,
number_of_threads: int = 0,
) -> RegionAdjacencyGraph:
"""Build a region adjacency graph from a 2D or 3D label image.
Nodes correspond to label ids from ``0`` to ``labels.max()``. Undirected
edges connect different labels that touch along the pixel or voxel grid's
direct neighborhood. The edge ids are deterministic and sorted
lexicographically by their endpoint ids.
"""
array = np.asarray(labels)
if array.ndim not in (2, 3):
raise ValueError(f"labels must be a 2D or 3D array, got ndim={array.ndim}")
if any(size == 0 for size in array.shape):
raise ValueError("labels must not have empty dimensions")
dtype = array.dtype
try:
run = _REGION_ADJACENCY_GRAPH_BY_DTYPE[dtype]
except KeyError as error:
supported = ", ".join(
str(dtype) for dtype in _REGION_ADJACENCY_GRAPH_BY_DTYPE
)
raise TypeError(
f"labels must have one of dtypes ({supported}), got dtype={dtype}"
) from error
number_of_threads = _normalize_number_of_threads(number_of_threads)
return run(np.ascontiguousarray(array), number_of_threads)
def project_node_labels_to_pixels(
rag: RegionAdjacencyGraph,
labels: np.ndarray,
node_labels,
*,
number_of_threads: int = 0,
) -> np.ndarray:
"""Map RAG node labels back to a pixel-wise segmentation.
``labels`` is the over-segmentation used to construct ``rag``. Each pixel
value is interpreted as a RAG node id and replaced by the corresponding
entry in the 1D ``node_labels`` array. The returned segmentation has the
same shape as ``labels`` and dtype ``uint64``.
"""
label_array = _normalize_labels(labels)
if tuple(int(size) for size in rag.shape) != label_array.shape:
raise ValueError(
"rag shape must match labels shape, got "
f"rag shape={tuple(rag.shape)}, labels shape={label_array.shape}"
)
node_label_array = np.asarray(node_labels, dtype=np.uint64)
if node_label_array.ndim != 1:
raise ValueError("node_labels must be a 1D array")
if node_label_array.shape[0] != rag.number_of_nodes:
raise ValueError("node_labels length must match rag number_of_nodes")
run = _PROJECT_NODE_LABELS_TO_PIXELS_BY_DTYPE[label_array.dtype]
return run(
rag,
label_array,
np.ascontiguousarray(node_label_array),
_normalize_number_of_threads(number_of_threads),
)
from . import agglomeration # noqa: E402 (must follow class/function definitions)
from . import features # noqa: E402
from . import lifted_multicut # noqa: E402
from . import multicut # noqa: E402
from . import mutex_watershed # noqa: E402
__all__ = [
"GridGraph2D",
"GridGraph3D",
"RegionAdjacencyGraph",
"UndirectedGraph",
"agglomeration",
"breadth_first_search",
"connected_components",
"edge_weighted_watershed",
"features",
"grid_graph",
"lifted_multicut",
"multicut",
"mutex_watershed",
"project_node_labels_to_pixels",
"region_adjacency_graph",
"undirected_graph",
]