Skip to main content

End-to-End Physics-Constrained Scientific Computing & Sci-ML Framework

Project description

Phaethon

Python NumPy PyTorch Pandas License

The End-to-End Physics-Constrained Scientific Computing & Sci-ML Framework.

Phaethon is a Python framework that unifies strict dimensional tensor algebra, Scientific Machine Learning (Sci-ML), and declarative data engineering. It is designed to ensure mathematical and physical integrity from the data ingestion layer to the final neural prediction.

📖 Documentation

For complete tutorials, API references, and architectural concepts, visit the official documentation:

📌 phaethon.readthedocs.io


⚡ Core Ecosystem

Phaethon builds directly upon the NumPy Array Protocol to operate as a zero-overhead native proxy, extending physical safety to modern ML ecosystems.

  1. Metaclass-Driven Algebra: Dynamic dimensional synthesis (e.g., u.Meter / u.Second -> MeterPerSecond). Inherently evaluates isomorphic firewalls, domain locks, and logarithmic math.
  2. Scientific Compute: Physics-aware linear algebra (ptn.linalg) and strictly bounded stochastic tensor generation (ptn.random).
  3. Classic Sci-ML: Equips Scikit-Learn with dimensional meta-estimators, axiom validators, and automated Buckingham Pi feature synthesis.
  4. Neural PDEs (PINNs): Deep PyTorch integration. The PTensor allows gradients to flow safely through physical laws using native calculus (pnn.grad, pnn.laplace).
  5. Hybrid Data Engineering: Normalizes raw CSVs/DataFrames and enforces physical limits at the ingestion layer using a Rust backend, bridging directly into PyTorch via .phx or .parquet formats.

⚛️ The Physics Engine: A Modern Alternative

Legacy unit libraries (like Pint or Astropy) rely on generic object wrappers and runtime string evaluation, resulting in high computational overhead and blind IDEs. Phaethon rebuilds dimensional analysis from the ground up using modern Python architecture:

  • Zero-Overhead NumPy Proxies (NEP 13/18): BaseUnit does not strictly subclass np.ndarray to avoid the brittle inheritance trap. Instead, it acts as a duck-typed protocol proxy, intercepting operations at the NumPy C-API layer. This allows it to natively fuse physics into floats, massive ndarrays, and even MaskedArrays without Python-level bottlenecks.
  • Metaclass JIT Caching: Physical dimensions are concrete Python classes. When dimensions interact algebraically, the _PhaethonUnitMeta dynamically synthesizes a new class blueprint in memory and caches it via LRU. Math operations operate at raw C-speed without string parsing.
  • Flawless Static Typing (DX): Because every dimension is a real class, IDEs (Mypy/Pylance) can strictly type-check physics out of the box (e.g., def calc(x: u.LengthUnit) will statically reject u.Kilogram).

Scientific Computing Example

Phaethon extends dimensional safety into advanced linear algebra and stochastic generation.

import phaethon as ptn
import phaethon.units as u

# 1. Zero-overhead NumPy tensor creation
mass_matrix = ptn.array([[10.0, 2.0], [3.0, 5.0]], u.Kilogram)
force_vector = u.Newton([50.0, 25.0]) # or just instantiate directly

# 2. Native Linear Algebra - Automatically synthesizes Acceleration (m/s²)
acceleration = ptn.linalg.solve(mass_matrix, force_vector)

# 3. Logarithmic Algebra & Isomorphic Firewalls natively supported
signal = u.DecibelMilliwatt(30) + u.DecibelMilliwatt(30)
# Evaluates precisely to ~33.01 dBm via implicit linearization

🚀 End-to-End Example: Physics-Informed Neural Networks

The following workflow demonstrates Phaethon's end-to-end capability: parsing raw sensor data, extracting it into a dimension-aware dataset, and training a neural network to solve Burgers' Equation while mathematically guaranteeing dimensional integrity.

Step 1: Ingesting Data (Schema)

Normalize chaotic inputs, apply boundary constraints, and extract data into PyTorch-ready tensors.

import pandas as pd
import phaethon as ptn
import phaethon.units as u

# Declaratively enforce physical laws on incoming data
class SensorSchema(ptn.Schema):
    x_pos: u.Meter = ptn.Field("Position", parse_string=True)
    t_time: u.Second = ptn.Field("Time", min=0.0, on_error="clip")

# Load and normalize a raw DataFrame
messy_df = pd.read_csv("sensor_telemetry.csv")
clean_df = SensorSchema.normalize(messy_df)

# Bridge directly into PyTorch PTensors (Physics-Aware Tensors)
dataset = SensorSchema.astensor(clean_df, requires_grad=['x_pos', 't_time'])

x_sensor = dataset['x_pos'].tensor
t_sensor = dataset['t_time'].tensor

Step 2: Neural Architecture (Assembly)

Standard PyTorch layers cannot digest physical metadata. Phaethon's assemble securely standardizes and strips the units, feeding pure features to the network before resurrecting the physics on the output.

import torch.nn as nn
import phaethon.pinns as pnn

class ShockwaveNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(2, 128), nn.SiLU(),
            nn.Linear(128, 128), nn.SiLU(),
            nn.Linear(128, 1)
        )
        
    def forward(self, x_raw, t_raw):
        # 1. Standardize inputs to base units safely
        x_std = x_raw.asunit(u.Meter)
        t_std = t_raw.asunit(u.Second)
        
        # 2. Assemble physical tensors into naked PyTorch features
        features = pnn.assemble(x_std, t_std, dim=-1)
        
        # 3. Forward Pass & Resurrect Physical Identity (Velocity)
        u_mag = self.net(features)
        return pnn.PTensor(u_mag, unit=u.MeterPerSecond), x_std, t_std

Step 3: Native Calculus & Loss Evaluation

Train the network using native differential calculus. Phaethon automatically synthesizes derivative units, and the ResidualLoss strictly ensures dimensional balance on the PDE.

model = ShockwaveNet()
pde_loss_fn = pnn.ResidualLoss()
nu = pnn.PTensor(0.01 / 3.14159, unit=u.SquareMeterPerSecond)

# Forward pass
u_pred, x_s, t_s = model(x_sensor, t_sensor)

# Native PyTorch Calculus (Automatically synthesizes derived units)
du_dt = pnn.grad(u_pred, t_s, create_graph=True)
du_dx = pnn.grad(u_pred, x_s, create_graph=True)
d2u_dx2 = pnn.laplace(u_pred, x_s)

# Formulate the PDE Residual (Burgers' Equation)
# If dimensions mismatch (e.g., subtracting Force from Acceleration), execution halts.
pde_residual = du_dt + (u_pred * du_dx) - (nu * d2u_dx2) 

# Compute Physics-Informed Penalty
loss = pde_loss_fn(pde_residual, target=0.0)
loss.backward()

📦 Installation & Modularity

Phaethon requires Python >= 3.11 and is compiled via Maturin (Rust backend). Install only the scientific stack you need to avoid environment bloat:

# Core Physics Engine Only
pip install phaethon

# Data Engineering (Pandas + Rapidfuzz)
pip install 'phaethon[dataframe]'

# Neural PDEs & PINNs (PyTorch)
pip install 'phaethon[torch]'

# Classic Sci-ML (Scikit-learn)
pip install 'phaethon[sklearn]'

# The Complete Sci-ML Ecosystem
pip install 'phaethon[all]'

🤝 Contributing

Contributions from the open-source community are greatly appreciated. Whether you are adding a new physical domain, optimizing the Rust backend, or expanding the Scikit-Learn transformers, your help is welcome.

Please read our CONTRIBUTING.md to set up your development environment, install the [dev] dependencies, and run the test suite.

📜 License

Distributed under the MIT License. See the LICENSE file for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

phaethon-0.4.2.tar.gz (331.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

phaethon-0.4.2-cp311-cp311-win_amd64.whl (308.4 kB view details)

Uploaded CPython 3.11Windows x86-64

phaethon-0.4.2-cp311-cp311-manylinux_2_34_x86_64.whl (452.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

phaethon-0.4.2-cp311-cp311-macosx_11_0_arm64.whl (411.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

phaethon-0.4.2-cp311-cp311-macosx_10_12_x86_64.whl (417.0 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file phaethon-0.4.2.tar.gz.

File metadata

  • Download URL: phaethon-0.4.2.tar.gz
  • Upload date:
  • Size: 331.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for phaethon-0.4.2.tar.gz
Algorithm Hash digest
SHA256 5f39c158cdd3570aa2ce697ba508b4307a6d60233cae04ca4eb9f64b2b96747a
MD5 e1a0ac5dcbdd03fb1fad0efef83cc6e9
BLAKE2b-256 1fbd938bbcdd6e6344a780a1076ad356974a859c07fe756e1f6586d1584ec878

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.2.tar.gz:

Publisher: publish.yml on rannd1nt/phaethon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phaethon-0.4.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: phaethon-0.4.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 308.4 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for phaethon-0.4.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f9e2d633f0b7e620a652788f981a37e69be7b4fdba6a8da24d07e7003f3fc290
MD5 56d36e9c5acb0c624c7131493b57407c
BLAKE2b-256 82bb61f7408d9a454d9b6701d8c14439302392f49a4ebb7bb8ef97f953853750

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.2-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on rannd1nt/phaethon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phaethon-0.4.2-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for phaethon-0.4.2-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4ce20a00e11cc4c70ae27be28cfb95da1513439420655cb6b91b9d453eec3609
MD5 1d1670871d77a02ae220541196d0729f
BLAKE2b-256 531867e5341c0e7c08f9d8ae25fa33a7a2b66be406800040c6158486b2b2bfeb

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.2-cp311-cp311-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on rannd1nt/phaethon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phaethon-0.4.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phaethon-0.4.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1769960bde30015fb2fa22192f834f0b1a755eff41a5a446224598b13fd85275
MD5 8df35b617d07fa4c12c4a1954c19045f
BLAKE2b-256 46cb1a9289684faed90d3c02a5413e86a1b6a7c0efd19f90bd19210f06327eea

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.2-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish.yml on rannd1nt/phaethon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file phaethon-0.4.2-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for phaethon-0.4.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a4542ab23087e8dfd5edb30f3d8aac945731333f72e86a4602611a90e7a174a4
MD5 ace57eed9ffdee2aa76e222d7d2ea0b9
BLAKE2b-256 10a4afa6eba2a321d5166441d09059ed6ee22ea44f746512a33834b82be03fb5

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.2-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: publish.yml on rannd1nt/phaethon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page