Skip to content

Commit 73c044e

Browse files
committed
feat(sv_splitting): copy parents and sv lineage
1 parent 460a871 commit 73c044e

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

pychunkedgraph/graph/attributes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ class Hierarchy:
143143
serializer=serializers.NumPyValue(dtype=basetypes.NODE_ID),
144144
)
145145

146+
FormerIdentity = _Attribute(
147+
key=b"former_ids",
148+
family_id="0",
149+
serializer=serializers.NumPyArray(dtype=basetypes.NODE_ID),
150+
)
151+
152+
NewIdentity = _Attribute(
153+
key=b"new_ids",
154+
family_id="0",
155+
serializer=serializers.NumPyArray(dtype=basetypes.NODE_ID),
156+
)
157+
146158

147159
class GraphMeta:
148160
key = b"meta"

pychunkedgraph/graph/edits_sv.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Supervoxel splitting and managing new IDs.
3+
"""
4+
5+
from typing import Iterable
6+
7+
import numpy as np
8+
9+
from pychunkedgraph.graph import ChunkedGraph
10+
from pychunkedgraph.graph.utils import basetypes
11+
from pychunkedgraph.graph.attributes import Hierarchy
12+
from pychunkedgraph.graph.utils.serializers import serialize_uint64
13+
14+
15+
def split_supervoxel(
16+
cg: ChunkedGraph, supervoxel_id: basetypes.NODE_ID
17+
) -> Iterable[basetypes.NODE_ID]:
18+
"""
19+
Lookup coordinates of given supervoxel in segmentation.
20+
Split it and update the coordinates with new IDs.
21+
Return new IDs.
22+
"""
23+
24+
25+
def copy_parents_and_create_lineage(
26+
cg: ChunkedGraph, old_id: basetypes.NODE_ID, new_ids: Iterable[basetypes.NODE_ID]
27+
) -> list:
28+
"""
29+
Copy parents column from `old_id` to each of `new_ids`.
30+
This makes it easy to get old hierarchy with `new_ids` using an older timestamp.
31+
Link `old_id` and `new_ids` to create a lineage at supervoxel layer.
32+
Returns a list of mutations to be persisted.
33+
"""
34+
result = []
35+
parent_cells = cg.client.read_node(old_id, properties=Hierarchy.Parent)
36+
37+
for new_id in new_ids:
38+
val_dict = {
39+
Hierarchy.FormerIdentity: np.array([old_id], dtype=basetypes.NODE_ID)
40+
}
41+
result.append(cg.client.mutate_row(serialize_uint64(new_id), val_dict))
42+
43+
for cell in parent_cells:
44+
result.append(
45+
cg.client.mutate_row(
46+
serialize_uint64(new_id),
47+
{Hierarchy.Parent: cell.value},
48+
time_stamp=cell.timestamp,
49+
)
50+
)
51+
52+
val_dict = {Hierarchy.NewIdentity: np.array(new_ids, dtype=basetypes.NODE_ID)}
53+
result.append(cg.client.mutate_row(serialize_uint64(old_id), val_dict))
54+
return result

0 commit comments

Comments
 (0)