Skip to main content

Use Pydantic to build complex types from simple parameters.

Project description

Use Pydantic to build complex types from parameter specifications.


Installation

  • uv add cyantic

Features

  • Build complex objects using intermediate Pydantic models.
  • Reference other values using @value:x.y.z
  • Import objects using @import:x.y.z
  • Define custom @reftag handlers (see tests)

Usage

from cyantic import Blueprint, blueprint, CyanticModel
from torch import Tensor
import torch
import yaml

# 1. Create and register some useful parameterisations
#       (or soon install from PyPi, i.e. `rye add cyantic-torch`)

@blueprint(Tensor)
class NormalTensor(Blueprint[Tensor]):

    mean: float
    std: float
    size: tuple[int, ...]

    def build(self) -> Tensor:
        return torch.normal(self.mean, self.std, size=self.size)

@blueprint(Tensor)
class UniformTensor(Blueprint[Tensor]):
    low: float
    high: float
    size: tuple[int, ...]

    def build(self) -> Tensor:
      return torch.empty(self.size).uniform_(self.low, self.high)


# 2. Write pydantic models using `CyanticModel` base class

class MyModel(CyanticModel):
    normal_tensor: Tensor
    uniform_tensor: Tensor


# 3. Validate from YAML files that specify the parameterisation

some_yaml = """common:
    size: [3, 5]
normal_tensor:
    mean: 0.0
    std: 0.1
    size: @value:common.size
uniform_tensor:
    low: -1.0
    std: 1.0
    size: @value:common.size
"""

# 4. Receive objects built from the parameterisations.

my_model = MyModel.model_validate(yaml.safe_load(some_yaml))
assert isinstance(my_model.normal_tensor, Tensor)
assert isinstance(my_model.uniform_tensor, Tensor)

Development

  • git clone https://github.com/flywhl/cyantic.git
  • cd cyantic
  • uv sync
  • just test

Flywheel

Science needs humble software tools. Flywheel is an open source collective building simple tools to preserve scientific momentum, inspired by devtools and devops culture.

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

cyantic-0.1.3.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

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

cyantic-0.1.3-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file cyantic-0.1.3.tar.gz.

File metadata

  • Download URL: cyantic-0.1.3.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.15

File hashes

Hashes for cyantic-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e70ead9f57e097cdb2996e237d951e3198e88c126e1e5ca7f892c5fd08635701
MD5 a2b5515759786096076c011bfe53f8a6
BLAKE2b-256 e037b9239bce79b0e7ba6c1dc154ffa9dfed3cdc88da32ad55b4620102e1cc2a

See more details on using hashes here.

File details

Details for the file cyantic-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: cyantic-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.15

File hashes

Hashes for cyantic-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 81ddcd3bb5377486fcb7b22386ac794b968bc683c1b70b6fab6542d32fbe754f
MD5 40ecfda3b752e0b86b5fb97abdda83ea
BLAKE2b-256 0c92f6fc7fc8bd1972f3c4767b5ed542a506a074d445366e533bc6eefe6ae55c

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