Skip to main content

Stack patches of the CMB temperature sky around local maxima

Project description

Made at Code/Astro PyPI version Python version License: MIT Development Status

cmbstack

cmbstack is a Python package for stacking patches of the Cosmic Microwave Background (CMB) temperature sky. It accepts input as a theoretical power spectrum, a HEALPix FITS file, or a map array already in memory. From there it detects local maxima, extracts gnomonic (flat-sky) patches around each peak, and averages them. This stacking procedure enhances the coherent peak profile while suppressing uncorrelated noise.

Installation

pip install cmbstack

Quick Start

From a power spectrum file:

from cmbstack.main import StackingPipeline

pipeline = StackingPipeline.from_cl("path/to/spectrum.dat", nside=1024, seed=42)
pipeline.run()

From an existing FITS map:

pipeline = StackingPipeline.from_fits("path/to/map.fits", field=0)
pipeline.run()

From a map array already in memory:

pipeline = StackingPipeline.from_map(sky_map)
pipeline.run()

See examples/from_theoretical_cl.ipynb for a full worked example.

Package Structure

cmbstack/
├── maps.py      — power-spectrum loading, map simulation, normalisation
├── stacking.py  — peak finding, patch extraction, stacking, radial profile
└── main.py      — StackingPipeline high-level class

Workflow

Note: Steps 1–2 apply when starting from a power spectrum. Use StackingPipeline.from_fits or from_map to skip them when working with a real or pre-simulated map.

1. Load the Power Spectrum — maps.load_cl

The input file contains the power spectrum as $D_\ell^{TT}$:

$$D_\ell \equiv \frac{\ell(\ell+1)}{2\pi} C_\ell$$

load_cl reads columns $(\ell, D_\ell)$ and converts to $C_\ell$:

$$C_\ell = \frac{2\pi}{\ell(\ell+1)} D_\ell, \qquad C_0 = C_1 = 0$$


2. Simulate a Sky Map — maps.simulate_map

A Gaussian random realization is drawn by sampling spherical harmonic coefficients $a_{\ell m}$ with variance $C_\ell$:

$$T(\hat{n}) = \sum_{\ell,m} a_{\ell m} , Y_{\ell m}(\hat{n}), \qquad \langle |a_{\ell m}|^2 \rangle = C_\ell$$

This calls healpy.synfast internally. An optional seed makes runs reproducible.


3. Normalise the Map — maps.normalize_map

Before peak detection, the map is standardised so that thresholds have a clear statistical meaning:

$$T_{\text{norm}}(\hat{n}) = \frac{T(\hat{n}) - \langle T \rangle}{\sigma}$$

After this step the map has mean $\approx 0$ and standard deviation $= 1$, so peaks are measured in units of $\sigma$.


4. Detect Peaks — stacking.find_peaks

Local maxima are identified with healpy.hotspots: a pixel is a maximum if its value exceeds every immediate HEALPix neighbour. Peaks are filtered by a significance threshold $\nu$ (default $\nu = 3\sigma$) and optionally capped at the $N$ highest:

$$\text{Peaks} = {\hat{n}p \in \text{Maxima} \mid T{\text{norm}}(\hat{n}_p) > \nu}$$

Returns sky positions as $(θ, φ)$ pairs in radians.


5. Extract Patches — stacking.extract_patches

For each peak a square patch is cut using a gnomonic (tangent-plane) projection centred on $\hat{n}_p$. Every patch shares the same fixed pixel grid (side length size_deg, pixel scale reso_arcmin), so the centre pixel always corresponds to the peak itself and patches can be co-added directly.


6. Stack — stacking.stack_patches

Patches are averaged pixel-by-pixel:

$$S = \frac{1}{N} \sum_{i=1}^{N} P_i$$

Incoherent noise averages towards zero; the coherent central profile survives.


7. Radial Profile — stacking.radial_profile

The 2D stacked image is collapsed to a 1D profile by azimuthal averaging in concentric annuli about the centre. Returns bin-centre radii in arcminutes and the mean temperature in each annulus.


Pipeline Constructors

StackingPipeline provides three entry points depending on where your data comes from:

Constructor Input Notes
from_cl(path, nside, seed) Power-spectrum file Simulates a Gaussian random map via healpy.synfast
from_fits(path, field=0) HEALPix FITS file Loads the map with maps.load_map; nside is inferred automatically
from_map(sky_map) NumPy array Accepts any in-memory HEALPix map; nside is inferred automatically

All three store the map in pipeline.map and share the same run() interface.


Map I/O Utilities

maps.load_map and maps.save_map wrap the healpy FITS readers for convenience:

from cmbstack import maps

m = maps.load_map("map.fits", field=0)   # wraps hp.read_map
maps.save_map("out.fits", m)             # wraps hp.write_map (overwrite=True by default)

Pipeline Object

StackingPipeline stores every intermediate product as an attribute:

Attribute Content
pipeline.map Raw simulated map
pipeline.normalized Normalised map (units of $\sigma$)
pipeline.positions Peak positions $(θ, φ)$ in radians
pipeline.patches List of 2D gnomonic patches
pipeline.stacked Mean stacked 2D image
pipeline.radius Radial bin centres (arcmin)
pipeline.profile Mean temperature per radial bin

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

cmbstack-0.0.3.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

cmbstack-0.0.3-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file cmbstack-0.0.3.tar.gz.

File metadata

  • Download URL: cmbstack-0.0.3.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for cmbstack-0.0.3.tar.gz
Algorithm Hash digest
SHA256 780586ff7badd263da8f9b0c222dfec17f42145cc252ecb23a34b77f887435aa
MD5 ac22718791012c4df76b708242c16adf
BLAKE2b-256 c8980c9a5ac2b69a21c8bb60703c76e55eeb9764ae2d225a3bd924bd2e41bc6d

See more details on using hashes here.

File details

Details for the file cmbstack-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: cmbstack-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for cmbstack-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 eff2e6023996b4ddbe042a522b59480d005444f65587a5e1b1fa879af41a58ce
MD5 ed6ed401d1377e88eaaaf6ffedc3085e
BLAKE2b-256 65a2e4b0150b8d0d08e04d42ac0e8402836777651b3d4d96979496069fe6b5e4

See more details on using hashes here.

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