How rannd1nt/phaethon works
Purpose and Public Surface
Phaethon is a Python 3.11+ physics-constrained scientific-computing and Sci-ML library. It combines dimension-aware NumPy algebra, validated tabular ingestion, scikit-learn integration, and PyTorch PINNs. Main package entry points are src/phaethon/__init__.py, with focused public facades at src/phaethon/units.py, src/phaethon/linalg.py, src/phaethon/random.py, src/phaethon/ml.py, and src/phaethon/pinns.py.
Dimensional Algebra Core
The unit system defines physical dimensions as Python types and supports derived-unit synthesis, compatibility checks, conversions, and domain-specific units. The base algebra/runtime is centered on src/phaethon/core/base.py, src/phaethon/core/axioms.py, and src/phaethon/core/units/__init__.py; concrete domains are distributed under src/phaethon/core/units/ (for example, length.py, force.py, and thermodynamics.py). src/phaethon/axiom.py exposes the constraint/axiom layer publicly.
Numerical Compute Boundary
Dimension-carrying values integrate with NumPy’s array protocols rather than requiring ndarray inheritance. Core tensor/array behavior and physics-aware math reside in src/phaethon/core/tensor.py, src/phaethon/core/linalg.py, and src/phaethon/core/random.py. Flow is: callers construct or convert values with a unit, algebra propagates/synthesizes units, and specialized linear-algebra or random operations preserve valid output dimensions.
Declarative Data Pipeline
Schemas turn external tabular data into normalized, physically validated datasets and tensor-ready columns. The public model is implemented in src/phaethon/core/schema.py, src/phaethon/core/dataset.py, and src/phaethon/core/semantics.py. Data-engine adapters are separated behind src/phaethon/core/backends/protocol.py, with equivalent Pandas and Polars implementations under src/phaethon/core/backends/pd_engine/engine.py and src/phaethon/core/backends/pl_engine/engine.py. This is the boundary for CSV/DataFrame inputs, field localization/conversion, validation, and imputation.
Rust Acceleration and I/O
A Maturin-built Rust extension supplies performance-sensitive backend functionality, with Rust crate definitions in Cargo.toml and native module code in rust_src/lib.rs. Python-side data I/O is coordinated by src/phaethon/core/io.py. The intended interchange boundary includes .phx and Parquet-oriented workflows, allowing normalized data to move toward tensor and ML stages without losing semantic metadata.
Sci-ML Adapters
Classic ML support wraps or augments scikit-learn-style workflows with dimension-aware estimators, feature generation, preprocessing, metrics, and selection. Its architectural surface is src/phaethon/core/ml/estimator.py, src/phaethon/core/ml/features.py, and src/phaethon/core/ml/preprocessing.py. This layer consumes validated numerical/semantic data and produces ML-compatible feature and estimator flows while enforcing physical constraints.
PINN and PyTorch Path
The PINN subsystem bridges physical units into autograd-compatible PyTorch tensors. src/phaethon/core/pinns/tensor.py provides PTensor; src/phaethon/core/pinns/calculus.py implements unit-aware derivatives; and src/phaethon/core/pinns/loss.py enforces dimensionally valid residual losses. Typical flow is schema dataset → PTensor inputs → unit-stripped/assembled neural features → unit-restored predictions → calculus operators → residual loss → standard PyTorch backpropagation.
Runtime Dependencies and Delivery
The material runtimes are Python, NumPy, optional Pandas/Polars, optional PyTorch, optional scikit-learn, and compiled Rust. Packaging and optional dependency groups are defined in pyproject.toml; CI and package publication are automated through .github/workflows/ci.yml and .github/workflows/publish.yml. Documentation is built for ReadTheDocs via .readthedocs.yaml.
Main entry points: base.py Unit runtime · schema.py Schema & semantics · dataset.py Normalized dataset · protocol.py Backend protocol
Components 24
Public API
- Phaethon package · Top-level package surface.
src/phaethon/__init__.py - Focused facades · Units, linalg, random, ML, and PINN public modules.
src/phaethon/units.py - Axiom API
src/phaethon/axiom.py
Dimensional Core
- Unit runtime · Defines dimension-aware values and derived-unit behavior.
src/phaethon/core/base.py - Constraints & axioms
src/phaethon/core/axioms.py - Unit catalog · Base and domain-specific unit definitions.
src/phaethon/core/units/__init__.py - Dimensioned tensors · Uses NumPy array protocols without ndarray inheritance.
src/phaethon/core/tensor.py - Linalg & random ops · Preserves valid output dimensions.
src/phaethon/core/linalg.py
Data Pipeline
- Schema & semantics · Localizes, converts, and validates physical fields.
src/phaethon/core/schema.py - Normalized dataset · Tensor-ready columns retaining semantic metadata.
src/phaethon/core/dataset.py - Backend protocol
src/phaethon/core/backends/protocol.py - Pandas engine
src/phaethon/core/backends/pd_engine/engine.py - Polars engine
src/phaethon/core/backends/pl_engine/engine.py - Semantic I/O · Coordinates .phx and Parquet-oriented workflows.
src/phaethon/core/io.py
Sci-ML
- Dimension-aware estimators · Constrained estimator workflows.
src/phaethon/core/ml/estimator.py - Features & preprocessing · Builds physically valid features and preprocessing flows.
src/phaethon/core/ml/features.py - PTensor bridge · Bridges units into autograd-compatible tensors.
src/phaethon/core/pinns/tensor.py - Unit-aware calculus
src/phaethon/core/pinns/calculus.py - Residual loss · Enforces dimensionally valid residuals before PyTorch backpropagation.
src/phaethon/core/pinns/loss.py
Native Build & Delivery
- Rust extension · Maturin-built performance-sensitive backend module.
rust_src/lib.rs - Package configuration · Defines optional runtime dependency groups and native build.
pyproject.toml - Rust crate
Cargo.toml - CI
.github/workflows/ci.yml - Publishing
.github/workflows/publish.yml
Connections 25
- Phaethon package → Focused facades: exports
- Phaethon package → Axiom API: exports
- Focused facades → Unit runtime: uses
- Axiom API → Constraints & axioms: exposes
- Unit runtime → Unit catalog: resolves units
- Unit runtime → Constraints & axioms: checks constraints
- Dimensioned tensors → Unit runtime: carries dimensions
- Linalg & random ops → Dimensioned tensors: operates on
- Schema & semantics → Unit runtime: converts fields
- Schema & semantics → Normalized dataset: normalizes into
- Backend protocol → Pandas engine: implemented by
- Backend protocol → Polars engine: implemented by
- Pandas engine → Schema & semantics: ingests tabular data
- Polars engine → Schema & semantics: ingests tabular data
- Semantic I/O → Normalized dataset: reads and writes
- Normalized dataset → Features & preprocessing: supplies validated data
- Features & preprocessing → Dimension-aware estimators: feeds features
- Normalized dataset → PTensor bridge: becomes inputs
- PTensor bridge → Unit-aware calculus: differentiated by
- Unit-aware calculus → Residual loss: produces residuals
- Rust extension → Backend protocol: accelerates backend work
- Package configuration → Rust extension: builds with Maturin
- Rust crate → Rust extension: defines crate
- CI → Package configuration: validates
- Publishing → Package configuration: publishes