Advance run scenarios - #1852
Conversation
|
As discussed with @davide-f , the implementation must allow to modify only a few entries in the config. E.g. it should be possible to specify in the config diff only a few parameters for Also, we need to find a way to avoid over-writing |
|
Sharing feedback from @tacwebservices who has been working with @tacwebservices I'm adding an explanation on that you have shared, feel free to correct me if needed. For |
|
For The error looked as follows: |
| def update(base_dictionary, diff_dictionary): | ||
| """ | ||
| A function to recursively update items in a dictionary which | ||
| intended usage is unpdating the base config according to the diff | ||
| config | ||
| """ | ||
| for key_to_apply, value_to_apply in diff_dictionary.items(): | ||
| if isinstance(value_to_apply, collections.abc.Mapping) and isinstance( | ||
| base_dictionary.get(key_to_apply), collections.abc.Mapping | ||
| ): | ||
| base_keys = set(base_dictionary[key_to_apply]) | ||
| diff_keys = set(value_to_apply) | ||
|
|
||
| if base_keys & diff_keys: | ||
| base_dictionary[key_to_apply] = update( | ||
| base_dictionary[key_to_apply], value_to_apply | ||
| ) | ||
| else: | ||
| base_dictionary[key_to_apply] = copy.deepcopy(value_to_apply) | ||
| else: | ||
| d[k] = v | ||
| return d | ||
| base_dictionary[key_to_apply] = copy.deepcopy(value_to_apply) | ||
|
|
||
| return base_dictionary |
There was a problem hiding this comment.
I've investigated this issue aiming to understand the needs. When looking at the pinned issue #1850 , I perceive the key issue is the inability to "delete" entries from the original dictionary.
We could explicitly capture that feature by enablind deleting options when the diff value is empty, such as "cutout-2013-era5: {}".
May this solve the issue? I'm attaching a code below that does that, though it requires polishing and local testing with run_all_scenarios. I've been testing with the notebook test_update.ipynb attached to this message.
Many thanks!
Moreover, please remind the release_note
| def update(base_dictionary, diff_dictionary): | |
| """ | |
| A function to recursively update items in a dictionary which | |
| intended usage is unpdating the base config according to the diff | |
| config | |
| """ | |
| for key_to_apply, value_to_apply in diff_dictionary.items(): | |
| if isinstance(value_to_apply, collections.abc.Mapping) and isinstance( | |
| base_dictionary.get(key_to_apply), collections.abc.Mapping | |
| ): | |
| base_keys = set(base_dictionary[key_to_apply]) | |
| diff_keys = set(value_to_apply) | |
| if base_keys & diff_keys: | |
| base_dictionary[key_to_apply] = update( | |
| base_dictionary[key_to_apply], value_to_apply | |
| ) | |
| else: | |
| base_dictionary[key_to_apply] = copy.deepcopy(value_to_apply) | |
| else: | |
| d[k] = v | |
| return d | |
| base_dictionary[key_to_apply] = copy.deepcopy(value_to_apply) | |
| return base_dictionary | |
| def update(base_dictionary, diff_dictionary): | |
| """ | |
| A function to recursively update items in a dictionary which | |
| intended usage is unpdating the base config according to the diff | |
| config | |
| """ | |
| for key_to_apply, value_to_apply in diff_dictionary.items(): | |
| if isinstance(value_to_apply, collections.abc.Mapping) and not set(value_to_apply): | |
| # if the diff value is an empty dict, delete the key from the base dict | |
| del base_dictionary[key_to_apply] | |
| elif isinstance(value_to_apply, collections.abc.Mapping) and isinstance( | |
| base_dictionary.get(key_to_apply), collections.abc.Mapping | |
| ): | |
| # if the diff value is a non-empty dict and the base value is also a dict, update the base dict with the diff dict | |
| base_dictionary[key_to_apply] = update( | |
| base_dictionary[key_to_apply], value_to_apply | |
| ) | |
| else: | |
| # if the diff value is not a dict, update the base dict with the diff value | |
| base_dictionary[key_to_apply] = copy.deepcopy(value_to_apply) | |
| return base_dictionary |
Relates to #1850 and aims to fix a duplication issue in the first place.
Changes proposed in this Pull Request
The PR is advancing
update()function which aims to update the base config by setting values according to the diff config. The revision adds functionality which is replacing a block under a key instead adding one more block under the same key.Additionally, some minor refactoring changes are introduced, such as adding a docstring and naming revision.
Checklist
envs/environment.yamlanddoc/requirements.txt.config.default.yamlandconfig.tutorial.yaml.test/(note tests are changing the config.tutorial.yaml)doc/configtables/*.csvand line references are adjusted indoc/user-guide/configuration.mdanddoc/tutorials/electricity-model.md.doc/assets/scripts/extract_config_snippets.pyaccordingly.configs/bundle_config.yaml.doc/release-notes.mdis amended in the format of previous release notes, including reference to the requested PR.