Skip to main content

field2pt

Power spectra, correlation functions, and multipoles of fields on regular 3D grids, using FFTW and OpenMP.

Installation

pip install field2pt

FFTW3 is required in single and double precision with its OpenMP libraries:

apt install libfftw3-dev                               # Debian, Ubuntu
dnf install fftw-devel                                 # RHEL, Fedora
CMAKE_PREFIX_PATH=/path/to/fftw pip install field2pt   # FFTW elsewhere

Inputs

Fields are caller-provided C-contiguous NumPy arrays with shape (nx, ny, nz) and dtype float32 or float64. They are treated as periodic. lbox is a scalar or three physical box lengths.

method describes the assignment window to remove: "NGP", "CIC", "TSC", "PCS", or "none". Use "none" when no such assignment window was applied.

Usage

import numpy as np
import field2pt as f2

rng = np.random.default_rng(42)
delta = rng.normal(size=(128, 128, 128)).astype(np.float32)

est = f2.Estimator(nmesh=delta.shape, lbox=1000.0, method="none")

pk = est.power_spectrum(delta, bins=64)
xi = est.correlation_function(delta, bins=32)

pk.k, pk.pk, pk.nmodes
xi.s, xi.xi, xi.ncells

An Estimator retains FFT workspaces for repeated measurements. Module-level functions provide the same operations for one-off calls.

Multipoles and cross statistics

pk = est.power_spectrum(delta, ells=(0, 2, 4), los="z")
pk.pk_ell0, pk.pk_ell2, pk.pk_ell4

cross = est.power_spectrum(delta1, delta2)
odd = est.power_spectrum(delta1, delta2, ells=(1, 3), los="z", part="imag")

xi = est.correlation_function(delta1, delta2, ells=(0, 1, 2), los="z")

Orders up to 10 are supported. los is required for nonzero multipoles. Auto spectra and the real part of cross spectra use even orders; the imaginary part of a cross spectrum uses odd orders.

bins may be a positive count or an array of edges. Counted bins accept range=[lower, upper] and scale="linear" or "log". Empty bins contain NaN.

Shot noise

pk = est.power_spectrum(delta, npart=number_of_particles)
pk = est.power_spectrum(delta, weights=particle_weights)
pk = est.power_spectrum(delta, shotnoise=value)

These subtract a constant Poisson term from the monopole. Nothing is subtracted by default or from cross spectra.

Velocity fields

velocity = (velocity_x, velocity_y, velocity_z)
result = est.velocity_power_spectrum(velocity, delta=delta)

result.theta.pk   # divergence spectrum
result.omega.pk   # vorticity spectrum
result.cross.pk   # delta x divergence, when delta is supplied

The components must have the same shape and dtype as delta.

Interlacing

For the common half-cell pair:

pk = est.interlaced_power_spectrum(delta, delta_shifted)

For more shifts, add one caller-prepared field at a time:

it = f2.Interlaced("diag4", nmesh=delta.shape, lbox=1000.0, method="none")
for shifted_field, shift in zip(shifted_fields, it.offsets()):
    it.add(shifted_field, shift)
pk = it.power_spectrum()

Built-in sets are "diag2", "diag4", "cube3", and "cube4".

Smoothing and marked fields

smoothed = est.smooth(delta, radius=20.0, kind="gaussian")
marked = f2.marked_field(
    delta, lbox=1000.0, radius=20.0, delta_s=4.0, power=1.0
)

Smoothing supports Gaussian and top-hat windows. Both operations preserve the input dtype.

Grid metadata

Instead of passing nmesh, lbox, and method separately, grid= accepts an object exposing .lbox and .p. Optional .nmesh and .bc attributes are validated when present. Every boundary must be periodic.

nthreads=0 uses the OpenMP default. A positive value sets the estimator's thread count.

Conventions

  • P(k) = (V/N^2) |delta(k)|^2, where N is the number of grid cells.
  • Assignment-window deconvolution removes smoothing but not aliased images.
  • nmodes counts independent Fourier modes.
  • float32 and float64 inputs retain their precision; arrays are not converted.

License

GPL-2.0-or-later; see LICENSE.

Metadata

Release files for field2pt 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for field2pt 1.0.0
File Size Uploaded Provenance Actions
field2pt-1.0.0.tar.gz 31.0 kB Without provenance Details for field2pt-1.0.0.tar.gz

Release files / field2pt-1.0.0.tar.gz

Download URL field2pt-1.0.0.tar.gz
Size 31.0 kB
Tags Source
SHA-256 checksum
How to use checksums
de6d9c047e825c58773e529c13ed87e30c77d81b7c96157227453740780e22c8
BLAKE2b-256 checksum
How to use checksums
6e4f68aa630188deb60d86ac84a4dc6909ec605714d488d185d6d1e665cc750a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for field2pt 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page