Skip to main content

Central (co)moment calculation/manipulation

Project description

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

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.
  • Both numpy array-like and xarray DataArray interfaces to 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!

Quick start

Use one of the following

pip install cmomy

or

conda install -c conda-forge cmomy

Example usage

>>> import numpy as np
>>> import cmomy

>>> np.random.seed(0)
>>> x = np.random.rand(100)
>>> m = x.mean()
>>> mom = np.array([((x - m) ** i).mean() for i in range(4)])
>>> c = cmomy.CentralMoments.from_vals(x, mom=3)

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

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

>>> c
<CentralMoments(val_shape=(2,), mom=(3,))>
array([[5.0000e+01, 5.0013e-01, 7.9827e-02, 2.1156e-03],
       [5.0000e+01, 4.4546e-01, 8.4914e-02, 1.5954e-03]])

# Reduce along an axis
>>> c.reduce(axis=0).cmom()
array([1.    , 0.    , 0.0831, 0.0016])

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

>>> cs = [cmomy.CentralMoments.from_vals(_, mom=3) 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.0831, 0.0016])

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.

A quick way to cache (most all) the Numba functions is to run the tests. This can be done with

conda/mamba/pip install pytest

pytest --pyargs cmomy

Documentation

See the documentation for a look at cmomy in action.

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 with Cookiecutter and the wpk-nist-gov/cookiecutter-pypackage Project template forked from audreyr/cookiecutter-pypackage.

Changelog

Changelog for cmomy

Unreleased

See the fragment files in changelog.d

v0.5.0 — 2023-06-14

Added

  • Package now available on conda-forge

  • Bumped maximum python version to 3.11

v0.4.1...v0.5.0

Changed

  • Testing now handled with nox.

v0.4.0 — 2023-05-02

Added

  • Moved module _docstrings_ to docstrings. This can be used by other modules.

Changed

  • Update package layout

  • New linters via pre-commit

  • Development env now handled by tox

  • Now use module-utilities to handle caching and docfiller.

v0.3.0...v0.4.0

v0.3.0 - 2023-04-24

Full set of changes: v0.2.2...v0.3.0

v0.2.2 - 2023-04-05

Full set of changes: v0.2.1...v0.2.2

v0.2.1 - 2023-04-05

Full set of changes: v0.2.0...v0.2.1

v0.2.0 - 2023-03-22

Full set of changes: v0.1.9...v0.2.0

v0.1.9 - 2023-02-15

Full set of changes: v0.1.8...v0.1.9

v0.1.8 - 2022-12-02

Full set of changes: v0.1.7...v0.1.8

v0.1.7 - 2022-09-28

Full set of changes: v0.1.6...v0.1.7

v0.1.6 - 2022-09-27

Full set of changes: v0.1.5...v0.1.6

v0.1.5 - 2022-09-26

Full set of changes: v0.1.4...v0.1.5

v0.1.4 - 2022-09-15

Full set of changes: v0.1.3...v0.1.4

v0.1.3 - 2022-09-15

Full set of changes: v0.1.2...v0.1.3

v0.1.2 - 2022-09-13

Full set of changes: v0.1.1...v0.1.2

v0.1.1 - 2022-09-13

Full set of changes: v0.1.0...v0.1.1

v0.1.0 - 2022-09-13

Full set of changes: v0.0.7...v0.1.0

v0.0.7 - 2021-05-18

Full set of changes: v0.0.6...v0.0.7

v0.0.6 - 2021-02-03

Full set of changes: v0.0.4...v0.0.6

v0.0.4 - 2020-12-21

Full set of changes: v0.0.3...v0.0.4

This software was developed by employees of the National Institute of Standards and Technology (NIST), an agency of the Federal Government. Pursuant to title 17 United States Code Section 105, works of NIST employees are not subject to copyright protection in the United States and are considered to be in the public domain. Permission to freely use, copy, modify, and distribute this software and its documentation without fee is hereby granted, provided that this notice and disclaimer of warranty appears in all copies.

THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.

Distributions of NIST software should also include copyright and licensing statements of any third-party software that are legally bundled with the code in compliance with the conditions of those licenses.

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-0.6.0.tar.gz (183.4 kB view hashes)

Uploaded Source

Built Distribution

cmomy-0.6.0-py3-none-any.whl (67.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page