mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-01 14:38:13 +08:00
renaming backend, cellxgene to server, client respectively
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
class QueryStringError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def _convert_variable(datatype, variable):
|
||||
"""
|
||||
Convert variable to number (float/int)
|
||||
Used for dataset metadata and for query string
|
||||
:param datatype: type to convert to
|
||||
:param variable: value of variable
|
||||
:return: converted variable
|
||||
:raises: ValueError
|
||||
"""
|
||||
try:
|
||||
if variable and datatype == "int":
|
||||
variable = int(variable)
|
||||
elif variable and datatype == "float":
|
||||
variable = float(variable)
|
||||
return variable
|
||||
except ValueError:
|
||||
raise
|
||||
|
||||
|
||||
def parse_filter(filter, schema):
|
||||
"""
|
||||
{key: variable_type
|
||||
value_type
|
||||
query
|
||||
|
||||
:param filter:
|
||||
:param schema:
|
||||
:return:
|
||||
"""
|
||||
query = {}
|
||||
for key in filter:
|
||||
value = filter.getlist(key)
|
||||
if key not in schema:
|
||||
raise QueryStringError("Error: key {} not in metadata schema".format(key))
|
||||
query[key] = {
|
||||
"variable_type": schema[key]["variabletype"],
|
||||
"value_type": schema[key]["type"]
|
||||
}
|
||||
if query[key]["variable_type"] == "categorical":
|
||||
query[key]["query"] = _convert_variable(query[key]["value_type"], value)
|
||||
elif query[key]["variable_type"] == "continuous":
|
||||
value = value[0]
|
||||
try:
|
||||
min, max = value.split(",")
|
||||
except ValueError:
|
||||
raise QueryStringError("Error: min,max format required for range for key {}, got {}".format(key, value))
|
||||
if min == "*":
|
||||
min = None
|
||||
if max == "*":
|
||||
max = None
|
||||
try:
|
||||
query[key]["query"] = {
|
||||
"min": _convert_variable(query[key]["value_type"], min),
|
||||
"max": _convert_variable(query[key]["value_type"], max)
|
||||
}
|
||||
except ValueError:
|
||||
raise QueryStringError(
|
||||
"Error: expected type {} for key {}, got {}".format(query[key]["type"], key, value)
|
||||
)
|
||||
return query
|
||||
@@ -0,0 +1,7 @@
|
||||
import json
|
||||
|
||||
|
||||
def parse_schema(filename):
|
||||
with open(filename) as fh:
|
||||
schema = json.load(fh)
|
||||
return schema
|
||||
@@ -0,0 +1,40 @@
|
||||
import json
|
||||
|
||||
from numpy import float32, integer
|
||||
from flask import make_response, jsonify, Response
|
||||
|
||||
|
||||
class Float32JSONEncoder(json.JSONEncoder):
|
||||
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 make_payload(data, errormessage="", errorcode=200):
|
||||
"""
|
||||
Creates JSON respons for requests
|
||||
:param data: json data
|
||||
:param errormessage: error message
|
||||
:param errorcode: http error code
|
||||
:return: flask json repsonse
|
||||
"""
|
||||
error = False
|
||||
if errormessage:
|
||||
error = True
|
||||
# Questionable
|
||||
data = json.loads(json.dumps(data, cls=Float32JSONEncoder))
|
||||
return make_response(jsonify({
|
||||
"data": data,
|
||||
"status": {
|
||||
"error": error,
|
||||
"errormessage": errormessage,
|
||||
}
|
||||
}), errorcode)
|
||||
|
||||
|
||||
def make_streaming_response(data_generator, errorcode=200, content_type="application/json"):
|
||||
# TODO headers
|
||||
return Response(data_generator, status=errorcode, content_type=content_type)
|
||||
Reference in New Issue
Block a user