From 3ee49058bcbab52a185810401c0746c5570c362a Mon Sep 17 00:00:00 2001 From: Teun Huijben Date: Thu, 29 Oct 2026 15:12:30 -0700 Subject: [PATCH 1/4] in Actions, first check then apply, to prevent changes to the graph that are not recorded in the (future) log --- src/funtracks/actions/_base.py | 29 ++- src/funtracks/actions/add_delete_node.py | 47 +++-- src/funtracks/user_actions/user_add_edge.py | 39 ++-- src/funtracks/user_actions/user_add_node.py | 35 ++-- .../user_actions/user_delete_edge.py | 15 +- .../user_actions/user_delete_nodes.py | 8 + .../user_actions/user_set_division.py | 6 + .../user_actions/user_update_nodes_attrs.py | 8 + .../user_actions/user_update_segmentation.py | 26 ++- tests/actions/test_action_group_rollback.py | 188 ++++++++++++++++++ tests/conftest.py | 72 ++++++- .../user_actions/test_user_add_delete_edge.py | 36 ++++ .../user_actions/test_user_add_delete_node.py | 27 +++ tests/user_actions/test_user_set_division.py | 14 +- .../test_user_update_nodes_attrs.py | 11 + .../test_user_update_segmentation.py | 18 ++ 16 files changed, 510 insertions(+), 69 deletions(-) create mode 100644 tests/actions/test_action_group_rollback.py diff --git a/src/funtracks/actions/_base.py b/src/funtracks/actions/_base.py index 40b9cbc4..3d68e1ab 100644 --- a/src/funtracks/actions/_base.py +++ b/src/funtracks/actions/_base.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING +import functools +from typing import TYPE_CHECKING, Any from typing_extensions import override @@ -56,8 +57,34 @@ class ActionGroup(Action): They can contain BasicActions or other ActionGroups. They are not passed to annotators - only the BasicActions they contain are. Examples: UserAddNode, UserDeleteEdge, etc. + + Subclasses apply their actions in ``__init__``. If that ``__init__`` raises, the + actions applied so far are undone, newest first, so a failing group leaves no + change outside the undo history. Like undo, this restores the solution, but + elements the group added stay in ``graph_full`` as soft-deleted candidates. """ + def __init_subclass__(cls, **kwargs: Any) -> None: + super().__init_subclass__(**kwargs) + # Only wrap an __init__ the class defines itself: an inherited one is + # already wrapped on the parent. + if "__init__" not in cls.__dict__: + return + init = cls.__init__ + + @functools.wraps(init) + def init_or_roll_back(self: ActionGroup, *args: Any, **kwargs: Any) -> None: + try: + init(self, *args, **kwargs) + except Exception: + # Undo what was applied before the error, newest first. actions + # does not exist yet if the error came before super().__init__. + for action in reversed(getattr(self, "actions", [])): + action.inverse() + raise + + cls.__init__ = init_or_roll_back # type: ignore[method-assign] + def __init__( self, tracks: Tracks, diff --git a/src/funtracks/actions/add_delete_node.py b/src/funtracks/actions/add_delete_node.py index 0be342d0..c048fae2 100644 --- a/src/funtracks/actions/add_delete_node.py +++ b/src/funtracks/actions/add_delete_node.py @@ -41,34 +41,45 @@ def __init__( super().__init__(tracks) self.tracks: Tracks # Narrow type from base class self.node = int(node) + self.validate_attributes(tracks, node, attributes) + self.attributes = attributes + self._apply() + + @staticmethod + def validate_attributes( + tracks: Tracks, node: Node, attributes: dict[str, Any], has_mask: bool = False + ) -> None: + """Check that the attributes are enough to add the node. + + User actions call this before applying anything, so that a node AddNode would + refuse is refused before any other change is made. + + Args: + tracks: The Tracks the node would be added to. + node: The node id, used in the error messages. + attributes: The node attributes. + has_mask: True if a mask will be added to the attributes later, so + position is not needed even though the mask is not there yet. - # Get keys from tracks features + Raises: + ValueError: If the time or tracklet attribute is missing, or if there + is neither a position nor a mask. + """ time_key = tracks.features.time_key track_id_key = tracks.features.tracklet_key pos_key = tracks.features.position_key - # validate the input if time_key not in attributes: raise ValueError(f"Must provide a time attribute for node {node}") if track_id_key not in attributes: raise ValueError(f"Must provide a {track_id_key} attribute for node {node}") - # Check for position — not needed if the default mask is in attributes - # (position will be computed from the mask by annotators) - if td.DEFAULT_ATTR_KEYS.MASK not in attributes: - if isinstance(pos_key, list): - if not all(key in attributes for key in pos_key): - raise ValueError( - f"Must provide position or segmentation for node {node}" - ) - else: - if pos_key not in attributes: - raise ValueError( - f"Must provide position or segmentation for node {node}" - ) - - self.attributes = attributes - self._apply() + # Position is not needed with a mask: annotators compute it from the mask + if has_mask or td.DEFAULT_ATTR_KEYS.MASK in attributes: + return + pos_keys = pos_key if isinstance(pos_key, list) else [pos_key] + if not all(key in attributes for key in pos_keys): + raise ValueError(f"Must provide position or segmentation for node {node}") def inverse(self) -> BasicAction: """Invert the action to delete nodes instead""" diff --git a/src/funtracks/user_actions/user_add_edge.py b/src/funtracks/user_actions/user_add_edge.py index caf8f3c8..837ce09b 100644 --- a/src/funtracks/user_actions/user_add_edge.py +++ b/src/funtracks/user_actions/user_add_edge.py @@ -49,6 +49,7 @@ def __init__( # Check if making a merge. If yes and force, remove the other edge and update # track ids. in_degree_target = len(self.tracks.predecessors(target)) # type: ignore + merge_edge = None if in_degree_target > 0: if not force: raise InvalidActionError( @@ -56,19 +57,28 @@ def __init__( "already has an in edge", forceable=True, ) - else: - pred = next(iter(self.tracks.graph_solution.predecessors(target))) - merge_edge = (pred, target) - warnings.warn( - f"Removing edge {merge_edge} to add new edge without merging.", - stacklevel=2, - ) - self.actions.append( - UserDeleteEdge(self.tracks, merge_edge, _top_level=False) - ) + pred = next(iter(self.tracks.graph_solution.predecessors(target))) + merge_edge = (pred, target) - # update track ids if needed + # Check the out degree of the source before applying anything, so that a + # failing check leaves the tracks unchanged. Removing the merge edge lowers it + # when that edge starts at the source. out_degree_source = len(self.tracks.successors(source)) + if merge_edge is not None and merge_edge[0] == source: + out_degree_source -= 1 + if out_degree_source > 1: + raise InvalidActionError( + f"Expected degree of 0 or 1 before adding edge, got {out_degree_source}" + ) + + if merge_edge is not None: + warnings.warn( + f"Removing edge {merge_edge} to add new edge without merging.", + stacklevel=2, + ) + self.actions.append(UserDeleteEdge(self.tracks, merge_edge, _top_level=False)) + + # update track ids if needed if out_degree_source == 0: # joining two segments # assign the track id and lineage id of the source node to the target # and all downstream nodes @@ -77,7 +87,7 @@ def __init__( self.actions.append( UpdateTrackIDs(self.tracks, target, new_track_id, new_lineage_id) ) - elif out_degree_source == 1: # creating a division + else: # creating a division # assign a new track id to existing child (lineage stays the same) successor = next(iter(self.tracks.graph_solution.successors(source))) self.actions.append( @@ -90,11 +100,6 @@ def __init__( ) ) - else: - raise InvalidActionError( - f"Expected degree of 0 or 1 before adding edge, got {out_degree_source}" - ) - self.actions.append(AddEdge(tracks, edge)) if _top_level: diff --git a/src/funtracks/user_actions/user_add_node.py b/src/funtracks/user_actions/user_add_node.py index 774a1399..a5d92ea8 100644 --- a/src/funtracks/user_actions/user_add_node.py +++ b/src/funtracks/user_actions/user_add_node.py @@ -71,6 +71,9 @@ def __init__( """ super().__init__(tracks, actions=[]) self.tracks: Tracks # Narrow type from base class + # Work on a copy: ids, mask and bbox are filled in below, and the caller's + # dict must not change, also not when the action fails. + attributes = dict(attributes) # Get keys from tracks features time_key = tracks.features.time_key @@ -88,7 +91,6 @@ def __init__( raise InvalidActionError( f"Node {node} already exists in the tracks, cannot add." ) - track_id = attributes[track_id_key] time = attributes[time_key] @@ -113,21 +115,16 @@ def __init__( break # check if you are adding a node to a track that divided previously + conflicting_edges: list[tuple[int, int]] = [] if pred is not None and len(self.tracks.successors(pred)) == 2: if not force: raise InvalidActionError( "Cannot add node here - upstream division event detected.", forceable=True, ) - else: - # Delete both conflicting edges in the upstream division. - succ_of_pred1, succ_of_pred2 = self.tracks.successors(pred) - self.actions.append( - UserDeleteEdge(tracks, (pred, succ_of_pred1), _top_level=False) - ) - self.actions.append( - UserDeleteEdge(tracks, (pred, succ_of_pred2), _top_level=False) - ) + # Delete both conflicting edges in the upstream division. + succ_of_pred1, succ_of_pred2 = self.tracks.successors(pred) + conflicting_edges = [(pred, succ_of_pred1), (pred, succ_of_pred2)] # check if you are adding a node to a track of which the parent track will divide # downstream @@ -144,11 +141,19 @@ def __init__( "Cannot add node here - downstream division of parent detected.", forceable=True, ) - else: - # Delete the conflicting edge - self.actions.append( - UserDeleteEdge(tracks, (pred_of_succ, succ), _top_level=False) - ) + # Delete the conflicting edge + conflicting_edges = [(pred_of_succ, succ)] + + # AddNode runs the same check, but only after the conflicting edges have been + # deleted. Run it here too, so that a failing check changes nothing. + AddNode.validate_attributes( + tracks, node, attributes, has_mask=mask is not None or pixels is not None + ) + + for conflicting_edge in conflicting_edges: + self.actions.append( + UserDeleteEdge(tracks, conflicting_edge, _top_level=False) + ) # Determine lineage_id from existing track nodes (if any) lineage_key = tracks.features.lineage_key diff --git a/src/funtracks/user_actions/user_delete_edge.py b/src/funtracks/user_actions/user_delete_edge.py index 098a73ac..90d65d74 100644 --- a/src/funtracks/user_actions/user_delete_edge.py +++ b/src/funtracks/user_actions/user_delete_edge.py @@ -35,8 +35,15 @@ def __init__( if not self.tracks.graph_solution.has_edge(*edge): raise InvalidActionError(f"Edge {edge} not in solution, can't remove") + # The out degree the source will have once the edge is gone. Check it before + # applying anything, so that a failing check leaves the tracks unchanged. + out_degree = len(self.tracks.successors(edge[0])) - 1 + if out_degree > 1: + raise InvalidActionError( + f"Expected degree of 0 or 1 after removing edge, got {out_degree}" + ) + self.actions.append(DeleteEdge(tracks, edge)) - out_degree = len(self.tracks.successors(edge[0])) if out_degree == 0: # removed a normal (non division) edge # orphaned segment gets new track id and new lineage id new_track_id = self.tracks.get_next_track_id() @@ -44,7 +51,7 @@ def __init__( self.actions.append( UpdateTrackIDs(self.tracks, edge[1], new_track_id, new_lineage_id) ) - elif out_degree == 1: # removed a division edge + else: # removed a division edge # sibling gets parent's track id (lineage stays the same) sibling = next(iter(self.tracks.graph_solution.successors(edge[0]))) new_track_id = self.tracks.get_track_id(edge[0]) @@ -55,10 +62,6 @@ def __init__( self.tracks, edge[1], lineage_id=self.tracks.get_next_lineage_id() ) ) - else: - raise InvalidActionError( - f"Expected degree of 0 or 1 after removing edge, got {out_degree}" - ) if _top_level: self.tracks.action_history.add_new_action(self) diff --git a/src/funtracks/user_actions/user_delete_nodes.py b/src/funtracks/user_actions/user_delete_nodes.py index dc9c750a..c004d585 100644 --- a/src/funtracks/user_actions/user_delete_nodes.py +++ b/src/funtracks/user_actions/user_delete_nodes.py @@ -2,6 +2,8 @@ from typing import TYPE_CHECKING +from funtracks.exceptions import InvalidActionError + from ..actions._base import ActionGroup from .user_delete_node import UserDeleteNode @@ -31,6 +33,12 @@ def __init__( ): super().__init__(tracks, actions=[]) self.tracks: Tracks # Narrow type from base class + # Check every node before deleting any, so that a failing check leaves the + # tracks unchanged. + nodes = list(dict.fromkeys(nodes)) # de-duplicate, keep order + for node in nodes: + if not tracks.graph_solution.has_node(node): + raise InvalidActionError(f"Node {node} not in solution, can't remove") for node in nodes: self.actions.append(UserDeleteNode(tracks, node, _top_level=False)) diff --git a/src/funtracks/user_actions/user_set_division.py b/src/funtracks/user_actions/user_set_division.py index 09cd80bf..055492ff 100644 --- a/src/funtracks/user_actions/user_set_division.py +++ b/src/funtracks/user_actions/user_set_division.py @@ -57,6 +57,12 @@ def __init__( f"{len(set(nodes))} ({list(nodes)})." ) + # Checked up front: a node outside the solution would otherwise only fail + # halfway, after conflicting edges have already been deleted. + for node in nodes: + if not tracks.graph_solution.has_node(node): + raise InvalidActionError(f"Node {node} not in solution") + times = {node: tracks.get_time(node) for node in nodes} earliest_time = min(times.values()) parents = [node for node, time in times.items() if time == earliest_time] diff --git a/src/funtracks/user_actions/user_update_nodes_attrs.py b/src/funtracks/user_actions/user_update_nodes_attrs.py index 40e088ec..ae749f89 100644 --- a/src/funtracks/user_actions/user_update_nodes_attrs.py +++ b/src/funtracks/user_actions/user_update_nodes_attrs.py @@ -2,6 +2,8 @@ from typing import TYPE_CHECKING +from funtracks.exceptions import InvalidActionError + from ..actions._base import ActionGroup from .user_update_node_attrs import UserUpdateNodeAttrs @@ -45,6 +47,12 @@ def __init__( f"Values list for attribute '{key}' has length {len(values)}, " f"expected {len(nodes)}" ) + # Check every node before updating any, so that a failing check leaves the + # tracks unchanged. Checked on graph_full, not the solution: UpdateNodeAttrs + # writes to graph_full, and soft-deleted candidates may be updated too. + for node in nodes: + if not tracks.graph_full.has_node(node): + raise InvalidActionError(f"Node {node} not in the tracks, can't update") for i, node in enumerate(nodes): node_attrs = {key: values[i] for key, values in attrs.items()} self.actions.append( diff --git a/src/funtracks/user_actions/user_update_segmentation.py b/src/funtracks/user_actions/user_update_segmentation.py index b8f57ff7..e579716f 100644 --- a/src/funtracks/user_actions/user_update_segmentation.py +++ b/src/funtracks/user_actions/user_update_segmentation.py @@ -7,6 +7,7 @@ import numpy as np from tracksdata.nodes import Mask +from funtracks.exceptions import InvalidActionError from funtracks.utils.tracksdata_utils import pixels_to_td_mask, union_td_masks from ..actions._base import ActionGroup @@ -176,6 +177,22 @@ def __init__( f"but entry lengths were {sorted(lengths)}." ) + # Decide what happens to each painted-over node before applying anything, so + # that a failing check leaves the tracks unchanged. Painting with new_value + # only changes new_value's own mask, so deciding this first is equivalent. + painted_over: list[tuple[Mask, int, bool]] = [] + for mask, _time, old_value in updates: + if old_value == 0: + continue + if not self.tracks.graph_solution.has_node(old_value): + raise InvalidActionError( + f"Cannot paint over node {old_value}: it is not in the solution." + ) + mask_old_value = self.tracks.graph_full.nodes[old_value]["mask"] + # If pixels fully overlap with the old_value mask, the node is deleted + fully_covered = mask.intersection(mask_old_value) == mask_old_value.mask.sum() + painted_over.append((mask, old_value, fully_covered)) + if new_value != 0 and updates: # name the node that was painted with node_to_select = new_value @@ -221,13 +238,8 @@ def __init__( # Now that the InvalidAction check for adding a new node has passed, we can add # actions for updating/deleting existing nodes - for mask, _time, old_value in updates: - if old_value == 0: - continue - # check if all pixels of old_value are removed - mask_old_value = self.tracks.graph_full.nodes[old_value]["mask"] - # If pixels fully overlaps with old_value mask, delete node - if mask.intersection(mask_old_value) == mask_old_value.mask.sum(): + for mask, old_value, fully_covered in painted_over: + if fully_covered: self.actions.append(UserDeleteNode(tracks, old_value, _top_level=False)) else: self.actions.append(UpdateNodeSeg(tracks, old_value, mask, added=False)) diff --git a/tests/actions/test_action_group_rollback.py b/tests/actions/test_action_group_rollback.py new file mode 100644 index 00000000..273b8320 --- /dev/null +++ b/tests/actions/test_action_group_rollback.py @@ -0,0 +1,188 @@ +"""A user action that raises partway is rolled back by ActionGroup. + +The user actions check their input before applying anything, but an unexpected +error after the first basic action must not leave the tracks half-changed either. +""" + +import polars as pl +import pytest + +from funtracks.actions import ( + ActionGroup, + AddEdge, + AddNode, + DeleteEdge, + DeleteNode, + UpdateNodeAttrs, + UpdateNodeSeg, + UpdateTrackIDs, +) +from funtracks.exceptions import InvalidActionError +from funtracks.user_actions import ( + UserAddNode, + UserConnectNodes, + UserDeleteNode, + UserMergeNodes, + UserSetDivision, + UserSwapPredecessors, + UserUpdateNodesAttrs, +) + +# Fixture graph: nodes 1 (t=0), 2 (t=1), 3 (t=1), 4 (t=2), 5 (t=4), 6 (t=4) +# Edges: 1 -> 2, 1 -> 3, 3 -> 4, 4 -> 5 + +BASIC_ACTIONS = [ + AddEdge, + AddNode, + DeleteEdge, + DeleteNode, + UpdateNodeAttrs, + UpdateNodeSeg, + UpdateTrackIDs, +] + + +def count_basic_actions(tracks, run) -> int: + """Count the basic actions that run(tracks) applies.""" + applied: list[ActionGroup] = [] + tracks.action_applied.connect(applied.append) + run(tracks) + tracks.action_applied.disconnect(applied.append) + return len(applied) + + +def fail_at_basic_action(monkeypatch, n: int) -> None: + """Make the n-th basic action constructed from now on raise before applying.""" + calls = 0 + for cls in BASIC_ACTIONS: + + def init(self, *args, _original=cls.__init__, **kwargs): + nonlocal calls + calls += 1 + if calls == n: + raise RuntimeError("injected failure") + _original(self, *args, **kwargs) + + monkeypatch.setattr(cls, "__init__", init) + + +def add_confidence(tracks): + tracks.graph_solution.add_node_attr_key( + "confidence", default_value=0.0, dtype=pl.Float64 + ) + + +def make_tracks(get_tracks, with_seg, setup): + tracks = get_tracks(ndim=3, with_seg=with_seg, prefill_track_ids=True) + # The fixture's prefilled iou values are rounded; recompute them so that a + # rollback, which recomputes them too, compares equal + tracks.annotators.compute() + if setup is not None: + setup(tracks) + return tracks + + +# (with_seg, setup, run): run must apply at least two basic actions +USER_ACTIONS = [ + pytest.param(True, None, lambda t: UserConnectNodes(t, [2, 6]), id="connect"), + pytest.param( + True, + None, + lambda t: UserMergeNodes(t, [2, 3], track_ids=3), + id="merge", + marks=pytest.mark.xfail( + strict=True, + reason="Inverting UpdateNodeSeg(added=True) restores the pixels but not a " + "tight bbox, so plain undo of a merge has the same mismatch", + ), + ), + pytest.param(True, None, lambda t: UserSwapPredecessors(t, (5, 6)), id="swap"), + pytest.param(True, None, lambda t: UserSetDivision(t, (3, 5, 6)), id="set_division"), + pytest.param(True, None, lambda t: UserDeleteNode(t, 4), id="delete_node"), + pytest.param( + False, + None, + # 1 divides upstream, so force deletes (1, 2) and (1, 3) first + lambda t: UserAddNode( + t, 7, attributes={"t": 2, "track_id": 1, "pos": [3, 4]}, force=True + ), + id="add_node_forced", + ), + pytest.param( + True, + add_confidence, + lambda t: UserUpdateNodesAttrs(t, [1, 2, 3], {"confidence": [0.1, 0.2, 0.3]}), + id="update_nodes_attrs", + ), +] + + +@pytest.mark.parametrize(("with_seg", "setup", "run"), USER_ACTIONS) +def test_failure_at_last_basic_action_rolls_back( + get_tracks, tracks_state, monkeypatch, with_seg, setup, run +): + dry_run = make_tracks(get_tracks, with_seg, setup) + n_actions = count_basic_actions(dry_run, run) + assert n_actions >= 2 + + tracks = make_tracks(get_tracks, with_seg, setup) + before = tracks_state(tracks, full=False) + fail_at_basic_action(monkeypatch, n_actions) + with pytest.raises(RuntimeError, match="injected failure"): + run(tracks) + assert tracks_state(tracks, full=False) == before + + +def test_set_division_onto_triple_division_rolls_back(get_tracks, tracks_state): + """Making the division deletes (3, 4) and (4, 5) and adds (3, 5) before it + reaches (1, 6), which it cannot delete because 1 is a three-way division.""" + tracks = make_tracks(get_tracks, with_seg=True, setup=None) + # Make 1 a three-way division with the basic action, which does no checks + AddEdge(tracks, (1, 6)) + before = tracks_state(tracks, full=False) + with ( + pytest.warns(UserWarning, match="Removing conflicting edge"), + pytest.raises(InvalidActionError, match="Expected degree of 0 or 1"), + ): + UserSetDivision(tracks, (3, 5, 6)) + assert tracks_state(tracks, full=False) == before + + +class _ApplyThenRaise(ActionGroup): + def __init__(self, tracks, edges, raise_before_super=False): + if raise_before_super: + raise RuntimeError("before super") + super().__init__(tracks, actions=[]) + for edge in edges: + self.actions.append(DeleteEdge(tracks, edge)) + raise RuntimeError("after applying") + + +class _Nested(ActionGroup): + def __init__(self, tracks): + super().__init__(tracks, actions=[]) + self.actions.append(DeleteEdge(tracks, (4, 5))) + self.actions.append(_ApplyThenRaise(tracks, [(3, 4)])) + + +class TestRollbackMechanics: + def test_applied_actions_are_inverted(self, get_tracks, tracks_state): + tracks = get_tracks(ndim=3, with_seg=False, prefill_track_ids=True) + before = tracks_state(tracks, full=False) + with pytest.raises(RuntimeError, match="after applying"): + _ApplyThenRaise(tracks, [(3, 4), (4, 5)]) + assert tracks_state(tracks, full=False) == before + + def test_raise_before_super_init(self, get_tracks, tracks_state): + tracks = get_tracks(ndim=3, with_seg=False, prefill_track_ids=True) + before = tracks_state(tracks) + with pytest.raises(RuntimeError, match="before super"): + _ApplyThenRaise(tracks, [], raise_before_super=True) + assert tracks_state(tracks) == before + + def test_nested_groups_roll_back_once(self, get_tracks, tracks_state): + tracks = get_tracks(ndim=3, with_seg=False, prefill_track_ids=True) + before = tracks_state(tracks, full=False) + with pytest.raises(RuntimeError, match="after applying"): + _Nested(tracks) + assert tracks_state(tracks, full=False) == before diff --git a/tests/conftest.py b/tests/conftest.py index 5dcd1cdc..4739c212 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from collections.abc import Callable -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import numpy as np import polars as pl @@ -13,8 +15,6 @@ ) if TYPE_CHECKING: - from typing import Any - from funtracks.data_model import Tracks @@ -378,7 +378,7 @@ def graph_3d_with_segmentation(tmp_path, backend) -> td.graph.BaseGraph: @pytest.fixture -def get_tracks(get_graph) -> Callable[..., "Tracks"]: +def get_tracks(get_graph) -> Callable[..., Tracks]: """Factory fixture to create Tracks or Tracks instances. Returns a factory function that can be called with: @@ -536,3 +536,67 @@ def _get_graph( ) return _get_graph + + +def _freeze(value: Any) -> Any: + """Turn an attribute value into something that compares with ==.""" + if isinstance(value, Mask): + return (tuple(np.asarray(value.bbox).tolist()), value.mask.tobytes()) + if isinstance(value, np.ndarray): + return tuple(value.tolist()) + if isinstance(value, pl.Series): + return tuple(value.to_list()) + if isinstance(value, list): + return tuple(_freeze(v) for v in value) + return value + + +def _tracks_state(tracks: Tracks, full: bool = True) -> dict[str, Any]: + """Everything a user action can change, in a form that compares with ==. + + Covers the nodes and edges with every attribute (solution flags included), the + track id bookkeeping, the next-node-id counter and the action history. The + segmentation is not compared separately: it is rendered from the masks. + + Args: + tracks: The tracks to snapshot. + full: If True, cover every node and edge in graph_full. If False, only those + in the solution: a rolled-back (or undone) addition leaves a soft-deleted + candidate behind in graph_full, so only the solution is restored exactly. + """ + graph = tracks.graph_full if full else tracks.graph_solution + node_keys = tracks.graph_full.node_attr_keys() + nodes = { + int(node): {key: _freeze(tracks.get_node_attr(node, key)) for key in node_keys} + for node in graph.node_ids() + } + edges = { + (row["source_id"], row["target_id"]): { + key: _freeze(value) for key, value in row.items() if key != "edge_id" + } + for row in tracks.graph_full.edge_attrs().to_dicts() + } + solution_edges = {tuple(e) for e in tracks.graph_solution.edge_list()} + if not full: + edges = {edge: attrs for edge, attrs in edges.items() if edge in solution_edges} + annotator = tracks.track_annotator + state = { + "nodes": nodes, + "edges": edges, + "solution_nodes": set(tracks.graph_solution.node_ids()), + "solution_edges": solution_edges, + "tracklets": {k: sorted(v) for k, v in annotator.tracklet_id_to_nodes.items()}, + "lineages": {k: sorted(v) for k, v in annotator.lineage_id_to_nodes.items()}, + "undo_stack": len(tracks.action_history.undo_stack), + "redo_stack": len(tracks.action_history.redo_stack), + } + if full: + # A rolled-back addition keeps its node id taken, like an undone one + state["max_node_id"] = tracks._max_node_id + return state + + +@pytest.fixture +def tracks_state() -> Callable[..., dict[str, Any]]: + """Snapshot a Tracks, to check that a failed user action left it unchanged.""" + return _tracks_state diff --git a/tests/user_actions/test_user_add_delete_edge.py b/tests/user_actions/test_user_add_delete_edge.py index 9364b78f..c48cfaa0 100644 --- a/tests/user_actions/test_user_add_delete_edge.py +++ b/tests/user_actions/test_user_add_delete_edge.py @@ -1,5 +1,6 @@ import pytest +from funtracks.actions import AddEdge from funtracks.exceptions import InvalidActionError from funtracks.user_actions import UserAddEdge, UserDeleteEdge @@ -136,3 +137,38 @@ def test_delete_edge_triple_div(get_tracks): InvalidActionError, match="Expected degree of 0 or 1 after removing edge, got 2" ): UserDeleteEdge(tracks, (1, 6)) + + +def test_add_edge_triple_div_forced_changes_nothing(get_tracks, tracks_state): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + # 1 is already a division, so the edge (1, 5) is invalid. Forcing would remove + # the in edge (4, 5) of 5 first, which must not happen when the out degree check + # fails. + before = tracks_state(tracks) + with pytest.raises( + InvalidActionError, match="Expected degree of 0 or 1 before adding edge" + ): + UserAddEdge(tracks, (1, 5), force=True) + assert tracks_state(tracks) == before + + +def test_force_re_add_existing_division_edge(get_tracks): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + # The merge edge removed by force starts at the source itself, so the source's + # out degree check must count it as gone. + with pytest.warns(UserWarning, match="Removing edge"): + UserAddEdge(tracks, (1, 3), force=True) + assert tracks.graph_solution.has_edge(1, 3) + assert tracks.graph_solution.has_edge(1, 2) + + +def test_delete_edge_triple_div_changes_nothing(get_tracks, tracks_state): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + # Make 1 a three-way division with the basic action, which does no checks + AddEdge(tracks, (1, 6)) + before = tracks_state(tracks) + with pytest.raises( + InvalidActionError, match="Expected degree of 0 or 1 after removing edge" + ): + UserDeleteEdge(tracks, (1, 2)) + assert tracks_state(tracks) == before diff --git a/tests/user_actions/test_user_add_delete_node.py b/tests/user_actions/test_user_add_delete_node.py index bd23541b..d2483bf1 100644 --- a/tests/user_actions/test_user_add_delete_node.py +++ b/tests/user_actions/test_user_add_delete_node.py @@ -213,3 +213,30 @@ def test_user_delete_nodes(self, get_tracks, ndim, with_seg): assert not graph.has_node(4) assert not graph.has_node(6) assert graph.has_edge(3, 5) + + def test_add_node_without_position_changes_nothing( + self, get_tracks, tracks_state, ndim, with_seg + ): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + # Track 3 has nodes 4 (t=2) and 5 (t=4), so adding to it at t=3 deletes the + # skip edge (4, 5) first. That must not happen when the node is invalid. + before = tracks_state(tracks) + with pytest.raises(ValueError, match="Must provide position or segmentation"): + UserAddNode(tracks, node=7, attributes={"t": 3, "track_id": 3}) + assert tracks_state(tracks) == before + + def test_delete_nodes_with_invalid_node_changes_nothing( + self, get_tracks, tracks_state, ndim, with_seg + ): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + before = tracks_state(tracks) + with pytest.raises(InvalidActionError, match="Node 99 not in solution"): + UserDeleteNodes(tracks, [5, 99]) + assert tracks_state(tracks) == before + + def test_delete_nodes_with_duplicates(self, get_tracks, ndim, with_seg): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + UserDeleteNodes(tracks, [5, 5]) + assert not tracks.graph_solution.has_node(5) + assert tracks.undo() + assert tracks.graph_solution.has_node(5) diff --git a/tests/user_actions/test_user_set_division.py b/tests/user_actions/test_user_set_division.py index a5c23d77..4a8653dc 100644 --- a/tests/user_actions/test_user_set_division.py +++ b/tests/user_actions/test_user_set_division.py @@ -1,7 +1,7 @@ import pytest from funtracks.exceptions import InvalidActionError -from funtracks.user_actions import UserAddEdge, UserSetDivision +from funtracks.user_actions import UserAddEdge, UserDeleteNode, UserSetDivision # Fixture graph: nodes 1 (t=0), 2 (t=1), 3 (t=1), 4 (t=2), 5 (t=4), 6 (t=4) # Edges: 1 -> 2, 1 -> 3, 3 -> 4, 4 -> 5 @@ -167,3 +167,15 @@ def test_break_only_when_both_edges_exist(self, get_tracks, ndim, with_seg): assert tracks.graph_solution.has_edge(1, 2) assert tracks.graph_solution.has_edge(1, 6) assert not tracks.graph_solution.has_edge(2, 6) + + def test_node_not_in_solution_changes_nothing( + self, get_tracks, tracks_state, ndim, with_seg + ): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + # Soft-delete 6: it stays in graph_full, so its time can still be read. + # Making the division would remove (3, 4) and (4, 5) before reaching 6. + UserDeleteNode(tracks, 6) + before = tracks_state(tracks) + with pytest.raises(InvalidActionError, match="Node 6 not in solution"): + UserSetDivision(tracks, (3, 5, 6)) + assert tracks_state(tracks) == before diff --git a/tests/user_actions/test_user_update_nodes_attrs.py b/tests/user_actions/test_user_update_nodes_attrs.py index b327b5a0..f422c6ac 100644 --- a/tests/user_actions/test_user_update_nodes_attrs.py +++ b/tests/user_actions/test_user_update_nodes_attrs.py @@ -2,6 +2,7 @@ import polars as pl import pytest +from funtracks.exceptions import InvalidActionError from funtracks.user_actions import UserUpdateNodesAttrs @@ -110,3 +111,13 @@ def test_protected_attr_raises(self, get_tracks, ndim, with_seg): with pytest.raises(ValueError, match="Cannot update attribute"): UserUpdateNodesAttrs(tracks, nodes=[1, 2], attrs={time_key: [0, 1]}) + + def test_missing_node_changes_nothing(self, get_tracks, tracks_state, ndim, with_seg): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + tracks.graph_solution.add_node_attr_key( + "confidence", default_value=0, dtype=pl.Float64 + ) + before = tracks_state(tracks) + with pytest.raises(InvalidActionError, match="Node 99 not in the tracks"): + UserUpdateNodesAttrs(tracks, nodes=[1, 99], attrs={"confidence": [0.5, 0.5]}) + assert tracks_state(tracks) == before diff --git a/tests/user_actions/test_user_update_segmentation.py b/tests/user_actions/test_user_update_segmentation.py index 1ae45034..074c7e48 100644 --- a/tests/user_actions/test_user_update_segmentation.py +++ b/tests/user_actions/test_user_update_segmentation.py @@ -328,6 +328,24 @@ def test_user_add_seg(self, get_tracks, ndim): assert tracks.get_track_id(node_id) == 10 +def test_painting_over_node_not_in_solution_changes_nothing(get_tracks, tracks_state): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + # Soft-delete node 6, then report one of its pixels as painted over with a new + # label. Adding the new node must not happen when the painted-over node is invalid. + pixels = td_mask_to_pixels(tracks.get_mask(6), tracks.get_time(6), ndim=tracks.ndim) + one_pixel = tuple(np.array([coords[0]]) for coords in pixels) + UserDeleteNodes(tracks, [6]) + before = tracks_state(tracks) + with pytest.raises(InvalidActionError, match="Cannot paint over node 6"): + UserUpdateSegmentation( + tracks, + new_value=7, + updated_pixels=[(one_pixel, 6)], + current_track_id=10, + ) + assert tracks_state(tracks) == before + + def test_missing_seg(get_tracks): tracks = get_tracks(ndim=3, with_seg=False, prefill_track_ids=True) with pytest.raises(ValueError, match="Cannot update non-existing segmentation"): From 1061e07e778ff4ce7b685199c254fa07ee4e5c3a Mon Sep 17 00:00:00 2001 From: Teun Huijben Date: Thu, 29 Oct 2026 15:28:39 -0700 Subject: [PATCH 2/4] User/basic actions for adding/deleting node features - so we can undo/redo group stuff in nTE --- src/funtracks/actions/__init__.py | 3 + src/funtracks/actions/add_delete_feature.py | 189 ++++++++++++++++++ src/funtracks/user_actions/__init__.py | 3 + .../user_actions/user_add_delete_feature.py | 72 +++++++ tests/actions/test_add_delete_feature.py | 128 ++++++++++++ .../test_user_add_delete_feature.py | 87 ++++++++ 6 files changed, 482 insertions(+) create mode 100644 src/funtracks/actions/add_delete_feature.py create mode 100644 src/funtracks/user_actions/user_add_delete_feature.py create mode 100644 tests/actions/test_add_delete_feature.py create mode 100644 tests/user_actions/test_user_add_delete_feature.py diff --git a/src/funtracks/actions/__init__.py b/src/funtracks/actions/__init__.py index 1721f305..3faaf3f4 100644 --- a/src/funtracks/actions/__init__.py +++ b/src/funtracks/actions/__init__.py @@ -1,5 +1,6 @@ from ._base import Action, ActionGroup, BasicAction from .add_delete_edge import AddEdge, DeleteEdge +from .add_delete_feature import AddFeature, DeleteFeature from .add_delete_node import AddNode, DeleteNode from .update_node_attrs import UpdateNodeAttrs from .update_segmentation import UpdateNodeSeg @@ -9,9 +10,11 @@ "Action", "ActionGroup", "AddEdge", + "AddFeature", "AddNode", "BasicAction", "DeleteEdge", + "DeleteFeature", "DeleteNode", "UpdateNodeAttrs", "UpdateNodeSeg", diff --git a/src/funtracks/actions/add_delete_feature.py b/src/funtracks/actions/add_delete_feature.py new file mode 100644 index 00000000..2b2073c3 --- /dev/null +++ b/src/funtracks/actions/add_delete_feature.py @@ -0,0 +1,189 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +import numpy as np +import polars as pl +import tracksdata as td + +from ._base import BasicAction + +if TYPE_CHECKING: + from funtracks.data_model import Tracks + from funtracks.features import Feature + +# Sparse column values: the node ids whose value differs from the feature's default, +# and their values, in the same order. +FeatureValues = tuple[np.ndarray, np.ndarray] + + +def _core_keys(tracks: Tracks) -> set[str]: + """Keys that the Tracks itself depends on, which no feature action may touch.""" + features = tracks.features + keys = { + features.time_key, + td.DEFAULT_ATTR_KEYS.MASK, + td.DEFAULT_ATTR_KEYS.BBOX, + "solution", + } + position_key = features.position_key + if isinstance(position_key, list): + keys.update(position_key) + elif position_key is not None: + keys.add(position_key) + for key in (features.tracklet_key, features.lineage_key): + if key is not None: + keys.add(key) + return keys + + +def _check_node_feature(key: str, feature: Feature) -> None: + if feature["feature_type"] != "node": + raise NotImplementedError( + f"Feature actions only support node features, but {key!r} has " + f"feature_type {feature['feature_type']!r}" + ) + + +def _differs(value: Any, default: Any) -> bool: + if value is None or default is None: + return value is not default + if isinstance(value, np.ndarray | list | tuple) or isinstance( + default, np.ndarray | list | tuple + ): + return not np.array_equal(np.asarray(value), np.asarray(default)) + return bool(value != default) + + +def _read_sparse_values(tracks: Tracks, key: str, default: Any) -> FeatureValues: + """Read the values of a node column that differ from its default. + + Reads graph_full, so soft-deleted candidates keep their value too. + """ + df = tracks.graph_full.node_attrs(attr_keys=[td.DEFAULT_ATTR_KEYS.NODE_ID, key]) + if df[key].dtype.is_numeric() or df[key].dtype == pl.Boolean: + # Fast path for scalar columns. ne_missing keeps nulls when the default is + # not null (and drops them when it is). + differs = ( + pl.col(key).is_not_null() + if default is None + else pl.col(key).ne_missing(default) + ) + df = df.filter(differs) + node_ids = df[td.DEFAULT_ATTR_KEYS.NODE_ID].to_numpy().astype(np.int64) + return node_ids, df[key].to_numpy() + + node_ids_list = [] + values_list = [] + for node_id, value in zip( + df[td.DEFAULT_ATTR_KEYS.NODE_ID].to_list(), df[key].to_list(), strict=True + ): + if _differs(value, default): + node_ids_list.append(node_id) + values_list.append(value) + values = np.empty(len(values_list), dtype=object) + values[:] = values_list + return np.asarray(node_ids_list, dtype=np.int64), values + + +class AddFeature(BasicAction): + """Action for adding a new node feature (an attribute column) to the Tracks. + + Meant for user-created features such as groups. Features computed by an + annotator are switched on with ``Tracks.enable_features`` instead, which is not + an action and is not undoable. + """ + + def __init__( + self, + tracks: Tracks, + key: str, + feature: Feature, + values: FeatureValues | None = None, + ): + """Create an action to add a new node feature. + + Args: + tracks (Tracks): The Tracks to add the feature to. + key (str): The key of the new feature. + feature (Feature): The feature metadata. Only node features are supported. + values (tuple[np.ndarray, np.ndarray] | None): Optional node ids and the + values to set for them. Nodes not listed get the feature's default + value. Used by the inverse of DeleteFeature to restore a column. + + Raises: + NotImplementedError: If the feature is not a node feature. + ValueError: If the key is already a feature, or is owned by an annotator. + """ + super().__init__(tracks) + _check_node_feature(key, feature) + if key in tracks.features: + raise ValueError(f"Feature {key!r} already exists") + if key in tracks.annotators.all_features: + raise ValueError( + f"Feature {key!r} is computed by an annotator; use enable_features" + ) + self.key = key + self.feature = feature + self.values = values + self._apply() + + def inverse(self) -> BasicAction: + """Invert the action to delete the feature again.""" + return DeleteFeature(self.tracks, self.key) + + def _apply(self) -> None: + self.tracks.add_feature(self.key, self.feature) + if self.values is not None and len(self.values[0]) > 0: + node_ids, values = self.values + self.tracks.graph_full.update_node_attrs( + attrs={self.key: list(values)}, + node_ids=[int(node) for node in node_ids], + ) + self.tracks.notify_annotators(self) + + +class DeleteFeature(BasicAction): + """Action for deleting a user-created node feature (an attribute column). + + Saves the values that differ from the feature's default, for every node in + graph_full (soft-deleted candidates included), so the action can be inverted. + """ + + def __init__(self, tracks: Tracks, key: str): + """Create an action to delete a node feature. + + Args: + tracks (Tracks): The Tracks to delete the feature from. + key (str): The key of the feature to delete. + + Raises: + NotImplementedError: If the feature is not a node feature. + ValueError: If the key is not a feature, is a core feature of the Tracks, + is owned by an annotator, or has derived features. + """ + super().__init__(tracks) + if key not in tracks.features: + raise ValueError(f"Feature {key!r} does not exist") + if key in _core_keys(tracks): + raise ValueError(f"Cannot delete core feature {key!r}") + if key in tracks.annotators.all_features: + raise ValueError( + f"Feature {key!r} is computed by an annotator; use disable_features" + ) + feature = tracks.features[key] + _check_node_feature(key, feature) + if feature.get("derived_features"): + raise ValueError(f"Cannot delete feature {key!r}, it has derived features") + self.key = key + self.feature = feature + self.values = _read_sparse_values(tracks, key, feature.get("default_value")) + self._apply() + + def inverse(self) -> BasicAction: + """Invert the action to re-add the feature with its saved values.""" + return AddFeature(self.tracks, self.key, self.feature, values=self.values) + + def _apply(self) -> None: + self.tracks.delete_feature(self.key) + self.tracks.notify_annotators(self) diff --git a/src/funtracks/user_actions/__init__.py b/src/funtracks/user_actions/__init__.py index 34670254..1ce381c2 100644 --- a/src/funtracks/user_actions/__init__.py +++ b/src/funtracks/user_actions/__init__.py @@ -1,5 +1,6 @@ from ._connect_utils import is_connected_chain from ._user_swap_predecessors import UserSwapPredecessors +from .user_add_delete_feature import UserAddFeature, UserDeleteFeature from .user_add_edge import UserAddEdge from .user_add_node import UserAddNode from .user_connect_nodes import UserConnectNodes @@ -19,9 +20,11 @@ __all__ = [ "UserAddEdge", + "UserAddFeature", "UserAddNode", "UserConnectNodes", "UserDeleteEdge", + "UserDeleteFeature", "UserDeleteNode", "UserDeleteNodes", "UserDisconnectNodes", diff --git a/src/funtracks/user_actions/user_add_delete_feature.py b/src/funtracks/user_actions/user_add_delete_feature.py new file mode 100644 index 00000000..f23322a3 --- /dev/null +++ b/src/funtracks/user_actions/user_add_delete_feature.py @@ -0,0 +1,72 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +from ..actions._base import ActionGroup +from ..actions.add_delete_feature import AddFeature, DeleteFeature + +if TYPE_CHECKING: + from funtracks.data_model import Tracks + from funtracks.features import Feature + + +class UserAddFeature(ActionGroup): + """User action for adding a new node feature, e.g. a group. + + Wraps the basic AddFeature action and adds it to the action history, so creating + the feature can be undone. + """ + + def __init__( + self, + tracks: Tracks, + key: str, + feature: Feature, + _top_level: bool = True, + ): + """ + Args: + tracks (Tracks): The tracks to add the feature to. + key (str): The key of the new feature. + feature (Feature): The feature metadata. Only node features are supported. + _top_level (bool): If True, add this action to the history and emit + refresh. Set to False when used as a sub-action inside a compound + action. Defaults to True. + """ + super().__init__(tracks, actions=[]) + self.tracks: Tracks # Narrow type from base class + self.actions.append(AddFeature(tracks, key, feature)) + + if _top_level: + self.tracks.action_history.add_new_action(self) + self.tracks.refresh.emit() + + +class UserDeleteFeature(ActionGroup): + """User action for deleting a user-created node feature, e.g. a group. + + Wraps the basic DeleteFeature action and adds it to the action history, so the + feature and its values can be restored by undo. + """ + + def __init__( + self, + tracks: Tracks, + key: str, + _top_level: bool = True, + ): + """ + Args: + tracks (Tracks): The tracks to delete the feature from. + key (str): The key of the feature to delete. + _top_level (bool): If True, add this action to the history and emit + refresh. Set to False when used as a sub-action inside a compound + action. Defaults to True. + """ + super().__init__(tracks, actions=[]) + self.tracks: Tracks # Narrow type from base class + self.actions.append(DeleteFeature(tracks, key)) + + if _top_level: + self.tracks.action_history.add_new_action(self) + self.tracks.refresh.emit() diff --git a/tests/actions/test_add_delete_feature.py b/tests/actions/test_add_delete_feature.py new file mode 100644 index 00000000..0ca86c5b --- /dev/null +++ b/tests/actions/test_add_delete_feature.py @@ -0,0 +1,128 @@ +import numpy as np +import pytest + +from funtracks.actions import AddFeature, DeleteFeature, UpdateNodeAttrs +from funtracks.features import Feature +from funtracks.user_actions import UserDeleteNode + + +def _group_feature() -> Feature: + return Feature( + feature_type="node", + value_type="bool", + num_values=1, + display_name="group", + default_value=False, + ) + + +@pytest.mark.parametrize("ndim", [3, 4]) +@pytest.mark.parametrize("with_seg", [True, False]) +def test_add_feature(get_tracks, ndim, with_seg): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + action = AddFeature(tracks, "group", _group_feature()) + assert "group" in tracks.features + assert "group" in tracks.graph_full.node_attr_keys() + assert tracks.get_node_attr(1, "group") is False + + inverse = action.inverse() + assert "group" not in tracks.features + assert "group" not in tracks.graph_full.node_attr_keys() + + inverse.inverse() + assert "group" in tracks.features + assert tracks.get_node_attr(1, "group") is False + + +@pytest.mark.parametrize("ndim", [3, 4]) +@pytest.mark.parametrize("with_seg", [True, False]) +def test_delete_feature_restores_values(get_tracks, ndim, with_seg): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + feature = _group_feature() + AddFeature(tracks, "group", feature) + UpdateNodeAttrs(tracks, 1, {"group": True}) + UpdateNodeAttrs(tracks, 2, {"group": True}) + # A soft-deleted node keeps its value in graph_full, and must get it back + UserDeleteNode(tracks, 2) + assert not tracks.graph_solution.has_node(2) + + action = DeleteFeature(tracks, "group") + assert "group" not in tracks.features + assert "group" not in tracks.graph_full.node_attr_keys() + np.testing.assert_array_equal(np.sort(action.values[0]), [1, 2]) + + inverse = action.inverse() + assert tracks.features["group"] == feature + assert tracks.get_node_attr(1, "group") is True + assert tracks.get_node_attr(2, "group") is True + assert tracks.get_node_attr(3, "group") is False + + inverse.inverse() + assert "group" not in tracks.features + assert "group" not in tracks.graph_full.node_attr_keys() + + +def test_delete_feature_object_values(get_tracks): + """Columns that are not numeric take the slow path, which must also be sparse.""" + tracks = get_tracks(ndim=3, with_seg=False, prefill_track_ids=True) + feature = Feature( + feature_type="node", + value_type="str", + num_values=1, + display_name="label", + default_value="", + ) + AddFeature(tracks, "label", feature) + UpdateNodeAttrs(tracks, 3, {"label": "a"}) + + action = DeleteFeature(tracks, "label") + assert action.values[0].tolist() == [3] + + action.inverse() + assert tracks.get_node_attr(3, "label") == "a" + assert tracks.get_node_attr(1, "label") == "" + + +def test_add_existing_feature_raises(get_tracks): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + AddFeature(tracks, "group", _group_feature()) + with pytest.raises(ValueError, match="already exists"): + AddFeature(tracks, "group", _group_feature()) + + +def test_add_annotator_feature_raises(get_tracks): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + tracks.disable_features(["area"]) + with pytest.raises(ValueError, match="computed by an annotator"): + AddFeature(tracks, "area", _group_feature()) + + +@pytest.mark.parametrize("key", ["t", "pos", "mask", "bbox", "solution", "track_id"]) +def test_delete_core_feature_raises(get_tracks, key): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + with pytest.raises(ValueError, match="core feature"): + DeleteFeature(tracks, key) + + +def test_delete_annotator_feature_raises(get_tracks): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + with pytest.raises(ValueError, match="computed by an annotator"): + DeleteFeature(tracks, "area") + + +def test_delete_missing_feature_raises(get_tracks): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + with pytest.raises(ValueError, match="does not exist"): + DeleteFeature(tracks, "group") + + +def test_edge_feature_raises(get_tracks): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + feature = _group_feature() + feature["feature_type"] = "edge" + with pytest.raises(NotImplementedError): + AddFeature(tracks, "edge_group", feature) + + tracks.add_feature("edge_group", feature) + with pytest.raises(NotImplementedError): + DeleteFeature(tracks, "edge_group") diff --git a/tests/user_actions/test_user_add_delete_feature.py b/tests/user_actions/test_user_add_delete_feature.py new file mode 100644 index 00000000..9b656772 --- /dev/null +++ b/tests/user_actions/test_user_add_delete_feature.py @@ -0,0 +1,87 @@ +import pytest + +from funtracks.features import Feature +from funtracks.user_actions import ( + UserAddFeature, + UserDeleteFeature, + UserUpdateNodesAttrs, +) + + +def _group_feature() -> Feature: + return Feature( + feature_type="node", + value_type="bool", + num_values=1, + display_name="group", + default_value=False, + ) + + +@pytest.mark.parametrize("ndim", [3, 4]) +@pytest.mark.parametrize("with_seg", [True, False]) +class TestUserAddDeleteFeature: + def test_add_undo_redo(self, get_tracks, ndim, with_seg): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + action = UserAddFeature(tracks, "group", _group_feature()) + assert tracks.action_history.undo_stack[-1] is action + assert "group" in tracks.features + + assert tracks.undo() + assert "group" not in tracks.features + assert "group" not in tracks.graph_full.node_attr_keys() + + assert tracks.redo() + assert "group" in tracks.features + assert tracks.get_node_attr(1, "group") is False + + def test_delete_undo_redo(self, get_tracks, ndim, with_seg): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + UserAddFeature(tracks, "group", _group_feature()) + UserUpdateNodesAttrs(tracks, nodes=[1, 3], attrs={"group": [True, True]}) + + UserDeleteFeature(tracks, "group") + assert "group" not in tracks.features + + assert tracks.undo() + assert "group" in tracks.features + assert [tracks.get_node_attr(n, "group") for n in [1, 2, 3]] == [ + True, + False, + True, + ] + + assert tracks.redo() + assert "group" not in tracks.features + + def test_undo_membership_after_group_delete(self, get_tracks, ndim, with_seg): + """Deleting a group, undoing that, then undoing an earlier membership change + must work: the column is back by the time the membership undo runs.""" + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + UserAddFeature(tracks, "group", _group_feature()) + UserUpdateNodesAttrs(tracks, nodes=[1, 3], attrs={"group": [True, True]}) + UserUpdateNodesAttrs(tracks, nodes=[2], attrs={"group": [True]}) + UserDeleteFeature(tracks, "group") + + assert tracks.undo() # the delete + assert tracks.undo() # adding node 2 + assert [tracks.get_node_attr(n, "group") for n in [1, 2, 3]] == [ + True, + False, + True, + ] + assert tracks.undo() # adding nodes 1 and 3 + assert [tracks.get_node_attr(n, "group") for n in [1, 2, 3]] == [ + False, + False, + False, + ] + assert tracks.undo() # creating the group + assert "group" not in tracks.features + + def test_toggles_do_not_add_history(self, get_tracks, ndim, with_seg): + tracks = get_tracks(ndim=ndim, with_seg=with_seg, prefill_track_ids=True) + if with_seg: + tracks.disable_features(["area"]) + tracks.enable_features(["area"]) + assert tracks.action_history.undo_stack == [] From b40362ac1ccb2b43729e2a1a3fcdebbc422d9a84 Mon Sep 17 00:00:00 2001 From: Teun Huijben Date: Thu, 29 Oct 2026 15:39:25 -0700 Subject: [PATCH 3/4] copy feature dict + check if feature already in graph --- src/funtracks/actions/add_delete_feature.py | 8 +++++++- tests/actions/test_add_delete_feature.py | 22 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/funtracks/actions/add_delete_feature.py b/src/funtracks/actions/add_delete_feature.py index 2b2073c3..cd3f1801 100644 --- a/src/funtracks/actions/add_delete_feature.py +++ b/src/funtracks/actions/add_delete_feature.py @@ -123,8 +123,14 @@ def __init__( raise ValueError( f"Feature {key!r} is computed by an annotator; use enable_features" ) + # A column that exists without being a registered feature (e.g. loaded from + # a geff) would keep its values here, and the inverse would then drop them. + if key in tracks.graph_full.node_attr_keys(): + raise ValueError(f"Node attribute {key!r} already exists in the graph") self.key = key - self.feature = feature + # Copy, so that a later change to the caller's dict (which add_feature + # stores as is) does not change the feature this action restores. + self.feature = feature.copy() self.values = values self._apply() diff --git a/tests/actions/test_add_delete_feature.py b/tests/actions/test_add_delete_feature.py index 0ca86c5b..92ff2e6e 100644 --- a/tests/actions/test_add_delete_feature.py +++ b/tests/actions/test_add_delete_feature.py @@ -1,4 +1,5 @@ import numpy as np +import polars as pl import pytest from funtracks.actions import AddFeature, DeleteFeature, UpdateNodeAttrs @@ -90,6 +91,27 @@ def test_add_existing_feature_raises(get_tracks): AddFeature(tracks, "group", _group_feature()) +def test_add_feature_over_unregistered_column_raises(get_tracks): + # A column in the graph that is not a registered feature, e.g. loaded from a + # geff: adding it as a feature would let the inverse drop its values. + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + tracks.graph_full.add_node_attr_key("group", default_value=False, dtype=pl.Boolean) + tracks.graph_full.update_node_attrs(attrs={"group": [True]}, node_ids=[1]) + with pytest.raises(ValueError, match="already exists in the graph"): + AddFeature(tracks, "group", _group_feature()) + assert "group" not in tracks.features + assert tracks.get_node_attr(1, "group") is True + + +def test_add_feature_copies_feature_dict(get_tracks): + tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) + feature = _group_feature() + action = AddFeature(tracks, "group", feature) + feature["display_name"] = "renamed" + action.inverse().inverse() + assert tracks.features["group"]["display_name"] == "group" + + def test_add_annotator_feature_raises(get_tracks): tracks = get_tracks(ndim=3, with_seg=True, prefill_track_ids=True) tracks.disable_features(["area"]) From a5a1c951819c1c49b2c7a050d66a27d80858a4ec Mon Sep 17 00:00:00 2001 From: Teun Huijben Date: Thu, 29 Oct 2026 15:43:30 -0700 Subject: [PATCH 4/4] dont roll back a useraction that is already in undo history --- src/funtracks/actions/_base.py | 17 +++++++++++++---- tests/actions/test_action_group_rollback.py | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/funtracks/actions/_base.py b/src/funtracks/actions/_base.py index 3d68e1ab..7bc13a5c 100644 --- a/src/funtracks/actions/_base.py +++ b/src/funtracks/actions/_base.py @@ -77,10 +77,19 @@ def init_or_roll_back(self: ActionGroup, *args: Any, **kwargs: Any) -> None: try: init(self, *args, **kwargs) except Exception: - # Undo what was applied before the error, newest first. actions - # does not exist yet if the error came before super().__init__. - for action in reversed(getattr(self, "actions", [])): - action.inverse() + # Once the action is in the undo history, Ctrl+Z can undo it, so an + # error raised after that (e.g. by a refresh listener) must not undo + # it here too. tracks and actions do not exist yet if the error came + # before super().__init__. + undo_stack = ( + self.tracks.action_history.undo_stack + if hasattr(self, "tracks") + else [] + ) + if not (undo_stack and undo_stack[-1] is self): + # Undo what was applied before the error, newest first + for action in reversed(getattr(self, "actions", [])): + action.inverse() raise cls.__init__ = init_or_roll_back # type: ignore[method-assign] diff --git a/tests/actions/test_action_group_rollback.py b/tests/actions/test_action_group_rollback.py index 273b8320..c7296a2c 100644 --- a/tests/actions/test_action_group_rollback.py +++ b/tests/actions/test_action_group_rollback.py @@ -186,3 +186,23 @@ def test_nested_groups_roll_back_once(self, get_tracks, tracks_state): with pytest.raises(RuntimeError, match="after applying"): _Nested(tracks) assert tracks_state(tracks, full=False) == before + + def test_no_rollback_once_in_history(self, get_tracks): + """An error from a refresh listener comes after the action is in the undo + history, so the action must stay applied and be undone by Ctrl+Z only.""" + tracks = get_tracks(ndim=3, with_seg=False, prefill_track_ids=True) + + def broken_listener(*args): + raise RuntimeError("listener broke") + + tracks.refresh.connect(broken_listener) + with pytest.raises(Exception, match="listener broke"): + UserDeleteNode(tracks, 4) + tracks.refresh.disconnect(broken_listener) + + assert not tracks.graph_solution.has_node(4) + assert len(tracks.action_history.undo_stack) == 1 + tracks.undo() + assert tracks.graph_solution.has_node(4) + assert tracks.graph_solution.has_edge(3, 4) + assert tracks.graph_solution.has_edge(4, 5)