phasecurvefit: Construct Paths through Phase-Space Points
Construct paths through phase-Space points, supporting many different algorithms.
Features
- JAX-powered: Fully compatible with JAX transformations (
jit,vmap,grad) - GPU-ready: Runs on CPU, GPU, or TPU via JAX
- Type-safe: Comprehensive (optionally runtime checked) type hints with
jaxtyping - Pluggable metrics: Customizable distance metrics for different physical interpretations
- Pluggable query strategies: Flexible neighbor search strategies (e.g., brute-force, KD-tree) to optimize performance
- Pluggable orderers: One interface over multiple ordering algorithms — the velocity-following walk and an MST backbone for near-closed loops
- Highly customizable ML setup and training: Well-chosen defaults with highly flexible customization for specific use-cases.
- Physical units: Optional support via
unxtfor unit-aware calculations
Installation
Install the core package:
pip install phasecurvefit[all]
Or with uv:
uv add phasecurvefit[all]
from source, using uv
uv add git+https://github.com/GalacticDynamics/phasecurvefit.git@main
You can customize the branch by replacing main with any other branch name.
building from source
cd /path/to/parent
git clone https://github.com/GalacticDynamics/phasecurvefit.git
cd phasecurvefit
uv pip install -e . # editable mode
Optional Dependencies
phasecurvefit has optional dependencies for extended functionality:
- unxt: Physical units support for phase-space calculations
- tree (jaxkd): Spatial KD-tree queries for large datasets
Install with optional dependencies:
# pip install phasecurvefit[all] # Install with all extras
pip install phasecurvefit[interop] # Install with unxt for unit support
pip install phasecurvefit[kdtree] # Install with jaxkd for KD-tree strategy
Or with uv:
# uv add phasecurvefit --extra all # installs all extras
uv add phasecurvefit --extra interop
uv add phasecurvefit --extra kdtree
Quick Start
import jax
import jax.numpy as jnp
import phasecurvefit as pcf
# Create phase-space observations as dictionaries (Cartesian coordinates)
pos = {
"x": jnp.array([0.0, 1.0, 2.0, 3.0, 4.0]),
"y": jnp.array([0.0, 0.5, 1.0, 1.5, 2.0]),
}
vel = {
"x": jnp.array([1.0, 1.0, 1.0, 1.0, 1.0]),
"y": jnp.array([0.5, 0.5, 0.5, 0.5, 0.5]),
}
# Step 1: Order the observations (use KD-tree for spatial neighbor prefiltering)
config = pcf.WalkConfig(strategy=pcf.strats.KDTree(k=3)) # k=3 for this small dataset
result = pcf.walk_local_flow(pos, vel, config=config, start_idx=0, metric_scale=1.0)
print(result.indices) # Initial ordering
# Step 2: Create normalizer and autoencoder
key = jax.random.key(0)
normalizer = pcf.nn.StandardScalerNormalizer(pos, vel)
autoencoder = pcf.nn.PathAutoencoder.make(
normalizer, gamma_range=result.gamma_range, key=key
)
# Step 3: Configure and run training
train_config = pcf.nn.TrainingConfig(
n_epochs_encoder=100, # Encoder-only epochs
n_epochs_both=50, # Joint training epochs
show_pbar=False, # Disable progress bar
)
# Train the autoencoder
result, _, losses = pcf.nn.train_autoencoder(
autoencoder, result, config=train_config, key=key
)
print(result.indices) # Post-training ordering
With Physical Units
When unxt is installed, you can use physical units throughout the workflow:
import jax
import jax.numpy as jnp
import phasecurvefit as pcf
import unxt as u
# Create phase-space observations with units
pos = {
"x": u.Q([0.0, 1.0, 2.0, 3.0, 4.0], "kpc"),
"y": u.Q([0.0, 0.5, 1.0, 1.5, 2.0], "kpc"),
}
vel = {
"x": u.Q([1.0, 1.0, 1.0, 1.0, 1.0], "km/s"),
"y": u.Q([0.5, 0.5, 0.5, 0.5, 0.5], "km/s"),
}
# Step 1: Order with units (units are preserved throughout)
metric_scale = u.Q(1.0, "kpc")
config = pcf.WalkConfig(strategy=pcf.strats.KDTree(k=3))
result = pcf.walk_local_flow(
pos,
vel,
config=config,
start_idx=0,
metric_scale=metric_scale,
usys=u.unitsystems.galactic,
)
# Step 2: Create normalizer and autoencoder (handles units automatically)
key = jax.random.key(0)
normalizer = pcf.nn.StandardScalerNormalizer(pos, vel)
autoencoder = pcf.nn.PathAutoencoder.make(
normalizer, gamma_range=result.gamma_range, key=key
)
result, _, losses = pcf.nn.train_autoencoder(
autoencoder, result, config=train_config, key=key
)
Orderers
The ordering step is pluggable. Every orderer implements the same interface —
order(positions, velocities) — and returns an OrderingResult that feeds the
autoencoder unchanged, so orderers are interchangeable:
LocalFlowOrderer— the velocity-following walk (wrapswalk_local_flow). Follows a coherent flow from a start point.MSTOrderer— a minimum-spanning-tree backbone. It needs no start point (the graph diameter finds the two tips itself), which makes it ideal for near-closed loops where the velocity field reverses and a single walk covers only one arm. Requires themstextra.
import jax.numpy as jnp
import phasecurvefit as pcf
# Points along a curve
t = jnp.linspace(0.0, 1.0, 60)
pos = {"x": 10.0 * t, "y": jnp.sin(3.0 * t)}
vel = {"x": jnp.ones(60), "y": 3.0 * jnp.cos(3.0 * t)}
# The velocity-following walk, via the orderer interface
walk_orderer = pcf.orderers.LocalFlowOrderer(metric_scale=1.0, start_idx=0)
walk_result = walk_orderer.order(pos, vel)
# The MST backbone (no start point needed)
mst_orderer = pcf.orderers.MSTOrderer(k=8, jump_cap=2.0)
mst_result = pcf.order(pos, vel, mst_orderer) # or mst_orderer.order(pos, vel)
# Either result feeds the autoencoder unchanged
print(mst_result.gamma_range) # (-1.0, 1.0)
MSTOrderer also has opt-in velocity mechanisms (velocity_weight,
sever_cos_threshold, orient_by_velocity) for self-overlapping streams. See
the
Orderers Guide
and the
Migration Guide.
Distance Metrics
The algorithm supports pluggable distance metrics to control how points are
ordered. The default metric is AlignedMomentumDistanceMetric, which combines
spatial proximity with velocity alignment:
import jax.numpy as jnp
import phasecurvefit as pcf
# Define simple Cartesian arrays (not quantities)
pos = {"x": jnp.array([0.0, 1.0, 2.0]), "y": jnp.array([0.0, 0.5, 1.0])}
vel = {"x": jnp.array([1.0, 1.0, 1.0]), "y": jnp.array([0.5, 0.5, 0.5])}
# Use default metric (AlignedMomentumDistanceMetric)
config = pcf.WalkConfig()
result = pcf.walk_local_flow(pos, vel, config=config, start_idx=0, metric_scale=1.0)
Using Different Metrics
phasecurvefit provides three built-in metrics:
- AlignedMomentumDistanceMetric (default): Combines spatial distance with velocity alignment (momentum-weighted nearest neighbor)
- FullPhaseSpaceDistanceMetric: True 6D Euclidean distance in phase space
- SpatialDistanceMetric: Pure spatial distance, ignoring velocity
import jax.numpy as jnp
import phasecurvefit as pcf
# Define simple Cartesian arrays (not quantities)
pos = {"x": jnp.array([0.0, 1.0, 2.0]), "y": jnp.array([0.0, 0.5, 1.0])}
vel = {"x": jnp.array([1.0, 1.0, 1.0]), "y": jnp.array([0.5, 0.5, 0.5])}
# Pure spatial ordering (ignores velocity)
config_spatial = pcf.WalkConfig(metric=pcf.metrics.SpatialDistanceMetric())
result = pcf.walk_local_flow(
pos, vel, config=config_spatial, start_idx=0, metric_scale=0.0
)
# Full 6D phase-space distance
config_phase = pcf.WalkConfig(metric=pcf.metrics.FullPhaseSpaceDistanceMetric())
result = pcf.walk_local_flow(
pos, vel, config=config_phase, start_idx=0, metric_scale=1.0
)
Custom Metrics
You can define custom metrics by subclassing AbstractDistanceMetric:
import jax
import jax.numpy as jnp
import phasecurvefit as pcf
class WeightedPhaseSpaceMetric(pcf.metrics.AbstractDistanceMetric):
"""Custom weighted phase-space metric."""
def __call__(self, current_pos, current_vel, positions, velocities, metric_scale):
# Compute position distance
pos_diff = jax.tree.map(jnp.subtract, positions, current_pos)
pos_dist_sq = sum(jax.tree.leaves(jax.tree.map(jnp.square, pos_diff)))
# Compute velocity distance
vel_diff = jax.tree.map(jnp.subtract, velocities, current_vel)
vel_dist_sq = sum(jax.tree.leaves(jax.tree.map(jnp.square, vel_diff)))
# Custom weighting scheme
return jnp.sqrt(pos_dist_sq + (metric_scale**2) * vel_dist_sq)
# Use custom metric via WalkConfig
config = pcf.WalkConfig(metric=WeightedPhaseSpaceMetric())
result = pcf.walk_local_flow(pos, vel, config=config, start_idx=0, metric_scale=1.0)
See the Metrics Guide for more details and examples.
Query Strategies
The algorithm supports pluggable query strategies to control how neighbors are found. A strategy determines which points are considered as potential next steps in the walk.
phasecurvefit provides two built-in strategies:
- BruteForce (default): Compute distances to all remaining points and select the nearest one. Efficient for small to medium datasets.
- KDTree: Use spatial KD-tree prefiltering to accelerate neighbor searches
for large datasets (requires optional
jaxkddependency).
Using Built-in Strategies
import jax.numpy as jnp
import phasecurvefit as pcf
# Define simple Cartesian arrays
pos = {"x": jnp.array([0.0, 1.0, 2.0]), "y": jnp.array([0.0, 0.5, 1.0])}
vel = {"x": jnp.array([1.0, 1.0, 1.0]), "y": jnp.array([0.5, 0.5, 0.5])}
# Default strategy (brute-force — no configuration needed)
config_brute = pcf.WalkConfig(strategy=pcf.strats.BruteForce())
result = pcf.walk_local_flow(
pos, vel, config=config_brute, start_idx=0, metric_scale=1.0
)
# KD-tree strategy for faster neighbor queries (large datasets)
config_kdtree = pcf.WalkConfig(strategy=pcf.strats.KDTree(k=2))
result = pcf.walk_local_flow(
pos, vel, config=config_kdtree, start_idx=0, metric_scale=1.0
)
Custom Query Strategies
You can define custom strategies by subclassing AbstractQueryStrategy:
import jax.numpy as jnp
import phasecurvefit as pcf
class SmallestIndexStrategy(pcf.strats.AbstractQueryStrategy):
"""Custom strategy: select the smallest unvisited index.
This is a toy example showing how to implement a custom strategy.
By returning uniform distances, argmin selects the smallest index
deterministically. In practice, distance-based strategies like BruteForce
are more useful.
"""
def init(self, positions, /, *, metadata):
"""No persistent state needed."""
return None
def query(
self,
state,
/,
current_pos,
current_vel,
positions,
velocities,
metric_fn,
metric_scale,
):
"""Return uniform distances to all points.
Since all distances are equal, the walk algorithm's argmin will
deterministically select the smallest unvisited index.
"""
# Get number of points
n_points = len(next(iter(positions.values())))
# Return uniform distances to all points
# argmin will pick the smallest unvisited index
distances = jnp.ones(n_points)
return pcf.strats.QueryResult(distances=distances, indices=None)
# Use custom strategy via WalkConfig
config = pcf.WalkConfig(strategy=SmallestIndexStrategy())
result = pcf.walk_local_flow(pos, vel, config=config, start_idx=0, metric_scale=1.0)
AI Usage Disclosure
Portions of this codebase (including tests and documentation) were refactored and generated with the assistance of Language Models. All AI contributions have been and will continue to be reviewed and verified by the human maintainers.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file phasecurvefit-0.3.0.tar.gz.
File metadata
- Download URL: phasecurvefit-0.3.0.tar.gz
- Upload date:
- Size: 3.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d93b5435f7a7c855caa3974ba155cefd8b8f56345c45ee041dca8623b72a0c1
|
|
| MD5 |
ecc1e994374ae1f2cbed77606406699f
|
|
| BLAKE2b-256 |
b727346ef0b6f28a062be3b4ed083ed5a101e7ec09124309a36f18c13c509611
|
Provenance
The following attestation bundles were made for phasecurvefit-0.3.0.tar.gz:
Publisher:
cd.yml on GalacticDynamics/phasecurvefit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phasecurvefit-0.3.0.tar.gz -
Subject digest:
5d93b5435f7a7c855caa3974ba155cefd8b8f56345c45ee041dca8623b72a0c1 - Sigstore transparency entry: 2164436665
- Sigstore integration time:
-
Permalink:
GalacticDynamics/phasecurvefit@5666b971e5af250004c43ced0a4104f91a40f38e -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/GalacticDynamics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@5666b971e5af250004c43ced0a4104f91a40f38e -
Trigger Event:
release
-
Statement type:
File details
Details for the file phasecurvefit-0.3.0-py3-none-any.whl.
File metadata
- Download URL: phasecurvefit-0.3.0-py3-none-any.whl
- Upload date:
- Size: 98.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
baec3363df58e3134be6da808db1ee1acc4c3cb9ca1cd369f297b7b67c263302
|
|
| MD5 |
451f7c3f4f4104d06f3c9c4b507e63ee
|
|
| BLAKE2b-256 |
c1b8665492c503de6969f84a2b1e9b50dacfd0806f890f8200bd89b1c5ce8ab0
|
Provenance
The following attestation bundles were made for phasecurvefit-0.3.0-py3-none-any.whl:
Publisher:
cd.yml on GalacticDynamics/phasecurvefit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phasecurvefit-0.3.0-py3-none-any.whl -
Subject digest:
baec3363df58e3134be6da808db1ee1acc4c3cb9ca1cd369f297b7b67c263302 - Sigstore transparency entry: 2164436675
- Sigstore integration time:
-
Permalink:
GalacticDynamics/phasecurvefit@5666b971e5af250004c43ced0a4104f91a40f38e -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/GalacticDynamics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@5666b971e5af250004c43ced0a4104f91a40f38e -
Trigger Event:
release
-
Statement type: