from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from mlflow.tensorflow import _TF2Wrapper from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, _compare_logged_code_paths, ) from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import EXTRA_PYFUNC_SERVING_TEST_ARGS = ([] if _is_available_on_pypi("tensorflow") else ["--env-manager", "local"]) SavedModelInfo = collections.namedtuple( "SavedModelInfo", [ "path", "meta_graph_tags", "signature_def_key", "inference_df", "expected_results_df", "raw_results", "raw_df", ], )
from mlflow.exceptions import MlflowException from mlflow.models import Model, infer_signature from mlflow.models.utils import _read_example from mlflow.tracking.artifact_utils import _download_artifact_from_uri from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from tests.conftest import tracking_uri_mock # pylint: disable=unused-import, E0611 from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = [] if _is_available_on_pypi("spacy") else ["--no-conda"] ModelWithData = namedtuple("ModelWithData", ["model", "inference_data"]) IS_SPACY_VERSION_NEWER_THAN_OR_EQUAL_TO_3_0_0 = Version(spacy.__version__) >= Version("3.0.0") @pytest.fixture(scope="module") def spacy_model_with_data(): # Creating blank model and setting up the spaCy pipeline nlp = spacy.blank("en") if IS_SPACY_VERSION_NEWER_THAN_OR_EQUAL_TO_3_0_0: from spacy.pipeline.tok2vec import DEFAULT_TOK2VEC_MODEL model = {
_logger = logging.getLogger(__name__) # This test suite is included as a code dependency when testing PyTorch model scoring in new # processes and docker containers. In these environments, the `tests` module is not available. # Therefore, we attempt to import from `tests` and gracefully emit a warning if it's unavailable. try: from tests.helper_functions import pyfunc_serve_and_score_model from tests.helper_functions import set_boto_credentials # pylint: disable=unused-import from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import except ImportError: _logger.warning( "Failed to import test helper functions. Tests depending on these functions may fail!" ) EXTRA_PYFUNC_SERVING_TEST_ARGS = [] if _is_available_on_pypi("torch") else [ "--no-conda" ] @pytest.fixture(scope="module") def data(): iris = datasets.load_iris() data = pd.DataFrame(data=np.c_[iris["data"], iris["target"]], columns=iris["feature_names"] + ["target"]) y = data["target"] x = data.drop("target", axis=1) return x, y def get_dataset(data):
from mlflow.store.artifact.s3_artifact_repo import S3ArtifactRepository from mlflow.tracking.artifact_utils import _download_artifact_from_uri from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from tests.helper_functions import set_boto_credentials # pylint: disable=unused-import from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = [] if _is_available_on_pypi("catboost") else [ "--no-conda" ] ModelWithData = namedtuple("ModelWithData", ["model", "inference_dataframe"]) def get_iris(): iris = datasets.load_iris() X = pd.DataFrame(iris.data[:, :2], columns=iris.feature_names[:2]) y = pd.Series(iris.target) return X, y def read_yaml(path): with open(path, "r") as f:
from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from tests.prophet.test_prophet_model_export import DataGeneration from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( _compare_conda_env_requirements, _assert_pip_requirements, pyfunc_serve_and_score_model, _is_available_on_pypi, _compare_logged_code_paths, ) pytestmark = pytest.mark.large EXTRA_PYFUNC_SERVING_TEST_ARGS = [] if _is_available_on_pypi("pmdarima") else [ "--no-conda" ] @pytest.fixture(scope="function") def model_path(tmpdir): return os.path.join(str(tmpdir), "model") @pytest.fixture def pmdarima_custom_env(tmpdir): conda_env = os.path.join(str(tmpdir), "conda_env.yml") _mlflow_conda_env(conda_env, additional_pip_deps=["pmdarima"]) return conda_env
from mlflow.tracking.artifact_utils import _download_artifact_from_uri from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from tests.helper_functions import set_boto_credentials # pylint: disable=unused-import from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = [] if _is_available_on_pypi("lightgbm") else [ "--no-conda" ] ModelWithData = namedtuple("ModelWithData", ["model", "inference_dataframe"]) @pytest.fixture(scope="session") def lgb_model(): iris = datasets.load_iris() X = pd.DataFrame( iris.data[:, :2], columns=iris.feature_names[:2] # we only take the first two features. ) y = iris.target
from mlflow.models.utils import _read_example from mlflow.tracking.artifact_utils import _download_artifact_from_uri from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from tests.conftest import tracking_uri_mock # pylint: disable=unused-import, E0611 from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, allow_infer_pip_requirements_fallback_if, _compare_logged_code_paths, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = ([] if _is_available_on_pypi("spacy") else ["--env-manager", "local"]) ModelWithData = namedtuple("ModelWithData", ["model", "inference_data"]) spacy_version = Version(spacy.__version__) IS_SPACY_VERSION_NEWER_THAN_OR_EQUAL_TO_3_0_0 = spacy_version >= Version( "3.0.0") @pytest.fixture(scope="module") def spacy_model_with_data(): # Creating blank model and setting up the spaCy pipeline nlp = spacy.blank("en") if IS_SPACY_VERSION_NEWER_THAN_OR_EQUAL_TO_3_0_0: from spacy.pipeline.tok2vec import DEFAULT_TOK2VEC_MODEL
from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from tests.helper_functions import set_boto_credentials # pylint: disable=unused-import from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, _compare_logged_code_paths, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = ([] if _is_available_on_pypi( "scikit-learn", module="sklearn") else ["--no-conda"]) ModelWithData = namedtuple("ModelWithData", ["model", "inference_data"]) @pytest.fixture(scope="session") def sklearn_knn_model(): iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. y = iris.target knn_model = knn.KNeighborsClassifier() knn_model.fit(X, y) return ModelWithData(model=knn_model, inference_data=X) @pytest.fixture(scope="session")
from mlflow.models import infer_signature, Model from mlflow.store.artifact.s3_artifact_repo import S3ArtifactRepository from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking.artifact_utils import _download_artifact_from_uri from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( _compare_conda_env_requirements, _assert_pip_requirements, pyfunc_serve_and_score_model, _compare_logged_code_paths, _is_available_on_pypi, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = ([] if _is_available_on_pypi("prophet") else ["--env-manager", "local"]) class DataGeneration: def __init__(self, **kwargs): self.shift = kwargs["shift"] self.start = datetime.strptime(kwargs["start"], "%Y-%M-%d") self.size = kwargs["size"] self.date_field = kwargs["date_field"] self.target_field = kwargs["target_field"] self.seasonal_period = kwargs["seasonal_period"] self.seasonal_freq = kwargs["seasonal_freq"] np.random.seed(42) def _period_gen(self):
from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, _compare_logged_code_paths, ) if Version(mx.__version__) >= Version("2.0.0"): array_module = mx.np else: array_module = mx.nd EXTRA_PYFUNC_SERVING_TEST_ARGS = ( [] if _is_available_on_pypi("mxnet") else ["--env-manager", "local"] ) @pytest.fixture def model_path(tmpdir): return os.path.join(tmpdir.strpath, "model") @pytest.fixture def gluon_custom_env(tmpdir): conda_env = os.path.join(str(tmpdir), "conda_env.yml") _mlflow_conda_env(conda_env, additional_pip_deps=["mxnet", "pytest"]) return conda_env
from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, _compare_logged_code_paths, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = ( [] if _is_available_on_pypi("scikit-learn", module="sklearn") else ["--env-manager", "local"] ) ModelWithData = namedtuple("ModelWithData", ["model", "inference_data"]) @pytest.fixture(scope="module") def sklearn_knn_model(): iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. y = iris.target knn_model = knn.KNeighborsClassifier() knn_model.fit(X, y) return ModelWithData(model=knn_model, inference_data=X)
from mlflow.tracking.artifact_utils import _download_artifact_from_uri from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, ) from tests.helper_functions import set_boto_credentials # pylint: disable=unused-import from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import EXTRA_PYFUNC_SERVING_TEST_ARGS = [] if _is_available_on_pypi("tensorflow") else ["--no-conda"] SavedModelInfo = collections.namedtuple( "SavedModelInfo", [ "path", "meta_graph_tags", "signature_def_key", "inference_df", "expected_results_df", "raw_results", "raw_df", ], )
from mlflow.utils.environment import _mlflow_conda_env from mlflow.utils.file_utils import TempDir from mlflow.utils.model_utils import _get_flavor_configuration from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS from tests.helper_functions import set_boto_credentials # pylint: disable=unused-import from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( pyfunc_serve_and_score_model, _compare_conda_env_requirements, _assert_pip_requirements, _is_available_on_pypi, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = ( [] if _is_available_on_pypi("scikit-learn", module="sklearn") else ["--no-conda"] ) ModelWithData = namedtuple("ModelWithData", ["model", "inference_data"]) @pytest.fixture(scope="session") def sklearn_knn_model(): iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. y = iris.target knn_model = knn.KNeighborsClassifier() knn_model.fit(X, y) return ModelWithData(model=knn_model, inference_data=X)
from mlflow import pyfunc from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.helper_functions import ( _compare_conda_env_requirements, _assert_pip_requirements, pyfunc_serve_and_score_model, _is_available_on_pypi, _compare_logged_code_paths, ) pytestmark = pytest.mark.large DS_FORMAT = "%Y-%m-%dT%H:%M:%S" EXTRA_PYFUNC_SERVING_TEST_ARGS = ( [] if _is_available_on_pypi("diviner") else ["--env-manager", "local"] ) @pytest.fixture def model_path(tmp_path): return tmp_path.joinpath("model") @pytest.fixture(scope="module") def diviner_data(): return example_data_generator.generate_example_data( column_count=3, series_count=4, series_size=365 * 4, start_dt="2019-01-01", days_period=1 )
from tests.helper_functions import mock_s3_bucket # pylint: disable=unused-import from tests.statsmodels.model_fixtures import ( ols_model, arma_model, glsar_model, gee_model, glm_model, gls_model, recursivels_model, rolling_ols_model, rolling_wls_model, wls_model, ) EXTRA_PYFUNC_SERVING_TEST_ARGS = ([] if _is_available_on_pypi("statsmodels") else ["--env-manager", "local"]) # The code in this file has been adapted from the test cases of the lightgbm flavor. def _get_dates_from_df(df): start_date = df["start"][0] end_date = df["end"][0] return start_date, end_date @pytest.fixture def model_path(tmpdir, subdir="model"): return os.path.join(str(tmpdir), subdir)