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 & improve grouped spi #50

Merged
merged 9 commits into from
May 10, 2024
Merged

fix & improve grouped spi #50

merged 9 commits into from
May 10, 2024

Conversation

valpesendorfer
Copy link
Member

@valpesendorfer valpesendorfer commented Feb 23, 2024

This PR contains fixes & improvements to the code relevant for grouped (along time dim) SPIs.

Changes:

  • pass attributes down in hdc.zonal.mean
  • allow for arbitrary group labels in hdc.algo.spi
  • fix calibration indices for grouped SPI calculations

@valpesendorfer valpesendorfer force-pushed the patch branch 3 times, most recently from 2c59561 to ac470b7 Compare March 19, 2024 11:44
@valpesendorfer valpesendorfer changed the title Patch release fix & improve grouped spi Mar 19, 2024
if not groups.dtype.name == "int16":
warn("Casting groups to int16!")
groups = groups.astype("int16")
groups, keys = to_linspace(np.array(groups, dtype="str"))
Copy link
Member Author

Choose a reason for hiding this comment

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

@Kirill888 the idea is that the user passes in a list / array / tuple / ... containing group labels which puts the time dimension into bins. The downstream function requires these groups to be in a linear space, so from 0 to len(groups)-1 which was the expected input previously.

To take this burden off the user, this allows any labels and converts them to a str array, which is then converted to linear space. Maybe not most efficient, but for these applications totally fine - does that make sense for you?

For a practical example, we'll be using this to calculate SPIs for the full timeseries, grouping the time dimension into dekads per year. The user can then pass simply xx.time.dekad.yidx in without having to make sure the groups array is 0 indexed.

hdc/algo/accessors.py Outdated Show resolved Hide resolved

if calstart_ix >= calstop_ix:
raise ValueError("calibration_start < calibration_stop!")
if calibration_start > tix[-1:]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this comparing str > [str]?

Copy link
Member Author

Choose a reason for hiding this comment

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

comparing str to pd.Timestamp

Copy link
Collaborator

Choose a reason for hiding this comment

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

why tix[-1:] and not tix[-1]?

Copy link
Member Author

Choose a reason for hiding this comment

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

needs to remain a pd.DateTimeIndex with one element to work ... maybe bit hacky & counterintuitive

Copy link
Member Author

Choose a reason for hiding this comment

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

we could explicitly convert the string to a date object (pd.Timestamp or whatever) and then compare the elements if that's clearer

@valpesendorfer valpesendorfer merged commit d91e6fd into main May 10, 2024
12 checks passed
@valpesendorfer valpesendorfer deleted the patch branch May 10, 2024 10:19
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 this pull request may close these issues.

2 participants