示例#1
0
def protect_rename_dims():
    """Protect :data:`RENAME_DIMS`.

    Use this fixture on tests which invoke code that imports :mod:`message_ix`, e.g.
    :func:`show_versions`. Importing :mod:`message_ix` has the side effect of adding
    values to :data:`RENAME_DIMS`. Using this fixture ensures that the environment for
    other tests is not altered.
    """
    from ixmp.reporting import RENAME_DIMS

    saved = deepcopy(RENAME_DIMS)  # Probably just copy() is sufficient
    yield
    RENAME_DIMS.clear()
    RENAME_DIMS.update(saved)
示例#2
0
def map_as_qty(set_df, full_set):
    """Convert *set_df* to a :class:`.Quantity`.

    For the MESSAGE sets named ``cat_*`` (see :ref:`mapping-sets`)
    :meth:`ixmp.Scenario.set` returns a :class:`~pandas.DataFrame` with two
    columns: the *category* set (S1) elements and the *category member* set
    (S2, also required as the argument `full_set`) elements.

    map_as_qty converts such a DataFrame (*set_df*) into a Quantity with two
    dimensions. At the coordinates *(s₁, s₂)*, the value is 1 if *s₂* is a
    mapped from *s₁*; otherwise 0.

    An category named 'all', containing all elements of `full_set`, is added
    automatically.

    See also
    --------
    broadcast_map
    """
    set_from, set_to = set_df.columns
    names = [RENAME_DIMS.get(c, c) for c in set_df.columns]

    # Add an 'all' mapping
    set_df = pd.concat([
        set_df,
        pd.DataFrame([('all', e) for e in full_set], columns=set_df.columns),
    ])

    # Add a value column
    set_df['value'] = 1

    return set_df.set_index([set_from, set_to])['value'] \
                 .rename_axis(index=names) \
                 .pipe(as_quantity)
示例#3
0
def test_configure(test_mp, test_data_path):
    # TODO test: configuration keys 'units', 'replace_units'

    # Configure globally; reads 'rename_dims' section
    configure(rename_dims={'i': 'i_renamed'})

    # Reporting uses the RENAME_DIMS mapping of 'i' to 'i_renamed'
    scen = make_dantzig(test_mp)
    rep = Reporter.from_scenario(scen)
    assert 'd:i_renamed-j' in rep, rep.graph.keys()
    assert ['seattle', 'san-diego'] == rep.get('i_renamed')

    # Original name 'i' are not found in the reporter
    assert 'd:i-j' not in rep, rep.graph.keys()
    pytest.raises(KeyError, rep.get, 'i')

    # Remove the configuration for renaming 'i', so that other tests work
    RENAME_DIMS.pop('i')