Skip to content

Commit 17b2d4f

Browse files
committed
Document #991
- Reorder, expand, and copyedit docs of default reporter contents. - Add #988, #989 to release notes.
1 parent 2a31faf commit 17b2d4f

File tree

2 files changed

+194
-74
lines changed

2 files changed

+194
-74
lines changed

RELEASE_NOTES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ All changes
4747
- :mod:`message_ix.message` includes :class:`.MESSAGE`.
4848
- :mod:`message_ix.message_macro` includes :class:`.MESSAGE_MACRO`.
4949

50+
- Improve :class:`.Reporter` and its documentation (:pull:`991`).
51+
52+
- Handle older scenarios—for instance, those without |input_cap|—in :meth:`.Reporter.from_scenario` (:issue:`988`).
53+
- Expand and test keys for multiple methods of calculating :ref:`reporter-historical` (:issue:`989`).
54+
5055
- Document the :ref:`minimum version of Java <install-java>` required for :class:`ixmp.JDBCBackend <ixmp.backend.jdbc.JDBCBackend>` (:pull:`962`).
5156
- Document :ref:`how to run a local PostgreSQL instance <install-postgres>`
5257
for local testing using :class:`ixmp.IXMP4Backend <ixmp.backend.ixmp4.IXMP4Backend>` (:pull:`981`).

doc/reporting.rst

Lines changed: 189 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Each layer of this “stack” builds on the features in the level below:
2121
- Calculations for specific technologies
2222
* - ``message_ix``
2323
- Energy model framework
24-
- Common sets/parameters (``output``)
24+
- MESSAGE-specific sets/parameters (``output``)
2525
- Derived quantities (``tom``)
2626
* - ``ixmp``
2727
- Optimization models & data
@@ -37,7 +37,7 @@ Each layer of this “stack” builds on the features in the level below:
3737
These features are accessible through :class:`.Reporter`, which can produce multiple **reports** from one or more Scenarios.
3838
A report and the quantities that enter it is identified by a **key**, and may…
3939

40-
- perform arbitrarily complex calculations while intelligently handling units;
40+
- perform arbitrarily complex calculations, handling dimensionality and units;
4141
- read and make use of data that is ‘exogenous’ to (not included in) a Scenario;
4242
- produce output as Python or R objects (in code), or write to files or databases;
4343
- calculate only a requested subset of quantities; and
@@ -57,10 +57,16 @@ See :doc:`genno:usage` in the genno documentation for an introduction to concept
5757
In :mod:`message_ix.report`:
5858

5959
- The :class:`.Reporter` class is an extended version of the :class:`genno.Computer` class.
60-
- :mod:`ixmp` parameters, scalars, equations, and time-series data all become quantities for the purpose of reporting.
61-
- For example, the |MESSAGEix| parameter ``resource_cost``, defined with the dimensions (node `n`, commodity `c`, grade `g`, year `y`) is identified by the key ``resource_cost:n-c-g-y``.
62-
When summed across the grade/`g` dimension, it has dimensions `n`, `c`, `y` and is identified by the key ``resource_cost:n-c-y``.
63-
- :meth:`.Reporter.from_scenario` automatically sets up keys and tasks (such as ``resource_cost:n-c-g-y``) that simply retrieve raw/unprocessed data from a :class:`~message_ix.Scenario` and return it as a :any:`genno.Quantity`.
60+
- :mod:`ixmp` parameters, scalars, equations, and time-series data
61+
all become ‘quantities’ for the purpose of reporting.
62+
- For example, the |MESSAGEix| parameter ``resource_cost``
63+
defined with the dimensions (node `n`, commodity `c`, grade `g`, year `y`)
64+
is identified by the key ``resource_cost:n-c-g-y``.
65+
When summed across the grade/`g` dimension, it has dimensions `n`, `c`, `y`
66+
and is identified by the key ``resource_cost:n-c-y``.
67+
- :meth:`.Reporter.from_scenario` automatically sets up keys and tasks
68+
(such as ``resource_cost:n-c-g-y``) that simply retrieve raw/unprocessed data from a :class:`~message_ix.Scenario`
69+
and return it as a :any:`genno.Quantity`.
6470
- Operators are defined as functions in modules including:
6571
:mod:`message_ix.report.operator`,
6672
:mod:`ixmp.report.operator`, and
@@ -72,10 +78,14 @@ Usage
7278

