Skip to main content

Library for parameter processing and validation with a focus on computational modeling projects

Project description

ParamTools

ParamTools defines the parameter input space for computational modeling projects.

  • Defines the default parameter space.
  • Facilitates adjusting that space.
  • Performs validation on the default space and the adjusted space.

How to use ParamTools

Subclass the Parameters class and set your specification schema and default specification files:

from paramtools import Parameters
from paramtools import get_example_paths

# Get paths to weather example files.
schema_, defaults_ = get_example_paths('weather')

class WeatherParams(Parameters):
    schema = schema_  # "schema.json"
    defaults = defaults_  # "defaults.json"

params = WeatherParams(
    initial_state={"month": "November", "dayofmonth": 1},
    array_first=True
)

print(params.state)
# output: {'month': 'November', 'dayofmonth': 1}

Parameters are available via instance attributes:

print(params.average_precipitation)
# output:  [[3.6] [3. ]]

Get the parameter's value object:

print(params.from_array("average_precipitation"))
# output:  [{'city': 'Atlanta, GA', 'month': 'November', 'value': 3.6}, {'city': 'Washington, D.C.', 'month': 'November', 'value': 3.0}]

Adjust the default specification:

adjustment = {
    "average_precipitation": [
        {"city": "Washington, D.C.", "month": "November", "value": 10},
        {"city": "Atlanta, GA", "month": "November", "value": 15},
    ]
}
params.adjust(adjustment)
print(params.from_array("average_precipitation"))
# output:  [{'city': 'Atlanta, GA', 'month': 'November', 'value': 15.0}, {'city': 'Washington, D.C.', 'month': 'November', 'value': 10.0}]

print(params.average_precipitation)
# output:  [[15.] [10.]]

Errors on invalid input:

adjustment["average_precipitation"][0]["value"] = "rainy"
params.adjust(adjustment)

# output:
Traceback (most recent call last):
  File "doc_ex.py", line 40, in <module>
    raise saved_exc
  File "doc_ex.py", line 30, in <module>
    params.adjust(adjustment)
  File "/home/henrydoupe/Documents/ParamTools/paramtools/parameters.py", line 123, in adjust
    raise self.validation_error
paramtools.exceptions.ValidationError: {'average_precipitation': ['Not a valid number: rainy.']}

Errors on input that's out of range:

adjustment["average_precipitation"][0]["value"] = 1000
adjustment["average_precipitation"][1]["value"] = 2000

params.adjust(adjustment, raise_errors=False)

print(params.errors)
# output:  {'average_precipitation': ['average_precipitation 1000.0 must be less than 50 for dimensions city=Washington, D.C. , month=November', 'average_precipitation 2000.0 must be less than 50 for dimensions city=Atlanta, GA , month=November']}

How to install ParamTools

Install from PyPI:

pip install paramtools

Install from source:

git clone https://github.com/hdoupe/ParamTools
cd ParamTools
pip install -e .

Documentation

Full documentation available at https://paramtools.readthedocs.io/.

Contributing

Contributions are welcome! Checkout CONTRIBUTING.md to get started.

Credits

ParamTools is built on top of the excellent marshmallow JSON schema and validation framework. I encourage everyone to checkout their repo and documentation. ParamTools was modeled off of Tax-Calculator's parameter processing and validation engine due to its maturity and sophisticated capabilities.

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

paramtools-0.3.3.tar.gz (50.7 kB view details)

Uploaded Source

Built Distribution

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

paramtools-0.3.3-py3-none-any.whl (61.8 kB view details)

Uploaded Python 3

File details

Details for the file paramtools-0.3.3.tar.gz.

File metadata

  • Download URL: paramtools-0.3.3.tar.gz
  • Upload date:
  • Size: 50.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for paramtools-0.3.3.tar.gz
Algorithm Hash digest
SHA256 0aff91c3ab58291625038fc79460542f12a17851ed8ddc7ebfc9b6ccca15416b
MD5 b3ee237cb031d3751d34a71ec894fe9e
BLAKE2b-256 e37ea0727e967eb753108e339db57fea590cfae2468125460dbcd53058d15048

See more details on using hashes here.

File details

Details for the file paramtools-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: paramtools-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 61.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for paramtools-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c313b381165e7a5b54d782acc32794a80a2c9d4f8adc0cc22b96fa99cc6413d8
MD5 07720b350cd555921e9aef66d823c396
BLAKE2b-256 43297018b967bc3fea3b344a8d791c4c60b7a2834838df27588dd594a0af19f3

See more details on using hashes here.

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