Skip to content

Commit

Permalink
Fix tests warnings (#1628)
Browse files Browse the repository at this point in the history
* Replace btdtri with betaincinv

The function scipy.special.btdtri was deprecated in SciPy 1.12, and will
be removed in SciPy 1.14.

scipy.special.betaincinv should be a drop-in replacement.

* Use plain datetime.now() in test

datetime.datetime.utcnow() is deprecated because it is too easy of a
footgun.

Since we don't need timezone-aware objects, now() has the same behaviour
(to the difference of the local time difference to UTC).

The exact time used (or timezone-equivalent) has no impact on this test,
the only requirements is for both calls to have the same timestamp.

* Remove concatenation with an empty dataframe

Concatenation of empty dataframes causes a FutureWarning in pandas.
None values in concatenation are ignored, as long as not all objects to
concatenate are None.

* Preserve the sparse nature of dataframes

In a sparse dataframe, all elements must have a sparse Dtype.
Values added after the fact need to be converted.
  • Loading branch information
e10e3 authored Nov 14, 2024
1 parent de119ab commit ada5ada
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion river/bandit/bayes_ucb.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def compute_index(self, arm_id):
"""the p-th quantile of the beta distribution for the arm"""
p = 1 - 1 / (self._n + 1)
posterior = self._posteriors[arm_id]
return scipy.special.btdtri(posterior.alpha, posterior.beta, p)
return scipy.special.betaincinv(posterior.alpha, posterior.beta, p)

def update(self, arm_id, *reward_args, **reward_kwargs):
"""Rewrite update function"""
Expand Down
6 changes: 3 additions & 3 deletions river/covariance/test_emp.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_covariance_update_sampled():
def test_covariance_update_many(ddof):
cov = covariance.EmpiricalCovariance(ddof=ddof)
p = 5
X_all = pd.DataFrame(columns=range(p))
X_all = None

for _ in range(p):
n = np.random.randint(1, 31)
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_covariance_update_many(ddof):
def test_covariance_update_many_shuffled(ddof):
cov = covariance.EmpiricalCovariance(ddof=ddof)
p = 5
X_all = pd.DataFrame(columns=range(p))
X_all = None

for _ in range(p):
n = np.random.randint(5, 31)
Expand All @@ -143,7 +143,7 @@ def test_covariance_update_many_sampled():
ddof = 1
cov = covariance.EmpiricalCovariance(ddof=ddof)
p = 5
X_all = pd.DataFrame(columns=range(p))
X_all = None

for _ in range(p):
n = np.random.randint(5, 31)
Expand Down
10 changes: 8 additions & 2 deletions river/naive_bayes/bernoulli.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,24 @@ def joint_log_likelihood_many(self, X: pd.DataFrame) -> pd.DataFrame:
unknown = [x for x in X.columns if x not in self.feature_counts]
missing = [x for x in self.feature_counts if x not in X.columns]

is_sparse = hasattr(X, "sparse")

if unknown:
X = X.drop(unknown, axis="columns")

if missing:
X[missing] = False
X[missing] = 0
if is_sparse:
# The new values need to be converted to preserve the sparseness of the dataframe.
# Input values can be intergers or floats, converting all to float preserves the behaviour without the need for complex conversion logic.
X = X.astype(pd.SparseDtype(float, 0.0))

index, columns = X.index, X.columns

if not self.class_counts or not self.feature_counts:
return pd.DataFrame(index=index)

if hasattr(X, "sparse"):
if is_sparse:
X = sparse.csr_matrix(X.sparse.to_coo())
X.data = X.data > self.true_threshold
else:
Expand Down
2 changes: 1 addition & 1 deletion river/utils/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def test_issue_1343():
"""
rmean = utils.TimeRolling(proba.MultivariateGaussian(), period=dt.timedelta(microseconds=1))
t = dt.datetime.utcnow()
t = dt.datetime.now()
rmean.update({"a": 0}, t=t)
rmean.update({"a": 1}, t=t)

0 comments on commit ada5ada

Please sign in to comment.