You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The time indexer in the xc.indices.stats.standardized_index_fit_params was not correctly stored in the attributes, leading to errors in different cases:
When there is no indexer, the indexer in the xc.indices.stats.standardized_index_fit_params was changed after to_netcdf and reloaded from xr.open_dataset(), leading to an error when running standardized_precipitation_index(pr, params=params);
When the value of indexer is a non-array single value, there will be an error when encoding the attribute params.attrs["time_indexer"] in stats.standardized_index_fit_params;
When there are multiple indexers, only one indexer is saved, leading to inconsistent behaviour when running standardized_precipitation_index(pr, params=params).
params.to_dataset(name="params").to_netcdf("params.nc")
params = xr.open_dataset("params.nc").params
spi = xci.standardized_precipitation_index(pr, params=params) # this line leads to the error
For the second issue,
params = xci.stats.standardized_index_fit_params(
pr_cal, freq="MS", window=1, dist="gamma", method="APP", fitkwargs={"floc": 0}, zero_inflated=True, month=2)
# executable only when an array is assigned to `month`
For the third issue,
params = xci.stats.standardized_index_fit_params(
pr_cal, freq="MS", window=1, dist="gamma", method="APP", fitkwargs={"floc": 0}, zero_inflated=True, drop=True, month=[2, 3]) # `drop=True` is not saved in params.attrs["time_indexer"]`
xci.standardized_precipitation_index(pr, params=params) # the months other than 2 & 3 were not dropped
Additional context
The error logs are attached here.
For the first issue,
File ~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:882, in standardized_index(da, freq, window, dist, method, zero_inflated, fitkwargs, cal_start, cal_end, params, **indexer)
[880](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:880) # Unpack attrs to None and {} if needed
[881](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:881) freq = None if freq == "" else freq
--> [882](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:882) indexer = {} if indexer[0] == "" else {indexer[0]: indexer[1:]}
[883](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:883) if cal_start or cal_end:
[884](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:884) warnings.warn(
[885](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:885) "Expected either `cal_{start|end}` or `params`, got both. The `params` input overrides other inputs."
[886](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:886) "If `cal_start`, `cal_end`, `freq`, `window`, and/or `dist` were given as input, they will be ignored."
[887](~/.conda/envs/syncfuture/lib/python3.11/site-packages/xclim/indices/stats.py:887) )
IndexError: string index out of range
Hem-W
changed the title
Reloading fitted paramers (standardized_index_fit_params) from netCDF lead to errors, and setting multiple indexer would lead to inconsistent behaviour
Reloading fitted paramers from NC leads to errors, and setting multiple indexer leads to inconsistent behaviour
Jul 18, 2024
Hem-W
changed the title
Reloading fitted paramers from NC leads to errors, and setting multiple indexer leads to inconsistent behaviour
Fitted paramers reloaded from NC lead to errors in standardized index, and setting multiple indexers leads to inconsistent behaviour
Jul 18, 2024
Thanks for the issue @Hem-W ! Indeed, we only tested this mechanic by loading the params in-memory, and forgot about all these issues when going through netCDF!
Setup Information
Description
The time indexer in the
xc.indices.stats.standardized_index_fit_params
was not correctly stored in the attributes, leading to errors in different cases:xc.indices.stats.standardized_index_fit_params
was changed afterto_netcdf
and reloaded fromxr.open_dataset()
, leading to an error when runningstandardized_precipitation_index(pr, params=params)
;params.attrs["time_indexer"]
instats.standardized_index_fit_params
;standardized_precipitation_index(pr, params=params)
.Steps To Reproduce
Load dataset:
For the first issue,
For the second issue,
For the third issue,
Additional context
The error logs are attached here.
For the first issue,
For the second issue,
For the third issue, the attributes in the
params
are likeand the obtained SPI would be like
.
Contribution
Code of Conduct
The text was updated successfully, but these errors were encountered: