from typing import List import pandas as pd from absl import logging from dill import dump, load import requests import os from sklearn.pipeline import Pipeline def has_columns(columns: List[str], dataframe: pd.DataFrame) -> bool: """ Checks if a dataframe has the columns specificed in 'columns' params: columns (List[str]) - list of columns dataframe (pd.DataFrame) - pandas DataFrame to check columns againsts returns: bool - True - if the dataframe has the columns, false otherwise """ return set(dataframe.columns).issuperset(set(columns)) def check_mlflow_connection(mlflow_tracking_server_uri: str) -> bool: """ Checks mlflow tracking server connection params: mlflow_tracking_server_uri (str) - mlflow tracking server returns: bool - returns True if there is an active connection, and False otherwise. """ res = requests.get(os.path.join(mlflow_tracking_server_uri, "api/2.0/mlflow/experiments/list")) return res.status_code == 200 def load_sklearn_model(model_path): """ Loads a serialized sklearn Model params: model_path (str) - Model Path returns: model (sklearn.Pipeline) - an sklearn pipeline which encodes the fitted feature processing pipeline and the trained model """ try: with open(model_path, 'rb') as file_obj: return load(file_obj) except Exception as err: logging.error(err) def save_sklearn_model(model: Pipeline, model_path: str) -> None: """ Saves an sklearn model params: model (sklearn Pipeline) - sklearn pipeline which contains the fitted feature processing pipeline and the fitted model returns: None """ try: with open(model_path, 'wb') as file_obj: dump(model, file_obj) except Exception as err: logging.error(err)