# nikpau/pytsa architecture

> Architecture diagram of the GitHub repository nikpau/pytsa, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nikpau/pytsa
- Repository: https://github.com/nikpau/pytsa

## Overview

#### 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: [`pytsa/tsea/search_agent.py`](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/search_agent.py) (SearchAgent), [`pytsa/decoder/ais_decoder.py`](https://github.com/nikpau/pytsa/blob/main/pytsa/decoder/ais_decoder.py) (AIS decoder), [`pytsa/tsea/targetship.py`](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/targetship.py) (TargetShip), [`pytsa/tsea/split.py`](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/split.py) (Track splitting)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_decode["Raw AIS decoding"]
  node_raw_csv["Raw AIS CSV<br/>external input"]
  node_decoder["AIS decoder<br/>decode coordinator<br/>[ais_decoder.py]"]
  node_file_schema["CSV schemas<br/>input/output schema<br/>[filedescriptor.py]"]
  node_decoded_csv["Decoded dynamic/static CSVs<br/>pipeline data"]
end

subgraph group_search["Search and extraction"]
  node_search_agent["SearchAgent<br/>workflow coordinator<br/>[search_agent.py]"]
  node_freeze["freeze query<br/>point-in-time flow<br/>[search_agent.py]"]
  node_extract["extract_all<br/>trajectory flow<br/>[search_agent.py]"]
  node_target_ship["TargetShip<br/>vessel state<br/>[targetship.py]"]
  node_interpolation["Interpolation<br/>state estimation<br/>[targetship.py]"]
  node_split_logic["Track splitting<br/>segmentation logic<br/>[split.py]"]
  node_quantiles["Split quantiles<br/>statistical resources<br/>[quantiles.pkl]"]
end

subgraph group_quality["Quality and interpretation"]
  node_inspector["Inspector and Recipe<br/>quality boundary<br/>[inspect.py]"]
  node_quality_rules["Built-in quality rules<br/>track predicates<br/>[rules.py]"]
  node_visualization["Visualization<br/>optional interpretation<br/>[ecdf.py]"]
end

node_public_api["Public API<br/>Python package surface<br/>[__init__.py]"]
node_domain["AIS domain model<br/>domain structures<br/>[structs.py]"]
node_geometry_assets["Geographic JSON assets<br/>bundled assets"]
node_rust_extension{{"Rust native crate<br/>native integration<br/>[lib.rs]"}}

node_public_api -->|"exports"| node_domain
node_public_api -->|"exposes"| node_decoder
node_public_api -->|"exposes"| node_search_agent
node_raw_csv -->|"ingests"| node_decoder
node_file_schema -->|"defines columns"| node_decoder
node_decoder -->|"writes"| node_decoded_csv
node_decoded_csv -->|"loads"| node_search_agent
node_domain -->|"uses queries and bounds"| node_search_agent
node_geometry_assets -.->|"supports regional queries"| node_search_agent
node_search_agent -->|"offers"| node_freeze
node_search_agent -->|"offers"| node_extract
node_freeze -->|"returns"| node_target_ship
node_extract -->|"segments messages"| node_split_logic
node_quantiles -.->|"supports α-method"| node_split_logic
node_split_logic -->|"builds tracks"| node_target_ship
node_target_ship -->|"observe&#40;&#41;"| node_interpolation
node_target_ship -->|"assesses extracted vessels"| node_inspector
node_quality_rules -->|"provides predicates"| node_inspector
node_inspector -->|"accepted/rejected results"| node_visualization
node_split_logic -.->|"splitting outputs"| node_visualization
node_rust_extension -.->|"native integration"| node_decoder

