-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example program
tracking_dummy.py
- Refactor and improve test infrastructure - Improve README about standalone usage
- Loading branch information
Showing
9 changed files
with
249 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
About | ||
Use MLflow and CrateDB to track the metrics and parameters of a dummy ML | ||
experiment program. | ||
- https://github.com/crate-workbench/mlflow-cratedb | ||
- https://mlflow.org/docs/latest/tracking.html | ||
Usage | ||
Before running the program, optionally define the `MLFLOW_TRACKING_URI` environment | ||
variable, in order to record events and metrics either directly into the database, | ||
or by submitting them to an MLflow Tracking Server. | ||
# Use CrateDB database directly | ||
export MLFLOW_TRACKING_URI="crate://crate@localhost/?schema=mlflow" | ||
# Use MLflow Tracking Server | ||
export MLFLOW_TRACKING_URI=http://127.0.0.1:5000 | ||
Resources | ||
- https://mlflow.org/ | ||
- https://github.com/crate/crate | ||
""" | ||
import logging | ||
import sys | ||
|
||
import mlflow | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
def run_experiment(): | ||
""" | ||
Run an MLflow dummy workflow, without any data. | ||
""" | ||
logger.info("Running experiment") | ||
mlflow.set_experiment("dummy-experiment") | ||
|
||
mlflow.log_metric("precision", 0.33) | ||
mlflow.log_metric("recall", 0.48) | ||
mlflow.log_metric("f1", 0.85) | ||
mlflow.log_metric("mttd", 42.42) | ||
mlflow.log_param("anomaly_threshold", 0.10) | ||
mlflow.log_param("min_alm_window", 3600) | ||
mlflow.log_param("alm_window_minutes", 60) | ||
mlflow.log_param("alm_suppress_minutes", 5) | ||
mlflow.log_param("ensemble_size", 25) | ||
|
||
|
||
def start_adapter(): | ||
logger.info("Initializing CrateDB adapter") | ||
import mlflow_cratedb # noqa: F401 | ||
|
||
|
||
def setup_logging(): | ||
logging.basicConfig(stream=sys.stderr, level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s") | ||
|
||
|
||
def main(): | ||
""" | ||
Run dummy experiment. | ||
""" | ||
setup_logging() | ||
start_adapter() | ||
run_experiment() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,57 @@ | ||
import mlflow | ||
import os | ||
|
||
import pytest | ||
|
||
from mlflow_cratedb import patch_all | ||
|
||
patch_all() | ||
|
||
import mlflow | ||
import mlflow.store.tracking.sqlalchemy_store as mlflow_tracking | ||
import sqlalchemy as sa | ||
|
||
ARTIFACT_URI = "testdrive_folder" | ||
|
||
# The canonical database schema used for test purposes is `testdrive`. | ||
DB_URI = "crate://crate@localhost/?schema=testdrive" | ||
|
||
@pytest.fixture(autouse=True) | ||
def reset_environment() -> None: | ||
""" | ||
Make sure software tests do not pick up any environment variables. | ||
""" | ||
if "MLFLOW_TRACKING_URI" in os.environ: | ||
del os.environ["MLFLOW_TRACKING_URI"] | ||
|
||
|
||
@pytest.fixture | ||
def db_uri() -> str: | ||
""" | ||
The canonical database schema used for testing purposes is `testdrive`. | ||
""" | ||
return "crate://crate@localhost/?schema=testdrive" | ||
|
||
|
||
@pytest.fixture | ||
def testdrive_engine(): | ||
yield mlflow.store.db.utils.create_sqlalchemy_engine_with_retry(DB_URI) | ||
def engine(db_uri): | ||
""" | ||
Provide an SQLAlchemy engine object using the `testdrive` schema. | ||
""" | ||
yield mlflow.store.db.utils.create_sqlalchemy_engine_with_retry(db_uri) | ||
|
||
|
||
@pytest.fixture | ||
def tracking_store(engine: sa.Engine) -> mlflow_tracking.SqlAlchemyStore: | ||
""" | ||
A fixture for providing an instance of `SqlAlchemyStore`. | ||
""" | ||
yield mlflow_tracking.SqlAlchemyStore(str(engine.url), ARTIFACT_URI) | ||
|
||
|
||
@pytest.fixture | ||
def reset_database(engine: sa.Engine): | ||
""" | ||
Make sure to reset the database by dropping and re-creating tables. | ||
""" | ||
from mlflow_cratedb.adapter.setup_db import _setup_db_create_tables, _setup_db_drop_tables | ||
|
||
_setup_db_drop_tables(engine=engine) | ||
_setup_db_create_tables(engine=engine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.