Migrating from v0.3 to v0.4#
In v0.4 the default ordering pipeline is an MST backbone refined by a SOM.
pcf.order(positions, velocities) with no orderer used to run the
velocity-following local-flow walk (in v0.3.1 and v0.3.2; v0.3.0 required an
orderer argument); it now runs
default_pipeline(), i.e.
MSTOrderer(...) | SOMOrderer(...). This is a breaking change to the package’s
primary entry point. Code that passes an orderer explicitly is unaffected.
v0.4 also removes pcf.walk_local_flow, which has warned since v0.3.1; see
below.
Warning
If you call pcf.order(pos, vel) without an orderer, your results change. If you
call pcf.walk_local_flow, it no longer exists. In both cases, to get the v0.3.1
walk, name it yourself:
pcf.order(pos, vel, pcf.orderers.LocalFlowOrderer())
What changed, and why#
The SOM is a smoothing stage: its prototypes average over many tracers, so the backbone it produces is far less sensitive to local noise than a greedy walk’s step-by-step choices. The MST ahead of it needs no progenitor or start index — the graph diameter finds the two ends of the curve itself — and handles near-closed loops where the velocity field reverses. Together they work on more data with fewer decisions from you, which is what a default should do. See the Orderers guide and the SOM guide.
Keeping the walk#
The v0.3.1 default is one explicit argument away, and reproduces it exactly:
import jax.numpy as jnp
import phasecurvefit as pcf
pos = {"x": jnp.array([0.0, 1.0, 2.0, 3.0, 4.0])}
vel = {"x": jnp.ones(5)}
# v0.3.1: the default. v0.4: name it.
walk = pcf.order(pos, vel, pcf.orderers.LocalFlowOrderer())
assert isinstance(walk, pcf.WalkLocalFlowResult)
assert walk.gamma_range == (0.0, 1.0)
Walk parameters (start_idx, metric_scale, direction, config, …) live on
LocalFlowOrderer(...), exactly as they did for v0.3’s explicit form.
What the new default returns#
v0.3.1 default (walk) |
v0.4 default (MST | SOM) |
|
|---|---|---|
Result type |
|
|
|
|
|
Needs a start index |
yes ( |
no |
|
|
a fitted polyline, which |
|
distance along the walk’s path |
arc length along the fitted backbone |
Tracers left unvisited |
those the walk skipped |
none: a gap in the stream is bridged, not dropped |
|
yes |
no — see below |
Things to check in code that consumed the old default:
Type checks.
isinstance(result, WalkLocalFlowResult)is now false.WalkLocalFlowResultsubclassesOrderingResult, so annotate and check againstOrderingResultto accept either.init=. The v0.3.1 default took its walk start index frominit; the new default has no use for one, sopcf.order(pos, vel, init=...)with no orderer raises aValueErrorrather than ignore it. Pass an explicit orderer to useinit.Gaps in the stream. A gap wider than the
k=10neighbours reach splits the neighbour graph. The default bridges the pieces along their shortest links (on_disconnected="connect"), so no side of the stream is dropped; the bridge can be long, so look at the ordering across the gap. Pass your ownMSTOrderer(..., on_disconnected=...)chain for the other policies.A hard-coded
gamma_range. Read it off the result (result.gamma_range) rather than assuming(0.0, 1.0); the autoencoder already does (PathAutoencoder.make(..., gamma_range=result.gamma_range)).Direction. The default orients
gammaalong the mean velocity (orient_by_velocity=True), so a stream ordered “with the flow” still is.Cost. The SOM adds
n_epochs(default 10) batch-Kohonen passes over the visited tracers, soorder()is no longer free to call in a tight loop.
Small inputs fall back to the MST#
The SOM needs at least n_prototypes (default 15) tracers to bin. Rather than
raise on small inputs — the library’s own examples are five points — the default
skips the SOM stage and returns the MST ordering alone:
import jax.numpy as jnp
import phasecurvefit as pcf
pos = {"x": jnp.array([0.0, 1.0, 2.0])}
vel = {"x": jnp.array([1.0, 1.0, 1.0])}
result = pcf.order(pos, vel) # 3 < 15 tracers: MST alone
assert result.gamma_range == (-1.0, 1.0)
assert result.indices.tolist() == [0, 1, 2]
Both stages report gamma_range == (-1.0, 1.0), so the fallback is invisible to
code that reads it off the result.
The default traces under jit, vmap and grad#
pcf.order(pos, vel) with no orderer runs inside jax.jit, jax.vmap and
jax.grad, as the walk default did in v0.3.1. So does any chain that ends in a
SOM, such as MSTOrderer() | SOMOrderer(). Samples an earlier stage left unvisited
stay unvisited. An explicit orderer traces too:
import jax
import jax.numpy as jnp
import phasecurvefit as pcf
t = jnp.linspace(0.0, jnp.pi, 40)
pos = {"x": 5.0 * jnp.cos(t), "y": 5.0 * jnp.sin(t)}
vel = {"x": -jnp.sin(t), "y": jnp.cos(t)}
walk = pcf.orderers.LocalFlowOrderer()
chord = jax.jit(lambda q, p: pcf.order(q, p, walk).chord)(pos, vel)
See the SOM guide and the JAX Integration guide.
Tuning the default#
The default’s MST stage is configured to work on data of any scale, not to be
tuned: jump_cap=inf, velocity_factor=30.0, orient_by_velocity=True,
on_disconnected="connect". It
deliberately does not use MSTOrderer()’s own defaults — jump_cap=3.0 is an
absolute length, and severs every edge of a sparse or large-scale dataset.
default_pipeline takes n_prototypes and any other
SOMOrderer keyword. For anything else — a finite
jump_cap, velocity-aware edges, outlier rejection with edge_clip_sigma — build
the chain and pass it:
import jax.numpy as jnp
import phasecurvefit as pcf
t = jnp.linspace(0.0, jnp.pi, 60)
pos = {"x": 5.0 * jnp.cos(t), "y": 5.0 * jnp.sin(t)}
vel = {"x": -jnp.sin(t), "y": jnp.cos(t)}
chain = pcf.orderers.MSTOrderer(
k=8, jump_cap=2.0, edge_clip_sigma=3.0, orient_by_velocity=True
) | pcf.orderers.SOMOrderer(n_prototypes=12)
result = pcf.order(pos, vel, chain)
assert int(result.n_visited) == 60
New in v0.4#
SOMOrdererand thephasecurvefit.sommodule: a Self-Organizing Map ordering stage (Starkman et al. 2023).a | bandChainOrderer: run orderers in sequence, each receiving the previous result asinit.default_pipeline(): the function behind the new default.fit_track(): order, refine, and train the autoencoder in one call (it uses the default pipeline).MSTOrderer(velocity_factor=...): the velocity penalty ofvelocity_weightin units of the median neighbour distance, so it needs no unit. The default pipeline setsvelocity_factor=30.0, so on a stream that crosses itself it crosses straight over instead of turning onto the other branch, and its SOM stage then uses velocities too. Earlier v0.4 development builds ordered on position alone; for that behaviour, chainMSTOrderer(jump_cap=float("inf"), orient_by_velocity=True, on_disconnected="connect") | SOMOrderer()yourself.
walk_local_flow is removed#
pcf.walk_local_flow, deprecated since v0.3.1, is gone. Its replacement is the
orderer, with the walk’s keyword arguments moved onto LocalFlowOrderer(...) — see
Migrating from v0.2 to v0.3:
# v0.3 (deprecated, warns):
result = pcf.walk_local_flow(pos, vel, start_idx=0, metric_scale=1.0)
# v0.4:
result = pcf.order(
pos, vel, pcf.orderers.LocalFlowOrderer(start_idx=0, metric_scale=1.0)
)
Note that pcf.order(pos, vel) with no orderer is not a replacement: it runs the
default MST | SOM pipeline. The walk’s usys keyword for Quantity inputs is passed
as metadata=pcf.StateMetadata(usys=...) to order. combine_results,
order_w and WalkLocalFlowResult are unchanged.
Dependency floors#
v0.4 requires unxt>=2.0 (was >=1.10.4) for the interop extra, and the
tutorials extra requires galax>=0.1.0 (was >=0.0.3). unxt 2 no longer
converts a dimensionful Quantity to a bare array with np.asarray; strip it in a
unit you name, e.g. u.ustrip("kpc", q), or in a unit system’s unit for its
dimension, e.g. u.ustrip(usys, q). To plot Quantities directly with matplotlib, install
unxt[interop-mpl], which the tutorials extra includes.
v0.4 also requires jaxmore>=0.6.1 (was >=0.4.0). The pcf.nn training loops
use jaxmore’s compiled training loop, so a repeat training call with the same
shapes and settings reuses its compiled program instead of compiling again.
Compatibility summary#
Symbol |
Status |
|---|---|
|
Changed: runs |
|
Unchanged |
|
Unchanged |
|
Removed (deprecated since v0.3.1); use |
|
Unchanged; subclasses |
|
Unchanged |
|
New |