-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from whylabs/dev/andy/mlflow
[bugfix] Fix mlfow tracking with predict API
- Loading branch information
Showing
6 changed files
with
35 additions
and
12 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
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,3 +1,3 @@ | ||
"""WhyLabs version number.""" | ||
|
||
__version__ = "0.1.13-dev3" | ||
__version__ = "0.1.13-dev8" |
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,25 +1,48 @@ | ||
import atexit | ||
from typing import Union | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
import whylogs | ||
import datetime | ||
|
||
from logging import getLogger | ||
from whylogs.app.config import WHYLOGS_YML | ||
|
||
logger = getLogger(__name__) | ||
|
||
PyFuncOutput = Union[pd.DataFrame, pd.Series, np.ndarray, list] | ||
|
||
|
||
class ModelWrapper(object): | ||
def __init__(self, model): | ||
self.model = model | ||
self.session = whylogs.get_or_create_session("/opt/ml/model/" + WHYLOGS_YML) | ||
self.logger = self.create_logger() | ||
self.ylog = self.create_logger() | ||
self.last_upload_time = datetime.datetime.utcnow() | ||
atexit.register(self.logger.close) | ||
atexit.register(self.ylog.close) | ||
|
||
def create_logger(self): | ||
# TODO: support different rotation mode and support custom name here | ||
return self.session.logger('live', with_rotation_time='m') | ||
|
||
def predict(self, df): | ||
self.logger.log_dataframe(df) | ||
output = self.model.predict(df) | ||
self.logger.log_dataframe(df) | ||
def predict(self, data: pd.DataFrame) -> PyFuncOutput: | ||
""" | ||
Wrapper around https://www.mlflow.org/docs/latest/_modules/mlflow/pyfunc.html#PyFuncModel.predict | ||
This allows us to capture input and predictions into whylogs | ||
""" | ||
self.ylog.log_dataframe(data) | ||
output = self.model.predict(data) | ||
|
||
if isinstance(output, np.ndarray) or isinstance(output, pd.Series): | ||
data = pd.DataFrame(data=output, columns=['prediction']) | ||
self.ylog.log_dataframe(data) | ||
elif isinstance(output, pd.DataFrame): | ||
self.ylog.log_dataframe(output) | ||
elif isinstance(output, list): | ||
for e in output: | ||
self.ylog.log(feature_name='prediction', value=e) | ||
else: | ||
logger.warning('Unsupported output type: %s', type(output)) | ||
return output |
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