-
Notifications
You must be signed in to change notification settings - Fork 134
Description
Describe the bug
When using mgwr.gwr.GWR.predict() to predict values on a set of coordinates (prediction_points) that is different from the coordinates used to fit the model (calibration_points), an IndexError occurs. The error message typically looks like IndexError: index [X] is out of bounds for axis 0 with size [Y], where Y is the number of calibration points and X is an index equal to or greater than Y.
This occurs even after ensuring the predictor matrix for the prediction points (X_pred) does not contain NaNs (e.g., by using np.nan_to_num).
To Reproduce
Steps to reproduce the behavior:
- Fit a GWR model using
Ncalibration points:gwr_model = GWR(calibration_coords, calibration_y, calibration_X, bw).fit()(wherecalibration_Xhas shape(N, k)) - Prepare prediction coordinates (
prediction_coords) and corresponding predictors (X_pred, shape(M, k)), whereM > N(e.g., predicting on a full grid). - Call the predict method:
gwr_model.predict(prediction_coords, X_pred) - Observe the
IndexError.
Traceback Analysis
The full traceback (see below) indicates the error originates within the _local_fit method called by joblib during parallel processing. More specifically, the traceback suggests that the predict method (at mgwr/gwr.py:412 in the traceback provided earlier) incorrectly calls self.fit() internally, which likely leads to the index mismatch when _local_fit tries to access self.X[i] (calibration data) using an index i derived from the prediction point loop.
Expected behavior
The predict() method should successfully compute predictions and local parameters for the prediction_coords without raising an IndexError, using the state of the already-fitted model. It should not be calling fit() internally during prediction.
Traceback (most recent call last):
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\externals\loky\process_executor.py", line 463, in _process_worker
r = call_item()
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\externals\loky\process_executor.py", line 291, in call
return self.fn(*self.args, **self.kwargs)
~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 598, in call
return [func(*args, **kwargs)
~~~~^^^^^^^^^^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\mgwr\gwr.py", line 268, in _local_fit
predy = np.dot(self.X[i], betas)[0]
~~~~~~^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\numpy_core\memmap.py", line 358, in getitem
res = super().getitem(index)
IndexError: index 58757 is out of bounds for axis 0 with size 58757
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "e:\Pankaj_2023\Visual_Studio\Python\aa", line 291, in
pred_results = gwr_model.predict(coords, X_coarse_full_imputed)
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\mgwr\gwr.py", line 412, in predict
gwr = self.fit(**fit_params)
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\mgwr\gwr.py", line 348, in fit
rslt = Parallel(n_jobs=self.n_jobs)(delayed(self._local_fit)(i) for i in range(m))
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 2007, in call
return output if self.return_generator else list(output)
~~~~^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 1650, in _get_outputs
yield from self._retrieve()
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 1754, in _retrieve
self._raise_error_fast()
~~~~~~~~~~~~~~~~~~~~~~^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 1789, in _raise_error_fast
error_job.get_result(self.timeout)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 745, in get_result
return self._return_or_raise()
~~~~~~~~~~~~~~~~~~~~~^^
File "E:\Pankaj_2023\Visual_Studio\Python.venv\Lib\site-packages\joblib\parallel.py", line 763, in _return_or_raise
raise self._result
IndexError: index 58757 is out of bounds for axis 0 with size 58757