Skip to content

Commit

Permalink
Black plot api
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Feb 16, 2021
1 parent 8209086 commit 5a1db7f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 46 deletions.
61 changes: 32 additions & 29 deletions ert_gui/tools/plot/plot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@


class PlotApi(object):

def __init__(self, facade):
self._facade = facade

def all_data_type_keys(self):
""" Returns a list of all the keys except observation keys. For each key a dict is returned with info about
the key"""
"""Returns a list of all the keys except observation keys. For each key a dict is returned with info about
the key"""

all_keys = self._facade.all_data_type_keys()
log_keys = [k for k in all_keys if k.startswith("LOG10_")]

return [{"key": key,
"index_type": self._key_index_type(key),
"observations": self._facade.observation_keys(key),
"has_refcase": self._facade.has_refcase(key),
"dimensionality": self._dimensionality_of_key(key),
"metadata": self._metadata(key),
"log_scale": key in log_keys}
for key in all_keys]

return [
{
"key": key,
"index_type": self._key_index_type(key),
"observations": self._facade.observation_keys(key),
"has_refcase": self._facade.has_refcase(key),
"dimensionality": self._dimensionality_of_key(key),
"metadata": self._metadata(key),
"log_scale": key in log_keys,
}
for key in all_keys
]

def _metadata(self, key):
meta = {}
Expand All @@ -36,19 +38,22 @@ def _metadata(self, key):
return meta

def get_all_cases_not_running(self):
""" Returns a list of all cases that are not running. For each case a dict with info about the case is
returned """
"""Returns a list of all cases that are not running. For each case a dict with info about the case is
returned"""
facade = self._facade
return [{"name": case,
"hidden": facade.is_case_hidden(case),
"has_data": facade.case_has_data(case)}
for case
in facade.cases()
if not facade.is_case_running(case)]
return [
{
"name": case,
"hidden": facade.is_case_hidden(case),
"has_data": facade.case_has_data(case),
}
for case in facade.cases()
if not facade.is_case_running(case)
]

def data_for_key(self, case, key):
""" Returns a pandas DataFrame with the datapoints for a given key for a given case. The row index is
the realization number, and the columns are an index over the indexes/dates"""
"""Returns a pandas DataFrame with the datapoints for a given key for a given case. The row index is
the realization number, and the columns are an index over the indexes/dates"""

