Pyam (compat.pyam)¶
The “IAMC data structure” is a particular data structure with either 6 or 7 dimensions: model, scenario, region, variable, unit, either year or time, and optionally subannual.
Data with this structure are usually stored in a tablular “IAMC format,” wherein each dimension is stored as one column, and the remaining column, labeled value, contains observation values.
Using add_as_pyam() (Computer.add(..., "as_pyam", ...)):
modelandscenarioare populated from the attributes of the object returned by the Computer keyscenario;variablecontains the name(s) of each of the quantities, or others constructed by collapse (see below);unitcontains the units associated with each of the quantities; andyear,time, and optionallysubannualcan be created using rename or collapse operations.
A callback function (collapse) can be supplied that modifies the data before it is converted to an IamDataFrame; for instance, to concatenate extra dimensions into variable labels.
Other dimensions can simply be dropped (with drop).
Dimensions that are not collapsed or dropped will appear as additional columns in the resulting IamDataFrame; this is valid, but non-standard data per the IAMC format.
For example, here the labels for the MESSAGEix t (technology) and m (mode) dimensions are appended to a fixed string to construct variable labels:
c = Computer
def m_t(df):
"""Collapse `t` and `m` dimensions to an IAMC 'Variable' string."""
df["variable"] = "Activity|" + df["t"] + "|" + df["m"]
return df
ACT = c.full_key('ACT')
keys = c.add(ACT, "as_pyam", "ya", collapse=m_t, drop=["t", "m"])
- genno.compat.pyam.HAS_PYAM = True¶
boolindicating whetherpyam: analysis and visualization of integrated-assessment & macro-energy scenariosis available.
|
Return a |
|
|
This module also registers implementations of concat() and write_report() that handle pyam.IamDataFrame objects.
- genno.compat.pyam.operator.add_as_pyam(func, c: Computer, quantities: Key | str | Iterable[Key | str], tag='iamc', /, **kwargs)[source]¶
Computer.add()helper foras_pyam().Add conversion of one or more quantities to the IAMC data structure.
- genno.compat.pyam.operator.quantity_from_iamc(qty: TQuantity | pyam.IamDataFrame | pandas.DataFrame, variable: str, *, fail: int | str = 'warning') TQuantity[source]¶
Extract data for a single measure from qty with IAMC-like structure.
- Parameters:
qty – Must have at least 2 dimensions named ‘v’ (or ‘variable’, any case) and ‘u’ (or ‘unit’, any case).
variable (
str) – Regular expression to match full labels on thevdimension of qty. If the expression contains match groups, they are used to rewritevlabels: only the contents of the first match group are kept. This may be used to discard a portion of the label.
- Returns:
The ‘variable’ dimension contains reduced labels. The
Quantity.unitsattribute contains the unique units for the subset of data.- Return type:
See also
unique_units_from_dim
Configuration¶
compat.pyam adds a handler for a iamc: configuration file section.
- pyam.iamc(info)¶
Handle one entry from the
iamc:config section.Computer-specific configuration.
Invokes
add_as_pyam()and adds additional computations to convert data fromQuantityto apyam.IamDataFrame. Each entry contains:variable:(str)Variable name. This is used two ways: it is placed in the
variablelabel of the resulting IamDataFrame; and the Computer key for executing the conversion is<variable>:iamc.base:(str)Key for the quantity to convert.
select:(dict, optional)Keyword arguments to
operator.select(). This selection is performed while data is inQuantityformat, before it is passed toas_pyam().rename:(dict, optional)Passed to
as_pyam().replace:(dict, optional)Passed to
as_pyam().drop:(listofstr, optional)Passed to
as_pyam().unit:(str, optional)Passed to
as_pyam().
Any further additional entries are passed as keyword arguments to
collapse(), which is then given as the collapse callback foras_pyam().collapse()formats thevariablelabels of the IamDataFrame. The variable name replacements from theiamc variable names:section of the config file are applied to all variables.
Utilities¶
- genno.compat.pyam.util.IAMC_DIMS = frozenset({'model', 'region', 'scenario', 'time', 'unit', 'variable', 'year'})¶
Dimensions of the IAMC data structure used by
pyam: analysis and visualization of integrated-assessment & macro-energy scenarios.
- genno.compat.pyam.util.clean_units(df: DataFrame, unit=None) DataFrame[source]¶
Convert magnitudes and units of df to unit in
str.- Raises:
ValueError – if there is more than one unit.