Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compute visualization results from operators #4324

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
}
)
Loading