Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

var has not key 'diagnostic' error when checking var from the recipe #2312

Closed
enekomartinmartinez opened this issue Jan 30, 2024 · 2 comments · Fixed by #2313
Closed

var has not key 'diagnostic' error when checking var from the recipe #2312

enekomartinmartinez opened this issue Jan 30, 2024 · 2 comments · Fixed by #2313

Comments

@enekomartinmartinez
Copy link
Contributor

Describe the bug
When a necessary entry is missing in a variable, instead of raising the correct error defined in

def variable(var: dict[str, Any], required_keys: Iterable[str]):
"""Check variables as derived from recipe."""
required = set(required_keys)
missing = required - set(var)
if missing:
raise RecipeError(
f"Missing keys {missing} in\n"
f"{pformat(var)}\n"
"for variable {var['variable_group']} in diagnostic "
f"{var['diagnostic']}")

another error is raised as var has no key 'diagnostic. Also a format f is missing in line 111.

Solving it with a PR.

Please attach
An example with missing mip won't produce the expected error message.

datasets:
- project: OBS
  institute: ukmo
  type: obs
  dataset: en4-v4.2.2
  freq_base: ''
  freq_folder: monthly_mean
  timerange: 190001/190001
diagnostics:
  temperature:
    variables:
      thetao: {}
    scripts: null
2024-01-30 16:30:49,330 UTC [3133476] ERROR   Program terminated abnormally, see stack trace below for more information:
Traceback (most recent call last):
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_main.py", line 518, in run
    fire.Fire(ESMValTool())
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/fire/core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
                                ^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/fire/core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_main.py", line 405, in run
    self._run(recipe, session)
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_main.py", line 447, in _run
    process_recipe(recipe_file=recipe, session=session)
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_main.py", line 127, in process_recipe
    recipe = read_recipe_file(recipe_file, session)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_recipe/recipe.py", line 73, in read_recipe_file
    return Recipe(raw_recipe, session, recipe_file=filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_recipe/recipe.py", line 714, in __init__
    self.datasets = Dataset.from_recipe(recipe_file, session)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/dataset.py", line 152, in from_recipe
    return datasets_from_recipe(recipe, session)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_recipe/to_datasets.py", line 386, in datasets_from_recipe
    variable_datasets = _get_datasets_for_variable(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_recipe/to_datasets.py", line 350, in _get_datasets_for_variable
    for facets, supplementaries in _get_facets_from_recipe(
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_recipe/to_datasets.py", line 327, in _get_facets_from_recipe
    yield _get_dataset_facets_from_recipe(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_recipe/to_datasets.py", line 281, in _get_dataset_facets_from_recipe
    check.variable(
  File "/gpfs/projects/bsc32/software/suselinux/11/software/ESMValTool/2.10.0/lib/python3.11/site-packages/esmvalcore/_recipe/check.py", line 112, in variable
    f"{var['diagnostic']}")
       ~~~^^^^^^^^^^^^^^
KeyError: 'diagnostic'
2024-01-30 16:30:49,405 UTC [3133476] INFO    
If you have a question or need help, please start a new discussion on https://github.com/ESMValGroup/ESMValTool/discussions
If you suspect this is a bug, please open an issue on https://github.com/ESMValGroup/ESMValTool/issues
To make it easier to find out what the problem is, please consider attaching the files run/recipe_*.yml and run/main_log_debug.txt from the output directory.
@enekomartinmartinez enekomartinmartinez changed the title var has not attribute 'diagnostic' error when checking var from the recipe var has not key 'diagnostic' error when checking var from the recipe Jan 30, 2024
@valeriupredoi
Copy link
Contributor

cheers for raising this @enekomartinmartinez ! diagnostic is an internal key of the big var dictionary assembled in the recipe reading and task assembly - if that's missing I'd imagine a whole lot of other alarms would ring - but, looking at the example you provide, it looks like a lot of stuff is missing already ie thetao: {} with a null script and no preprocessing would mean there is nothing to be done here - @bouweandela I thought we had a working case of absolutely nothing to be done?

@enekomartinmartinez
Copy link
Contributor Author

Thanks @valeriupredoi

I tried to provide a minimal example to make it more simple. That's why I am not passing preprocessors or scripts, but the recipe above should be functional. The only missing necessary part is the documentation section and the mip information, which was consciously removed to raise the error.

The fact thetao has no entries is not a problem as long as the necessary information is provided at the dataset level.

Note that when the recipe checks are passed, the diagnostic is not inside the var dictionary but the opposite, so when var is extracted in to_datasets._get_facets_from_recipe, the variable dictionary doesn't have that information. This is why check.duplicate_datasets, which is called in the same function, needs to take diagnostic_name and variable_group as arguments. The diagnostic and variable_group are added after the checks are done, at the end of to_datasets._get_datasets_for_variable.

As the preprocessor is missing in the variable, the default preprocessor is taken, and that works as expected (its done here and documented here in the note) and produces the output correctly in other jobs (when the mip is given).

The issue here is that the mip entry is missing, which could be defined at the dataset level or variable level (as both dictionaries are merged in loops). If you prefer having a more realistic example, you can try running a real recipe but removing one of the mandatory keys for the variable as mip. You will get the KeyError, instead that the expected RecipeError

Note that the fix is already done in #2313.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants