<jupyter_start><jupyter_text>ConvenienceThis is an example of aneris' `convenience` module. This module doesn't have anywhere near the error checking of aneris' other features, but it does make it slightly simpler to calibrate timeseries and it adds unit handling onto aneris' harmonisation.<jupyter_code>import matplotlib.pyplot as plt
import pandas as pd

import aneris.tutorial
import aneris.convenience
plt.rcParams["figure.figsize"] = (12, 8)<jupyter_output><empty_output><jupyter_text>We start by loading some dummy data.<jupyter_code>model, hist, _ = aneris.tutorial.load_data()
model
hist<jupyter_output><empty_output><jupyter_text>The data must be set up slightly differently to use the convenience methods (it should match the format provided by [scmdata](https://github.com/openscm/scmdata) and [pyam](https://github.com/IAMconsortium/pyam) aka the IAMC style).<jupyter_code>def convert_to_iamc_style(inp, idx=("model", "scenario", "region", "variable", "unit")):
    out = inp.copy()
    out.columns = out.columns.str.lower()
    out = out.set_index(list(idx))
    out.columns = out.columns.map(int)
    
    return out

hist_iamc_style = convert_to_iamc_style(hist)
model_iamc_style = convert_to_iamc_style(model)
model_iamc_style<jupyter_output><empty_output><jupyter_text>We're also going to only harmonise the World data.<jupyter_code>hist_iamc_style = hist_iamc_style[hist_iamc_style.index.get_level_values("region") == "World"]
model_iamc_style = model_iamc_style[model_iamc_style.index.get_level_values("region") == "World"]
model_iamc_style<jupyter_output><empty_output><jupyter_text>Finally, we alter the units of the historical data.<jupyter_code>hist_iamc_style *= 1000
hist_iamc_style.index = hist_iamc_style.index.set_levels(["kt BC / yr"], level="unit")
hist_iamc_style<jupyter_output><empty_output><jupyter_text>Now we harmonise the data using the convenience methods. Note how the historical data's units have been converted to the input data's units before harmonisation.<jupyter_code>model_harmonised = aneris.convenience.harmonise_all(
    scenarios=model_iamc_style,
    history=hist_iamc_style,
    harmonisation_year=2005,
)
model_harmonised<jupyter_output><empty_output><jupyter_text>Make a plot to examine (doing this without scmdata/pyam is fiddly).<jupyter_code>model_iamc_style_pdf = model_iamc_style.copy()
model_iamc_style_pdf["harmonised"] = False

model_harmonised_pdf = model_harmonised.copy()
model_harmonised_pdf["harmonised"] = True

pd.concat([model_iamc_style_pdf, model_harmonised_pdf]).groupby(["harmonised", "variable"]).mean().T.plot()<jupyter_output><empty_output><jupyter_text>The above plot makes clear that the default harmonisation method is `reduce_ratio_2080`. We can override this using the `overrides` argument, which takes a pandas DataFrame as input.<jupyter_code>overrides = pd.DataFrame([
    {"variable": "prefix|Emissions|BC|suffix", "method": "reduce_offset_2030"},
    {"variable": "prefix|Emissions|BC|sector1|suffix", "method": "reduce_ratio_2100"},
])
overrides
model_harmonised_overrides = aneris.convenience.harmonise_all(
    scenarios=model_iamc_style,
    history=hist_iamc_style,
    harmonisation_year=2005,
    overrides=overrides
)
model_harmonised_overrides<jupyter_output><empty_output><jupyter_text>A quick plot shows the change in output as a result of overriding the method.<jupyter_code>model_iamc_style_pdf = model_iamc_style.copy()
model_iamc_style_pdf["harmonised"] = False

model_harmonised_overrides_pdf = model_harmonised_overrides.copy()
model_harmonised_overrides_pdf["harmonised"] = True

pd.concat([model_iamc_style_pdf, model_harmonised_overrides_pdf]).groupby(["harmonised", "variable"]).mean().T.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1034020894722743, 'fraction_numerical': 0.007500669702652022, 'mean_word_length': 7.9330143540669855, 'pattern_counts': {'":': 4, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '426406', 'license': 'Apache-2.0', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1284, 'n_words': 255, 'path': 'doc/source/convenience.ipynb', 'repo_name': 'fbenke-pik/aneris', 'size': '3733'}