8000 Selectors do not raise a warning, if input values are NaN by padix-key · Pull Request #26 · aivant/peppr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Selectors do not raise a warning, if input values are NaN #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/peppr/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def name(self) -> str:
return "mean"

def select(self, values: np.ndarray, smaller_is_better: bool) -> float:
if np.isnan(values).all():
return np.nan
return np.nanmean(values)


Expand All @@ -87,6 +89,8 @@ def name(self) -> str:
return "median"

def select(self, values: np.ndarray, smaller_is_better: bool) -> float:
if np.isnan(values).all():
return np.nan
return np.nanmedian(values)


Expand All @@ -100,6 +104,8 @@ def name(self) -> str:
return "Oracle"

def select(self, values: np.ndarray, smaller_is_better: bool) -> float:
if np.isnan(values).all():
return np.nan
if smaller_is_better:
return np.nanmin(values)
else:
Expand All @@ -126,6 +132,8 @@ def name(self) -> str:

def select(self, values: np.ndarray, smaller_is_better: bool) -> float:
top_values = values[: self._k]
if np.isnan(top_values).all():
return np.nan
if smaller_is_better:
return np.nanmin(top_values)
else:
Expand Down Expand Up @@ -160,6 +168,8 @@ def select(self, values: np.ndarray, smaller_is_better: bool) -> float:
range(len(values)), size=self._k, replace=False
)
top_values = values[random_indices]
if np.isnan(top_values).all():
return np.nan
if smaller_is_better:
return np.nanmin(top_values)
else:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ def test_selectors(selector, expected_value):
assert selected_value == expected_value


@pytest.mark.filterwarnings("error")
@pytest.mark.parametrize("smaller_is_better", [False, True])
@pytest.mark.parametrize(
"selector",
[
peppr.MeanSelector(),
peppr.OracleSelector(),
peppr.TopSelector(5),
peppr.TopSelector(1),
peppr.RandomSelector(5, seed=0),
],
ids=lambda selector: selector.name,
)
def test_nan_values(selector, smaller_is_better):
"""
Check that :meth:`Selector.select()` returns NaN if all values are NaN, without
raising a warnings.
If any value is not NaN, the selector should return an actual value.
"""
values = np.full(10, np.nan)
assert np.isnan(selector.select(values, smaller_is_better))
# Expect a non-NaN value if the input contains any non-NaN value
values = np.concatenate([np.arange(9), [np.nan]])
assert not np.isnan(selector.select(values, smaller_is_better))
Comment on lines +31 to +54
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pytest.mark.filterwarnings("error")

Would this fail if we raised a warning? 🤔

Copy link
Collaborator Author
@padix-key padix-key May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes



def test_random_selector():
"""
Test the RandomSelector's statistical behavior.
Expand Down
0