|
2 | 2 | from enum import Enum
|
3 | 3 | from typing import List
|
4 | 4 | from datetime import datetime
|
| 5 | +from urllib.parse import quote_plus, urlencode |
5 | 6 | import os
|
6 | 7 | import logging
|
| 8 | +import itertools |
| 9 | +from requests.exceptions import HTTPError |
7 | 10 |
|
8 |
| -import requests |
9 |
| - |
10 |
| -from fairgraph.client import KGClient |
| 11 | +from fairgraph.client import KGClient, SCOPE_MAP |
11 | 12 | from fairgraph.base import KGQuery, KGProxy, as_list
|
12 | 13 | from fairgraph.brainsimulation import ValidationResult as ValidationResultKG, ValidationActivity
|
13 | 14 |
|
@@ -46,6 +47,12 @@ def query_results(
|
46 | 47 | # from header
|
47 | 48 | token: HTTPAuthorizationCredentials = Depends(auth),
|
48 | 49 | ):
|
| 50 | + return _query_results(passed, project_id, model_instance_id, test_instance_id, model_id, test_id, model_alias, test_alias, score_type, size, |
| 51 | +from_index, token) |
| 52 | + |
| 53 | + |
| 54 | +def _query_results(passed, project_id, model_instance_id, test_instance_id, model_id, test_id, model_alias, test_alias, score_type, size, |
| 55 | +from_index, token): |
49 | 56 | filter_query, context = build_result_filters(
|
50 | 57 | model_instance_id,
|
51 | 58 | test_instance_id,
|
@@ -76,6 +83,56 @@ def query_results(
|
76 | 83 | return response
|
77 | 84 |
|
78 | 85 |
|
| 86 | +def expand_combinations(D): |
| 87 | + keys, values = zip(*D.items()) |
| 88 | + return [dict(zip(keys, v)) for v in itertools.product(*[as_list(v) for v in values])] |
| 89 | + |
| 90 | + |
| 91 | +def _query_results2(passed, project_id, model_instance_id, test_instance_id, model_id, test_id, model_alias, test_alias, score_type, size, |
| 92 | +from_index, token): |
| 93 | + # todo : more sophisticated handling of size and from_index |
| 94 | + path = "/modelvalidation/simulation/validationresult/v0.1.0" |
| 95 | + query_id = "test" # "vf" |
| 96 | + scope = SCOPE_MAP["latest"] |
| 97 | + query_parameters = { |
| 98 | + "start": 0, #from_index, |
| 99 | + "size": 100000, #size, |
| 100 | + "vocab": "https://schema.hbp.eu/myQuery/", |
| 101 | + "scope": scope |
| 102 | + } |
| 103 | + for filter_name in ("passed", "project_id", "model_instance_id", "test_instance_id", |
| 104 | + "model_id", "test_id", "model_alias", "test_alias", "score_type"): |
| 105 | + value = locals()[filter_name] |
| 106 | + if value is not None: |
| 107 | + query_parameters[filter_name] = value |
| 108 | + query_parameters_list = expand_combinations(query_parameters) |
| 109 | + response = [] |
| 110 | + for query_parameters in query_parameters_list: |
| 111 | + query_string = urlencode(query_parameters, doseq=True) |
| 112 | + url = f"{path}/{query_id}/instances?" + query_string |
| 113 | + print(url) |
| 114 | + try: |
| 115 | + kg_response = kg_client._kg_query_client.get(url) |
| 116 | + except HTTPError as err: |
| 117 | + if err.response.status_code == 403: |
| 118 | + kg_response = None |
| 119 | + else: |
| 120 | + raise |
| 121 | + if kg_response and "results" in kg_response: |
| 122 | + for result in kg_response["results"]: |
| 123 | + try: |
| 124 | + obj = ValidationResult.from_kg_query(result) |
| 125 | + except ConsistencyError as err: # todo: count these and report them in the response |
| 126 | + logger.warning(str(err)) |
| 127 | + else: |
| 128 | + response.append(obj) |
| 129 | + if len(response) >= size + from_index: |
| 130 | + break |
| 131 | + if len(response) >= size + from_index: |
| 132 | + break |
| 133 | + return response[from_index:from_index + size] |
| 134 | + |
| 135 | + |
79 | 136 | @router.get("/results/{result_id}", response_model=ValidationResult)
|
80 | 137 | def get_result(result_id: UUID, token: HTTPAuthorizationCredentials = Depends(auth)):
|
81 | 138 | result = ValidationResultKG.from_uuid(str(result_id), kg_client, api="nexus")
|
|
0 commit comments