Skip to content

Commit

Permalink
Add format to numpy and pandas parameter types (#53)
Browse files Browse the repository at this point in the history
* Add format to numpy and pandas parameter types

Co-authored-by: Neehar Duvvuri <neduvvur@microsoft.com>
  • Loading branch information
needuv and needuv authored Jan 11, 2022
1 parent 95199ee commit de2d33c
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions inference_schema/parameter_types/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
TIME_FORMAT = "%H:%M:%S.%f %z"
ERR_PYTHON_DATA_NOT_JSON_SERIALIZABLE = "Invalid python data sample provided: ensure that the data is fully JSON " \
"serializable to be able to generate swagger schema from it. Actual error: {}"


class SWAGGER_FORMAT_CONSTANTS:
NUMPY_FORMAT = "numpy.ndarray"
PANDAS_FORMAT = "pandas.DataFrame:{}"
2 changes: 2 additions & 0 deletions inference_schema/parameter_types/numpy_parameter_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from .abstract_parameter_type import AbstractParameterType
from ._swagger_from_dtype import Dtype2Swagger
from ._constants import SWAGGER_FORMAT_CONSTANTS


class NumpyParameterType(AbstractParameterType):
Expand Down Expand Up @@ -96,6 +97,7 @@ def input_to_swagger(self):
swagger_schema = Dtype2Swagger.handle_swagger_array(swagger_item_type, shape)
items_count = len(self.sample_input)
swagger_schema['example'] = self._get_swagger_sample(self.sample_input, items_count, swagger_schema['items'])
swagger_schema["format"] = SWAGGER_FORMAT_CONSTANTS.NUMPY_FORMAT
return swagger_schema

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion inference_schema/parameter_types/pandas_parameter_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
from .abstract_parameter_type import AbstractParameterType
from ._util import get_swagger_for_list, get_swagger_for_nested_dict
from ._constants import SWAGGER_FORMAT_CONSTANTS


class PandasParameterType(AbstractParameterType):
Expand Down Expand Up @@ -143,5 +144,5 @@ def input_to_swagger(self):
elif data_type.startswith('timedelta'):
swagger_schema['properties']['data']['items']['properties'][str(column_name)]['format'] = \
'timedelta'

swagger_schema["format"] = SWAGGER_FORMAT_CONSTANTS.PANDAS_FORMAT.format(self.orient)
return swagger_schema
2 changes: 2 additions & 0 deletions tests/resources/sample_nested_input_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"properties": {
"input1": {
"type": "array",
"format": "pandas.DataFrame:records",
"items": {
"type": "object",
"required": ["name", "state"],
Expand All @@ -22,6 +23,7 @@
},
"input2": {
"type": "array",
"format": "numpy.ndarray",
"items": {
"type": "object",
"properties": {
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/sample_nested_output_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"properties": {
"output1": {
"type": "array",
"format": "pandas.DataFrame:records",
"items": {
"type": "object",
"required": ["state"],
Expand All @@ -16,6 +17,7 @@
},
"output2": {
"type": "array",
"format": "numpy.ndarray",
"items": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions tests/resources/sample_numpy_input_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"properties": {
"param": {
"type": "array",
"format": "numpy.ndarray",
"items": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions tests/resources/sample_numpy_output_schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"type": "array",
"format": "numpy.ndarray",
"items": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions tests/resources/sample_pandas_datetime_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"properties": {
"param": {
"type": "array",
"format": "pandas.DataFrame:records",
"items": {
"type": "object",
"required": [
Expand Down
1 change: 1 addition & 0 deletions tests/resources/sample_pandas_input_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"properties": {
"param": {
"type": "array",
"format": "pandas.DataFrame:records",
"items": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions tests/resources/sample_pandas_output_schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"type": "array",
"format": "pandas.DataFrame:records",
"items": {
"type": "object",
"properties": {
Expand Down

0 comments on commit de2d33c

Please sign in to comment.