Skip to content

Commit

Permalink
Fix compute visualization results from operators (#4324)
Browse files Browse the repository at this point in the history
* add np casting to json encoder

* isinstance

* add test

* add await
  • Loading branch information
benjaminpkane authored May 9, 2024
1 parent ba743bd commit 59139cf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
25 changes: 22 additions & 3 deletions fiftyone/server/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
| `voxel51.com <https://voxel51.com/>`_
|
"""

from json import JSONEncoder
import traceback
import typing as t
import logging

from bson import json_util
import numpy as np

from fiftyone.core.utils import run_sync_task

Expand All @@ -18,6 +21,23 @@
from starlette.requests import Request


class Encoder(JSONEncoder):
def default(self, o):
if isinstance(o, np.floating):
return float(o)

if isinstance(o, np.integer):
return int(o)

return JSONEncoder.default(self, o)


async def create_response(response: dict):
return Response(
await run_sync_task(lambda: json_util.dumps(response, cls=Encoder))
)


def route(func):
async def wrapper(
endpoint: HTTPEndpoint, request: Request, *args
Expand All @@ -30,9 +50,8 @@ async def wrapper(
if isinstance(response, Response):
return response

return Response(
await run_sync_task(lambda: json_util.dumps(response))
)
return await create_response(response)

except Exception as e:
logging.exception(e)
return JSONResponse(
Expand Down
32 changes: 32 additions & 0 deletions tests/unittests/server_decorators_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
FiftyOne Server decorators.
| Copyright 2017-2024, Voxel51, Inc.
| `voxel51.com <https://voxel51.com/>`_
|
"""

import unittest

import numpy as np

from fiftyone.server.decorators import create_response


class TestNumPyResponse(unittest.IsolatedAsyncioTestCase):
async def test_numpy_response(self):
await create_response(
{
"float16": np.array([16.0], dtype=np.float16),
"float32": np.array([32.0], dtype=np.float32),
"float64": np.array([64.0], dtype=np.float64),
"int8": np.array([8], dtype=np.int8),
"int16": np.array([8], dtype=np.int16),
"int32": np.array([8], dtype=np.int32),
"int64": np.array([8], dtype=np.int64),
"uint8": np.array([8], dtype=np.uint8),
"uint6": np.array([8], dtype=np.uint16),
"uint32": np.array([8], dtype=np.uint32),
"uint64": np.array([8], dtype=np.uint64),
}
)

0 comments on commit 59139cf

Please sign in to comment.