Skip to content

Commit

Permalink
Fix kriging analysis bugs (#302)
Browse files Browse the repository at this point in the history
* Update `requirements-dev.txt`

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add `jupyter-book` tests to `tox`

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Kriging_bug_removal (#7)

* Updates to report generation test file

* General updates

* Report generation functionality

* Bugfixes to the kriging method

* Revert report changes for just kriging debug merging

* Fix to test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
brandynlucca and pre-commit-ci[bot] authored Nov 16, 2024
1 parent 248c411 commit db10cc2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
13 changes: 9 additions & 4 deletions echopop/spatial/krige.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion echopop/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions echopop/tests/test_pydantic_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
(
Expand Down
2 changes: 1 addition & 1 deletion echopop/utils/validate_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit db10cc2

Please sign in to comment.