Skip to content

Commit

Permalink
docs(decomposer): improve error messages (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicrie authored Aug 25, 2024
1 parent 7b9764d commit 9d34060
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions tests/models/test_decomposer.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_raise_warning_for_low_init_rank_reduction(mock_data_array):
decomposer = Decomposer(
n_modes=target_variance, init_rank_reduction=init_rank_reduction
)
warn_msg = ".*components were computed which explain.*of the variance but.*of explained variance was requested. Consider increasing the `init_rank_reduction`"
warn_msg = "Dataset has .* components, explaining .* of the variance. However, .*explained variance was requested. Please consider increasing `init_rank_reduction`"
with pytest.warns(UserWarning, match=warn_msg):
decomposer.fit(mock_data_array)

Expand All @@ -243,7 +243,7 @@ def test_compute_at_least_one_component(mock_data_array):
)

# Warning is raised to indicate that the value of init_rank_reduction is too low
warn_msg = "`init_rank_reduction=.*` is too low and results in zero components. One component will be computed instead."
warn_msg = "`init_rank_reduction=.*` is too low resulting in zero components. One component will be computed instead."
with pytest.warns(UserWarning, match=warn_msg):
decomposer.fit(mock_data_array)

Expand Down
6 changes: 3 additions & 3 deletions xeofs/models/decomposer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ def fit(self, X, dims=("sample", "feature")):
self.n_modes_precompute = int(rank * self.init_rank_reduction)
if self.n_modes_precompute < 1:
warnings.warn(
f"`init_rank_reduction={self.init_rank_reduction}` is too low and results in zero components. One component will be computed instead."
f"`init_rank_reduction={self.init_rank_reduction}` is too low resulting in zero components. One component will be computed instead."
)
self.n_modes_precompute = 1

# TODO(nicrie): perhaps we can just set n_modes to rank if it is larger than rank (possible solution for #158)
if self.n_modes_precompute > rank:
raise ValueError(
f"n_modes must be smaller or equal to the rank of the data object (rank={rank})"
f"n_modes must be less than or equal to the rank of the dataset (rank = {rank})."
)

# Check if data is small enough to use exact SVD
Expand Down Expand Up @@ -212,7 +212,7 @@ def fit(self, X, dims=("sample", "feature")):
)
if n_modes_required > self.n_modes_precompute:
warnings.warn(
f"{self.n_modes_precompute} components were computed which explain {total_explained_variance:.2%} of the variance but {self.n_modes:.2%} of explained variance was requested. Consider increasing the `init_rank_reduction`."
f"Dataset has {self.n_modes_precompute} components, explaining {total_explained_variance:.2%} of the variance. However, {self.n_modes:.2%} explained variance was requested. Please consider increasing `init_rank_reduction`."
)
n_modes_required = self.n_modes_precompute

Expand Down

0 comments on commit 9d34060

Please sign in to comment.