Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/funtracks/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,9 +10,11 @@
"Action",
"ActionGroup",
"AddEdge",
"AddFeature",
"AddNode",
"BasicAction",
"DeleteEdge",
"DeleteFeature",
"DeleteNode",
"UpdateNodeAttrs",
"UpdateNodeSeg",
Expand Down
38 changes: 37 additions & 1 deletion src/funtracks/actions/_base.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -56,8 +57,43 @@ 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:
# 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]

def __init__(
self,
tracks: Tracks,
Expand Down
195 changes: 195 additions & 0 deletions src/funtracks/actions/add_delete_feature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
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"
)
# 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
# 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()

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)
47 changes: 29 additions & 18 deletions src/funtracks/actions/add_delete_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
3 changes: 3 additions & 0 deletions src/funtracks/user_actions/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,9 +20,11 @@

__all__ = [
"UserAddEdge",
"UserAddFeature",
"UserAddNode",
"UserConnectNodes",
"UserDeleteEdge",
"UserDeleteFeature",
"UserDeleteNode",
"UserDeleteNodes",
"UserDisconnectNodes",
Expand Down
Loading
Loading