Choose an orderer#
This guide shows how to pick the orderer that suits your curve. The MST backbone, The local-flow walk and The self-organizing map explain how each one works.
Start with the default#
pcf.order(pos, vel) with no orderer runs the default pipeline, an
MSTOrderer refined by a
SOMOrderer. It needs no start point and suits most
curves that don’t cross themselves. Pass another orderer as the third argument when
its mechanism fits your data better:
Orderer |
Best for |
Mechanism |
|---|---|---|
open streams, and curves that cross themselves where the velocity field stays coherent through the crossings |
a greedy walk that follows the velocity field from a start point |
|
near-closed loops, and streams with no known start point, for example where the velocity reverses at an unknown progenitor |
k-nearest-neighbour graph, minimum spanning tree, its longest path as the backbone, then arc length along it |
|
refining any initial ordering, and a continuous along-track coordinate |
a one-dimensional self-organizing map, a smooth backbone through its prototypes, then arc length along it |
|
density fields, such as an image of a stream, and wide or weighted point clouds |
a kernel density estimate of the weighted samples, the path along its ridge as the backbone, then arc length along it |
The walk and the MST complement each other. The walk needs a start point and follows the flow. Where the velocity reverses at a progenitor it has to start at the progenitor and walk both ways (Use the local-flow walk). The MST needs no start point, because the longest path of the tree finds the two ends itself.
Order with the local-flow walk#
Give the walk a start index and a metric_scale (Choose and tune the walk’s metric):
import jax.numpy as jnp
import phasecurvefit as pcf
pos = {"x": jnp.linspace(0.0, 5.0, 20), "y": jnp.zeros(20)}
vel = {"x": jnp.ones(20), "y": jnp.zeros(20)}
walk = pcf.orderers.LocalFlowOrderer(metric_scale=1.0, start_idx=0)
res = pcf.order(pos, vel, walk)
assert int(res.n_visited) == 20
Use the local-flow walk covers its options: direction, gaps and stopping conditions.
Order with the MST alone#
The MST orders by position by default:
res = pcf.order(pos, vel, pcf.orderers.MSTOrderer(k=5, jump_cap=1.0))
assert res.gamma_range == (-1.0, 1.0)
assert int(res.n_visited) == 20
kis the number of neighbours each sample links to.jump_capcuts edges longer than its value before the tree is built. Set it above the typical spacing between neighbouring samples and below the distance between separate arms or the two ends of a near-closed loop. It is an absolute length, and its default of 3.0 cuts every edge of a dataset whose spacing is larger than that.on_disconnectedchooses what happens when the neighbour graph falls into pieces, for example whenjump_capis too small or a gap in the stream is wider than thekneighbours reach:"raise"(the default) raises,"warn"orders the largest piece and warns,"largest"does the same silently, and"connect"joins the pieces along their shortest links and orders everything.
Use velocities in the MST#
Four options bring velocity into the MST. All are off in MSTOrderer() itself.
velocity_weightaddsvelocity_weight * (1 - cos(v_i, v_j))to each edge length, so arms that are close in position but move in opposite directions are not joined. Set it on the scale of the spacing between neighbouring samples.velocity_factoris the same penalty in units of the median distance between neighbours, so it needs no tuning to the data’s scale. The default pipeline usesvelocity_factor=30.sever_cos_thresholddrops edges whose velocity cosine is below the threshold. This cuts the seam where a near-closed loop’s velocity reverses, and the edges between branches at a crossing.orient_by_velocityflips the ordering so that \(\gamma\) increases along the mean velocity.
mst = pcf.orderers.MSTOrderer(
k=5, jump_cap=1.0, sever_cos_threshold=0.0, orient_by_velocity=True
)
res = pcf.order(pos, vel, mst)
On a curve that crosses itself many times, the MST needs these options to stay on its own branch, and the local-flow walk is usually the better choice (Ordering a Self-Intersecting Epitrochoid with the MST Backbone).
Refine with the SOM#
A SOMOrderer smooths an ordering it is handed.
Chain it after another orderer (Chain orderers); Refine an ordering with a self-organizing map covers its
options.
Order a density field#
For an image of a stream, make each pixel a sample weighted by its density
(Prepare your data) and order with a RidgeOrderer. It
follows the ridge of the weighted density, so it finds the middle of a wide or
blurred stream where the MST’s longest path wanders across it. It works out the
stream’s width from the data. Chain a SOM to refine it:
g = jnp.linspace(-1.5, 1.5, 61)
X, Y = jnp.meshgrid(g, g)
image = jnp.exp(-((jnp.hypot(X, Y) - 1.0) ** 2) / (2 * 0.1**2)) * (Y > 0)
keep = image.ravel() > 0.05 # each bright pixel becomes a sample
pix = {"x": X.ravel()[keep], "y": Y.ravel()[keep]}
pix_vel = {"x": jnp.zeros_like(pix["x"]), "y": jnp.zeros_like(pix["y"])}
ridge = pcf.orderers.RidgeOrderer() | pcf.orderers.SOMOrderer(
n_prototypes=20, metric=pcf.metrics.SpatialDistanceMetric()
)
res = pcf.order(pix, pix_vel, ridge, weights=image.ravel()[keep])
It doesn’t use velocities, so the direction of the ordering is arbitrary. The width
it uses, bandwidth, is the RMS distance of the samples from the ridge, found by
iterating from a wide first guess. Pass bandwidth= to set it yourself, for
example to smooth over clumps in a noisy image. It finds the ends to within about
one bandwidth.