def test_bad_filter(self): bad_annotation_type = ImmutableMultiDict([("obs:tissue", "lung")]) with self.assertRaises(QueryStringError): parse_filter(bad_annotation_type, self.schema) bad_axis = ImmutableMultiDict([("xyz:n_genes", "100,1000")]) with self.assertRaises(QueryStringError): parse_filter(bad_axis, self.schema)
def get(self): accept_type = request.args.get("accept-type", None) # request.args is immutable args = request.args.copy() args.pop("accept-type", None) try: filter_ = parse_filter(ImmutableMultiDict(args), current_app.data.schema["annotations"]) except QueryStringError as e: return make_response(e.message, HTTPStatus.BAD_REQUEST) # TODO support CSV try: # TODO store mime_type when more than one is supported get_mime_type( acceptable_types=["application/json"], query_param=accept_type, header=request.accept_mimetypes ) except MimeTypeError as e: return make_response(e.message, HTTPStatus.NOT_ACCEPTABLE) try: return make_response((jsonify(current_app.data.data_frame(filter_, axis=Axis.OBS))), HTTPStatus.OK) except FilterError as e: return make_response(e.message, HTTPStatus.BAD_REQUEST) except ValueError as e: # JSON encoding failure, usually due to bad data warnings.warn(JSON_NaN_to_num_warning_msg) return make_response(str(e), HTTPStatus.INTERNAL_SERVER_ERROR)
def test_boolean_filter(self): schema = {"obs": [{"name": "bool_filter", "type": "boolean"}]} filter_dict = ImmutableMultiDict([("obs:bool_filter", "false")]) filter_ = parse_filter(filter_dict, schema) self.assertIn("obs", filter_) self.assertEqual(filter_["obs"]["annotation_value"], [{ "name": "bool_filter", "values": [False] }])
def test_filter_contiunous(self): filterMock = MagicMock() filterMock.__iter__.return_value = iter(["n_genes"]) filterMock.getlist.return_value = ["0,100"] query = parse_filter(filterMock, self.schema) assert query == { "n_genes": { "variable_type": "continuous", "value_type": "int", "query": { "min": 0, "max": 100 } } } filterMock.__iter__.return_value = iter(["n_genes"]) filterMock.getlist.return_value = ["*,100"] query = parse_filter(filterMock, self.schema) assert query == { "n_genes": { "variable_type": "continuous", "value_type": "int", "query": { "min": None, "max": 100 } } } filterMock.__iter__.return_value = iter(["n_genes"]) filterMock.getlist.return_value = ["0,*"] query = parse_filter(filterMock, self.schema) assert query == { "n_genes": { "variable_type": "continuous", "value_type": "int", "query": { "min": 0, "max": None } } }
def test_filter_categorical(self): filterMock = MagicMock() filterMock.__iter__.return_value = iter(["louvain"]) filterMock.getlist.return_value = ["B cells", "T cells"] query = parse_filter(filterMock, self.schema) assert query == { "louvain": { "variable_type": "categorical", "value_type": "string", "query": ["B cells", "T cells"] } } filterMock.__iter__.return_value = iter(["cluster"]) filterMock.getlist.return_value = ["1", "2"] query = parse_filter(filterMock, self.schema) assert query == { "cluster": { "variable_type": "categorical", "value_type": "int", "query": [1, 2] } }
def get(self): payload = { "metadata": [], "cellcount": 0, "graph": [], "ranges": {}, } # get query params cells_filter = parse_filter(request.args, current_app.data.schema) filtered_data = current_app.data.filter_cells(cells_filter) payload["metadata"] = current_app.data.metadata(filtered_data) payload["ranges"] = current_app.data.metadata_ranges(filtered_data) payload["graph"] = current_app.data.create_graph(filtered_data) payload["cellcount"] = current_app.data.cell_count return make_payload(payload)
def test_complex_filter(self): filter_dict = ImmutableMultiDict([("obs:louvain", "NK cells"), ("obs:louvain", "CD8 T cells"), ("obs:n_counts", "3000,*")]) filter_ = parse_filter(filter_dict, self.schema) self.assertIn("obs", filter_) self.assertEqual( filter_["obs"]["annotation_value"], [ { "name": "louvain", "values": ["NK cells", "CD8 T cells"] }, { "name": "n_counts", "max": None, "min": 3000.0 }, ], )