Skip to content

Commit

Permalink
Support string in array-like predictions (#7)
Browse files Browse the repository at this point in the history
Fix bug identified in #6 whereby models that returns predictions in
array-like structures, containing values unsupported by `numpy.isnan`
would error at runtime.

Solution is to use `pandas.isnull` which has support for a broader
range of types.

Thanks to @1inuxoid for the PR that identified this bug!
  • Loading branch information
bloomonkey authored Jan 13, 2023
1 parent a3ad120 commit 52e4f6c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.2] - 2022-01-13
### Fixed
- Support strings for predictions in array-like structures

## [0.3.1] - 2022-11-24
### Fixed
- Assume that prediction output field(s) may be nullable. It's not uncommon to want ML models to return null or nan values at inference time (e.g. extrapolation outside of training data range).
Expand Down
2 changes: 1 addition & 1 deletion fastapi_mlflow/predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ def convert_predictions_to_python(results) -> List[Dict[str, Any]]:
# Replace NaN with None
response_data = []
for row in results:
value = row if not np.isnan(row) else None
value = row if not pd.isnull(row) else None
response_data.append({"prediction": value})
return response_data
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fastapi-mlflow"
version = "0.3.1"
version = "0.3.2"
description = "Deploy mlflow models as JSON APIs with minimal new code."
authors = ["John Harrison <[email protected]>"]
readme = "README.md"
Expand Down
20 changes: 20 additions & 0 deletions tests/test_predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Copyright (C) 2022, Auto Trader UK
"""
from datetime import datetime
from inspect import signature
from typing import Union

Expand Down Expand Up @@ -158,6 +159,25 @@ def test_convert_predictions_to_python_ndarray():
] == response_data


def test_convert_predictions_to_python_ndarray_strings():
predictions = np.array(["foo", "bar", None])
response_data = convert_predictions_to_python(predictions)
assert [
{"prediction": "foo"},
{"prediction": "bar"},
{"prediction": None},
] == response_data


def test_convert_predictions_to_python_ndarray_datetimes():
predictions = np.array([datetime(2023, 1, 1), None])
response_data = convert_predictions_to_python(predictions)
assert [
{"prediction": datetime(2023, 1, 1)},
{"prediction": None},
] == response_data


def test_convert_predictions_to_python_series():
predictions = pd.Series([1, 2, 3, np.nan])
response_data = convert_predictions_to_python(predictions)
Expand Down

0 comments on commit 52e4f6c

Please sign in to comment.