Skip to content
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

Meta support for model/scenario levels #353

Merged
merged 38 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8b486c2
Meta: add first dummy methods and tests
fonfon Jul 23, 2020
beecfe6
Meta: rename scenario meta methods
fonfon Jul 23, 2020
e4e5ba1
Meta: add Platform method to get/set meta
fonfon Jul 27, 2020
9f5c59e
Minor changes
zikolach Jul 28, 2020
05e29ee
Update Meta tests
fonfon Jul 29, 2020
24e142c
Update ixmp.jar
zikolach Jul 29, 2020
f602c39
Meta: Implement Platform.remove meta; add tests
fonfon Jul 29, 2020
84d1677
Meta: improve validation of arguments
fonfon Jul 30, 2020
0fc7407
Meta: add tests setting/retrieving booleans
fonfon Jul 30, 2020
41e34ff
Meta: improve implementation, test scenario meta args
fonfon Jul 30, 2020
afe9657
Expose adding model/scenario names
zikolach Jul 30, 2020
db6df29
Meta: add more tests
fonfon Jul 30, 2020
9032806
Create abstract base methods for adding model/scenario
fonfon Jul 30, 2020
3054d37
Add models/scenarios list methods
zikolach Jul 30, 2020
0a48369
Meta: add release notes, update documentation
fonfon Jul 30, 2020
554f5bf
Add wrapping/unwrapping metadata list values + test
zikolach Jul 30, 2020
65b67b2
Remove duplicate set_scenario_meta method
fonfon Jul 30, 2020
5c47c2e
Fix saving mixed metadata + test
zikolach Jul 30, 2020
87681fa
Meta: documentation, release notes, consistent names
fonfon Jul 30, 2020
41e6aec
Meta: parametrize tests; fix Scenario.add_meta bug
fonfon Jul 30, 2020
261efb8
Fix wrapping integer values (in list metadata)
zikolach Jul 31, 2020
c11cdc7
Rename Platform.list_models to .models/.scenarios
fonfon Aug 1, 2020
ce037f3
Meta tests: make constants upper-case
fonfon Aug 1, 2020
f36f77f
Meta: minor doc updates
fonfon Aug 1, 2020
6df062f
Meta: update ixmp.jar, update+add tests
fonfon Aug 6, 2020
9ba12e3
Meta documentation update: valid args
fonfon Aug 6, 2020
7699af5
Updated ixmp.jar
fonfon Aug 6, 2020
832654f
Flake8 fix: unused f-string
fonfon Aug 6, 2020
6205c42
Platform.get_meta: add 'strict' parameter
fonfon Aug 10, 2020
9c03359
Remove calls of get|set|remove_scenario_meta
fonfon Aug 13, 2020
004fa85
Order backend jdbc and base methods.
fonfon Aug 13, 2020
ff03ede
Adapt changed error message.
fonfon Aug 13, 2020
84ffa4a
Rename scenario/model methods
fonfon Aug 13, 2020
1f7cba3
Update documentation.
fonfon Aug 13, 2020
e7c48ee
Fix minor flake8 syntax error.
fonfon Aug 13, 2020
a939e1e
Update release notes and backend test
fonfon Aug 17, 2020
43a45a5
Order backend test methods alphabetically.
fonfon Aug 17, 2020
8f78a8c
Minor release notes adaption.
fonfon Aug 17, 2020
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
12 changes: 12 additions & 0 deletions RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ Next release
All changes
-----------

- :pull:`353`: Add meta functionality.

- :meth:`Platform.add_model` using :meth:`.Backend.add_model`
- :meth:`Platform.add_scenario` using :meth:`.Backend.add_scenario`
- :meth:`Platform.models` using :meth:`.Backend.models`
- :meth:`Platform.scenarios` using :meth:`.Backend.scenarios`
- :meth:`Platform.get_meta` using :meth:`.Backend.get_meta`
- :meth:`Platform.set_meta` using :meth:`.Backend.set_meta`
- :meth:`Platform.remove_meta` using :meth:`.Backend.remove_meta`
- :meth:`Scenario.remove_meta` using :meth:`.Backend.remove_scenario_meta`
- deprecate :meth:`Scenario.delete_meta`

- :pull:`349`: Avoid modifying indexers dictionary in :meth:`.AttrSeries.sel`.
- :pull:`343`: Add region/unit parameters to :meth:`.Platform.export_timeseries_data`.
- :pull:`347`: Preserve dtypes of index columns in :func:`.data_for_quantity`.
Expand Down
3 changes: 2 additions & 1 deletion doc/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ There are a number of guides out there, e.g. on docutils_.
Building the docs locally
-------------------------

Install the dependencies, above.
Install the dependencies, above. Repeat the installation steps to be able to
refer to code that was changed since the initial installation.

From the command line, run::

Expand Down
12 changes: 10 additions & 2 deletions doc/source/api-backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,24 @@ Backend API
.. autosummary::
:nosignatures:

add_model
add_scenario
close_db
get_auth
get_doc
get_log_level
get_meta
get_nodes
get_scenarios
get_units
models
open_db
read_file
remove_meta
scenarios
set_doc
set_log_level
set_meta
set_node
set_unit
write_file
Expand Down Expand Up @@ -142,15 +149,16 @@ Backend API

