Source code for vista.tracks.track

"""
Track data model for representing temporal object trajectories.

This module defines the Track class, which represents a single object trajectory
across multiple frames with support for multiple coordinate systems (pixel, geodetic,
time-based), visualization styling, and data persistence.
"""

import datetime
import pathlib
import uuid
from dataclasses import dataclass, field
from typing import Optional, Union

import numpy as np
import pandas as pd
import pyqtgraph as pg
from numpy.typing import NDArray
from PyQt6.QtCore import Qt

from vista.imagery.imagery import Imagery
from vista.sensors.sensor import Sensor
from vista.temporal_data.temporal_data import TemporalData
from vista.utils.geodetic_mapping import map_geodetic_to_pixel


[docs] @dataclass(kw_only=True) class Track(TemporalData): """ Represents a single object trajectory across multiple frames. A Track contains temporal position data for a moving object, with support for multiple coordinate systems (pixel, geodetic, time-based) and rich visualization options. Tracks can be created manually, loaded from files, or generated by tracking algorithms. Parameters ---------- name : str Unique identifier for this track rows : NDArray[np.float64] Row (vertical) pixel coordinates for each frame columns : NDArray[np.float64] Column (horizontal) pixel coordinates for each frame sensor : Sensor Sensor object providing coordinate conversion capabilities Attributes ---------- color : str, optional Color for track visualization, by default 'g' (green) marker : str, optional Marker style for current position ('o', 's', 't', 'd', '+', 'x', 'star'), by default 'o' (circle) line_width : int, optional Width of line connecting track points, by default 2 marker_size : int, optional Size of current position marker, by default 12 visible : bool, optional Whether track is visible in viewer, by default True tail_length : int, optional Number of previous frames to show (0 = all history), by default 0 complete : bool, optional If True, show entire track regardless of current frame, by default False show_line : bool, optional Whether to draw line connecting track points, by default True line_style : str, optional Qt line style ('SolidLine', 'DashLine', 'DotLine', 'DashDotLine', 'DashDotDotLine'), by default 'SolidLine' labels : set[str], optional Set of text labels for categorizing/filtering tracks, by default empty set extraction_metadata : dict, optional Extraction metadata containing image chips and signal detection results. Dictionary with keys: 'chip_size' (int), 'chips' (NDArray with shape (n_points, diameter, diameter)), 'signal_masks' (boolean NDArray with same shape), 'noise_stds' (NDArray with shape (n_points,)), by default None Methods ------- __getitem__(slice) Slice track by frame range from_dataframe(df, sensor, name) Create Track from pandas DataFrame with coordinate conversion length Property that returns cumulative Euclidean distance along track copy() Create a deep copy of the track to_csv(file) Save track to CSV file to_dataframe() Convert track to pandas DataFrame Notes ----- - Track coordinates can be provided as pixel (row/col) or geodetic (lat/lon/alt) - Times can be used instead of frames if included at creation - The from_dataframe() method handles coordinate system conversions automatically - Track length is computed lazily and cached for performance """ name: str rows: NDArray[np.float64] columns: NDArray[np.float64] sensor: Sensor # Styling attributes color: str = "g" # Green by default marker: str = "o" # Circle by default line_width: int = 2 marker_size: int = 12 visible: bool = True tail_length: int = 0 # 0 means show all history, >0 means show only last N frames complete: bool = False # If True, show complete track regardless of current frame and override tail_length show_line: bool = True # If True, show line connecting track points line_style: str = "SolidLine" # Line style: 'SolidLine', 'DashLine', 'DotLine', 'DashDotLine', 'DashDotDotLine' labels: set[str] = field(default_factory=set) # Set of labels for this track label_time: Optional[datetime.datetime] = None # UTC timestamp labels were last applied labeler: Optional[str] = None # Username of the person who last applied labels tracker: Optional[str] = None # Name of tracker this track belongs to # Extraction metadata extraction_metadata: Optional[dict] = None # Dict containing 'chip_size', 'chips', 'signal_masks', 'noise_stds' # Uncertainty visualization (2D covariance matrix: [[C00, C01], [C01, C11]]) covariance_00: Optional[NDArray[np.float64]] = None # Row variance (C_row_row) covariance_01: Optional[NDArray[np.float64]] = None # Row-column covariance (C_row_col) covariance_11: Optional[NDArray[np.float64]] = None # Column variance (C_col_col) show_uncertainty: bool = False # Whether to display uncertainty ellipses # Private attributes _length: int = field(init=False, default=None) # Performance optimization: cached data structures _cached_pen: object = field(default=None, init=False, repr=False) # Cached PyQtGraph pen _cached_brush: object = field(default=None, init=False, repr=False) # Cached PyQtGraph brush _pen_params: tuple = field(default=None, init=False, repr=False) # Parameters used for cached pen _brush_params: tuple = field(default=None, init=False, repr=False) # Parameters used for cached brush _cached_lons: Optional[NDArray[np.float64]] = field(default=None, init=False, repr=False) # Cached longitude coords _cached_lats: Optional[NDArray[np.float64]] = field(default=None, init=False, repr=False) # Cached latitude coords uuid: str = field(init=False, default=None) def __post_init__(self): super().__post_init__() self.uuid = uuid.uuid4() def __eq__(self, other): if not isinstance(other, Track): return False return self.uuid == other.uuid
[docs] def __getitem__(self, s): if isinstance(s, slice) or isinstance(s, np.ndarray): # Handle slice objects track_slice = self.copy() if track_slice.times is not None: track_slice.times = track_slice.times[s] if track_slice.frames is not None: track_slice.frames = track_slice.frames[s] track_slice.rows = track_slice.rows[s] track_slice.columns = track_slice.columns[s] # Slice extraction metadata if present if track_slice.extraction_metadata is not None: track_slice.extraction_metadata = { "chip_size": track_slice.extraction_metadata["chip_size"], "chips": track_slice.extraction_metadata["chips"][s], "signal_masks": track_slice.extraction_metadata["signal_masks"][s], "noise_stds": track_slice.extraction_metadata["noise_stds"][s], } # Slice uncertainty data if present if track_slice.covariance_00 is not None: track_slice.covariance_00 = track_slice.covariance_00[s] if track_slice.covariance_01 is not None: track_slice.covariance_01 = track_slice.covariance_01[s] if track_slice.covariance_11 is not None: track_slice.covariance_11 = track_slice.covariance_11[s] # Slice cached geodetic coords if present if track_slice._cached_lons is not None: track_slice._cached_lons = track_slice._cached_lons[s] if track_slice._cached_lats is not None: track_slice._cached_lats = track_slice._cached_lats[s] return track_slice else: raise TypeError("Invalid index or slice type.")
[docs] def __len__(self): return len(self.rows)
def __str__(self): return self.__repr__() def __repr__(self): s = f"{self.__class__.__name__}({self.name})" s += "\n" + len(s) * "-" + "\n" s += str(self.to_dataframe()) return s
[docs] def get_visible_indices(self, val: Union[np.datetime64, int], is_time: bool): """ Get indices of track points that should be visible at the current value. Parameters ---------- val : np.datetime64, int Current value, either a time or frame number is_time : bool Whether the value is a time (True) or frame (False) Returns ------- NDArray or None Array of indices for visible track points, or None if no points visible """ # A track cannot be shown along an axis (times or frames) it does not have if is_time and self.times is None: return None if not is_time and self.frames is None: return None if self.complete: # Show entire track return np.arange(len(self)) if val is None: return None # Find points up to current value if is_time: mask = self.times <= val else: mask = self.frames <= val if self.tail_length > 0 and np.any(mask): if is_time: # need to sort since times can, technically, be unordered sorter = np.argsort(self.times) index = np.where(mask[sorter])[0][-1] # last true index in sorted times index = max(index - self.tail_length, 0) # start of tail index = sorter[index] # convert back to index in initial array time_diff = val - self.times # Only show last N times # need to use the index of the tail to get the maximum allowed length if val <= np.max(self.times): max_time_diff = val - self.times[index] else: max_time_diff = np.max(self.times) - self.times[index] mask &= (time_diff <= max_time_diff) & (time_diff >= 0) else: # Only show last N frames frame_diff = val - self.frames mask &= (frame_diff <= self.tail_length) & (frame_diff >= 0) indices = np.where(mask)[0] return indices if len(indices) > 0 else None
[docs] def get_geodetic_coords(self) -> tuple[NDArray[np.float64], NDArray[np.float64]] | None: """Get geodetic coordinates for all track points, computing and caching if needed. Projects each track point using its own frame's sensor geometry, so the result represents the true geographic location at each time step. The result is cached so subsequent calls return immediately. Returns ------- tuple[NDArray[np.float64], NDArray[np.float64]] or None (longitudes, latitudes) in degrees, or None if the sensor cannot geolocate. """ if self._cached_lons is not None and self._cached_lats is not None: return self._cached_lons, self._cached_lats if not self.sensor.can_geolocate() or self.times is None: return None # Single vectorized call — sensor handles time grouping internally locations = self.sensor.pixel_to_geodetic(self.times, self.rows, self.columns) lons = np.asarray(locations.lon.deg, dtype=np.float64) lats = np.asarray(locations.lat.deg, dtype=np.float64) # Points which do not intersect Earth have NaN geocentric coordinates, which astropy reports as # lat=90, lon=0 with a NaN height. Set those invalid locations to NaN invalid = np.isnan(np.asarray(locations.height.value, dtype=np.float64)) lons[invalid] = np.nan lats[invalid] = np.nan self._cached_lons = lons self._cached_lats = lats return self._cached_lons, self._cached_lats
[docs] def invalidate_caches(self): """Invalidate cached data structures when track data changes.""" super().invalidate_caches() self._cached_pen = None self._cached_brush = None self._pen_params = None self._brush_params = None self._length = None self._cached_lons = None self._cached_lats = None
[docs] def get_pen(self, width=None, style=None): """ Get cached PyQtGraph pen object, creating only if parameters changed. Parameters ---------- width : int, optional Line width override, uses self.line_width if None style : str, optional Line style override, uses self.line_style if None Returns ------- pg.mkPen PyQtGraph pen object """ actual_width = width if width is not None else self.line_width actual_style = style if style is not None else self.line_style # Map string style to Qt constant style_map = { "SolidLine": Qt.PenStyle.SolidLine, "DashLine": Qt.PenStyle.DashLine, "DotLine": Qt.PenStyle.DotLine, "DashDotLine": Qt.PenStyle.DashDotLine, "DashDotDotLine": Qt.PenStyle.DashDotDotLine, } qt_style = style_map.get(actual_style, Qt.PenStyle.SolidLine) params = (self.color, actual_width, qt_style) if self._pen_params != params: self._cached_pen = pg.mkPen(color=self.color, width=actual_width, style=qt_style) self._pen_params = params return self._cached_pen
[docs] def get_brush(self): """ Get cached PyQtGraph brush object for marker fill, creating only if parameters changed. Returns ------- pg.mkBrush PyQtGraph brush object """ params = (self.color,) if self._brush_params != params: self._cached_brush = pg.mkBrush(color=self.color) self._brush_params = params return self._cached_brush
[docs] def has_uncertainty(self) -> bool: """ Check if track has uncertainty data. Returns ------- bool True if track has all three covariance matrix elements (C00, C01, C11), False otherwise """ return self.covariance_00 is not None and self.covariance_01 is not None and self.covariance_11 is not None
[docs] def get_uncertainty_ellipse_parameters( self, ) -> Optional[tuple[NDArray[np.float64], NDArray[np.float64], NDArray[np.float64]]]: """ Convert covariance matrix to ellipse parameters for visualization. Computes the semi-major axis length, semi-minor axis length, and rotation angle from the 2D covariance matrix at each track point. Returns ------- Optional[tuple[NDArray[np.float64], NDArray[np.float64], NDArray[np.float64]]] Tuple of (semi_major_axis, semi_minor_axis, rotation_degrees) arrays, or None if no uncertainty data. Rotation is in degrees, counter-clockwise from horizontal (positive column axis). """ if not self.has_uncertainty(): return None # Eigenvalue decomposition of 2x2 covariance matrix # For [[C00, C01], [C01, C11]], eigenvalues are: # lambda = 0.5 * (C00 + C11 +/- sqrt((C00 - C11)^2 + 4*C01^2)) trace = self.covariance_00 + self.covariance_11 discriminant = np.sqrt( np.maximum((self.covariance_00 - self.covariance_11) ** 2 + 4 * self.covariance_01**2, 0) ) lambda1 = 0.5 * (trace + discriminant) # Larger eigenvalue lambda2 = 0.5 * (trace - discriminant) # Smaller eigenvalue # Semi-axes are square roots of eigenvalues semi_major = np.sqrt(np.maximum(lambda1, 0)) semi_minor = np.sqrt(np.maximum(lambda2, 0)) # Rotation angle (in degrees, counter-clockwise from horizontal) # arctan2(2*C01, C00 - C11) gives twice the rotation angle rotation_rad = 0.5 * np.arctan2(2 * self.covariance_01, self.covariance_00 - self.covariance_11) rotation_deg = np.degrees(rotation_rad) return semi_major, semi_minor, rotation_deg
[docs] def get_uncertainty_radius(self) -> Optional[NDArray[np.float64]]: """ Compute the geometric mean radius of uncertainty ellipses. The geometric mean radius is computed as the fourth root of the covariance matrix determinant: sqrt(sqrt(det(Cov))) = sqrt(sqrt(C00*C11 - C01^2)). This represents the radius of a circle with the same area as the uncertainty ellipse. Returns ------- Optional[NDArray[np.float64]] Array of geometric mean radii for each track point, or None if uncertainty data is not available """ if not self.has_uncertainty(): return None # Geometric mean radius = sqrt(sqrt(det(covariance_matrix))) det = self.covariance_00 * self.covariance_11 - self.covariance_01**2 return np.sqrt(np.sqrt(np.maximum(det, 0)))
[docs] @classmethod def from_dataframe( cls, df: pd.DataFrame, sensor: Sensor, imagery: Optional[Imagery] = None, name: str = None, ): """ Create Track from pandas DataFrame with automatic coordinate conversion. Supports multiple input coordinate systems with automatic conversion: - Times or Frames (conversion requires Imagery with times) - Rows/Columns or Lat/Lon/Alt → Rows/Columns (Geodetic requires sensor and times) Parameters ---------- df : pd.DataFrame DataFrame containing track data with required columns based on coordinate system (see Notes) sensor : Sensor Sensor object for coordinate conversions imagery : Imagery, optional Imagery used to convert times and frames. name : str, optional Track name, by default taken from df["Track"] Returns ------- Track New Track object with converted coordinates Raises ------ ValueError If required columns are missing or coordinate conversion fails Notes ----- Required columns (one set from each group): Temporal coordinates (choose one): - "Times" : Timestamps (preferred) - "Frames" : Frame numbers Spatial coordinates (choose one): - "Rows" and "Columns" : Pixel coordinates (preferred) - "Latitude (deg)", "Longitude (deg)", "Altitude (km)" : Geodetic coordinates (requires sensor with geolocation capability) Optional styling columns: - "Color", "Marker", "Line Width", "Marker Size", "Visible", "Complete", "Show Line", "Line Style", "Tail Length", "Labels", "Tracker" """ if name is None: name = str(df["Track"][0]) kwargs = {} # Parse tracker from dataframe if not provided if "Tracker" in df.columns: tracker_val = df["Tracker"].iloc[0] if pd.notna(tracker_val) and tracker_val: tracker = str(tracker_val) kwargs["tracker"] = tracker if "Color" in df.columns: kwargs["color"] = df["Color"].iloc[0] if "Marker" in df.columns: kwargs["marker"] = df["Marker"].iloc[0] if "Line Width" in df.columns: kwargs["line_width"] = df["Line Width"].iloc[0] if "Marker Size" in df.columns: kwargs["marker_size"] = df["Marker Size"].iloc[0] if "Tail Length" in df.columns: kwargs["tail_length"] = df["Tail Length"].iloc[0] if "Visible" in df.columns: kwargs["visible"] = df["Visible"].iloc[0] if "Complete" in df.columns: kwargs["complete"] = df["Complete"].iloc[0] if "Show Line" in df.columns: kwargs["show_line"] = df["Show Line"].iloc[0] if "Line Style" in df.columns: kwargs["line_style"] = df["Line Style"].iloc[0] if "Labels" in df.columns: # Parse labels from comma-separated string labels_str = df["Labels"].iloc[0] if pd.notna(labels_str) and labels_str: labels_str = str( labels_str ) # Make sure labels are parsed as a string even if they're something like `1` kwargs["labels"] = set(label.strip() for label in labels_str.split(",")) else: kwargs["labels"] = set() if "Label Time" in df.columns: label_time_val = df["Label Time"].iloc[0] if pd.notna(label_time_val) and label_time_val != "": try: kwargs["label_time"] = pd.to_datetime(label_time_val).to_pydatetime() except (ValueError, TypeError): kwargs["label_time"] = None if "Labeler" in df.columns: labeler_val = df["Labeler"].iloc[0] if pd.notna(labeler_val) and labeler_val != "": kwargs["labeler"] = str(labeler_val) times = None if "Times" in df.columns: times = pd.to_datetime(df["Times"], format="mixed").to_numpy().astype("datetime64[ns]") frames = None if "Frames" in df.columns: frames = df["Frames"].to_numpy() # attempt any conversions if able and necessary if imagery is not None: times, frames = imagery.fill_times_frames(times, frames) if times is None and frames is None: raise ValueError(f"Track '{name}' must have either 'Times' or 'Frames' column") # Handle uncertainty data (optional) - covariance matrix elements # Only populate if all three columns exist and all values are valid (not NaN) if "Covariance 00" in df.columns and "Covariance 01" in df.columns and "Covariance 11" in df.columns: cov_00 = df["Covariance 00"].to_numpy(dtype=np.float64) cov_01 = df["Covariance 01"].to_numpy(dtype=np.float64) cov_11 = df["Covariance 11"].to_numpy(dtype=np.float64) # Only set covariance if all values are valid (no NaN or inf) if np.all(np.isfinite(cov_00)) and np.all(np.isfinite(cov_01)) and np.all(np.isfinite(cov_11)): kwargs["covariance_00"] = cov_00 kwargs["covariance_01"] = cov_01 kwargs["covariance_11"] = cov_11 # Determine rows/columns - priority: Rows/Columns > geodetic-to-pixel mapping has_geodetic = ( "Latitude (deg)" in df.columns and "Longitude (deg)" in df.columns and "Altitude (km)" in df.columns ) initial_lons = None initial_lats = None if "Rows" in df.columns and "Columns" in df.columns: # Row/Column take precedence rows = df["Rows"].to_numpy() columns = df["Columns"].to_numpy() # Preserve geodetic coords if also present (avoids re-projection later) if has_geodetic: initial_lons = df["Longitude (deg)"].to_numpy(dtype=np.float64) initial_lats = df["Latitude (deg)"].to_numpy(dtype=np.float64) elif has_geodetic: # Need geodetic-to-pixel conversion if sensor is None: raise ValueError( f"Track '{name}' has geodetic coordinates (Lat/Lon/Alt) but no row/column. " "Sensor required for geodetic-to-pixel mapping." ) if not sensor.can_geolocate(): raise ValueError( f"Track '{name}' has geodetic coordinates (Lat/Lon/Alt) but sensor '{sensor.name}' " "does not support geolocation." ) if times is None: raise ValueError( f"Track '{name}' has geodetic coordinates (Lat/Lon/Alt) but no corresponding times. " "Times required for geodetic-to-pixel mapping." ) initial_lons = df["Longitude (deg)"].to_numpy(dtype=np.float64) initial_lats = df["Latitude (deg)"].to_numpy(dtype=np.float64) # Map geodetic to pixel using sensor rows, columns = map_geodetic_to_pixel( initial_lats, initial_lons, df["Altitude (km)"].to_numpy(), times, sensor, ) else: raise ValueError( f"Track '{name}' must have either 'Rows' and 'Columns' columns, " "or 'Latitude (deg)', 'Longitude (deg)', and 'Altitude (km)' columns" ) # Enable show_uncertainty by default if uncertainty data is present if "covariance_00" in kwargs and "covariance_01" in kwargs and "covariance_11" in kwargs: # Only set to True if not already explicitly set if "show_uncertainty" not in kwargs: kwargs["show_uncertainty"] = True track = cls( name=name, times=times, frames=frames, rows=rows, columns=columns, sensor=sensor, **kwargs, ) # Pre-populate geodetic cache if coords were available in the dataframe if initial_lons is not None and initial_lats is not None: track._cached_lons = initial_lons track._cached_lats = initial_lats return track
@property def length(self): """ Cumulative Euclidean distance along the track path. Computes the sum of pixel distances between consecutive track points. Result is cached for performance. Returns ------- float Total track length in pixels, or 0.0 if track has fewer than 2 points """ if self._length is None: if len(self) < 2: self._length = 0.0 else: self._length = np.sum(np.sqrt(np.diff(self.rows) ** 2 + np.diff(self.columns) ** 2)) return self._length
[docs] def copy(self): """ Create a deep copy of this track object. Returns ------- Track New Track object with copied arrays and styling attributes """ # Deep copy extraction metadata if present extraction_metadata_copy = None if self.extraction_metadata is not None: extraction_metadata_copy = { "chip_size": self.extraction_metadata["chip_size"], "chips": self.extraction_metadata["chips"].copy(), "signal_masks": self.extraction_metadata["signal_masks"].copy(), "noise_stds": self.extraction_metadata["noise_stds"].copy(), } track_copy = self.__class__( name=self.name, times=self.times.copy() if self.times is not None else None, frames=self.frames.copy() if self.frames is not None else None, rows=self.rows.copy(), columns=self.columns.copy(), sensor=self.sensor, color=self.color, marker=self.marker, line_width=self.line_width, marker_size=self.marker_size, visible=self.visible, tail_length=self.tail_length, complete=self.complete, show_line=self.show_line, line_style=self.line_style, labels=self.labels.copy(), label_time=self.label_time, labeler=self.labeler, tracker=self.tracker, extraction_metadata=extraction_metadata_copy, covariance_00=self.covariance_00.copy() if self.covariance_00 is not None else None, covariance_01=self.covariance_01.copy() if self.covariance_01 is not None else None, covariance_11=self.covariance_11.copy() if self.covariance_11 is not None else None, show_uncertainty=self.show_uncertainty, ) # Preserve cached geodetic coords if self._cached_lons is not None: track_copy._cached_lons = self._cached_lons.copy() if self._cached_lats is not None: track_copy._cached_lats = self._cached_lats.copy() return track_copy
[docs] def to_csv(self, file: Union[str, pathlib.Path]): self.to_dataframe().to_csv(file, index=None)
[docs] def to_dataframe(self) -> pd.DataFrame: """Convert track to DataFrame Raises: ValueError: If geolocation/time requested but imagery is missing required data """ data = { "Tracker": len(self) * [self.tracker or ""], "Track": len(self) * [self.name], "Rows": self.rows, "Columns": self.columns, "Color": self.color, "Marker": self.marker, "Line Width": self.line_width, "Marker Size": self.marker_size, "Tail Length": self.tail_length, "Visible": self.visible, "Complete": self.complete, "Show Line": self.show_line, "Line Style": self.line_style, "Labels": ", ".join(sorted(self.labels)) if self.labels else "", "Label Time": self.label_time.isoformat() if self.label_time is not None else "", "Labeler": self.labeler or "", } if self.times is not None: data["Times"] = self.times.astype(str) if self.frames is not None: data["Frames"] = self.frames # Include geolocation if possible geodetic = self.get_geodetic_coords() if geodetic is not None: data["Longitude (deg)"] = geodetic[0] data["Latitude (deg)"] = geodetic[1] # Single vectorized call for altitude locations = self.sensor.pixel_to_geodetic(self.times, self.rows, self.columns) data["Altitude (km)"] = np.asarray(locations.height.to("km").value) else: # Sensor cannot geolocate - fill with NaN data["Latitude (deg)"] = np.full(len(self), np.nan) data["Longitude (deg)"] = np.full(len(self), np.nan) data["Altitude (km)"] = np.full(len(self), np.nan) # Include extraction metadata if present if self.extraction_metadata is not None: chips = self.extraction_metadata.get("chips") masks = self.extraction_metadata.get("signal_masks") noise = self.extraction_metadata.get("noise_stds") if chips is not None and masks is not None: data["Signal Total"] = np.sum(chips * masks, axis=(1, 2)) data["Signal Pixels"] = np.sum(masks, axis=(1, 2)).astype(float) if noise is not None: data["Noise Std"] = noise # Include uncertainty data if present if self.has_uncertainty(): data["Covariance 00"] = self.covariance_00 data["Covariance 01"] = self.covariance_01 data["Covariance 11"] = self.covariance_11 return pd.DataFrame(data)