Skip to content

Commit

Permalink
Try pass in distance array to linkage method
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Jan 20, 2025
1 parent 5c52e23 commit 314e1c7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/ert/analysis/misfit_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ def cluster_responses(
Cluster responses using hierarchical clustering based on Spearman correlation.
Observations that tend to vary similarly across different simulation runs will be clustered together.
"""
correlation = spearmanr(responses).statistic
if isinstance(correlation, np.float64):
correlation = np.array([[1, correlation], [correlation, 1]])
linkage_matrix = linkage(correlation, "average", "euclidean")
distance = 1 - np.abs(spearmanr(responses).statistic)
if isinstance(distance, np.float64):
distance = np.array(distance)
linkage_matrix = linkage(
distance[np.triu_indices(distance.shape[0], k=1)], "average", "euclidean"
)
return fcluster(linkage_matrix, nr_clusters, criterion="maxclust", depth=2)


Expand Down
1 change: 0 additions & 1 deletion tests/ert/unit_tests/analysis/test_es_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def obs() -> polars.DataFrame:


@pytest.mark.integration_test
@pytest.mark.flaky(reruns=5)
@pytest.mark.parametrize(
"misfit_preprocess", [[["*"]], [], [["FOPR"]], [["FOPR"], ["WOPR_OP1_1*"]]]
)
Expand Down
4 changes: 2 additions & 2 deletions tests/ert/unit_tests/analysis/test_misfit_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def test_misfit_preprocessor(nr_observations):
nr_realizations = 1000
Y = np.ones((nr_observations, nr_realizations))
parameters_a = rng.standard_normal(nr_realizations)
parameters_b = rng.standard_normal(nr_realizations)
parameters_b = rng.normal(scale=5, size=nr_realizations)
for i in range(nr_observations - 1):
Y[i] = i + 1 * parameters_a
Y[-1] = 5 + 1 * parameters_b
obs_errors = Y.mean(axis=1)
obs_errors = np.array([0.1] * nr_observations)
Y_original = Y.copy()
obs_error_copy = obs_errors.copy()
result, *_ = main(Y, obs_errors)
Expand Down

0 comments on commit 314e1c7

Please sign in to comment.