Skip to content

Commit

Permalink
Add additional error checking and warnings to stats route
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhorton committed Jun 25, 2021
1 parent 1d30d47 commit 6b3762f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/mp_api/routes/search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class SearchStats(BaseModel):
"estimator of the distribution, equally spaced "
"between specified minimum and maximum values.",
)
warnings: List[str] = Field(
None,
title="Warnings",
description="Any warnings generated while generating statistics.",
)


class XASSearchData(BaseModel):
Expand Down
10 changes: 9 additions & 1 deletion src/mp_api/routes/search/query_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,20 @@ def post_process(self, docs):
min_val = self.min_val
max_val = self.max_val
num_samples = len(docs)
warnings = []

values = [d[field] for d in docs]
values = [d[field] for d in docs if field in d]
if not min_val:
min_val = min(values)
if not max_val:
max_val = max(values)

if len(values) != len(docs):
warnings += [
"Some documents have field missing.",
f"Only {len(values)} of {len(docs)} ({100*len(values)/len(docs):.2f}%) have {field} field present.",
]

kernel = gaussian_kde(values)

distribution = list(
Expand All @@ -238,6 +245,7 @@ def post_process(self, docs):
distribution=distribution,
median=median,
mean=mean,
warnings=warnings,
)

return [response]
Expand Down

0 comments on commit 6b3762f

Please sign in to comment.