Skip to main content

Hypothesis strategies for Awkward Array

Project description

hypothesis-awkward

Hypothesis strategies for Awkward Arrays.

pypi-badge pypi-python-badge

test-badge codecov-badge

Hypothesis is a property-based testing library. Its strategies are Python functions that strategically generate test data that can fail test cases in pytest and other testing frameworks. Once a test fails, Hypothesis searches for the simplest sample that causes the same error. Hypothesis automatically explores edge cases; developers do not need to craft test data manually.

Property-based testing is useful for finding edge cases in array libraries and in code that uses them. In fact, Hypothesis strategies for NumPy and pandas data types are included in Hypothesis itself. Xarray provides strategies for its data structure. The Apache Arrow codebase has strategies for PyArrow, which are not officially documented in its API reference.

This package, hypothesis-awkward, is a collection of Hypothesis strategies for Awkward Array, which can represent a wide variety of layouts of nested, variable-length, and mixed-type data. The current version of this package includes strategies that generate samples with certain types of layouts. The goal is to develop strategies that can generate fully general Awkward Arrays with multiple options to control the layout, data types, missing values, masks, and other array attributes. These strategies can help close in on edge cases in tools that use Awkward Array, and Awkward Array itself.

[!NOTE]

This package is early work in progress and still experimental. The APIs may change over time.

Installation

You can install the package from PyPI using pip:

pip install hypothesis-awkward

This also installs Hypothesis and Awkward Array as dependencies unless they are already installed.

The strategy arrays()

The function arrays() is the main strategy. It is currently experimental and developed in strategies/constructors/. The plan is to have arrays() generate fully general Awkward Arrays with many options to control the output arrays.

Sample outputs of arrays()

You can see sample outputs of the current version of arrays() in the test case:

from hypothesis import given

import awkward as ak
import hypothesis_awkward.strategies as st_ak


@given(array=st_ak.constructors.arrays())
def test_array(array: ak.Array) -> None:
    print(f'{array=!r}')

For example, this might print:

array=<Array [] type='0 * bool'>
array=<Array [] type='0 * var * bool'>
array=<Array [[], [], [], []] type='4 * var * 2 * timedelta64[W]'>
array=<Array [] type='0 * var * bool'>
array=<Array [] type='0 * var * timedelta64[us]'>
array=<Array [] type='0 * var * bool'>
array=<Array [[[52, 51113, 30]], []] type='2 * var * var * uint64'>
array=<Array [] type='0 * timedelta64[ns]'>
array=<Array [] type='0 * 0 * bool'>
array=<Array [[[-9223372036854773716], ...]] type='1 * 3 * 1 * datetime64[Y]'>
array=<Array [] type='0 * int32'>
array=<Array [9223372036854775807 seconds, ...] type='3 * timedelta64[s]'>
array=<Array [-9223372036854775590 seconds, ...] type='3 * timedelta64[s]'>
array=<Array [-9223372036854775590 seconds] type='1 * timedelta64[s]'>

The current version generates arrays with NumpyArray as leaf contents that can be nested multiple levels deep in RegularArray, ListOffsetArray, and ListArray lists.

The API of arrays()

def arrays(
    dtypes: st.SearchStrategy[np.dtype] | None = None,
    max_size: int = 10,
    allow_nan: bool = False,
    allow_regular: bool = True,
    allow_list_offset: bool = True,
    allow_list: bool = True,
    max_depth: int = 5,
):
Parameter Description
dtypes A strategy for NumPy scalar dtypes used in NumpyArray. If None, the default strategy that generates any scalar dtype supported by Awkward Array is used.
max_size Maximum total number of scalar values in the generated array.
allow_nan No NaN/NaT values are generated if False.
allow_regular No RegularArray is generated if False.
allow_list_offset No ListOffsetArray is generated if False.
allow_list No ListArray is generated if False.
max_depth Maximum depth of nested arrays.

Other strategies

In addition to arrays() mentioned above, this package includes other strategies that generate Awkward Arrays and related data types.

NumPy

These strategies are related to the section of Awkward Array User Guide "How to convert to/from NumPy".

Strategy Data type
from_numpy Awkward Arrays created from NumPy arrays
numpy_arrays NumPy arrays that can be converted to Awkward Array
numpy_dtypes NumPy dtypes (simple or array) supported by Awkward Array
supported_dtypes NumPy dtypes (simple only) supported by Awkward Array
supported_dtype_names Names of NumPy dtypes (simple only) supported by Awkward Array

Python lists

These strategies are related to the section of Awkward Array User Guide "How to convert to/from Python objects".

Strategy Data type
from_list Awkward Arrays created from Python lists
lists Nested Python lists for which Awkward Arrays can be created
items_from_dtype Python built-in type values for a given NumPy dtype
builtin_safe_dtypes NumPy dtypes with corresponding Python built-in types
builtin_safe_dtype_names Names of NumPy dtypes with corresponding Python built-in types

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

hypothesis_awkward-0.1.0.tar.gz (64.5 kB view details)

Uploaded Source

Built Distribution

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

hypothesis_awkward-0.1.0-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file hypothesis_awkward-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for hypothesis_awkward-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9470bfa0421cd8ff71e9592f9d9acaca34e2a3882c403ec7502259cc09d26f76
MD5 28c9d6f5505afb560f2365ca6837a72f
BLAKE2b-256 1474a9e28d39791202b5f6e451b3ed7b8cf676003681e9dfa9b414f01ddd7381

See more details on using hashes here.

Provenance

The following attestation bundles were made for hypothesis_awkward-0.1.0.tar.gz:

Publisher: pypi.yml on TaiSakuma/hypothesis-awkward

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

File details

Details for the file hypothesis_awkward-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hypothesis_awkward-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2c904d67893b0221cfa1ebf326d39c2363a9737647a61076cf9ba503c6151df
MD5 2ded31f2b4dcd9f59c59e8a0cc1e75f9
BLAKE2b-256 9a3faa27667256e521b6e291b0fd445f2f8ce88a52180a31b2c7aa166bd6414f

See more details on using hashes here.

Provenance

The following attestation bundles were made for hypothesis_awkward-0.1.0-py3-none-any.whl:

Publisher: pypi.yml on TaiSakuma/hypothesis-awkward

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