Metadata-Version: 2.4
Name: sum_stat
Version: 0.1.0
Summary: Cosmological summary statistics from galaxy catalogues and lensing data
Author-email: Johan Comparat <johan.comparat@pm.me>
License: MIT License
        
        Copyright (c) 2026 Johan Comparat
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/JohanComparat/sum_stat
Project-URL: Documentation, https://sum-stat.readthedocs.io
Project-URL: Repository, https://github.com/JohanComparat/sum_stat
Project-URL: Bug Tracker, https://github.com/JohanComparat/sum_stat/issues
Keywords: cosmology,summary statistics,galaxy surveys,JAX,two-point correlation,luminosity function,stellar mass function,galaxy-galaxy lensing
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Scientific/Engineering :: Astronomy
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: astropy
Requires-Dist: matplotlib
Requires-Dist: jax
Requires-Dist: jaxlib
Requires-Dist: h5py
Requires-Dist: healpy
Requires-Dist: treecorr
Requires-Dist: corrfunc
Requires-Dist: dsigma
Requires-Dist: numba
Requires-Dist: tqdm
Requires-Dist: pandas
Requires-Dist: scikit-learn
Requires-Dist: pyfnntw
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx; extra == "docs"
Requires-Dist: sphinx-rtd-theme; extra == "docs"
Requires-Dist: sphinxcontrib-napoleon; extra == "docs"
Requires-Dist: numpydoc; extra == "docs"
Requires-Dist: myst-parser; extra == "docs"
Dynamic: license-file

# sum_stat

