monocongo/climate_indices

NumbaNotImplementedError in computing pearson_parameters

Closed this issue · 6 comments

Hello, I'm using your package (Python 3.11 on Windows) for calculating SPI and SPEI indices, starting from timeseries of precipitation and evapotranspiration derived from ERA5 Land. As for SPI, the package worked. Unfortunately, when I try to compute SPEI, I encounter a problem in applying the compute.pearson_parameter function on the climate water balance timeseries (daily precipitation - evapotranspiration). The error message is the following:

              NumbaNotImplementedError                  Traceback (most recent call last)
              Cell In[49], line 34
                   21 scaled_values = compute.scale_values(
                   22     values,
                   23     scale=scale,
                   24     periodicity=periodicity,
                   25 )
                   28 # from scipy.stats import pearson3
                   29 # param = pearson3.fit(values)
                   30 # pdf_fitted = pearson3.pdf(date,*param[:-2], loc=param[-2], scale=param[-1])
              ---> 34 prob_zeros, locs, scales, skews = compute.pearson_parameters(
                   35     scaled_values,
                   36     data_start_year=initial_year,
                   37     calibration_start_year=calibration_year_initial,
                   38     calibration_end_year=calibration_year_final,
                   39     periodicity=periodicity,
                   40 )
              
              File [c:\Users\...\miniconda3\envs\eccodesenv\Lib\site-packages\numba\core\dispatcher.py:468], in_DispatcherBase._compile_for_args(self, *args, **kws)
                  464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
                  465                f"by the following argument(s):\n{args_str}\n")
                  466         e.patch_message(msg)
              --> 468     error_rewrite(e, 'typing')
                  469 except errors.UnsupportedError as e:
              ...
              
              NumbaNotImplementedError: Failed in object mode pipeline (step: object mode frontend)
              Failed in object mode pipeline (step: object mode backend)
              ,  raise (Var(message, compute.py:279))
              During: lowering " raise (Var(message, compute.py:279))" at c:\Users\...\miniconda3\envs\eccodesenv\Lib\site-packages\climate_indices\compute.py (281)

I tested the same function (pearson_parameters) on the precipitation timeseries used for the SPI and it did not work either.
Any suggestions?

Thank you in advance

Giulia

It does not make any difference if i use bic: climate water balance or prec: precipitation series in the compute.pearson_parameters. Here is the code:

prec_E5L_rsum = prec_E5L.rolling("30D", min_periods=30).sum().dropna()
evap_E5L_rsum = evap_E5L.rolling("30D", min_periods=30).sum().dropna()
bic_rsum = bic_E5L_daily.rolling("30D", min_periods=30).sum().dropna()

initial_year = bic_rsum.index[0].year
calibration_year_initial = bic_rsum.index[0].year
calibration_year_final = bic_rsum.index[-1].year
period_times = 366
scale = 1
periodicity = compute.Periodicity.daily

values = bic_rsum.values

scaled_values = compute.scale_values(
values,
scale=scale,
periodicity=periodicity,
)

prob_zeros, locs, scales, skews = compute.pearson_parameters(
scaled_values,
data_start_year=initial_year,
calibration_start_year=calibration_year_initial,
calibration_end_year=calibration_year_final,
periodicity=periodicity,
)
pearson_params = {"prob_zero":prob_zeros, "loc":locs, "scale":scales, "skew":skews}

speival = indices.spei(
precips_mm= prec_E5L_rsum,
pet_mm= evap_E5L_rsum,
scale=scale,
distribution=indices.Distribution.pearson,
data_start_year=initial_year,
calibration_year_initial=calibration_year_initial,
calibration_year_final=calibration_year_final,
periodicity=compute.Periodicity.monthly,
fitting_params=pearson_params,
)
I adapted the code following this example for the SPI calculation: https://github.com/martinvonk/SPEI/blob/main/doc/examples/example04_package_comparison.ipynb

I'm sorry you've run into issues with this package, Giulia. I have an impending update that will maybe solve this issue for you. In the next version, we drop the usage of numba. If you know how to install via a non-master branch and can give that a try then I recommend that, as it may solve the issue for you in the meantime. Here's my PR with the latest refactored code: #526

Thank you for your quick response. When do you think the update will be ready? I wish you good work!

Testing the Development Branch

Follow these steps to install and test your code against the development branch I mentioned above:

  1. Clone the repository and switch to the development branch:
git clone -b issue_522_pyproject_poetry https://github.com/monocongo/climate_indices.git
cd climate_indices

If you don't have Poetry installed, you can install it with:

python -m pip install poetry
  1. Create a virtual environment and install dependencies:
poetry shell
poetry install

Now you are in a virtual environment provided by poetry, so you can run your code there to see if the changes on that branch will affect your result. Once you've completed your work there and want to exit the virtual environment you can get out with a single exit command:

exit

The PR for the changes is ready to go I've just been reticent to pull the trigger. This issue may be the impetus I needed!

It worked! Thank you very much for the detailed explanation!

Thanks for following up, @GiuliaIsNotAvailable. I'm happy we've fixed your issue!