diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 889ca0b..4df841a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,6 +15,7 @@ repos: - id: mypy args: [--no-strict-optional, --ignore-missing-imports] files: ^(hamilflow/|tests/) + additional_dependencies: [types-PyYAML] - repo: https://github.com/ambv/black rev: 24.4.2 hooks: diff --git a/datasets/miminal.yaml b/datasets/miminal.yaml new file mode 100644 index 0000000..86c2f07 --- /dev/null +++ b/datasets/miminal.yaml @@ -0,0 +1,11 @@ +version: 0.1.0 +models: + brownian_motion: + definition: + system: + sigma: 1 + delta_t: 1 + initial_condition: + x0: 0 + args: + n_steps: 100 diff --git a/hamilflow/cli.py b/hamilflow/cli.py new file mode 100644 index 0000000..91817f1 --- /dev/null +++ b/hamilflow/cli.py @@ -0,0 +1,39 @@ +import os +from pathlib import Path + +import click + +from hamilflow.dataset.config import Dispatcher, read_yaml + + +@click.group(invoke_without_command=True) +@click.pass_context +def hamilflow(ctx): + if ctx.invoked_subcommand is None: + click.echo("Hello {}".format(os.environ.get("USER", ""))) + click.echo( + "Welcome to HamilFlow. Use `hamilflow --help` to find all the commands." + ) + else: + click.echo("Loading Service: %s" % ctx.invoked_subcommand) + + +@hamilflow.command() +@click.argument("path", type=click.Path(exists=True, path_type=Path), required=True) +def generate(path: Path): + """Generate dataset based on an input config file. + + :param path: location of the config file. + """ + + click.secho(f"Reading config from: {path}") + + config = read_yaml(path) + dispatcher = Dispatcher() + + for model_name, model_config in config["models"].items(): + click.secho(model_config) + model = dispatcher[model_name](**model_config["definition"]) + data = model(**model_config["args"]) + + # ... # save data or load into a data container. diff --git a/hamilflow/dataset/__init__.py b/hamilflow/dataset/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hamilflow/dataset/config.py b/hamilflow/dataset/config.py new file mode 100644 index 0000000..b13f464 --- /dev/null +++ b/hamilflow/dataset/config.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass +from pathlib import Path +from typing import Any + +import yaml + +from hamilflow.models.brownian_motion import BrownianMotion + + +@dataclass +class Dispatcher: + brownian_motion = BrownianMotion + + def __getitem__(self, item: str) -> Any: + return getattr(self, item) + + +def read_yaml(path: Path) -> dict: + return yaml.safe_load(path.read_text()) diff --git a/pyproject.toml b/pyproject.toml index 3ee33e6..6dba2d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ pydantic = "^2.6.1" loguru = "^0.7.2" matplotlib = "^3.8.3" scipy = "^1.12.0" +click = "^8.1.7" [tool.poetry.group.test.dependencies] @@ -38,6 +39,8 @@ plotly = "^5.19.0" [tool.poetry.group.dev.dependencies] commitizen = "*" +[tool.poetry.scripts] +hamilflow = "hamilflow.cli:hamilflow" [tool.commitizen] name = "cz_conventional_commits"