if key.startswith("LOG10_"):
key = key[6:]
Expand Down Expand Up @@ -103,17 +108,17 @@ def _add_index_range(self, data):
"""
arrays = [data.columns.to_list(), list(range(len(data.columns)))]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['key_index', 'data_index'])
index = pd.MultiIndex.from_tuples(tuples, names=["key_index", "data_index"])
data.columns = index

def refcase_data(self, key):
""" Returns a pandas DataFrame with the data points for the refcase for a given data key, if any.
The row index is the index/date and the column index is the key."""
"""Returns a pandas DataFrame with the data points for the refcase for a given data key, if any.
The row index is the index/date and the column index is the key."""
return self._facade.refcase_data(key)

def history_data(self, key, case=None):
""" Returns a pandas DataFrame with the data points for the history for a given data key, if any.
The row index is the index/date and the column index is the key."""
"""Returns a pandas DataFrame with the data points for the history for a given data key, if any.
The row index is the index/date and the column index is the key."""
return self._facade.history_data(key, case)

def _dimensionality_of_key(self, key):
Expand All @@ -129,5 +134,3 @@ def _key_index_type(self, key):
return "VALUE"
else:
return None


1 change: 0 additions & 1 deletion tests/excluded_files_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def get_files_excluded_from_black(root):
"ert_gui/tools/plot/plot_case_model.py",
"ert_gui/tools/plot/plot_case_selection_widget.py",
"ert_gui/tools/plot/plot_widget.py",
"ert_gui/tools/plot/plot_api.py",
"ert_gui/tools/plot/customize/customize_plot_dialog.py",
"ert_gui/tools/plot/widgets/copy_style_to_dialog.py",
"ert_gui/tools/plot/widgets/clearable_line_edit.py",
Expand Down
92 changes: 76 additions & 16 deletions tests/gui/tools/plot/test_plot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@
)


class PlotApiTest(TestCase):
def api(self):
config_file = 'snake_oil.ert'
config_file = "snake_oil.ert"

rc = ResConfig(user_config_file=config_file)
rc.convertToCReference(None)
Expand All @@ -124,32 +125,91 @@ def api(self):
api = PlotApi(facade)
return api

@tmpdir(os.path.join(SOURCE_DIR, 'test-data/local/snake_oil'))
@tmpdir(os.path.join(SOURCE_DIR, "test-data/local/snake_oil"))
def test_all_keys_present(self):
api = self.api()

key_defs = api.all_data_type_keys()
keys = {x["key"] for x in key_defs}
expected = {'BPR:1,3,8', 'BPR:445', 'BPR:5,5,5', 'BPR:721', 'FGIP', 'FGIPH', 'FGOR', 'FGORH', 'FGPR', 'FGPRH',
'FGPT', 'FGPTH', 'FOIP', 'FOIPH', 'FOPR', 'FOPRH', 'FOPT', 'FOPTH', 'FWCT', 'FWCTH', 'FWIP',
'FWIPH', 'FWPR', 'FWPRH', 'FWPT', 'FWPTH', 'TIME', 'WGOR:OP1', 'WGOR:OP2', 'WGORH:OP1', 'WGORH:OP2',
'WGPR:OP1', 'WGPR:OP2', 'WGPRH:OP1', 'WGPRH:OP2', 'WOPR:OP1', 'WOPR:OP2', 'WOPRH:OP1', 'WOPRH:OP2',
'WWCT:OP1', 'WWCT:OP2', 'WWCTH:OP1', 'WWCTH:OP2', 'WWPR:OP1', 'WWPR:OP2', 'WWPRH:OP1', 'WWPRH:OP2',
'SNAKE_OIL_PARAM:BPR_138_PERSISTENCE', 'SNAKE_OIL_PARAM:BPR_555_PERSISTENCE',
'SNAKE_OIL_PARAM:OP1_DIVERGENCE_SCALE', 'SNAKE_OIL_PARAM:OP1_OCTAVES', 'SNAKE_OIL_PARAM:OP1_OFFSET',
'SNAKE_OIL_PARAM:OP1_PERSISTENCE', 'SNAKE_OIL_PARAM:OP2_DIVERGENCE_SCALE',
'SNAKE_OIL_PARAM:OP2_OCTAVES', 'SNAKE_OIL_PARAM:OP2_OFFSET', 'SNAKE_OIL_PARAM:OP2_PERSISTENCE',
'SNAKE_OIL_GPR_DIFF@199', 'SNAKE_OIL_OPR_DIFF@199', 'SNAKE_OIL_WPR_DIFF@199'}
expected = {
"BPR:1,3,8",
"BPR:445",
"BPR:5,5,5",
"BPR:721",
"FGIP",
"FGIPH",
"FGOR",
"FGORH",
"FGPR",
"FGPRH",
"FGPT",
"FGPTH",
"FOIP",
"FOIPH",
"FOPR",
"FOPRH",
"FOPT",
"FOPTH",
"FWCT",
"FWCTH",
"FWIP",
"FWIPH",
"FWPR",
"FWPRH",
"FWPT",
"FWPTH",
"TIME",
"WGOR:OP1",
"WGOR:OP2",
"WGORH:OP1",
"WGORH:OP2",
"WGPR:OP1",
"WGPR:OP2",
"WGPRH:OP1",
"WGPRH:OP2",
"WOPR:OP1",
"WOPR:OP2",
"WOPRH:OP1",
"WOPRH:OP2",
"WWCT:OP1",
"WWCT:OP2",
"WWCTH:OP1",
"WWCTH:OP2",
"WWPR:OP1",
"WWPR:OP2",
"WWPRH:OP1",
"WWPRH:OP2",
"SNAKE_OIL_PARAM:BPR_138_PERSISTENCE",
"SNAKE_OIL_PARAM:BPR_555_PERSISTENCE",
"SNAKE_OIL_PARAM:OP1_DIVERGENCE_SCALE",
"SNAKE_OIL_PARAM:OP1_OCTAVES",
"SNAKE_OIL_PARAM:OP1_OFFSET",
"SNAKE_OIL_PARAM:OP1_PERSISTENCE",
"SNAKE_OIL_PARAM:OP2_DIVERGENCE_SCALE",
"SNAKE_OIL_PARAM:OP2_OCTAVES",
"SNAKE_OIL_PARAM:OP2_OFFSET",
"SNAKE_OIL_PARAM:OP2_PERSISTENCE",
"SNAKE_OIL_GPR_DIFF@199",
"SNAKE_OIL_OPR_DIFF@199",
"SNAKE_OIL_WPR_DIFF@199",
}
self.assertSetEqual(expected, keys)

@tmpdir(os.path.join(SOURCE_DIR, 'test-data/local/snake_oil'))
@tmpdir(os.path.join(SOURCE_DIR, "test-data/local/snake_oil"))
def test_observation_key_present(self):
api = self.api()
key_defs = api.all_data_type_keys()
expected_obs = {
'FOPR': ['FOPR'],
'WOPR:OP1': ['WOPR_OP1_108', 'WOPR_OP1_190', 'WOPR_OP1_144', 'WOPR_OP1_9', 'WOPR_OP1_72', 'WOPR_OP1_36'],
'SNAKE_OIL_WPR_DIFF@199': ["WPR_DIFF_1"]
"FOPR": ["FOPR"],
"WOPR:OP1": [
"WOPR_OP1_108",
"WOPR_OP1_190",
"WOPR_OP1_144",
"WOPR_OP1_9",
"WOPR_OP1_72",
"WOPR_OP1_36",
],
"SNAKE_OIL_WPR_DIFF@199": ["WPR_DIFF_1"],
}

for key_def in key_defs:
Expand Down

0 comments on commit 5a1db7f

Please sign in to comment.