brightway-lca / brightway2-parameters

Library for storing, validating, and calculating with parameters
BSD 3-Clause "New" or "Revised" License
1 stars 2 forks source link

yield as a variable name #7

Open ccomb opened 1 year ago

ccomb commented 1 year ago

Agribalyse 3.1.1 contains Input parameters and formulas which use the yield keyword as a variable (such as a grain yield) :

Example from line 2348611 of the Simapro CSV export of AGB 3..1:

Input parameters
yield;8750;Lognormal;1,2;0;0;No;kg/ha.a dry matter^?

This data leads to a ValueError in the basic_validation() when the Simapro extractor tries to parse the data;

Traceback (most recent call last):
  File "/home/.../import_agb311.py", line 62, in <module>
    agb_importer = bw.SimaProCSVImporter(agb_csv_filepath, "agribalyse3.1")
  File "/home/.../brightway2-io/bw2io/importers/simapro_csv.py", line 45, in __init__
    self.data, self.global_parameters, self.metadata = SimaProCSVExtractor.extract(
  File "/home/.../brightway2-io/bw2io/extractors/simapro_csv.py", line 127, in extract
    ds, index = cls.read_data_set(
  File "/home/.../brightway2-io/bw2io/extractors/simapro_csv.py", line 604, in read_data_set
    ps = ParameterSet(
  File "/home/.../brightway2-parameters/bw2parameters/parameter_set.py", line 26, in __new__
    return DefaultParameterSet(*args, **kwargs)
  File "/home/.../brightway2-parameters/bw2parameters/parameter_set.py", line 34, in __init__
    self.basic_validation()
  File "/home/.../brightway2-parameters/bw2parameters/parameter_set.py", line 119, in basic_validation
    raise ValueError(
ValueError: Parameter label yield not a valid Python name

The intended import procedure is the same as in https://github.com/sc-gcoste/brightway-agribalyse3.1/blob/master/build_agb3.1_database.ipynb

The easiest operational solution is to modify the input data to rename yield to something else which is accepted as a variable by the ast parser. But it sould be better to handle it in the code to improved robutness. I've tried to find a solution but I'm unsure at which level it should be. Either in asteval (which explicitely rejects yield, or in bw2parameters, or in bw2io?

BenPortner commented 1 year ago

Hi @ccomb,

Thanks again for this error analysis. I believe the best way to go here is to have a renaming strategy (in bw2io) for the Agribalyse importer. For example, the strategy could go through the data, and rename parameters names yield to Yield, yields, crop_yield...

Ben

ccomb commented 1 year ago

Thanks, I've looked at asteval, there is hardcoded list of names considered as operators or internal names. Maybe it could be used during the parsing phase?