mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-16 21:37:59 +08:00
* dead code and route removal * more dead code cleanup * fix scanpy_engine tests * lint * add missing catch in filter parsing * update scanpy NaN tests * more fbs tests and dead test removal * remove forced default for content type negotiation * bit of cleanup * more fbs test cleanup * lint * remove swagger * swagger cleanup * lint * correctly handle lack of templates * more dead code removal * remove unused files * fix dev build * lint
31 lines
994 B
Python
31 lines
994 B
Python
import json
|
|
from numpy import float32, integer
|
|
|
|
|
|
class Float32JSONEncoder(json.JSONEncoder):
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
NaN/Infinities are illegal in standard JSON. Python extends JSON with
|
|
non-standard symbols that most JavaScript JSON parsers do not understand.
|
|
The `allow_nan` parameter will force Python simplejson to throw an ValueError
|
|
if it runs into non-finite floating point values which are unsupported by
|
|
standard JSON.
|
|
"""
|
|
kwargs["allow_nan"] = False
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def default(self, obj):
|
|
if isinstance(obj, float32):
|
|
return float(obj)
|
|
elif isinstance(obj, integer):
|
|
return int(obj)
|
|
return json.JSONEncoder.default(self, obj)
|
|
|
|
|
|
def custom_format_warning(msg, *args, **kwargs):
|
|
return f"[cellxgene] Warning: {msg} \n"
|
|
|
|
|
|
def jsonify_scanpy(data):
|
|
return json.dumps(data, cls=Float32JSONEncoder, allow_nan=False)
|