Skip to main content

Central (co)moment calculation/manipulation

Project description

Repo Docs PyPI license PyPI version Conda (channel only) Code style: ruff uv

cmomy

A Python package to calculate and manipulate Central (co)moments. The main features of cmomy are as follows:

  • numba accelerated computation of central moments and co-moments
  • Routines to combine, and resample central moments.
  • Supports numpy array and xarray DataArray or Dataset based data.
  • Routines to convert between central and raw moments.

Overview

cmomy is an open source package to calculate central moments and co-moments in a numerical stable and direct way. Behind the scenes, cmomy makes use of numba to rapidly calculate moments. A good introduction to the type of formulas used can be found here.

Features

  • Fast calculation of central moments and central co-moments with weights
  • Support for scalar or vector inputs
  • numpy and xarray api's
  • bootstrap resampling

Status

This package is actively used by the author. Please feel free to create a pull request for wanted features and suggestions!

Example usage

>>> import numpy as np
>>> import cmomy
>>> rng = cmomy.default_rng(seed=0)
>>> x = rng.random(100)
>>> m = x.mean()
>>> mom = np.array([((x - m) ** i).mean() for i in range(4)])
>>> c = cmomy.wrap_reduce_vals(x, mom=3, axis=0)

>>> np.testing.assert_allclose(c.cmom(), mom, atol=1e-8)
>>> c.cmom()
array([ 1.    ,  0.    ,  0.0919, -0.0061])

# break up into chunks
>>> c = cmomy.wrap_reduce_vals(x.reshape(-1, 2), mom=3, axis=0)

>>> c
<CentralMomentsArray(mom_ndim=1)>
array([[ 5.0000e+01,  5.3019e-01,  8.0115e-02, -4.3748e-03],
       [ 5.0000e+01,  5.6639e-01,  1.0297e-01, -8.9911e-03]])

# Reduce along an axis
>>> c.reduce(axis=0).cmom()
array([ 1.    ,  0.    ,  0.0919, -0.0061])

# unequal chunks
>>> x0, x1, x2 = x[:20], x[20:60], x[60:]

>>> cs = [cmomy.wrap_reduce_vals(_, mom=3, axis=0) for _ in (x0, x1, x2)]

>>> c = cs[0] + cs[1] + cs[2]

>>> np.testing.assert_allclose(c.cmom(), mom, atol=1e-8)
>>> c.cmom()
array([ 1.    ,  0.    ,  0.0919, -0.0061])

Installation

Use one of the following

pip install cmomy

or

conda install -c conda-forge cmomy

Note on caching

This code makes extensive use of the numba python package. This uses a jit compiler to speed up vital code sections. This means that the first time a function called, it has to compile the underlying code. However, caching has been implemented. Therefore, the very first time you run a function, it may be slow. But all subsequent uses (including other sessions) will be already compiled. You can pre-compile the cmomy by running

python -m cmomy.compile

If you'll be using cmomy in parallel (e.g., using multiprocessing), make sure to pre-compile cmomy, or to turn off caching by setting the environment variable CMOMY_NUMBA_CACHE=0.

Documentation

See the documentation for a look at cmomy in action.

What's new?

See changelog.

License

This is free software. See LICENSE.

Related work

This package is used extensively in the newest version of thermoextrap. See here.

Contact

The author can be reached at wpk@nist.gov.

Credits

This package was created using Cookiecutter with the usnistgov/cookiecutter-nist-python template.

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

cmomy-1.1.1.tar.gz (168.6 kB view details)

Uploaded Source

Built Distribution

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

cmomy-1.1.1-py3-none-any.whl (154.1 kB view details)

Uploaded Python 3

File details

Details for the file cmomy-1.1.1.tar.gz.

File metadata

  • Download URL: cmomy-1.1.1.tar.gz
  • Upload date:
  • Size: 168.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cmomy-1.1.1.tar.gz
Algorithm Hash digest
SHA256 91c54601e36f576057631caebda40b5654708245d8be031e80fd7e9c16b2da62
MD5 aecaa67d71ac2ec33d6b920100f32c8a
BLAKE2b-256 1a4613119f329f8d954348ddf2955200286be50f2f0a8c89eef6187eba89eb58

See more details on using hashes here.

Provenance

The following attestation bundles were made for cmomy-1.1.1.tar.gz:

Publisher: cd.yml on usnistgov/cmomy

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

File details

Details for the file cmomy-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: cmomy-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 154.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cmomy-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52f1e2e882d3cd7713a4c51ee36c554375a3561e4b505d4f16bcf395c833ac30
MD5 c034e991d40639a926efe339261f8c83
BLAKE2b-256 41bd2b18258cd6b8ee5f6259f7b5d4aaf29f204089a707eca1fe7d08b10348fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for cmomy-1.1.1-py3-none-any.whl:

Publisher: cd.yml on usnistgov/cmomy

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