def get_put_image_tags(name, index): if flask.request.method == "GET": require_permissions(["read"]) image_collection = require_backend("image-collection", name) return flask.jsonify(tags=image_collection.tags(index)) if flask.request.method == "PUT": require_permissions(["annotate"]) tags = flask.request.json["tags"] for tag in tags: if not tag: flask.abort( flask.make_response( flask.jsonify(message="Tag category cannot be empty."), 400)) image_collection = require_backend("image-collection", name) saved = image_collection.put_tags(index, tags) if not saved: flask.abort( flask.make_response( flask.jsonify(message="This dataset is read-only."), 400)) return flask.jsonify()
def put_delete_favorites_service_name(service, name): if flask.request.method == "PUT": require_permissions(["write"]) label = flask.request.json["label"] require_backend("favorites").create(service, name, label) return flask.jsonify() elif flask.request.method == "DELETE": require_permissions(["delete"]) require_backend("favorites").delete(service, name) return flask.jsonify()
def get_document(name, index): require_permissions(["read"]) document_collection = require_backend("document-collection", name) document = document_collection.get(index) if isinstance(document, str): return flask.send_file(document) else: raise RuntimeError(f"Unknown document type: {type(document)}")
def get_image(name, index): require_permissions(["read"]) image_collection = require_backend("image-collection", name) image = image_collection.get(index) if isinstance(image, str): return flask.send_file(image) else: raise RuntimeError(f"Unknown image type: {type(image)}")
def get_put_image_bboxes(name, index): if flask.request.method == "GET": require_permissions(["read"]) image_collection = require_backend("image-collection", name) return flask.jsonify(bboxes=image_collection.bboxes(index)) if flask.request.method == "PUT": require_permissions(["annotate"]) bboxes = flask.request.json["bboxes"] for bbox in bboxes: for key in ["left", "top", "width", "height", "category"]: if key not in bbox: flask.abort( flask.make_response( flask.jsonify( message= "Bounding box missing required key: {key}"), 400)) if not bbox["category"]: flask.abort( flask.make_response( flask.jsonify( message="Bounding box category cannot be empty."), 400)) if bbox["width"] == 0 and bbox["height"] == 0: flask.abort( flask.make_response( flask.jsonify(message="Bounding box cannot be empty."), 400)) image_collection = require_backend("image-collection", name) saved = image_collection.put_bboxes(index, bboxes) if not saved: flask.abort( flask.make_response( flask.jsonify(message="This dataset is read-only."), 400)) return flask.jsonify()
def get_post_layouts(): if flask.request.method == "GET": require_permissions(["read"]) return flask.jsonify(layout=application.config["layout"]) elif flask.request.method == "POST": require_permissions([ "read" ]) # We don't require write permissions to save a layout, by design. layouts = require_backend("layouts") lid = layouts.put(content=flask.request.json["layout"]) return flask.jsonify(lid=lid)
def get_document_count(name): require_permissions(["read"]) document_collection = require_backend("document-collection", name) return flask.jsonify(count=len(document_collection))
def post_timeseries_plot(name): require_permissions(["read"]) timeseries_collection = require_backend("timeseries-collection", name) height = int(float(flask.request.json.get("height", 500))) keys = flask.request.json.get("keys", []) legend_corner = flask.request.json.get("legend", "bottom-right") max_samples = int(float(flask.request.json.get("max_samples", 1000))) smoothing = float(flask.request.json.get("smoothing", "0")) width = int(float(flask.request.json.get("width", 500))) yscale = flask.request.json.get("yscale", "linear") timeseries = [timeseries_collection.get(key) for key in keys] canvas = toyplot.Canvas(width=width, height=height) axes = canvas.cartesian(xlabel="Step", yscale=yscale) palette = toyplot.color.brewer.palette("Set2") legend = [] for key in keys: color_index = int( hashlib.sha256(key.split("/")[0].encode("utf8")).hexdigest(), 16) % len(palette) color = palette[color_index] timeseries = timeseries_collection.get(key) if timeseries is None: continue if "values" not in timeseries: continue y = timeseries["values"] if "indices" in timeseries: indices = timeseries["indices"] else: indices = numpy.arange(len(y)) x = indices if "markers" in timeseries: marker = timeseries["markers"] else: marker = [None] * len(y) if smoothing: alpha = 1 - smoothing b = [alpha] a = [1, alpha - 1] zi = scipy.signal.lfiltic(b, a, y[0:1], [0]) ys = scipy.signal.lfilter(b, a, y, zi=zi)[0] axes.plot(x, y, color=color, opacity=0.1) legend.append((key, axes.plot(x, ys, color=color, marker=marker))) else: legend.append((key, axes.plot(x, y, color=color, marker=marker))) if legend and legend_corner != "none": height = len(legend) * 16 inset = 50 canvas.legend(legend, corner=(legend_corner, inset, 100, height)) return flask.jsonify({"plot": toyplot.html.tostring(canvas)})
def get_timeseries(name): require_permissions(["read"]) timeseries_collection = require_backend("timeseries-collection", name) count = len(timeseries_collection) keys = list(timeseries_collection.keys()) return flask.jsonify(count=count, keys=keys)
def get_image_metadata(name, index): require_permissions(["read"]) image_collection = require_backend("image-collection", name) return flask.jsonify(metadata=image_collection.metadata(index))
def get_image_collection_categories(name): require_permissions(["read"]) image_collection = require_backend("image-collection", name) return flask.jsonify({"categories": image_collection.categories})
def get_image_collection(name): require_permissions(["read"]) image_collection = require_backend("image-collection", name) return flask.jsonify(count=len(image_collection))
def get_layouts_lid(lid): layouts = require_backend("layouts") layout = layouts.get(lid=lid) if layout is None: flask.abort(404) return flask.jsonify(layout=layout)
def get_favorites(): require_permissions(["read"]) favorites = require_backend("favorites") return flask.jsonify(favorites=favorites.get())