Skip to content

Commit

Permalink
Platform.get_meta: add 'strict' parameter
Browse files Browse the repository at this point in the history
'strict' will only retrieve meta indicators from the exact given level
(model, scenario, version combination).
  • Loading branch information
fonfon committed Aug 10, 2020
1 parent 832654f commit 58354a6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ixmp/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,9 @@ def get_meta(self, model: str, scenario: str, version: int) -> dict:
filter meta by a scenario
version : int or str, optional
retrieve meta of a specific model/scenario run version
strict : bool, optional
only retrieve indicators from the requested model-scenario-version
level
Returns
-------
Expand Down
6 changes: 4 additions & 2 deletions ixmp/backend/jdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,11 @@ def item_delete_elements(self, s, type, name, keys):
self.cache_invalidate(*args)

def get_meta(self, model: str = None, scenario: str = None,
version: int = None) -> dict:
version: int = None, strict : bool = False) -> dict:
self._validate_meta_args(model, scenario, version)
meta = self.jobj.getMeta(model, scenario, version)
if version is not None:
version = java.Long(version)
meta = self.jobj.getMeta(model, scenario, version, strict)
return {entry.getKey(): _unwrap(entry.getValue())
for entry in meta.entrySet()}

Expand Down
43 changes: 40 additions & 3 deletions ixmp/tests/core/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
'sample_bool': False}
META_ENTRIES = [
{'sample_int': 3},
]
"""
{'sample_string': 'string_value'},
{'sample_bool': False},
{
Expand All @@ -20,6 +22,7 @@
},
{'mixed_category': ['string', 0.01, 2, True]},
]
"""
DANTZIG = models['dantzig']


Expand Down Expand Up @@ -98,10 +101,12 @@ def test_unique_meta_model_scenario(mp, meta):


@pytest.mark.parametrize('meta', META_ENTRIES)
def test_setting_multiple_models(mp, meta):
def test_get_meta_strict(mp, meta):
"""
Test meta indicators on various scenario/model combinations and levels.
Set meta indicators on several model/scenario/version levels and test
the 'strict' parameter of get_meta().
"""
# set meta on various levels
model_meta = {'model_int': 3, 'model_string': 'string_value',
'model_bool': False}
scenario_meta = {'scenario_int': 3, 'scenario_string': 'string_value',
Expand All @@ -110,6 +115,8 @@ def test_setting_multiple_models(mp, meta):
'sample_bool2': False}
meta3 = {'sample_int3': 3, 'sample_string3': 'string_value3',
'sample_bool3': False, 'mixed3': ['string', 0.01, 2, True]}
meta_scen = {'sample_int4': 3, 'sample_string4': 'string_value4',
'sample_bool4': False, 'mixed4': ['string', 0.01, 2, True]}
scenario2 = 'standard 2'
model2 = 'canning problem 2'
mp.add_scenario(scenario2)
Expand All @@ -127,24 +134,54 @@ def test_setting_multiple_models(mp, meta):
mp.set_meta(meta, **DANTZIG)
mp.set_meta(meta2, **dantzig2)
mp.set_meta(meta3, **dantzig3)
scen = ixmp.Scenario(mp, **DANTZIG, version="new")
scen.commit('save dummy scenario')
scen.set_meta(meta_scen)

# Retrieve and validate meta indicators
# model
obs1 = mp.get_meta(model=DANTZIG['model'])
assert obs1 == model_meta
obs2 = mp.get_meta(scenario=DANTZIG['scenario'])
# scenario
obs2 = mp.get_meta(scenario=DANTZIG['scenario'], strict=True)
assert obs2 == scenario_meta
# model+scenario
obs3 = mp.get_meta(**DANTZIG)
exp3 = copy.copy(meta)
exp3.update(model_meta)
exp3.update(scenario_meta)
assert obs3 == exp3
# model+scenario, strict
obs3_strict = mp.get_meta(**DANTZIG, strict=True)
assert obs3_strict == meta
assert obs3 != obs3_strict

# second model+scenario combination
obs4 = mp.get_meta(**dantzig2)
exp4 = copy.copy(meta2)
exp4.update(scenario_meta)
assert obs4 == exp4
# second model+scenario combination, strict
obs4_strict = mp.get_meta(**dantzig2, strict=True)
assert obs4_strict == meta2
assert obs4 != obs4_strict

# second model+scenario combination
obs5 = mp.get_meta(**dantzig3)
exp5 = copy.copy(meta3)
assert obs5 == exp5

# model+scenario+version
obs6 = mp.get_meta(**DANTZIG, version=scen.version)
exp6 = copy.copy(meta_scen)
exp6.update(meta)
exp6.update(model_meta)
exp6.update(scenario_meta)
assert obs6 == exp6
obs6_strict = mp.get_meta(DANTZIG['model'], DANTZIG['scenario'],
scen.version, strict=True)
assert obs6_strict == meta_scen


@pytest.mark.parametrize('meta', META_ENTRIES)
def test_unique_meta_scenario(mp, meta):
Expand Down

0 comments on commit 58354a6

Please sign in to comment.