7379
A |MESSAGEix| reporting workflow has the following steps:
7480

75-
1. Obtain a :class:`.Scenario` object from an :class:`~ixmp.Platform`.
76-
2. Use :meth:`.Reporter.from_scenario` to prepare a Reporter object with many calculations automatically prepared.
77-
3. (optionally) Use the built-in features of :class:`.Reporter` to describe additional calculations.
78-
4. Use :meth:`.get` 1 or more times to execute tasks, including all the calculations on which they depend:
81+
1. Obtain a :class:`.Scenario` object from a :class:`~ixmp.Platform`.
82+
2. Use :meth:`.Reporter.from_scenario` to prepare a Reporter object
83+
with many calculations automatically prepared.
84+
See below.
85+
3. (optionally) Use the built-in features of :class:`.Reporter`
86+
to describe additional calculations.
87+
4. Use :meth:`.get` 1 or more times to execute tasks,
88+
including all the calculations on which they depend:
7989

8090
.. code-block:: python
8191
@@ -88,12 +98,173 @@ A |MESSAGEix| reporting workflow has the following steps:
8898
rep.get("all")
8999
90100
Note that keys and tasks are **described** in steps (2–3), but they are not **executed** until :meth:`.get` is called—or the results of one task are required by another.
91-
This design allows the Reporter to skip unneeded (and potentially slow) computations and deliver good performance.
101+
This design allows the Reporter to skip unneeded (and potentially slow)
102+
tasks and re-use intermediate results to deliver good performance.
92103
The Reporter's :attr:`~genno.Computer.graph` may contain thousands of tasks for retrieving model quantities and calculating derived quantities, but a particular call to :meth:`.get` may only execute a few of these.
93104

