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.1.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.1-cp311-cp311-win_amd64.whl (308.3 kB view details)

Uploaded CPython 3.11Windows x86-64

phaethon-0.4.1-cp311-cp311-manylinux_2_34_x86_64.whl (452.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

phaethon-0.4.1-cp311-cp311-macosx_11_0_arm64.whl (411.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

phaethon-0.4.1-cp311-cp311-macosx_10_12_x86_64.whl (416.9 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: phaethon-0.4.1.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.1.tar.gz
Algorithm Hash digest
SHA256 53991c2864931421aafa33ecfcea6730c73b96730c77322d176c2c39fe2397d4
MD5 3d89b0e5ff05d01f4a072f60b8e9a228
BLAKE2b-256 01d7b5f071cc54ca9bbe79a471db4d3c2645ca3a5042c3c7eb3a59ff5fe90829

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.1.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.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: phaethon-0.4.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 308.3 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.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 85800360384601584ded91511672d0c8059206980674ffce1871b631b7805b49
MD5 d996ee868dbdd77f68490659278d9660
BLAKE2b-256 b7a5479b40c9e4a2ff397dff9c253596acb592b9363b0c450505184b090a6167

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.1-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.1-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for phaethon-0.4.1-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 67e8c2b8ec2e953f748eba62eddd7ffe8d54960ce978f81ccf86488595a75e1f
MD5 7b646073482c6cb76aa35a67deef5cce
BLAKE2b-256 19d9c830dc6882ee2660ffd9aeb5ff5d2f904d3edc13cb123b1b4e7629c622b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.1-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.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phaethon-0.4.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 64862b5ea89b2e86b70bd7639c14916a9a47c9ee4dc12a43d8ca467d107daadd
MD5 b1343d0c8a169d455e81000982f3e0b2
BLAKE2b-256 a48ab6861cf7ee4b31e596b7ce7493e67424379419827a72a0fccc3cf14ba406

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.1-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.1-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for phaethon-0.4.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c8e895776e5d8699f4feae1ca203b9cfae8276b95ac15de0cc075a9461bade53
MD5 78719f628fe133d45c8d07e7e4106a2a
BLAKE2b-256 b7d41f659d2a65f9ccd4c521d956e6655847871003ab3b7facaff1b474e27fe8

See more details on using hashes here.

Provenance

The following attestation bundles were made for phaethon-0.4.1-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