click node_public_api "https://github.com/nikpau/pytsa/blob/main/pytsa/__init__.py"
click node_domain "https://github.com/nikpau/pytsa/blob/main/pytsa/structs.py"
click node_decoder "https://github.com/nikpau/pytsa/blob/main/pytsa/decoder/ais_decoder.py"
click node_file_schema "https://github.com/nikpau/pytsa/blob/main/pytsa/decoder/filedescriptor.py"
click node_search_agent "https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/search_agent.py"
click node_freeze "https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/search_agent.py"
click node_extract "https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/search_agent.py"
click node_target_ship "https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/targetship.py"
click node_interpolation "https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/targetship.py"
click node_split_logic "https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/split.py"
click node_quantiles "https://github.com/nikpau/pytsa/blob/main/pytsa/data/quantiles/quantiles.pkl"
click node_inspector "https://github.com/nikpau/pytsa/blob/main/pytsa/trajectories/inspect.py"
click node_quality_rules "https://github.com/nikpau/pytsa/blob/main/pytsa/trajectories/rules.py"
click node_visualization "https://github.com/nikpau/pytsa/blob/main/pytsa/visualization/ecdf.py"
click node_rust_extension "https://github.com/nikpau/pytsa/blob/main/src/lib.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_raw_csv,node_decoder,node_file_schema,node_decoded_csv toneBlue
class node_search_agent,node_freeze,node_extract,node_target_ship,node_interpolation,node_split_logic,node_quantiles toneAmber
class node_inspector,node_quality_rules,node_visualization toneMint
class node_public_api,node_domain,node_geometry_assets,node_rust_extension toneNeutral
```

## Components

### Raw AIS decoding

Converts external AIS records into decoded CSV inputs.

- **Raw AIS CSV**: external input · Date-named dynamic or static AIS records.
- [AIS decoder](https://github.com/nikpau/pytsa/blob/main/pytsa/decoder/ais_decoder.py): decode coordinator · Decodes payloads with optional multiprocessing. · `pytsa/decoder/ais_decoder.py`
- [CSV schemas](https://github.com/nikpau/pytsa/blob/main/pytsa/decoder/filedescriptor.py): input/output schema · `pytsa/decoder/filedescriptor.py`
- **Decoded dynamic/static CSVs**: pipeline data · Persistent input boundary for search workflows.

### Search and extraction

Builds vessel state and trajectories from decoded data.

- [SearchAgent](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/search_agent.py): workflow coordinator · Scopes decoded data to a bounding box and coordinates queries or extraction. · `pytsa/tsea/search_agent.py`
- [freeze query](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/search_agent.py): point-in-time flow · Finds nearby ships for a TimePosition. · `pytsa/tsea/search_agent.py`
- [extract_all](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/search_agent.py): trajectory flow · Processes eligible MMSIs, optionally in parallel. · `pytsa/tsea/search_agent.py`
- [TargetShip](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/targetship.py): vessel state · Encapsulates a vessel and its tracks. · `pytsa/tsea/targetship.py`
- [Interpolation](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/targetship.py): state estimation · Produces interpolated position, speed, and course observations. · `pytsa/tsea/targetship.py`
- [Track splitting](https://github.com/nikpau/pytsa/blob/main/pytsa/tsea/split.py): segmentation logic · Divides ordered vessel messages into tracks, including α-method splitting. · `pytsa/tsea/split.py`
- [Split quantiles](https://github.com/nikpau/pytsa/blob/main/pytsa/data/quantiles/quantiles.pkl): statistical resources · Precomputed resources for segmentation. · `pytsa/data/quantiles/quantiles.pkl`

### Quality and interpretation

Assesses and optionally visualizes extracted trajectories.

- [Inspector and Recipe](https://github.com/nikpau/pytsa/blob/main/pytsa/trajectories/inspect.py): quality boundary · Partitions extracted vessels into accepted and rejected outputs. · `pytsa/trajectories/inspect.py`
- [Built-in quality rules](https://github.com/nikpau/pytsa/blob/main/pytsa/trajectories/rules.py): track predicates · `pytsa/trajectories/rules.py`
- [Visualization](https://github.com/nikpau/pytsa/blob/main/pytsa/visualization/ecdf.py): optional interpretation · Plots and interprets trajectory or splitting results. · `pytsa/visualization/ecdf.py`

### Other components

- [Public API](https://github.com/nikpau/pytsa/blob/main/pytsa/__init__.py): Python package surface · `pytsa/__init__.py`
- [AIS domain model](https://github.com/nikpau/pytsa/blob/main/pytsa/structs.py): domain structures · Messages, tracks, bounding boxes, and time-position queries. · `pytsa/structs.py`
- **Geographic JSON assets**: bundled assets · Regional geometry support.
- [Rust native crate](https://github.com/nikpau/pytsa/blob/main/src/lib.rs): native integration · Performance-oriented extension boundary. · `src/lib.rs`

## Connections

- 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