105+
.. _reporter-default:
106+
107+
Default reporter contents
108+
-------------------------
109+
110+
:meth:`message_ix.Reporter.from_scenario <.Reporter.from_scenario>`
111+
returns a new Reporter instance pre-filled with many keys and tasks
112+
based on the contents of the :class:`.Scenario` argument.
113+
114+
These include the contents added by
115+
:meth:`ixmp.Reporter.from_scenario <ixmp.report.Reporter.from_scenario>`
116+
—that is, every :mod:`ixmp` set, parameter, variable, and equation available
117+
in the Scenario—
118+
plus additional keys for derived quantities specific to the MESSAGE model structure.
119+
These automatic contents are prepared using:
120+
121+
.. autosummary::
122+
123+
~message_ix.report.TASKS0
124+
~message_ix.report.PYAM_CONVERT
125+
~message_ix.report.TASKS1
126+
~message_ix.report.get_tasks
127+
128+
…and include the following:
129+
130+
Sets
131+
- Keys matching the standard short symbols for |MESSAGEix| sets/dimensions,
132+
according to :data:`.common.DIMS`.
133+
Thus for instance:
134+
135+
.. code-block:: python
136+
137+
rep.get("n")
138+
139+
…returns a Python :class:`list`
140+
with the elements of the |MESSAGEix| set named "node".
141+
These lists can be used as input to other tasks,
142+
or to construct data structures for indexing, aggregation, and other operators.
143+
- ``y0``: the ``firstmodelyear`` or :math:`y_0` (:class:`int`).
144+
- ``y::model``, :class:`list` of :class:`int`:
145+
only the periods in the `year` set (``y``/:math:`Y`)
146+
that are equal to or greater than ``y0``.
147+
- ``map_<name>``: "one-hot" or indicator quantities
148+
for the respective |MESSAGEix| mapping sets ``cat_<name>``.
149+
150+
Model solution data
151+
The following quantities combine |MESSAGEix| parameter data (exogenous)
152+
and variable data (endogenous; the optimal solution to the MESSAGE linear program)
153+
in commonly-used ways.
154+
155+
Each is available in its full dimensionality
156+
(the union of the dimensions of its operands)
157+
and as partial sums over all combinations of 1 or more dimensions.
158+
Use :meth:`~genno.Computer.full_key` to retrieve the full-dimensionality
159+
:class:`~genno.Key` for any of these quantities.
160+
161+
- ``in`` = ``input`` × ``ACT``;
162+
that is, the product of ``input`` (input intensity) and ``ACT`` (activity).
163+
- ``out`` = ``output`` × ``ACT``
164+
- ``emi`` = ``emission_factor`` × ``ACT``
165+
- ``inv`` = ``inv_cost`` × ``CAP_NEW``
166+
- ``fom`` = ``fix_cost`` × ``CAP``.
167+
The name is an abbreviation for "Fixed Operation and Maintenance costs".
168+
- ``vom`` = ``var_cost`` × ``ACT``,
169+
"Variable Operation and Maintenance costs".
170+
- ``tom`` = ``fom`` + ``vom``,
171+
"Total Operation and Maintenance costs".
172+
- ``land_out`` = ``land_output`` × ``LAND``
173+
- ``land_use_qty`` = ``land_use`` × ``LAND``
174+
- ``land_emi`` = ``land_emission`` × ``LAND``
175+
- ``addon conversion``,
176+
the model parameter ``addon_conversion`` (note space versus underscore),
177+
except broadcast across individual add-on technologies (`ta`)
178+
rather than add-on types (`type_addon`).
179+
- ``addon up``, which is ``addon_up`` similarly broadcast.
180+
- ``addon ACT`` = ``addon conversion`` × ``ACT``
181+
- ``addon in`` = ``input`` × ``addon ACT``
182+
- ``addon out`` = ``output`` × ``addon ACT``
183+
- ``addon potential`` = ``addon up`` × ``addon ACT``,
184+
the maximum potential activity by add-on technology.
185+
- ``price emission``,
186+
the model variable ``PRICE_EMISSION``
187+
broadcast across emission species (`e`) *and* technologies (`t`)
188+
rather than types (`type_emission`, `type_tec`).
189+
190+
.. _reporter-historical:
191+
192+
Historical and reference values
193+
The following quantities combine |MESSAGEix| parameter data.
194+
Because they do not include variable data,
195+
they are entirely exogenous and do not depend on the model solution.
196+
Their names correspond to the keys above:
197+
for example, ``in:*:historical+current`` and ``in:*:historical+weighted``
198+
correspond to ``in:*``: the latter is *within* the model time horizon,
199+
and the former are *before* the first model period.
200+
201+
- ``in:*:historical+full`` = ``input`` × ``historical_activity``.
202+
This is an intermediate quantity,
203+
and in most cases should not be used directly.
204+
It is used to calculate ``{…}:*:historical+current``
205+
and ``{…}:*:historical+weighted``. [#key-notation]_
206+
207+
Similar quantities exist for ``out``, ``emi``, and ``inv``,
208+
with keys like ``out:*:historical+full`` etc.
209+
- ``in:*:historical+current``:
210+
The subset of ``in:*:historical+full`` where :math:`y^V = y^A`.
211+
Use this quantity to represent an assumption that
212+
*all activity in each historical period* :math:`y^A < y_0` *is attributable to
213+
the technology vintage constructed in the same ('current') period*.
214+
In other words, this uses ``input`` intensity values for :math:`(y^V=y^A, y^A)`,
215+
and omits/ignores values for earlier vintages :math:`(y^V < y^A, y^A)`,
216+
even if the |technical_lifetime| and |historical_new_capacity| of those earlier vintages
217+
implies they could be active in the period |yA|.
218+
219+
Similar quantities exist for ``out``, ``emi``, and ``inv``.
220+
- ``in:*:historical+weighted``:
221+
a sum of ``in:*:historical+full`` across the ``yv`` dimension,
222+
weighted by ``share:*:in+historical``.
223+
The latter quantity **must** be supplied exogenously by the user.
224+
This can be done by overriding the placeholder task created by
225+
:meth:`.from_scenario`:
226+
227+
.. code-block:: python
228+
229+
k = rep.full_key("share:*:in+historical")
230+
rep.add(k, ...) # Some task that returns share values, e.g. from file
231+
232+
``share:*:{…}+historical`` values give the fractional contribution
233+
of each technology vintage :math:`y^V`
234+
to the ``historical_activity`` in each :math:`y^A`.
235+
Therefore they **should** sum to 1.0 across the ``yv`` dimension
236+
for each combination of other indices.
237+
238+
Similar quantities exist for ``out``, ``emi``, and ``inv``.
239+
- ``in:*:ref+current``, ``in:*:ref+weighted``, and ``in:*:ref+full``:
240+
Same as above, but computed using ``ref_activity`` instead of ``historical_activity``.
241+
242+
Similar quantities exist for ``out``, ``emi``, and ``inv``.
243+
244+
.. [#key-notation] These string representations of :class:`genno.Key` use
245+
an asterisk for the dimensions (such as ``name:*:tag``)
246+
to indicate “all dimensions for this ``name`` and ``tag``”;
247+
and ``{…}`` to indicate “any string”.
248+
249+
.. _default-reports:
250+
251+
Time series data (:data:`.PYAM_CONVERT`, :data:`.TASKS1`)
252+
Tasks that transform :class:`~genno.Quantity` with varying number of dimensions
253+
to the :mod:`ixmp` :ref:`structure for time-series data <ixmp:data-tsdata>`
254+
(also called the “IAMC data structure”),
255+
specifically as instances of :class:`pyam.IamDataFrame`.
256+
257+
These include:
258+
259+
- ``<name>::pyam`` for most of the above derived quantities.
260+
- ``CAP::pyam`` (from ``CAP``)
261+
- ``CAP_NEW::pyam`` (from ``CAP_NEW``)
262+
- ``message::system``, ``message::costs``, and ``message::emissions``:
263+
concatenation of subsets of the above time series data.
264+
- ``message::default``: concatenation of all of the above time series data.
94265

95266
Customization
96-
=============
267+
-------------
97268

98269
A Reporter prepared with :meth:`.from_scenario` always contains a key
99270
``scenario``, referring to the Scenario to be reported.
@@ -112,8 +283,12 @@ The method :meth:`.Reporter.add` can be used to add *arbitrary* Python code that
112283
rep.add("custom", (my_custom_report, "scenario"))
113284
rep.get("custom")
114285
115-
In this example, the function ``my_custom_report()`` *could* run to thousands of lines; read to and write from multiple files; invoke other programs or Python scripts; etc.
116-
In order to take advantage of the performance-optimizing features of the Reporter, such calculations can instead be composed from atomic (i.e. small, indivisible) operators or functions.
286+
In this example, the function ``my_custom_report()`` **may** run to thousands of lines;
287+
read to and write from multiple files;
288+
invoke other programs or Python scripts; etc.
289+
In order to take advantage of the performance-optimizing features of the Reporter,
290+
such calculations **should** instead be composed from atomic (small, indivisible) operators
291+
or functions.
117292
See the :mod:`genno` documentation for more.
118293

119294
API reference
@@ -146,66 +321,6 @@ Their documentation is repeated below for convenience.
146321
configure
147322
.. currentmodule:: message_ix.report
148323

149-
:meth:`ixmp.Reporter.from_scenario <ixmp.report.Reporter.from_scenario>` automatically adds keys based on the contents of the :class:`.Scenario` argument;
150-
that is, every :mod:`ixmp` set, parameter, variable, and equation available in the Scenario.
151-
:meth:`message_ix.Reporter.from_scenario <.Reporter.from_scenario>` extends this to add additional keys for derived quantities specific to the MESSAGEix model framework.
152-
These include:
153-
154-
.. tip:: Use :meth:`~genno.Computer.full_key` to retrieve the full-dimensionality :class:`~genno.Key` for any of these quantities.
155-
156-
- ``out`` = ``output`` × ``ACT``; that is, the product of ``output`` (output efficiency) and ``ACT`` (activity)
157-
- ``out_hist`` = ``output`` × ``ref_activity`` (historical reference activity)
158-
- ``in`` = ``input`` × ``ACT``
159-
- ``in_hist`` = ``input`` × ``ref_activity``
160-
- ``emi`` = ``emission_factor`` × ``ACT``
161-
- ``emi_hist`` = ``emission_factor`` × ``ref_activity``
162-
- ``inv`` = ``inv_cost`` × ``CAP_NEW``
163-
- ``inv_hist`` = ``inv_cost`` × ``ref_new_capacity``
164-
- ``fom`` = ``fix_cost`` × ``CAP``; the name refers to "Fixed Operation and Maintenance costs"
165-
- ``fom_hist`` = ``fix_cost`` × ``ref_capacity``
166-
- ``vom`` = ``var_cost`` × ``ACT``; "Variable Operation and Maintenance costs"
167-
- ``vom_hist`` = ``var_cost`` × ``ref_activity``
168-
- ``tom`` = ``fom`` + ``vom``; "Total Operation and Maintenance costs"
169-
- ``land_out`` = ``land_output`` × ``LAND``
170-
- ``land_use_qty`` = ``land_use`` × ``LAND``
171-
- ``land_emi`` = ``land_emission`` × ``LAND``
172-
- ``addon conversion``, the model parameter ``addon_conversion`` (note space versus underscore), except broadcast across individual add-on technologies (`ta`) rather than add-on types (`type_addon`).
173-
- ``addon up``, which is ``addon_up`` similarly broadcast.
174-
- ``addon ACT`` = ``addon conversion`` × ``ACT``
175-
- ``addon in`` = ``input`` × ``addon ACT``
176-
- ``addon out`` = ``output`` × ``addon ACT``
177-
- ``addon potential`` = ``addon up`` × ``addon ACT``, the maximum potential activity by add-on technology.
178-
- ``price emission``, the model variable ``PRICE_EMISSION`` broadcast across emission species (`e`) *and* technologies (`t`) rather than types (`type_emission`, `type_tec`).
179-
180-
Other added keys include:
181-
182-
- :mod:`message_ix` adds the standard short symbols for |MESSAGEix| dimensions (sets) based on :data:`.common.DIMS`.
183-
Each of these is also available in a Reporter: for example :py:`rep.get("n")` returns a list with the elements of the |MESSAGEix| set named "node";
184-
:py:`rep.get("t")` returns the elements of the set "technology", and so on.
185-
These keys can be used as input to other computations.
186-
- ``y0`` = the ``firstmodelyear`` or :math:`y_0` (:class:`int`).
187-
- ``y::model`` = only the periods in the `year` set (``y``) that are equal to or greater than ``y0``.
188-
189-
.. _default-reports:
190-
191-
- Computations to convert internal :class:`~genno.Quantity` data format to the IAMC data format, specifically as :class:`pyam.IamDataFrame` objects.
192-
These include:
193-
194-
- ``<name>::pyam`` for most of the above derived quantities.
195-
- ``CAP::pyam`` (from ``CAP``)
196-
- ``CAP_NEW::pyam`` (from ``CAP_NEW``)
197-
198-
- ``map_<name>`` as "one-hot" or indicator quantities for the respective |MESSAGEix| mapping sets ``cat_<name>``.
199-
- Standard reports ``message::system``, ``message::costs``, and ``message::emissions`` per :data:`TASKS1`.
200-
- The report ``message::default``, collecting all of the above reports.
201-
202-
These automatic contents are prepared using:
203-
204-
.. autosummary::
205-
206-
TASKS0
207-
PYAM_CONVERT
208-
TASKS1
209324

210325
.. autoclass:: Reporter
211326
:show-inheritance:

0 commit comments

Comments
 (0)