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

Fix multiple temporal avg calls on same dataset breaking #329

Merged
merged 1 commit into from
Aug 25, 2022

Conversation

tomvothecoder
Copy link
Collaborator

Description

This was caused by initializing the ds.temporal class once with self._time_bounds. Instead, it needs to be set every time the user runs temporal averaging because this self._time_bounds can change between each call depending on user settings (e.g., drop incomplete djf).

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

If applicable:

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass with my changes (locally and CI/CD build)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have noted that this is a breaking change for a major release (fix or feature that would cause existing functionality to not work as expected)

- This was caused by initializing the `ds.temporal` class once with `self._time_bounds`. Instead, it needs to be set every time the user runs temporal averaging because this `self._time_bounds` can change between each call depending on user settings (e.g., drop incomplete djf).
@tomvothecoder tomvothecoder added type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors. Priority: Critical labels Aug 24, 2022
@tomvothecoder tomvothecoder requested a review from lee1043 August 24, 2022 23:29
@tomvothecoder tomvothecoder self-assigned this Aug 24, 2022
@tomvothecoder
Copy link
Collaborator Author

tomvothecoder commented Aug 24, 2022

Hi @lee1043 and @gleckler1, this PR fixes the issue where multiple temporal averaging calls on the same dataset object breaks.

This is what happens:

import xcdat
infile = "/p/css03/esgf_publish/CMIP6/CMIP/CSIRO-ARCCSS/ACCESS-CM2/historical/r1i1p1f1/Amon/ts/gn/v20191108/ts_Amon_ACCESS-CM2_historical_r1i1p1f1_gn_185001-201412.nc"
d = xcdat.open_dataset(lf, data_var=var)

# `self._time_bounds is initialized in the `.temporal` class
d_ac = d.temporal.climatology(var, freq="month", weighted=True)  # okay

# `self._time_bounds is reused since it is initialized already, and now have been adjusted to drop incomplete DJF seasons
d_clim = d.temporal.climatology(var, freq="season", weighted=True, season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},)  # okay

# `self._time_bounds is reused again, but with dropped incomplete DJF seasons. This results in mismatching bounds shape with the time coordinates.
d_ac = d.temporal.climatology(var, freq="month", weighted=True)  # error when repeat after get seasonal climatology

The solution: Initialize self._time_bounds every time temporal averaging methods to avoid reusing previously initialized values (which could have been modified).

Comment on lines +741 to +743
# The time bounds.
self._time_bounds = self._dataset.bounds.get_bounds("T")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this line here originally, but made the silly mistake of moving it to the class__init__ method in another PR which caused this bug.

@codecov-commenter
Copy link

Codecov Report

Merging #329 (b7d8b1f) into main (e38fe72) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #329   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines         1191      1191           
=========================================
  Hits          1191      1191           
Impacted Files Coverage Δ
xcdat/temporal.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@lee1043
Copy link
Collaborator

lee1043 commented Aug 25, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
No open projects
Status: Done
3 participants