Skip to main content

STreamer INfall with Gradients

Project description

STING

STreamer INfall with Gradients

PyPI version codecov License: MIT Python 3.12 Python 3.13 Python 3.14

STING (STreamer INfall with Gradients) is a Python package for quickly fitting streamline models to molecular line position-position-velocity (PPV) data of asymmetric infalling material around young protostars. It uses a gradient descent method, powered by JAX and Optax, and can find best-fit streamline parameters of a streamer, and their uncertainties, in 10-20 seconds on a normal astronomy laptop (e.g. MacBook).

The streamline model used is the analytic solutions of Mendoza et al. (2009).

What does STING do?

With STING you can:

  1. Extract a 1D streamline from a spectral cube. Given a PPV cube containing candidate streamer emission, and the coordinates of its protostar, STING reduces the PPV cube to a set of npoints representative (RA offset, Dec offset, velocity) points with corresponding uncertainties
  2. Model a streamline. The streamline model used by STING is taken from Mendoza et al. (2009) — a ballistic model of rotating infall, dominated by the gravitational force of a sink mass at the star position.
  3. Quickly fit the model to your data. Streamline parameters are optimised with the Adam optimiser to minimise a chi-squared loss between the model and the observed streamline. Current parameters supported for optimisation: r0, theta0, phi0, v_r0, omega or rc, mass, inc, pa. You can optimise any combination of these.
  4. Quantify uncertainties. Parameter uncertainties are estimated from the Hessian of the loss at the best-fit point, and propagated into the model (e.g. as "streamline spaghetti" plots).
  5. Visualise the result. Pre-built plotting functions include: morphology, velocity vs. projected radius on sky plane, loss curves, parameter correlation and uncertainty sampling, and per-epoch animations of the fit converging.

Installation

It is recommended to install STING in a virtual environment:

python -m venv /path/to/new/venv
source /path/to/new/venv/bin/activate

pip (recommended)

The quickest way to install STING is

pip install sting

which also installs all of STING's dependencies automatically.

If you want to install with additional development dependencies (e.g. for running the test suite):

pip install sting[dev]

from source

git clone https://github.com/Lauren4476/sting.git
cd sting
pip install .

installs STING and all of its dependencies.

Notes on dependencies

STING requires Python ≥ Python 3.12.

Dependencies (installed automatically when using either of the methods above):

  • numpy>=2.0.2
  • astropy>=7.0.0
  • jax>=0.9.0
  • optax>=0.2.6
  • matplotlib>=3.9.0
  • pandas>=2.2.0
  • spectral_cube>=0.6.6

Quick start

A full worked example with data is provided in examples/sting_example_run.ipynb. The core workflow is:

# --- Imports ---
from astropy import units as u
from astropy.io import fits
from astropy.coordinates import SkyCoord
from spectral_cube import SpectralCube
from sting import extract_streamline, gradient_descent, outputs

# --- Settings ---
star_position = SkyCoord("3h28m55.569s", "+31d14m37.025s", frame='fk5')
distance = 293        # distance to protostar star in parsecs
v_lsr = 7.5           # km/s, systemic velocity

# --- 1. Extract 1D streamline from the cube ---
hdu = fits.open("data/example_streamer_cluster_data.fits")[0]
cube = SpectralCube.read(hdu).with_spectral_unit(
    u.km / u.s, rest_value=hdu.header["RESTFRQ"] * u.Hz
)
streamer_cube = extract_streamline.extract_streamer_subcube(
    cube,
    vmin=6 * u.km / u.s, vmax=8 * u.km / u.s,
    xmin=-5 * u.arcsec, xmax=5 * u.arcsec,
    ymin=-12 * u.arcsec, ymax=0.5 * u.arcsec,
    rms_thresh=4,
)
streamer = extract_streamline.reduce_to_1D(streamer_cube, star_position, n_elements=10)

