Skip to main content

chika: a dataclass-based simple and easy config tool

Project description

chika

chika is a simple and easy config tool for hierarchical configurations.

Requirements

  • Python>=3.9 (typing.get_* requires Python3.8 or higher)

Installation

pip install -U chika

or

pip install -U git+https://github.com/moskomule/chika

Usage

Write typed configurations using chika.config, which depends on dataclass.

# main.py
import enum
import chika


@chika.config
class ModelConfig:
    name: str = chika.choices('resnet', 'densenet')
    depth: int = 10


@chika.config
class DataConfig:
    # values that needs to be specified
    name: str = chika.required(help="name of dataset")


class Optims(str, enum.Enum):
    # currently, only StrEnum is supported
    sgd = "sgd"
    adam = "adam"


@chika.config
class OptimConfig:
    name: Optims = Optims.sgd
    steps: list[int] = chika.sequence(100, 150)


@chika.config
class BaseConfig:
    model: ModelConfig
    data: DataConfig
    optim: OptimConfig

    seed: int = chika.with_help(1, help="random seed")
    use_amp: bool = False
    gpu: int = chika.choices(*range(torch.cuda.device_count()), help="id of gpu")

Then, wrap the main function with chika.main(BaseConfig).

@chika.main(BaseConfig)
def main(cfg: BaseConfig):
    set_seed(cfg.seed)
    model = ModelRegistry(cfg.model)
    ...

Now, main.py can be executed as...

python main.py --use_amp
# cfg.use_amp == True

python main.py --model config/densenet.yaml
# cfg.model.name == densenet
# cfg.model.depth == 12

python main.py --model config/densenet.yaml --model.depth 24
# cfg.model.name == densenet
# cfg.model.depth == 24

python main.py --optim.decay_steps 120 150
# config.optim.decay_steps == [120, 150]

python main.py -h
#usage: test.py [-h] [--model MODEL] [--model.name {resnet,densenet}] [--model.depth MODEL.DEPTH] [--data DATA] --data.name DATA.NAME [--optim OPTIM] [--optim.steps OPTIM.STEPS [OPTIM.STEPS ...]]
#               [--seed SEED] [--use_amp] [--gpu {1,2,3}]
#
#optional arguments:
#  -h, --help            show this help message and exit
#  --model MODEL         load {yaml,yml,json} file for model if necessary
#  --model.name {resnet,densenet}
#                        (default: 'resnet')
#  --model.depth MODEL.DEPTH
#                        (default: 10)
#  --data DATA           load {yaml,yml,json} file for data if necessary
#  --data.name DATA.NAME
#                        name of dataset (required) (default: None)
#  --optim OPTIM         load {yaml,yml,json} file for optim if necessary
#  --optim.steps OPTIM.STEPS [OPTIM.STEPS ...]
#                        (default: [100, 150])
#  --seed SEED           random seed (default: 1)
#  --use_amp             (default: False)
#  --gpu {1,2,3}         id of gpu (default: 1)

Child configs can be updated via YAML or JSON files.

# config/densenet.yaml
model:
  name: densenet
  depth: 12 

For chika.Config, the following functions are prepared:

def with_help(default, help): ...


# add help message
def choices(*values, help): ...


# add candidates that should be selected
def sequence(*values, size, help): ...


# add a list. size can be specified
def required(*, help): ...


# add a required value
def bounded(default, _from, _to, *help): ...
# add boundaries

Working Directory

change_job_dir=True creates a unique directory for each run.

@chika.main(BaseConfig, change_job_dir=True)
def main(cfg):
    print(Path(".").resolve())
    # /home/user/outputs/0901-122412-558f5a
    print(Path(".") / "weights.pt")
    # /home/user/outputs/0901-122412-558f5a/weights.pt
    print(chika.original_path)
    # /home/user
    print(chika.resolve_original_path("weights.pt"))
    # /home/user/weights.pt

Other APIs

from chika import ChikaConfig

cfg = ChikaConfig.from_dict(...)

cfg.to_dict()
# {"model": {"name": "resnet", "zero_init": True, ...}, ...}

Known issues and ToDos

  • Configs cannot be nested twice or more than twice. Config(Config(...)) is valid, but Config(Config(Config(...))) is invalid.

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

chika-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

chika-0.1.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: chika-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for chika-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1e07224f465ad1991bed0a85379fa430a9a62970bb36a1b2582ef9a7a0ad40e9
MD5 e0c7012cd3491672c04b9e82f05582ab
BLAKE2b-256 0ddb4dba47e1e8c08363163f9d443e338b08a4a6238588164be7fa3a6a9247d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: chika-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for chika-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2cc0b13e7f6a4555539c5828b4ac061e4dc9f56ac5bad506819dbcc9335cfd7a
MD5 ee6c433deeb3a6ae908cb3e1261d3dc1
BLAKE2b-256 c077361b2a1186c800b9f42b382a200669e283a8bf8a7f1aef08278968ea5305

See more details on using hashes here.

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