clone
delete_item
get_meta
get_scenario_meta
has_solution
init_item
item_delete_elements
item_get_elements
item_set_elements
item_index
list_items
set_meta
remove_scenario_meta
set_scenario_meta

Methods related to :class:`message_ix.Scenario`:

Expand Down
23 changes: 18 additions & 5 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,24 @@ Platform
set_log_level
units

The methods
:meth:`~.base.Backend.open_db`,
:meth:`~.base.Backend.close_db`,
:meth:`~.base.Backend.get_doc`, and
:meth:`~.base.Backend.set_doc` may also be called via Platform.
The following backend methods are available via Platform too:

.. autosummary::
backend.base.Backend.add_model
backend.base.Backend.add_scenario
backend.base.Backend.close_db
backend.base.Backend.get_doc
backend.base.Backend.get_meta
backend.base.Backend.models
backend.base.Backend.open_db
backend.base.Backend.remove_meta
backend.base.Backend.scenarios
backend.base.Backend.set_doc
backend.base.Backend.set_meta

These methods can be called like normal Platform methods, e.g.::

$ platform_instance.close_db()


TimeSeries
Expand Down
140 changes: 128 additions & 12 deletions ixmp/backend/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import ABC, abstractmethod
from copy import copy
from typing import Generator
import json

from ixmp.core import TimeSeries, Scenario
Expand Down Expand Up @@ -53,6 +54,42 @@ def get_auth(self, user, models, kind):
"""
return {model: True for model in models}

@abstractmethod
def add_model(self, name: str):
""" Add (register) new model name.
khaeru marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
user : str, new model name
"""

@abstractmethod
def add_scenario(self, name: str):
""" Add (register) new scenario name.

Parameters
----------
user : str, new scenario name
"""

@abstractmethod
def models(self) -> Generator[str, None, None]:
""" List existing model names.

Returns
-------
list of str
"""

@abstractmethod
def scenarios(self) -> Generator[str, None, None]:
""" List existing scenario names.

Returns
-------
list of str
"""

@abstractmethod
def get_nodes(self):
"""Iterate over all nodes stored on the Platform.
Expand Down Expand Up @@ -888,31 +925,110 @@ def item_delete_elements(self, s: Scenario, type, name, keys):
"""

@abstractmethod
def get_meta(self, s: Scenario):
"""Return all meta.
def get_meta(self, model: str, scenario: str, version: int, strict: bool
) -> dict:
"""Retrieve meta.

Parameters
----------
model : str, optional
filter meta by a model
scenario : str, optional
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
-------
dict (str -> any)
Mapping from meta keys to values.
Mapping from meta category keys to values.

See also
--------
s_get_meta
Raises
------
ValueError
On unsupported model-scenario-version combinations.
Supported combinations are: (model), (scenario), (model, scenario),
(model, scenario, version)
"""

@abstractmethod
def set_meta(self, meta: dict, model: str, scenario: str, version: int):
"""Set meta categories.

Parameters
----------
meta : dict, containing meta key/value category pairs
model : str, optional
model name that meta should be attached to
scenario : str, optional
scenario name that meta should be attached to
version : int, optional
run version that meta should be attached to

Returns
-------
None

Raises
------
ValueError
On unsupported model-scenario-version combinations.
Supported combinations are: (model), (scenario), (model, scenario),
(model, scenario, version)
"""

@abstractmethod
def remove_meta(self, categories: list, model: str, scenario: str,
version: int):
"""Remove meta categories.

Parameters
----------
categories : list of str, meta-category keys to remove
model : str, optional
only remove meta of a specific model
scenario : str, optional
only remove meta of a specific scenario
version : int, optional
only remove meta of a specific model/scenario run version

Returns
-------
None

Raises
------
ValueError
On unsupported model-scenario-version combinations.
Supported combinations are: (model), (scenario), (model, scenario),
(model, scenario, version)
"""

@abstractmethod
def get_scenario_meta(self, s: Scenario):
"""Return all scenario meta.

Returns
-------
dict (str -> any)
Mapping from meta keys to values.
"""

@abstractmethod
def set_meta(self, s: Scenario, name_or_dict, value=None):
"""Set single or multiple meta entries.
def set_scenario_meta(self, s: Scenario, name_or_dict, value=None):
Copy link
Contributor Author

@zikolach zikolach Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be duplicated after renaming

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""Set single or multiple scenario meta entries.

Parameters
----------
name_or_dict : str or dict
If the argument is dict, it used as a mapping of meta
categories (names) to values. Otherwise, use the argument
as the meta attribute name.
as the meta category name.
value : str or number or bool, optional
Meta attribute value.
Meta category value.

Returns
-------
Expand All @@ -925,8 +1041,8 @@ def set_meta(self, s: Scenario, name_or_dict, value=None):
"""

@abstractmethod
def delete_meta(self, s, name):
"""Remove single or multiple meta entries.
def remove_scenario_meta(self, s, name):
"""Remove single or multiple scenario meta entries.

Parameters
----------
Expand Down
Loading