-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathresponse_aggregation.py
More file actions
49 lines (44 loc) · 1.79 KB
/
response_aggregation.py
File metadata and controls
49 lines (44 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from webviz_config.common_cache import CACHE
import numpy as np
@CACHE.memoize(timeout=CACHE.TIMEOUT)
def filter_and_sum_responses(
dframe, ensemble, response, filteroptions=None, aggregation="sum"
):
"""Cached wrapper for _filter_and_sum_responses"""
return _filter_and_sum_responses(
dframe=dframe,
ensemble=ensemble,
response=response,
filteroptions=filteroptions,
aggregation=aggregation,
)
def _filter_and_sum_responses(
dframe, ensemble, response, filteroptions=None, aggregation="sum",
):
"""Filter response dataframe for the given ensemble
and optional filter columns. Returns dataframe grouped and
aggregated per realization."""
df = dframe.copy()
df = df.loc[df["ENSEMBLE"] == ensemble]
if filteroptions:
for opt in filteroptions:
if opt["type"] == "multi" or opt["type"] == "single":
if isinstance(opt["values"], list):
df = df.loc[df[opt["name"]].isin(opt["values"])]
else:
if opt["name"] == "DATE" and isinstance(opt["values"], str):
df = df.loc[df["DATE"].astype(str) == opt["values"]]
else:
df = df.loc[df[opt["name"]] == opt["values"]]
elif opt["type"] == "range":
df = df.loc[
(df[opt["name"]] >= np.min(opt["values"]))
& (df[opt["name"]] <= np.max(opt["values"]))
]
if aggregation == "sum":
return df.groupby("REAL").sum().reset_index()[["REAL", response]]
if aggregation == "mean":
return df.groupby("REAL").mean().reset_index()[["REAL", response]]
raise ValueError(
f"Aggregation of response file specified as '{aggregation}'' is invalid. "
)