|
| 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