# --- 2. Set an initial guess ---
# parameters you want STING to optimise
initial_opt_params = {
    'r0': 1500.0 * u.au,
    'theta0': 40.0 * u.deg,
    'phi0': 100.0 * u.deg,
    'omega': 5e-13 * (1 / u.s),
    'v_r0': 0.1 * u.km / u.s,
    'mass': 4.0 * u.Msun,
    'inc': -45 * u.deg,
    'pa': 194 * u.deg
}
# parameters you want to keep fixed
fixed_params = {
    'rmin': 50.0 * u.au,
    'deltar': 30.0 * u.au,
    'v_lsr': v_lsr * u.km / u.s,
}
model_params, initial_opt_params, fixed_params = gradient_descent.prepare_model_params(
    initial_opt_params, fixed_params
)

# --- 3. Set bounds for 'r0', 'mass', if any of these are in initial_opt_params ---
param_bounds = {
    'r0': (200.0, 10000.0) * u.au,
    'mass': (3.0, 5.0) * u.Msun,
}

# --- 4. (optional) It is recommended to set priors for 'mass', 'inc', 'pa' if any of these are in initial_opt_params, to help break degeneracy ---
# format 'parameter': (mean, sigma) * u.Unit
priors = {
    'mass': (4.0, 1.0) * u.Msun,
    'inc': (-45, 10) * u.deg,
    'pa': (194, 10) * u.deg
}

# --- 5. Fit the streamline ---
best_opt_params, loss_history, param_errors = gradient_descent.fit_streamline(
    initial_opt_params,
    fixed_params,
    streamer,
    distance,
    param_bounds=param_bounds,
    priors=priors
    n_epochs=1000,
    v_lsr=v_lsr,
    save_folder="sting_results",
)

This saves best-fit parameters, an optimisation log, and diagnostic plots to sting_results/. From there, the outputs module offers further plotting and analaysis tools.

Recommended use case (best practice)

STING is intended for fitting kinematic streamer models to interferometric molecular line observations (e.g. ALMA, NOEMA) of young stellar objects with asymmetric infall candidates visible in PPV space. It is best suited to:

  • High spectral and spatial resolution observations (~0.1 km/s, ~300 au resolution or better)
  • Sources with a well-constrained distance and systemic velocity
  • Sources with some prior constraints on sink mass, inclination, and position axis.
  • Streamer candidates that have already been isolated from the rest of the cube by preprocessing.

Priors

It is possible to run STING without specifying a priors argument. However, PPV observations only contain data in 3 dimensions (RA, Dec, LOS velocity), while the streamline model has 6 dimensions (3D position, 3D velocity). As a result, some parameter combinations are "coupled" and may be only weakly constrained by the observations.

Common degeneracies:

  • sink mass, mass, and angular velocity, omega or centrifugal radius, rc
  • source inclination, inc, and streamline initial polar angle, theta0
  • source position angle, pa, and streamline starting azimuthal angle, phi0

As a result, when both parameters in one of these pairs are allowed to vary optimisation may converge more slowly as the optimiser drifts along a nearly-flat direction, and uncertainty estimates will become large.

STING supports Gaussian priors on any optimisable parameter, specified as (mean, sigma). These are included in the loss function as additional chi-squared penalty terms, penalising the optimiser for adjusting parameters further from the prior mean. Where independent constraints are available from previous work, I recommend applying priors to mass, inc, and pa. This helps constrain otherwise degenerate parameter combinations and reduces uncertainties.

Streamer preprocessing

STING's extract_streamline module assumes you hand it a cube that already contains mostly streamer emission — it only applies simple velocity/spatial cuts and an RMS threshold (in extract_streamer_subcube). It cannot yet separate streamer emission from overlapping envelope, outflow, or disk emission for you. (stay tuned)

