IndEcol / pymrio

Multi-Regional Input-Output Analysis in Python.
http://pymrio.readthedocs.io/en/latest/
Other
161 stars 71 forks source link

Making consistency checks for pymrio #74

Open konstantinstadler opened 3 years ago

konstantinstadler commented 3 years ago

Currently, changing some values in a dataframe, append rows, ... does not trigger a re-calculation b/c all the dataframes are present. This should be changed to have some hash codes for the data accompanied with some functions to check consistency. A calc_all would then check for any changed data, delete the previous derived data and recalculate the system.

konstantinstadler commented 3 years ago

For a short term solution for appending rows to extensions highlighting the issue:

import pymrio

tio = pymrio.load_test()

tio.calc_all()

tio.emissions.S.shape
# (2, 48)

new_S = tio.emissions.S.iloc[0,:].copy()
new_S.name = ("emission_new", "air")
tio.emissions.S = tio.emissions.S.append(new_S)

tio.emissions.S.shape
# (3, 48)

tio.emissions.get_index()
# MultiIndex([('emission_type1',   'air'),
            # ('emission_type2', 'water')],
           # names=['stressor', 'compartment'])

# NOTE: The issue is the contradicting information in S and F, we need to make 
# the satellite account consistent again. There are two ways to do that:
# First, we can remove the flows and multipliers:
tio.emissions.reset_to_coefficients()
tio.emissions.M = None

tio.emissions.get_index()
# MultiIndex([('emission_type1',   'air'),
            # ('emission_type2', 'water'),
            # (  'emission_new',   'air')],
           # names=['stressor', 'compartment'])

# and then calculate everything again:

tio.calc_all()

# Alternatively, we can just make a new extensions with the new S matrix

del tio
tio = pymrio.load_test()

tio.calc_all()

new_S = tio.emissions.S.iloc[0,:].copy()
new_S.name = ("emission_new", "air")
tio.new_emissions = pymrio.Extension(S = tio.emissions.S.append(new_S), name='new_emissions')
# optional (e.g for saving memory):
# del tio.emissions
tio.calc_all()
Lars-Nun commented 3 years ago

This solution works well. Thanks for the update!