Skip to main content

Programmatic parameter sweeps for pydantic.

Project description

pydantic-sweep

pydantic_sweep is a library to programmatically, safely and flexibly define complex parameter sweeps over pydantic models in Python.

PyPI - Version Static Badge GitHub branch check runs GitHub License

Highlights:

  • Specify parameter sweeps in Python
  • Flexibility: specify complex parameter combinations by chaining simple functional operations
  • Safety checks for parameter combinations (get meaningful errors early)
  • pydantic field validation
  • Conversion between json/yaml/python code representation of models

For example, the following code will instantiate models with (x=5, sub=Sub1(s=1) and (x=6, sub=Sub1(s=2) and try each of those with seed values of seed=43 and seed=44, leading to four different configurations:

import pydantic_sweep as ps

class Sub1(ps.BaseModel):
    s: int = 5

class Sub2(ps.BaseModel):
    y: str = "hi"

class Model(ps.BaseModel):
    seed: int = 42
    x: int = 5
    sub: Sub1 | Sub2

# We want to test across two seeds
configs = ps.config_product(
    ps.field("seed", [43, 44]),
    # And two specific Sub1 and `x` combinations
    ps.config_zip(
        ps.field("x", [ps.DefaultValue, 6]),
        ps.field("sub.s", [1, 2]),
    )
)
# This includes safety checks that Sub1 / Sub2 are uniquely specified
models = ps.initialize(Model, configs)

# The code above is equivalent to
models_manual = [
    Model(seed=43, sub=Sub1(s=1)),
    Model(seed=43, x=6, sub=Sub1(s=2)), 
    Model(seed=44, sub=Sub1(s=1)),
    Model(seed=44, x=6, sub=Sub1(s=2)), 
]
assert models == models_manual

# We can also check that we didn't accidentally duplicate a setting
ps.check_unique(models)

While in this toy example, manually specifying the combinations may still be viable, the library allows infinitely combining different configs and sub-models, making it a powerful tool for large-scale experiment definition. To learn mode about the capabilities of the library please visit the documentation page.

Installation

You can install the library by checking out the repo and running

pip install 'pydantic-sweep'

License

The main code-base is licensed under MPL-2.0 a weak copy-left license that allows commercial use. See the license file for the exact clauses and this FAQ for a high-level description.

An exception from this are the documentation in the docs and example folders as well as this README file, which are licensed under the 0BSD: a highly permissive license that does not require attribution. That way, you are free to copy & paste example code into your use-case. See here for a high-level description.

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

pydantic_sweep-0.3.7.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

pydantic_sweep-0.3.7-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_sweep-0.3.7.tar.gz.

File metadata

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

File hashes

Hashes for pydantic_sweep-0.3.7.tar.gz
Algorithm Hash digest
SHA256 9ccb21123638484820a318efd22c85abce7c90771d6dc3ad167fe305576cc402
MD5 64ef327d64f770496c8e7c2287c1585d
BLAKE2b-256 5b60274a24bcd68833cf86c4ef4a6563ed70b36c8543f3c47e0697181a5abb26

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_sweep-0.3.7.tar.gz:

Publisher: release.yml on befelix/pydantic_sweep

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

File details

Details for the file pydantic_sweep-0.3.7-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pydantic_sweep-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 75cdcea6749216c712eea5b6fa775d162e982f7e2859dbae46eb1cb4f0b0c07c
MD5 63362e57873aa3a98b16cce9fc73d0a3
BLAKE2b-256 872b6a26c281f56e436c1d41b4550d091a8df5bf0ad09f6a5277dfcd76ded6c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_sweep-0.3.7-py3-none-any.whl:

Publisher: release.yml on befelix/pydantic_sweep

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