Skip to main content

Hackable hyperparameter configuration with decorators.

Project description

Ornamentalist

Ornamentalist is a tiny library for configuring functions with fixed hyperparameters in Python. The goal is to allow research code to be more flexible and hackable, without losing readability.

The core thing ornamentalist does is it allows you to specify the parameters of a function as Configurable. Ornamentalist will then replace the function with a 'partial' version of itself, where the parameters are fixed to the values supplied by your configuration.

I encourage you to read the short blog post to understand the motivation behind this libary and why I think it's a good solution. For worked examples of how to use ornamentalist with other tools such as hydra, argparse, or submitit, check out the examples/ directory.

You can install ornamentalist with pip:

pip install ornamentalist

Ornamentalist is only 1-file, so feel free to copy-paste it into your projects if you prefer.

Usage

Using ornamentalist is straightforward:

  1. Mark hyperparameters as configurable by setting their default value to ornamentalist.Configurable.
  2. Decorate the function with @ornamentalist.configure().
  3. Create a config dictionary at the start of your program (usually, these values will be suppled by argparse or hydra etc.)
  4. Call ornamentalist.setup_config(config) before running any configurable functions.

Quickstart

Tip: You can find this file in examples/basics.py. Download and play with it to get a feel for ornamentalist :).

import ornamentalist
from ornamentalist import Configurable


# basic usage of ornamentalist...
# setting verbose=True is useful for debugging
@ornamentalist.configure(verbose=True)
def add_n(x: int, n: int = Configurable):
    print(x + n)


# by default, ornamentalist looks for parameters
# in CONFIG_DICT[func.__name__],
# you can override this with a custom key like so
@ornamentalist.configure(name="greeting_config")
def greet(name: str = Configurable):
    print(f"Hello, {name}")


# you can even use ornamentalist on classes!
class MyClass:
    # you probably want to give constructors custom
    # names, else they will just be "__init__"
    @ornamentalist.configure(name="myclass.init")
    def __init__(self, a: float = Configurable):
        print(a)


if __name__ == "__main__":
    # usually config would be supplied by argparse or
    # hydra etc. but we will hardcode it here...
    config = {
        "add_n": {"n": 5},
        # greeting_config and myclass_init are the
        # custom names we specified earlier
        "greeting_config": {"name": "Alice"},
        "myclass.init": {"a": 4.5},
    }
    ornamentalist.setup_config(config)

    add_n(10)
    greet()
    c = MyClass()

    # you can access the config dict anywhere in your program
    # through `ornamentalist.get_config()`
    assert ornamentalist.get_config() == config

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

ornamentalist-1.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

ornamentalist-1.0.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file ornamentalist-1.0.0.tar.gz.

File metadata

  • Download URL: ornamentalist-1.0.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for ornamentalist-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3a85c05a97609e534a90ddc569379d241cdf940c65ecebe01b4b3a385c761cd9
MD5 ef0573aa7a0623205f522a337136cee4
BLAKE2b-256 2721162f760e41c6772c00b6aef271d675fc8f899143af14467ddee4991484a5

See more details on using hashes here.

File details

Details for the file ornamentalist-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ornamentalist-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8dec0953ee6b4663cfc2c7a02f67e8a0a214e313d54d795f1f2678c29d4692e8
MD5 4d220190d64616add4bcc0a794346cd2
BLAKE2b-256 6990240e4f7b9e442ce1e41da42e7231dfa15c8979e0e7144871bb1baa3e235b

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