Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI using flags #108

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,22 @@ python -m carps.utils.check_missing rundir

# Gather logs from files
python -m carps.analysis.gather_data rundir
```

# Commands using Flags
```bash
# Fill database
carps --create_cluster_configs --optimizer DUMMY/config --problem DUMMY/config

# Run from database (with local env)
carps --run_from_db

# Run local with filelogging
carps --run --optimizer DUMMY/config --problem DUMMY/config --seed 0

# Check missing runs (creates runcommands_missing.sh)
carps --check_missing

# Gather logs from files
carps --gather_data
```
113 changes: 113 additions & 0 deletions carps/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
# CLI.

This module defines command-line options using flags.

This includes the entry point for the programs execution.
"""

import subprocess
from typing import Any

from absl import app, flags

FLAGS = flags.FLAGS
flags.DEFINE_boolean(
"create_cluster_configs",
short_name="c",
default=False,
help="Create cluster configs in the database. Passing paths to the "
"optimizer and problem config to be used is required.",
)
flags.DEFINE_boolean(
"run_from_db",
short_name="db",
default=False,
help="Start runs for all configs in the database.",
)
flags.DEFINE_boolean(
"run",
short_name="r",
default=False,
help="Start run for provided optimizer, problem, and seed. Passing paths to "
"the optimizer and problem config to be used is required, as well as "
"providing a seed.",
)
flags.DEFINE_boolean(
"reset_experiments",
short_name="re",
default=False,
help="Reset status of experiments in the database.",
)
flags.DEFINE_boolean(
"check_missing", short_name="cm", default=False, help="Check for missing runs."
)
flags.DEFINE_boolean(
"gather_data",
short_name="gd",
default=False,
help="Gather logs from the run files.",
)
flags.DEFINE_string(
"optimizer", short_name="o", default=None, help="Path to optimizer config."
)
flags.DEFINE_string(
"problem", short_name="p", default=None, help="Path to problem config."
)
flags.DEFINE_string(
"seed",
short_name="s",
default=None,
help="Seed to be used when running the experiment.",
)
flags.DEFINE_string(
"rundir",
short_name="rd",
default="runs",
help="Path to the run directory where results and logs are stored.",
)


def main(argv: Any) -> None:
"""Call the function to execute."""
if FLAGS.create_cluster_configs:
if FLAGS.optimizer is None or FLAGS.problem is None:
print("Please provide optimizer and problem.")
return
subprocess.call(
[
"python",
"-m",
"carps.container.create_cluster_configs",
f"+optimizer={FLAGS.optimizer}",
f"+problem={FLAGS.problem}",
]
)
if FLAGS.run_from_db:
subprocess.call(["python", "-m", "carps.run_from_db"])
if FLAGS.run:
if FLAGS.optimizer is None or FLAGS.problem is None or FLAGS.seed is None:
print("Please provide optimizer, problem and seed.")
return
subprocess.call(
[
"python",
"-m",
"carps.run",
f"+optimizer={FLAGS.optimizer}",
f"+problem={FLAGS.problem}",
f"seed={FLAGS.seed}",
]
)
if FLAGS.reset_experiments:
subprocess.call(["python", "-m", "carps.utils.database.reset_experiments"])
if FLAGS.check_missing:
subprocess.call(["python", "-m", "carps.utils.check_missing", FLAGS.rundir])
if FLAGS.gather_data:
subprocess.call(["python", "-m", "carps.analysis.gather_data", FLAGS.rundir])


if __name__ == "__main__":
pass

app.run(main)
75 changes: 48 additions & 27 deletions carps/container/create_cluster_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,33 @@ def main(cfg: DictConfig) -> None:
"""
cfg_dict = OmegaConf.to_container(cfg=cfg, resolve=True)

experiment_configuration_file_path = cfg.pyexperimenter_configuration_file_path or Path(__file__).parent / "py_experimenter.yaml"

database_credential_file_path = cfg.database_credential_file_path or Path(__file__).parent / "credentials.yaml"
if database_credential_file_path is not None and not database_credential_file_path.exists():
experiment_configuration_file_path = (
cfg.pyexperimenter_configuration_file_path
or Path(__file__).parent / "py_experimenter.yaml"
)

database_credential_file_path = (
cfg.database_credential_file_path or Path(__file__).parent / "credentials.yaml"
)
if (
database_credential_file_path is not None
and not database_credential_file_path.exists()
):
database_credential_file_path = None

experimenter = PyExperimenter(experiment_configuration_file_path=experiment_configuration_file_path,
name="carps",
database_credential_file_path=database_credential_file_path,
log_level=logging.INFO,
use_ssh_tunnel=OmegaConf.load(experiment_configuration_file_path).PY_EXPERIMENTER.Database.use_ssh_tunnel
)
experimenter = PyExperimenter(
experiment_configuration_file_path=experiment_configuration_file_path,
name="carps",
database_credential_file_path=database_credential_file_path,
log_level=logging.INFO,
use_ssh_tunnel=OmegaConf.load(
experiment_configuration_file_path
).PY_EXPERIMENTER.Database.use_ssh_tunnel,
)

cfg_json = OmegaConf.to_container(cfg, resolve=True)

# This value will always be unique so it
# This value will always be unique so it
# disables duplicate checking when adding entries to the database.
# Py_experimenter will add a creation date so the information
# is not lost.
Expand All @@ -54,19 +65,23 @@ def main(cfg: DictConfig) -> None:
cfg_str = json.dumps(cfg_json)
cfg_hash = hashlib.sha256(cfg_str.encode()).hexdigest()

rows = [{
"config": cfg_str,
"config_hash": cfg_hash,
"benchmark_id": cfg_dict["benchmark_id"],
"problem_id": cfg_dict["problem_id"],
"optimizer_id": cfg_dict["optimizer_id"],
"optimizer_container_id": cfg_dict["optimizer_container_id"],
"seed": cfg_dict["seed"],
"n_trials": cfg_dict["task"]["n_trials"],
"time_budget": cfg_dict["task"]["time_budget"],
}]

column_names = list(experimenter.db_connector.database_configuration.keyfields.keys())
rows = [
{
"config": cfg_str,
"config_hash": cfg_hash,
"benchmark_id": cfg_dict["benchmark_id"],
"problem_id": cfg_dict["problem_id"],
"optimizer_id": cfg_dict["optimizer_id"],
"optimizer_container_id": cfg_dict["optimizer_container_id"],
"seed": cfg_dict["seed"],
"n_trials": cfg_dict["task"]["n_trials"],
"time_budget": cfg_dict["task"]["time_budget"],
}
]

column_names = list(
experimenter.db_connector.database_configuration.keyfields.keys()
)

exists = False

Expand All @@ -78,16 +93,22 @@ def main(cfg: DictConfig) -> None:
for e in existing_rows:
if e["config_hash"] == cfg_hash:
exists = True
logger.info("Experiment not added to the database because config hash already exists!")
logger.info(
"Experiment not added to the database because config hash already exists!"
)
except DatabaseConnectionError as e:
if "1146" in e.args[0] or "no such table" in e.args[0]:
logger.info("Database empty, will fill.:)")
logger.info("Database does not exist and will be created.")
else:
raise e

if not exists:
experimenter.fill_table_with_rows(rows)

logger.info("Experiment was added to the database.")
logger.info(
"Next, add more experiments or run all experiments in the database "
"by calling 'carps --run_from_db'."
)
return None


Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name = "CARP-S"
version = "0.1.0"
dependencies = [
"typing_extensions", # Better typing
"tomli",
"pre-commit",
"tomli",
"pre-commit",
"pytest==6.2.4",
"coverage==4.5.4",
"ruff",
Expand Down Expand Up @@ -45,6 +45,9 @@ classifiers = [
]
license = { file = "LICENSE" }

[project.scripts]
carps = "carps.cli:main"

[project.optional-dependencies]
dev = ["carps[doc, tooling, test, examples]"]
tooling = ["commitizen", "pre-commit", "ruff"]
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def read_file(filepath: str) -> str:
"numpy"
],
extras_require=extras_require,
entry_points={
"console_scripts": ["carps = carps.cli:main"],
},
test_suite="pytest",
platforms=["Linux"],
classifiers=[
Expand Down