[![PyPI version](https://img.shields.io/pypi/v/sum_stat.svg)](https://pypi.org/project/sum_stat/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
[![CI](https://github.com/JohanComparat/sum_stat/actions/workflows/ci.yml/badge.svg)](https://github.com/JohanComparat/sum_stat/actions/workflows/ci.yml)
[![Documentation](https://readthedocs.org/projects/sum-stat/badge/?version=latest)](https://sum-stat.readthedocs.io/en/latest/)

Python package for measuring cosmological summary statistics from galaxy catalogues and lensing data, built on JAX for differentiable, JIT-compilable estimators.

## Features

| Module | Estimators |
|---|---|
| **Two-point correlation functions** | `w_theta`, `xi_r`, `wp`, `xi_multipoles` |
| **Power spectra** | `cl_angular`, `pk3d`, `pk_multipoles` |
| **Galaxy–galaxy lensing** | `delta_sigma`, `delta_sigma_nfw_jax`, `delta_sigma_from_xi_jax` |
| **Luminosity / stellar-mass functions** | `luminosity_function_vmax`, `stellar_mass_function_vmax`, `luminosity_function_swml`, `cumulative_luminosity_function_cminus`, `fit_schechter_sty` |
| **Independence tests** | `efron_petrosian_tau` (Efron & Petrosian 1992), `rauzy_completeness` (Rauzy 2001) |
| **Redshift distributions** | `nz_histogram`, `nz_kde` |
| **Covariance** | `jackknife_covariance`, `bootstrap_covariance` |
| **I/O** | `SummaryStatWriter`, `SummaryStatReader` (HDF5) |
| **Catalogues** | `GalaxyCatalogue`, `ShapeCatalogue`, `PhotoZCalibTable` |
| **Cosmology** | JAX-differentiable comoving distance, volume, Σ_crit |

## Installation

```bash
mamba env create -f environment.yml
mamba activate sum_stat
pip install -e .
```

## Quick start

```python
import numpy as np
import sum_stat as ss
from astropy.cosmology import FlatLambdaCDM

cosmo = FlatLambdaCDM(H0=67.36, Om0=0.3111)

# --- Catalogues ----------------------------------------------------------
cat  = ss.GalaxyCatalogue(ra=ra, dec=dec, redshift=z, weight=w, mag_limit=22.5)
rand = ss.GalaxyCatalogue(ra=ra_r, dec=dec_r, redshift=z_r)

# --- Two-point correlation functions -------------------------------------
import numpy as np
theta_bins = np.logspace(-2, 1, 20)          # degrees
theta, w, vw = ss.w_theta(cat, rand, theta_bins)

r_bins = np.logspace(-1, 2, 20)              # Mpc/h
r, xi, vxi = ss.xi_r(cat, rand, r_bins, cosmo)

r, wp_val, vwp = ss.wp(cat, rand, r_bins, pi_max=60.0, cosmo=cosmo)

s_bins = np.logspace(-1, 2, 20)
s, xi_dict = ss.xi_multipoles(cat, rand, s_bins, ells=(0, 2, 4), cosmo=cosmo)
# xi_dict[0], xi_dict[2], xi_dict[4] → monopole, quadrupole, hexadecapole

# --- Power spectra -------------------------------------------------------
ell_bins = np.geomspace(10, 3000, 30)
ell, cl, vcl = ss.cl_angular(cat, rand, ell_bins)

k_bins = np.logspace(-2, 0, 30)
k, pk, vpk = ss.pk3d(cat, rand, k_bins, cosmo=cosmo)

# --- Galaxy–galaxy lensing -----------------------------------------------
import dsigma.helpers as dh
from astropy.table import Table

# Build a dsigma-formatted source table (see dsigma docs for survey details)
table_s = dh.dsigma_table(Table({
    "ra": ra_s, "dec": dec_s, "z": z_s,
    "e_1": e1, "e_2": e2, "w": w_s, "m": m, "e_rms": e_rms,
    "R_11": R11, "R_22": R22, "R_12": R12, "R_21": R21,
}), "source")

rp_bins = np.logspace(-1, 1.5, 15)          # Mpc
corrections = {"scalar_shear_response_correction": True,
               "shear_responsivity_correction": True}
rp, ds, cov = ss.delta_sigma(cat, table_s, rp_bins, cosmo,
                              corrections=corrections)

# --- Luminosity / stellar-mass functions ---------------------------------
from sum_stat.lf_smf.swml import luminosity_function_swml
from sum_stat.lf_smf.cminus import cumulative_luminosity_function_cminus
from sum_stat.lf_smf.independence import efron_petrosian_tau, rauzy_completeness

M_bins = np.linspace(-24, -18, 20)
area_sr = 0.3

# 1/Vmax estimator (Schmidt 1968)
M, phi, phi_err = ss.luminosity_function_vmax(cat, 0.1, 0.5, 22.5, M_bins, area_sr, cosmo)

# SWML estimator (Efstathiou, Ellis & Peterson 1988)
M, phi_sw, phi_sw_err = luminosity_function_swml(cat, 0.1, 0.5, 22.5, M_bins, area_sr, cosmo)

# C⁻ non-parametric cumulative estimator (Lynden-Bell 1971)
M_sorted, Phi, Phi_err = cumulative_luminosity_function_cminus(cat, 0.1, 0.5, 22.5, area_sr, cosmo)

# STY parametric Schechter fit
result = ss.fit_schechter_sty(cat, 0.1, 0.5, 22.5, M_bins, area_sr, cosmo)

logM_bins = np.linspace(9, 12, 20)
logM, smf, smf_err = ss.stellar_mass_function_vmax(cat, 0.1, 0.5, logM_bins, area_sr, cosmo)

# Efron–Petrosian independence test (τ ~ N(0,1) under H₀)
ep = efron_petrosian_tau(cat.abs_mag, cat.redshift, 22.5, cosmo)
# Rauzy completeness tests
rz = rauzy_completeness(cat.abs_mag, cat.redshift, 22.5, cosmo)

# --- Covariance ----------------------------------------------------------
cov_jk = ss.jackknife_covariance(estimator_func, cat, n_patches=20)
cov_bt = ss.bootstrap_covariance(estimator_func, cat, n_bootstrap=200)

# --- I/O (HDF5) ----------------------------------------------------------
writer = ss.SummaryStatWriter("results.h5")
writer.write_twopcf("angular_2pcf/sample_A",
                    theta, w, cov_jk, theta_bins,
                    estimator="landy-szalay", cosmo=cosmo, meta={})

reader = ss.SummaryStatReader("results.h5")
data = reader.read_twopcf("angular_2pcf/sample_A")
```


## JAX cosmology utilities

All functions in `sum_stat.cosmology` are JIT-compiled and differentiable with respect to cosmological parameters:

```python
from sum_stat.cosmology import comoving_distance_jax, critical_surface_density_jax
import jax

chi  = comoving_distance_jax(z=0.5, h=0.6736, omega_m=0.3111)   # [Mpc]
dchi = jax.grad(comoving_distance_jax)(0.5, 0.6736, 0.3111)     # d chi / d z

sigma_crit = critical_surface_density_jax(z_l=0.3, z_s=0.8,
                                          h=0.6736, omega_m=0.3111)  # [M_sun/pc^2]
```

## Development

```bash
# tests
pytest tests/

# formatting / linting
black sum_stat/
ruff check sum_stat/

# documentation
cd docs && make html
```

## Citation

If you use `sum_stat` in your research, please cite it as:

```bibtex
@software{comparat2026sumstat,
  author  = {Comparat, Johan},
  title   = {{sum\_stat}: Cosmological summary statistics from galaxy catalogues and lensing data},
  year    = {2026},
  version = {0.1.0},
  url     = {https://github.com/JohanComparat/sum_stat},
  license = {MIT},
}
```

A `CITATION.cff` file is also provided for automated citation tools (GitHub "Cite this repository" button).

## Contributing

Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, coding conventions, and how to submit a pull request.

## License

MIT — see [LICENSE](LICENSE).

## Package structure

```
sum_stat/
├── sum_stat/
│   ├── catalogue.py       # GalaxyCatalogue, ShapeCatalogue, PhotoZCalibTable
│   ├── cosmology.py       # JAX-differentiable distances and Σ_crit
│   ├── covariance/        # jackknife_covariance, bootstrap_covariance
│   ├── io/                # SummaryStatWriter, SummaryStatReader
│   ├── lensing/           # delta_sigma, NFW model
│   ├── lf_smf/            # 1/Vmax, SWML, C⁻, STY fit, EP τ, Rauzy
│   ├── nz/                # nz_histogram, nz_kde
│   ├── powspec/           # cl_angular, pk3d, pk_multipoles
│   └── twopcf/            # w_theta, xi_r, wp, xi_multipoles
├── tests/
├── docs/
├── environment.yml
└── pyproject.toml
```
