def test_kedro_mlflow_config_setup_tracking_priority(mocker, tmp_path, config_dir): """Test if the mlflow_tracking uri set is the one of mlflow.yml if it also eist in credentials. Args: mocker ([type]): [description] tmp_path ([type]): [description] config_dir ([type]): [description] """ # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project mocker.patch("kedro_mlflow.utils._is_kedro_project", lambda x: True) (tmp_path / "conf/base/credentials.yml").write_text( yaml.dump(dict(my_mlflow_creds=dict(mlflow_tracking_uri="mlruns2")))) config = KedroMlflowConfig( project_path=tmp_path, mlflow_tracking_uri="mlruns1", credentials="my_mlflow_creds", ) context = load_context(tmp_path) config.setup(context) assert mlflow.get_tracking_uri() == (tmp_path / "mlruns1").as_uri()
def test_kedro_mlflow_config_experiment_was_deleted(kedro_project_with_mlflow_conf): # create an experiment with the same name and then delete it mlflow_tracking_uri = (kedro_project_with_mlflow_conf / "mlruns").as_uri() mlflow_client = MlflowClient(mlflow_tracking_uri) mlflow_client.create_experiment("exp1") mlflow_client.delete_experiment( mlflow_client.get_experiment_by_name("exp1").experiment_id ) # the config must restore properly the experiment config = KedroMlflowConfig( project_path=kedro_project_with_mlflow_conf, mlflow_tracking_uri="mlruns", experiment_opts=dict(name="exp1"), ) project_metadata = _get_project_metadata(kedro_project_with_mlflow_conf) _add_src_to_path(project_metadata.source_dir, kedro_project_with_mlflow_conf) configure_project(project_metadata.package_name) with KedroSession.create( "fake_project", project_path=kedro_project_with_mlflow_conf ): config.setup() assert "exp1" in [exp.name for exp in config.mlflow_client.list_experiments()]
def test_kedro_mlflow_config_setup_tracking_priority(kedro_project_with_mlflow_conf): """Test if the mlflow_tracking uri set is the one of mlflow.yml if it also eist in credentials. Args: mocker ([type]): [description] tmp_path ([type]): [description] """ # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project (kedro_project_with_mlflow_conf / "conf/base/credentials.yml").write_text( yaml.dump(dict(my_mlflow_creds=dict(mlflow_tracking_uri="mlruns2"))) ) config = KedroMlflowConfig( project_path=kedro_project_with_mlflow_conf, mlflow_tracking_uri="mlruns1", credentials="my_mlflow_creds", ) project_metadata = _get_project_metadata(kedro_project_with_mlflow_conf) _add_src_to_path(project_metadata.source_dir, kedro_project_with_mlflow_conf) configure_project(project_metadata.package_name) with KedroSession.create( "fake_project", project_path=kedro_project_with_mlflow_conf ): config.setup() assert ( mlflow.get_tracking_uri() == (kedro_project_with_mlflow_conf / "mlruns1").as_uri() )
def test_kedro_mlflow_config_bad_long_parameters_strategy(tmp_path): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project (tmp_path / ".kedro.yml").write_text( yaml.dump(dict(context_path="fake/path"))) with pytest.raises(KedroMlflowConfigError, match="'long_parameters_strategy' must be one of "): KedroMlflowConfig( project_path=tmp_path, node_hook_opts=dict(long_parameters_strategy="does_not_exist"), )
def get_mlflow_config(context: KedroContext): try: conf_mlflow_yml = context.config_loader.get("mlflow*", "mlflow*/**") except MissingConfigException: raise KedroMlflowConfigError( "No 'mlflow.yml' config file found in environment. Use ``kedro mlflow init`` command in CLI to create a default config file." ) conf_mlflow = KedroMlflowConfig(context.project_path) conf_mlflow.from_dict(conf_mlflow_yml) return conf_mlflow
def test_kedro_mlflow_config_validate_uri_local(mocker, tmp_path, uri): mocker.patch("kedro_mlflow.utils._is_kedro_project", return_value=True) mocker.patch("mlflow.tracking.MlflowClient", return_value=None) mocker.patch( "kedro_mlflow.framework.context.config.KedroMlflowConfig._get_or_create_experiment", return_value=None, ) config = KedroMlflowConfig(project_path=tmp_path) assert config._validate_uri(uri=uri).startswith(r"file:///") # relative
def test_kedro_mlflow_config_bad_long_parameters_strategy( kedro_project_with_mlflow_conf, ): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project with pytest.raises( KedroMlflowConfigError, match="'long_parameters_strategy' must be one of " ): KedroMlflowConfig( project_path=kedro_project_with_mlflow_conf, node_hook_opts=dict(long_parameters_strategy="does_not_exist"), )
def test_from_dict_to_dict_idempotent(mocker, kedro_project_with_mlflow_conf): mocker.patch("mlflow.tracking.MlflowClient", return_value=None) mocker.patch( "kedro_mlflow.framework.context.config.KedroMlflowConfig._get_or_create_experiment", return_value=None, ) config = KedroMlflowConfig(project_path=kedro_project_with_mlflow_conf) original_config_dict = config.to_dict() # modify config config.from_dict(original_config_dict) assert config.to_dict() == original_config_dict
def test_kedro_mlflow_config_new_experiment_does_not_exists(mocker, tmp_path): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project mocker.patch("kedro_mlflow.utils._is_kedro_project", return_value=True) config = KedroMlflowConfig( project_path=tmp_path, mlflow_tracking_uri="mlruns", experiment_opts=dict(name="exp1"), ) assert "exp1" in [ exp.name for exp in config.mlflow_client.list_experiments() ]
def get_mlflow_config(session: Optional[KedroSession] = None): session = session or get_current_session() context = session.load_context() try: conf_mlflow_yml = context.config_loader.get("mlflow*", "mlflow*/**") except MissingConfigException: raise KedroMlflowConfigError( "No 'mlflow.yml' config file found in environment. Use ``kedro mlflow init`` command in CLI to create a default config file." ) conf_mlflow = KedroMlflowConfig(context.project_path) conf_mlflow.from_dict(conf_mlflow_yml) return conf_mlflow
def get_mlflow_config(project_path=None, env="local"): if project_path is None: project_path = Path.cwd() project_path = Path(project_path) conf_paths = [ str(project_path / "conf" / "base"), str(project_path / "conf" / env), ] config_loader = ConfigLoader(conf_paths=conf_paths) conf_mlflow_yml = config_loader.get("mlflow*", "mlflow*/**") conf_mlflow = KedroMlflowConfig(project_path=project_path) conf_mlflow.from_dict(conf_mlflow_yml) return conf_mlflow
def test_kedro_mlflow_config_init(kedro_project_with_mlflow_conf): # kedro_project_with_mlflow_conf is a global fixture in conftest config = KedroMlflowConfig(project_path=kedro_project_with_mlflow_conf) assert config.to_dict() == dict( mlflow_tracking_uri=(kedro_project_with_mlflow_conf / "mlruns").as_uri(), credentials=None, disable_tracking=KedroMlflowConfig.DISABLE_TRACKING_OPTS, experiments=KedroMlflowConfig.EXPERIMENT_OPTS, run=KedroMlflowConfig.RUN_OPTS, ui=KedroMlflowConfig.UI_OPTS, hooks=dict(node=KedroMlflowConfig.NODE_HOOK_OPTS), )
def test_kedro_mlflow_config_init(tmp_path): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project (tmp_path / ".kedro.yml").write_text( yaml.dump(dict(context_path="fake/path"))) config = KedroMlflowConfig(project_path=tmp_path) assert config.to_dict() == dict( mlflow_tracking_uri=(tmp_path / "mlruns").as_uri(), experiments=KedroMlflowConfig.EXPERIMENT_OPTS, run=KedroMlflowConfig.RUN_OPTS, ui=KedroMlflowConfig.UI_OPTS, hooks=dict(node=KedroMlflowConfig.NODE_HOOK_OPTS), )
def test_kedro_mlflow_config_setup_export_credentials(mocker, tmp_path, config_dir): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project mocker.patch("kedro_mlflow.utils._is_kedro_project", lambda x: True) (tmp_path / "conf/base/credentials.yml").write_text( yaml.dump(dict(my_mlflow_creds=dict(fake_mlflow_cred="my_fake_cred")))) # the config must restore properly the experiment config = KedroMlflowConfig(project_path=tmp_path, credentials="my_mlflow_creds") context = load_context(tmp_path) config.setup(context) assert os.environ["fake_mlflow_cred"] == "my_fake_cred"
def test_kedro_mlflow_config_experiment_exists(mocker, tmp_path, config_dir): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project mocker.patch("kedro_mlflow.utils._is_kedro_project", return_value=True) # create an experiment with the same name mlflow_tracking_uri = (tmp_path / "mlruns").as_uri() MlflowClient(mlflow_tracking_uri).create_experiment("exp1") config = KedroMlflowConfig( project_path=tmp_path, mlflow_tracking_uri="mlruns", experiment_opts=dict(name="exp1"), ) context = load_context(tmp_path) config.setup(context) assert "exp1" in [ exp.name for exp in config.mlflow_client.list_experiments() ]
def test_kedro_mlflow_config_setup_set_tracking_uri(mocker, tmp_path, config_dir): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project mocker.patch("kedro_mlflow.utils._is_kedro_project", lambda x: True) # create an experiment with the same name and then delete it mlflow_tracking_uri = (tmp_path / "awesome_tracking").as_uri() # the config must restore properly the experiment config = KedroMlflowConfig( project_path=tmp_path, mlflow_tracking_uri="awesome_tracking", experiment_opts=dict(name="exp1"), ) context = load_context(tmp_path) config.setup(context) assert mlflow.get_tracking_uri() == mlflow_tracking_uri
def test_kedro_mlflow_config_new_experiment_does_not_exists( kedro_project_with_mlflow_conf, ): config = KedroMlflowConfig( project_path=kedro_project_with_mlflow_conf, mlflow_tracking_uri="mlruns", experiment_opts=dict(name="exp1"), ) project_metadata = _get_project_metadata(kedro_project_with_mlflow_conf) _add_src_to_path(project_metadata.source_dir, kedro_project_with_mlflow_conf) configure_project(project_metadata.package_name) with KedroSession.create( "fake_project", project_path=kedro_project_with_mlflow_conf ): config.setup() assert "exp1" in [exp.name for exp in config.mlflow_client.list_experiments()]
def test_kedro_mlflow_config_experiment_was_deleted(mocker, tmp_path): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project mocker.patch("kedro_mlflow.utils._is_kedro_project", lambda x: True) # create an experiment with the same name and then delete it mlflow_tracking_uri = (tmp_path / "mlruns").as_uri() mlflow_client = MlflowClient(mlflow_tracking_uri) mlflow_client.create_experiment("exp1") mlflow_client.delete_experiment( mlflow_client.get_experiment_by_name("exp1").experiment_id) # the config must restore properly the experiment config = KedroMlflowConfig( project_path=tmp_path, mlflow_tracking_uri="mlruns", experiment_opts=dict(name="exp1"), ) assert "exp1" in [ exp.name for exp in config.mlflow_client.list_experiments() ]
def test_kedro_mlflow_config_setup_export_credentials(kedro_project_with_mlflow_conf): (kedro_project_with_mlflow_conf / "conf/base/credentials.yml").write_text( yaml.dump(dict(my_mlflow_creds=dict(fake_mlflow_cred="my_fake_cred"))) ) # the config must restore properly the experiment config = KedroMlflowConfig( project_path=kedro_project_with_mlflow_conf, credentials="my_mlflow_creds" ) project_metadata = _get_project_metadata(kedro_project_with_mlflow_conf) _add_src_to_path(project_metadata.source_dir, kedro_project_with_mlflow_conf) configure_project(project_metadata.package_name) with KedroSession.create( "fake_project", project_path=kedro_project_with_mlflow_conf ): config.setup() assert os.environ["fake_mlflow_cred"] == "my_fake_cred"
def test_kedro_mlflow_config_experiment_exists(mocker, kedro_project_with_mlflow_conf): # create an experiment with the same name mlflow_tracking_uri = ( kedro_project_with_mlflow_conf / "conf" / "local" / "mlruns" ).as_uri() MlflowClient(mlflow_tracking_uri).create_experiment("exp1") config = KedroMlflowConfig( project_path=kedro_project_with_mlflow_conf, mlflow_tracking_uri="mlruns", experiment_opts=dict(name="exp1"), ) project_metadata = _get_project_metadata(kedro_project_with_mlflow_conf) _add_src_to_path(project_metadata.source_dir, kedro_project_with_mlflow_conf) configure_project(project_metadata.package_name) with KedroSession.create( "fake_project", project_path=kedro_project_with_mlflow_conf ): config.setup() assert "exp1" in [exp.name for exp in config.mlflow_client.list_experiments()]
def test_kedro_mlflow_config_setup_set_tracking_uri(kedro_project_with_mlflow_conf): # create an experiment with the same name and then delete it mlflow_tracking_uri = (kedro_project_with_mlflow_conf / "awesome_tracking").as_uri() # the config must restore properly the experiment config = KedroMlflowConfig( project_path=kedro_project_with_mlflow_conf, mlflow_tracking_uri="awesome_tracking", experiment_opts=dict(name="exp1"), ) project_metadata = _get_project_metadata(kedro_project_with_mlflow_conf) _add_src_to_path(project_metadata.source_dir, kedro_project_with_mlflow_conf) configure_project(project_metadata.package_name) with KedroSession.create( "fake_project", project_path=kedro_project_with_mlflow_conf ): config.setup() assert mlflow.get_tracking_uri() == mlflow_tracking_uri
def get_mlflow_config(context: KedroContext): conf_mlflow_yml = context.config_loader.get("mlflow*", "mlflow*/**") conf_mlflow = KedroMlflowConfig(context.project_path) conf_mlflow.from_dict(conf_mlflow_yml) return conf_mlflow
def test_kedro_mlflow_config_init_wrong_path(tmp_path): # create a ".kedro.yml" file to identify "tmp_path" as the root of a kedro project with pytest.raises(KedroMlflowConfigError, match="not a valid path to a kedro project"): KedroMlflowConfig(project_path=tmp_path)