The preprocessing steps I use and recommend to isolate a pure streamer cube for input to STING are:

  • Multi-Gaussian spectral fitting for the spectrum at each pixel in the original PPV cube to separate kinematically distinct components (e.g. streamer vs. envelope vs. disc) that overlap in velocity. Tools like pyspeckit or scousepy work well for this. I use the AIC criterion to determine which number of Gaussians best fit each spectrum.
  • DBSCAN clustering (e.g. with scikit-learn) on the resulting collection of gaussians, to extract the spatially and kinematically coherent cluster of gaussians that corresponds to the streamer candidate, discarding noise and unrelated structures.

Source code overview

Module Contains
extract_streamline Extracts streamer sub-cubes and reduces them to a 1D weighted-mean streamline with uncertainties.
stream_lines_grad JAX-differentiable forward model of the streamline, following Mendoza et al. (2009).
gradient_descent Main body. Parameter preparation, loss function, and Adam-based fit_streamline optimisation loop.
errors Streamline parameter uncertainty estimation at the best fit parameters.
outputs Result-saving and plotting: morphology, position–velocity diagrams, loss curves, correlation/uncertainty plots, and per-epoch animations.

Contributing / Issues

Contributions of all kinds are very welcome! Here's how to do it.

Reporting bugs and getting support

Reports of bugs or unexpected behaviour are welcome via opening an Issue on the GitHub issue tracker. To help diagnose the problem, please include:

  • a short description of the problem and what you expected to happen instead
  • a code example that reproduces the issue
  • the versions of STING and its dependencies (listed above) you are using

For general questions about how to use STING, open an Issue with the label question. You can also get in touch directly at lmason@mpe.mpg.de.

Contributing code

Bug fixes and contributions of new features are very welcome. To contribute code:

  • fork the repository on GitHub, and create a branch for your change
  • install STING in development mode: pip install -e ".[dev]"
  • make your changes, and add or update the tests as needed
  • run the test suite to check nothing is broken: pytest
  • open a pull request against the main branch with a description of what your change does and why

If you have an idea for a feature but are unsure how to approach it, feel free to open an Issue to discuss it first!

All contributors who submit an accepted pull request will be listed in the repository's Contributor list on GitHub.

Known issues

  • The radius sampling grid must reach down to r_low. r_low = max(rmin, 0.5*rc). For jit compilation (what makes STING so fast, see this explanation), arrays must be constant length across optimisation epochs. If npoints and deltar are too small for the chosen r0, the sampled radius grid won't extend down to r_low, and the model will raise an error. If you encounter this, increase npoints and/or deltar.

Citing STING

tbc, get in touch at lmason@mpe.mpg.de

Credits

STING is written and maintained by Lauren Mason (lmason@mpe.mpg.de, @Lauren4476).

Licence

STING is released under the MIT Licence.

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

sting-0.2.1.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

sting-0.2.1-py3-none-any.whl (56.7 kB view details)

Uploaded Python 3

File details

Details for the file sting-0.2.1.tar.gz.

File metadata

  • Download URL: sting-0.2.1.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sting-0.2.1.tar.gz
Algorithm Hash digest
SHA256 89e222b469b83d894aac3b0331d0a2dcb9cac98282acfe361062e2159476655e
MD5 bbede0b37dbf988c7c7068ab2e54a34f
BLAKE2b-256 add45905f1e99a10db9145f42c7b99470d8918f9bc0166cc9a836194ee082cf1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sting-0.2.1.tar.gz:

Publisher: publish.yml on Lauren4476/sting

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

File details

Details for the file sting-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: sting-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 56.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sting-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f586f6738e5da22d1e27ce624a627d955bef9c8a25fb73130cb22ce67ce9dcf5
MD5 282531531d3e1eaee5a734fe18a2de37
BLAKE2b-256 71aca7eaa842e3cf621c4a0ad165f4960b196b524b0af0f29e4c3ee9cd8f7fef

See more details on using hashes here.

Provenance

The following attestation bundles were made for sting-0.2.1-py3-none-any.whl:

Publisher: publish.yml on Lauren4476/sting

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