How nikpau/pytsa works
Purpose and Public Surface
PyTSA is a Python library for converting raw AIS CSV records into decoded vessel messages, searchable vessel states, and segmented/refined trajectories. The primary public API is exposed through pytsa/__init__.py; core domain structures are in pytsa/structs.py. It is a library workflow, not a service or web application.
Raw AIS Decoding Pipeline
decode() ingests date-named CSV files containing either dynamic AIS types 1/2/3/18 or static type 5 records, decodes AIVDM payloads, and writes decoded CSVs. The decoder coordinates file-level processing and optional multiprocessing in pytsa/decoder/ais_decoder.py; expected input/output column schemas are defined in pytsa/decoder/filedescriptor.py. This stage is the boundary between external raw data and PyTSA’s decoded-message model.
Domain Model and Spatial/Temporal Inputs
AIS messages, tracks, bounding boxes, and time-position query objects are represented centrally in pytsa/structs.py. A caller defines a BoundingBox as the searchable geographic frame and supplies decoded dynamic and static CSVs. TimePosition binds a query time with location, allowing later stages to evaluate nearby vessel state consistently.
Search-Agent Orchestration
SearchAgent is the main coordinator in pytsa/tsea/search_agent.py. It loads decoded dynamic/static inputs, scopes work to its bounding box, and offers two main flows:
• freeze(TimePosition): find ships near a location/time and return TargetShip objects.
• extract_all(...): process all eligible MMSIs into trajectory-bearing TargetShip objects, potentially in parallel.
This is the primary API boundary from caller-provided files and query parameters to vessel/trajectory results.
Target-Ship State and Interpolation
pytsa/tsea/targetship.py encapsulates a vessel and its Track collection. For point-in-time monitoring, SearchAgent.freeze() produces ships whose observe() output is interpolated latitude, longitude, speed-over-ground, and course-over-ground. Linear and spline interpolation support separates irregular AIS observations from simulation-ready vessel state.
Trajectory Segmentation
Extraction groups messages by vessel and divides time-ordered message streams into tracks. pytsa/tsea/split.py contains split-point logic applied to consecutive AIS messages, including the α-method described by the project’s associated publication. pytsa/tsea/search_agent.py selects supported extraction methods and controls whether segmentation is skipped; precomputed statistical resources supporting this process live under pytsa/data/quantiles/quantiles.pkl and pytsa/data/quantiles/independent/iquantiles.pkl.
Trajectory Assessment and Output Refinement
Inspector and composable negative-filter Recipe rules in pytsa/trajectories/inspect.py evaluate extracted tracks and partition results into accepted and rejected dict[MMSI, TargetShip] outputs. Built-in quality/filter rules are provided by pytsa/trajectories/rules.py; callers can supply custom track predicates. This forms a deliberate post-extraction quality boundary rather than embedding application-specific acceptance logic into splitting.
Runtime, Assets, and Visualization
The package runs on Python, with a Rust crate entry point at src/lib.rs and Cargo.toml, indicating a native-extension or performance-oriented integration boundary. Bundled geographic JSON assets under pytsa/data/geometry/ support regional geometry use cases. Optional result interpretation and plotting are separated into pytsa/visualization/ecdf.py and pytsa/visualization/misc.py; they consume trajectory/splitting outputs rather than participating in core extraction.
Main entry points: search_agent.py SearchAgent · ais_decoder.py AIS decoder · targetship.py TargetShip · split.py Track splitting
Components 18
Raw AIS decoding
Converts external AIS records into decoded CSV inputs.
- Raw AIS CSV · Date-named dynamic or static AIS records.
- AIS decoder · Decodes payloads with optional multiprocessing.
pytsa/decoder/ais_decoder.py - CSV schemas
pytsa/decoder/filedescriptor.py - Decoded dynamic/static CSVs · Persistent input boundary for search workflows.
Search and extraction
Builds vessel state and trajectories from decoded data.
- SearchAgent · Scopes decoded data to a bounding box and coordinates queries or extraction.
pytsa/tsea/search_agent.py - freeze query · Finds nearby ships for a TimePosition.
pytsa/tsea/search_agent.py - extract_all · Processes eligible MMSIs, optionally in parallel.
pytsa/tsea/search_agent.py - TargetShip · Encapsulates a vessel and its tracks.
pytsa/tsea/targetship.py - Interpolation · Produces interpolated position, speed, and course observations.
pytsa/tsea/targetship.py - Track splitting · Divides ordered vessel messages into tracks, including α-method splitting.
pytsa/tsea/split.py - Split quantiles · Precomputed resources for segmentation.
pytsa/data/quantiles/quantiles.pkl
Quality and interpretation
Assesses and optionally visualizes extracted trajectories.
- Inspector and Recipe · Partitions extracted vessels into accepted and rejected outputs.
pytsa/trajectories/inspect.py - Built-in quality rules
pytsa/trajectories/rules.py - Visualization · Plots and interprets trajectory or splitting results.
pytsa/visualization/ecdf.py
Other components
- Public API
pytsa/__init__.py - AIS domain model · Messages, tracks, bounding boxes, and time-position queries.
pytsa/structs.py - Geographic JSON assets · Regional geometry support.
- Rust native crate · Performance-oriented extension boundary.
src/lib.rs
Connections 21
- Public API → AIS domain model: exports
- Public API → AIS decoder: exposes
- Public API → SearchAgent: exposes
- Raw AIS CSV → AIS decoder: ingests
- CSV schemas → AIS decoder: defines columns
- AIS decoder → Decoded dynamic/static CSVs: writes
- Decoded dynamic/static CSVs → SearchAgent: loads
- AIS domain model → SearchAgent: uses queries and bounds
- Geographic JSON assets → SearchAgent: supports regional queries
- SearchAgent → freeze query: offers
- SearchAgent → extract_all: offers
- freeze query → TargetShip: returns
- extract_all → Track splitting: segments messages
- Split quantiles → Track splitting: supports α-method
- Track splitting → TargetShip: builds tracks
- TargetShip → Interpolation: observe()
- TargetShip → Inspector and Recipe: assesses extracted vessels
- Built-in quality rules → Inspector and Recipe: provides predicates
- Inspector and Recipe → Visualization: accepted/rejected results
- Track splitting → Visualization: splitting outputs
- Rust native crate → AIS decoder: native integration