NREL / celavi

Codebase for the Circular Economy Lifecycle Assessment and VIsualization (CELAVI) modeling framework.
https://nrel.github.io/celavi/
GNU General Public License v3.0
9 stars 7 forks source link

KeyError in save_costgraph_outputs when simulating for short time spans #202

Closed rjhanes closed 3 months ago

rjhanes commented 3 months ago

Bug description

Original error message, thrown when using the national wind blade dataset to run 1-2 years ending at 2016:

60587 - 2016 - Transportation - transportation shortcut calculations done
LCA inventory does not exist for 2016 in use glass fiber
LCA emissions inventory does not exist for 2016 in use glass fiber
LCA inventory does not exist for 2016 in use epoxy
LCA emissions inventory does not exist for 2016 in use epoxy
60672 - 2016 - Transportation - transportation shortcut calculations done
Creating diagnostic visualizations for run 0 at 3589 s
Traceback (most recent call last):
  File "/Users/mayadl/anaconda3/envs/celavi/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/mayadl/anaconda3/envs/celavi/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/mayadl/Documents/GitHub/celavi/celavi/__main__.py", line 25, in <module>
    Scenario(parser=PARSER)
  File "/Users/mayadl/Documents/GitHub/celavi/celavi/scenario.py", line 119, in __init__
    self.postprocess()
  File "/Users/mayadl/Documents/GitHub/celavi/celavi/scenario.py", line 543, in postprocess
    self.netw.save_costgraph_outputs()
  File "/Users/mayadl/Documents/GitHub/celavi/celavi/costgraph.py", line 1061, in save_costgraph_outputs
    _out = pd.DataFrame(
  File "/Users/mayadl/anaconda3/envs/celavi/lib/python3.8/site-packages/pandas/core/frame.py", line 8884, in explode
    counts0 = self[columns[0]].apply(mylen)
  File "/Users/mayadl/anaconda3/envs/celavi/lib/python3.8/site-packages/pandas/core/frame.py", line 3761, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Users/mayadl/anaconda3/envs/celavi/lib/python3.8/site-packages/pandas/core/indexes/range.py", line 349, in get_loc
    raise KeyError(key)
KeyError: 'destination_facility_id'

Same error message observed when running the national dataset for 2020 and for 2010-2012.

Bug was reproduced using the tiny dataset and setting the start and end years to various 2-3 year time spans around 2016, and was traced back to an empty pathway_crit_history in CostGraph. pathway_crit_history was empty because it makes a record of EOL pathways selected according to the criterion ("crit") value, and no wind blades had entered EOL due to the brief time span. During postprocessing, pathway_crit_history is munged into a human readable format and saved to file as part of the simulation results. When it is empty, the munging and saving fails and produces the above error message. Simulation results generated and postprocessed before this code is executed are saved normally.

Resolution

This is technically not a bug because CELAVI is running as intended, but the error can be better handled to provide useful feedback to the user.

Lines 1061-1072 in costgraph.py will be inserted into a try/except statement that postprocesses normally (maintains current functionality) when there is no error thrown, and prints an error message when a KeyError is detected. This will allow the code to complete execution and print the "simulation finished" message, without saving the empty pathway criterion history.

rjhanes commented 3 months ago

Closed in release v1.3.4