Skip to content

Commit

Permalink
Fix template + update_attr bug (#405)
Browse files Browse the repository at this point in the history
<!-- Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [x] This PR addresses an already opened issue (for bug fixes /
features)
    - This PR fixes #404 
- [ ] (If applicable) Documentation has been added / updated (for bug
fixes / features).
- [ ] (If applicable) Tests have been added.
- [x] This PR does not seem to break the templates.
- [x] CHANGES.rst has been updated (with summary of main changes).
- [x] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added.

### What kind of change does this PR introduce?

* A few fixes to the templates due to changes in xscen and xarray
* Fix #404, I don't know why this worked in the past but not now. From
my understanding there were 2 problems:
1) We iterate over `others` and assign it to `others`. This breaks the
iteration.
2) In the first instance, we pass a dict of dataset to create a dict of
str (l.137). Then, when we iterate over `others` in l.147, it is no
longuer a dict of dataset, we can't get to `attrs`.
Let me know if I misunderstood something.

### Does this PR introduce a breaking change?
i don't think so.

### Other information:
  • Loading branch information
juliettelavoie authored May 15, 2024
2 parents a0ac4b7 + b142557 commit 91a8500
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ Changelog

v0.9.1 (unreleased)
-------------------
Contributors to this version: Pascal Bourgault (:user:`aulemahal`), Trevor James Smith (:user:`Zeitsperre`).
Contributors to this version: Pascal Bourgault (:user:`aulemahal`), Trevor James Smith (:user:`Zeitsperre`), Juliette Lavoie (:user:`juliettelavoie`).

Bug fixes
^^^^^^^^^
* Fixed defaults for ``xr_combine_kwargs`` in ``extract_dataset``. (:pull:`402`).
* Fixed defaults for ``xr_combine_kwargs`` in ``extract_dataset`` (:pull:`402`).
* Fixed bug with `xs.utils.update_attr`(:issue:`404`, :pull:`405`).
* Fixed template 1 bugs due to changes in versions of dependencies. ( :pull:`405`).

Internal changes
^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion templates/1-basic_workflow_with_config/config1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ extract:
- '1950'
- '2100'
other_search_criteria: # put the simulations you want here
processing_level: raw
mip_era: CMIP6
experiment:
- ssp245
Expand Down Expand Up @@ -540,7 +541,7 @@ logging: # general logging args
class : logging.StreamHandler
formatter: default
level : INFO
# file:
# file: #Uncomment if you want a log file, don't forget to also uncomment the filename in paths1.yml.
# class: logging.FileHandler
# formatter: default
# level : DEBUG
Expand Down
8 changes: 4 additions & 4 deletions templates/1-basic_workflow_with_config/paths1_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ dask:
local_directory: DASK_PATH
dashboard_address: YOUR_RANDOM_NUMBER

logging:
handlers:
file:
filename: PATH/logger.log
#logging: #Uncomment if you want a log file
# handlers:
# file:
# filename: PATH/logger.log

utils:
stack_drop_nans:
Expand Down
3 changes: 2 additions & 1 deletion templates/1-basic_workflow_with_config/workflow1.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
ds,
ds[list(ds.data_vars)[0]]
.isel(time=0, drop=True)
.notnull(),
.notnull()
.compute(),
)
# Prepare the filename for the zarr file, using the format specified in paths1.yml
path = CONFIG["paths"]["task"].format(**cur)
Expand Down
11 changes: 7 additions & 4 deletions xscen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,23 @@ def update_attr(
others = others or []
# .strip(' .') removes trailing and leading whitespaces and dots
if attr in ds.attrs:
others = {

others_attrs = {
f"attr{i}": dso.attrs.get(attr, "").strip(" .")
for i, dso in enumerate(others, 1)
}
ds.attrs[attr] = new.format(attr=ds.attrs[attr].strip(" ."), **others, **fmt)
ds.attrs[attr] = new.format(
attr=ds.attrs[attr].strip(" ."), **others_attrs, **fmt
)
# All existing locales
for key in fnmatch.filter(ds.attrs.keys(), f"{attr}_??"):
loc = key[-2:]
others = {
others_attrs = {
f"attr{i}": dso.attrs.get(key, dso.attrs.get(attr, "")).strip(" .")
for i, dso in enumerate(others, 1)
}
ds.attrs[key] = TRANSLATOR[loc](new).format(
attr=ds.attrs[key].strip(" ."), **others, **fmt
attr=ds.attrs[key].strip(" ."), **others_attrs, **fmt
)


Expand Down

0 comments on commit 91a8500

Please sign in to comment.