8000 Get customized analysis results with result_type by vnadhan · Pull Request #205 · vikinganalytics/mvg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Get customized analysis results with result_type #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions mvg/mvg.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def __init__(self, endpoint: str, token: str):
self.endpoint = endpoint
self.token = token

self.mvg_version = self.parse_version("v0.14.5")
self.tested_api_version = self.parse_version("v0.5.6")
self.mvg_version = self.parse_version("v0.14.6")
self.tested_api_version = self.parse_version("v0.5.12")

# Get API version
try:
Expand Down Expand Up @@ -1075,6 +1075,7 @@ def get_analysis_results(
request_id: str,
offset: int = None,
limit: int = None,
result_type: str = None,
) -> dict:
"""Retrieves an analysis with given request_id
The format of the result structure depends on the feature.
Expand All @@ -1089,6 +1090,10 @@ def get_analysis_results(
limit: int
maximum number of items to be returned from each
dictionary in the data for "results" key [optional].
result_type: str
The type of results to return. By default, the type is
'full' implying the complete analysis results will be
returned. The allowed types varies between features [optional].

Returns
-------
Expand All @@ -1105,6 +1110,8 @@ def get_analysis_results(
params["offset"] = offset
if limit is not None:
params["limit"] = limit
if result_type is not None:
params["result_type"] = result_type

response = get_paginated_analysis_results(self._request, url, params)
return response
Expand Down
7 changes: 5 additions & 2 deletions mvg/utils/response_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def get_paginated_analysis_results(request: Callable, url: str, params: Dict) ->
is_paginated = all(f in results for f in paginator_model_fields)
if is_paginated:
# Fields with paginated data
paginated_fields = ["timestamps", "labels", "uncertain", "mode_probability"]
# We might have to later re-enable this to not make this
# automatically paginate list-based values.
# paginated_fields = ["timestamps", "labels", "uncertain",
# "mode_probability"]

num_items = results["total"]
limit = results["limit"]
Expand All @@ -101,7 +104,7 @@ def get_paginated_analysis_results(request: Callable, url: str, params: Dict) ->
params["offset"] = offset
_results = request("get", url, params=params).json()["results"]
for key in _results:
if key in paginated_fields:
if isinstance(_results[key], list):
results[key] += _results[key]

# Construct the response to return
Expand Down
1 change: 1 addition & 0 deletions requirements_docs.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
papermill
ipywidgets
ipykernel
tqdm
sphinx
sphinx-rtd-theme
Expand Down
55 changes: 55 additions & 0 deletions tests/test_api_analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,58 @@ def test_get_analysis_info(session: MVG, waveform_source_multiaxial_001):

analysis_info = session.get_analysis_info(request_id)
assert analysis_info["request_status"] == "successful"


def test_get_analysis_results_with_valid_result_type(
session: MVG, waveform_source_multiaxial_001
):
source_id, source_info = waveform_source_multiaxial_001
timestamps = source_info["timestamps"]
pattern = source_info["pattern"]

parameters = {"n_trials": 1}
request = session.request_analysis(source_id, "ModeId", parameters=parameters)
request_id = request["request_id"]
result_type = "compressed"
session.wait_for_analyses([request_id])
response = session.get_analysis_results(request_id, result_type=result_type)

results = response["results"]
num_timestamps_in_modes = [6, 14, 5] # from the pattern
assert response["status"] == "successful"
assert results["count"] == num_timestamps_in_modes
for channel in pattern.keys():
assert all(label in pattern[channel] for label in results["label"])
assert all(ts in timestamps for ts in results["start_timestamp"])
assert all(ts in timestamps for ts in results["end_timestamp"])


def test_get_analysis_results_with_noncompliant_result_type(
session: MVG, waveform_source_with_measurements
):
source_id = waveform_source_with_measurements

feature = "KPIDemo"
request = session.request_analysis(source_id, feature)
request_id = request["request_id"]
result_type = "compressed"
session.wait_for_analyses([request_id])

with pytest.raises(MVGAPIError) as exc:
session.get_analysis_results(request_id, result_type=result_type)
assert f"Feature {feature} supports only result types in" in str(exc)


def test_get_analysis_results_with_invalid_result_type(
session: MVG, waveform_source_with_measurements
):
source_id = waveform_source_with_measurements

request = session.request_analysis(source_id, "KPIDemo")
request_id = request["request_id"]
result_type = "someresultype"
session.wait_for_analyses([request_id])

with pytest.raises(MVGAPIError) as exc:
session.get_analysis_results(request_id, result_type=result_type)
assert f"Feature result type '{result_type}' does not exist." in str(exc)
0