diff --git a/echopop/spatial/krige.py b/echopop/spatial/krige.py index 106dc2a1..1be5addd 100644 --- a/echopop/spatial/krige.py +++ b/echopop/spatial/krige.py @@ -22,7 +22,10 @@ def kriging(transect_data: pd.DataFrame, mesh_data: pd.DataFrame, settings_dict: # Extract biological variable values # ---- Define the variable name - variable_name = settings_dict["variable"] + if "density" not in settings_dict["variable"]: + variable_name = settings_dict["variable"] + "_density" + else: + variable_name = settings_dict["variable"] # ---- Extract array variable_data = transect_data[variable_name].to_numpy() @@ -83,6 +86,8 @@ def kriging(transect_data: pd.DataFrame, mesh_data: pd.DataFrame, settings_dict: # ---- Calculate the integrated variable when distributed over area # --------- Compute area area = settings_dict["kriging_parameters"]["A0"] * mesh_data["fraction_cell_in_polygon"] + # -------- Drop erroneous negative values along edge + kriged_values[kriged_values[:, 0] < 0, 0] = 0.0 # -------- Distribute biological variable over area survey_estimate = np.nansum(kriged_values[:, 0] * area) # ---- Compute the georeferenced CV at each mesh node @@ -478,7 +483,7 @@ def adaptive_search_radius( def kriging_lambda( anisotropy: float, lagged_semivariogram: np.ndarray, - kriging_matrix: np.ndarray, + kriging_matrix_input: np.ndarray, ): """ Apply singular value decomposition (SVD) to compute kriging (lambda) weights @@ -489,14 +494,14 @@ def kriging_lambda( Anisotropy ratio. lagged_semivariogram: np.array Lagged semivariogram - kriging_matrix: np.array + kriging_matrix_input: np.array Kriging matrix. """ # Singular value decomposition (SVD) # ---- U: left singular vectors (directions of maximum variance) # ---- Sigma: singular values (amount of variance captured by each singular vector, U) # ---- VH: conjugate transpose of the right singular vectors - U, Sigma, VH = np.linalg.svd(kriging_matrix, full_matrices=True) + U, Sigma, VH = np.linalg.svd(kriging_matrix_input, full_matrices=True) # Create Sigma mask informed by the ratio-threshold # ---- The ratio between all singular values and their respective diff --git a/echopop/survey.py b/echopop/survey.py index 113a7538..21cabc0c 100644 --- a/echopop/survey.py +++ b/echopop/survey.py @@ -908,7 +908,10 @@ def kriging_analysis( and "model_config" in self.input["statistics"]["variogram"] ) else ( - self.input["statistics"]["variogram"]["model_config"] + { + **self.input["statistics"]["variogram"]["model_config"], + **{"model": ["exponential", "bessel"], "n_lags": 30}, + } if ( not variogram_parameters and "model_config" in self.input["statistics"]["variogram"] diff --git a/echopop/tests/test_pydantic_validation.py b/echopop/tests/test_pydantic_validation.py index 5b22dd9b..689b0e3d 100644 --- a/echopop/tests/test_pydantic_validation.py +++ b/echopop/tests/test_pydantic_validation.py @@ -1124,12 +1124,12 @@ def test_KrigingParameterInputs_model_structure(description): (dict(), None, "Both 'correlation_range' and 'search_radius' arguments are missing"), ( dict(correlation_range=1.0), - dict(anisotropy=0.0, kmin=3, kmax=10, correlation_range=1.0, search_radius=3.0), + dict(anisotropy=1e-3, kmin=3, kmax=10, correlation_range=1.0, search_radius=3.0), None, ), ( dict(correlation_range=1.0, search_radius=5.0), - dict(anisotropy=0.0, kmin=3, kmax=10, correlation_range=1.0, search_radius=5.0), + dict(anisotropy=1e-3, kmin=3, kmax=10, correlation_range=1.0, search_radius=5.0), None, ), ( diff --git a/echopop/utils/validate_dict.py b/echopop/utils/validate_dict.py index ad202757..26908880 100644 --- a/echopop/utils/validate_dict.py +++ b/echopop/utils/validate_dict.py @@ -914,7 +914,7 @@ def create(cls, **kwargs): class KrigingParameterInputs( BaseModel, arbitrary_types_allowed=True, title="kriging model parameters ('kriging_parameters')" ): - anisotropy: realposfloat = Field(default=0.0, allow_inf_nan=False) + anisotropy: realposfloat = Field(default=1e-3, allow_inf_nan=False) kmin: posint = Field(default=3, ge=3) kmax: posint = Field(default=10, ge=3) correlation_range: Optional[realposfloat] = Field(default=None, gt=0.0, allow_inf_nan=False)