Skip to content

Commit fcd5b24

Browse files
authored
Fix pandas warnings about .ravel() (#449)
Pandas was issuing FutureWarnings about calling `Series.ravel`, which could be fixed by calling `np.ravel()` instead, and a keyword argument of the groupby `.apply` changing the default to not include the index in the calculations (which is the right thing to do).
1 parent 07b270b commit fcd5b24

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

doc/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ html: api
4141
@echo "Building HTML files."
4242
@echo
4343
# Set PYGMT_USE_EXTERNAL_DISPLAY to "false" to disable external display
44-
PYGMT_USE_EXTERNAL_DISPLAY="false" $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
44+
ENSAIO_DATA_FROM_GITHUB=true PYGMT_USE_EXTERNAL_DISPLAY="false" $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
4545
@echo
4646
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
4747

verde/base/least_squares.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
from warnings import warn
1111

12+
import numpy as np
1213
from sklearn.linear_model import LinearRegression, Ridge
1314
from sklearn.preprocessing import StandardScaler
1415

@@ -65,6 +66,6 @@ def least_squares(jacobian, data, weights, damping=None, copy_jacobian=False):
6566
regr = LinearRegression(fit_intercept=False)
6667
else:
6768
regr = Ridge(alpha=damping, fit_intercept=False)
68-
regr.fit(jacobian, data.ravel(), sample_weight=weights)
69+
regr.fit(jacobian, np.ravel(data), sample_weight=weights)
6970
params = regr.coef_ / scaler.scale_
7071
return params

verde/base/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def score_estimator(scoring, estimator, coordinates, data, weights=None):
5454
result = np.mean(
5555
[
5656
scorer(
57-
DummyEstimator(pred.ravel()),
57+
DummyEstimator(np.ravel(pred)),
5858
coordinates,
59-
data[i].ravel(),
59+
np.ravel(data[i]),
6060
sample_weight=weights[i],
6161
)
6262
for i, pred in enumerate(predicted)
@@ -251,7 +251,7 @@ def check_fit_input(coordinates, data, weights, unpack=True):
251251
)
252252
if any(i.size != j.size for i in weights for j in data):
253253
raise ValueError("Weights must have the same size as the data array.")
254-
weights = tuple(i.ravel() for i in weights)
254+
weights = tuple(np.ravel(i) for i in weights)
255255
else:
256256
weights = tuple([None] * len(data))
257257
if unpack:
@@ -291,4 +291,4 @@ def n_1d_arrays(arrays, n):
291291
(array([0, 1, 2, 3]), array([0, 1, 2, 3]))
292292
293293
"""
294-
return tuple(np.atleast_1d(i).ravel() for i in arrays[:n])
294+
return tuple(np.ravel(np.atleast_1d(i)) for i in arrays[:n])

verde/blockreduce.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ def filter(self, coordinates, data, weights=None): # noqa: A003
174174
"data{}".format(i): attach_weights(self.reduction, w)
175175
for i, w in enumerate(weights)
176176
}
177-
columns = {"data{}".format(i): comp.ravel() for i, comp in enumerate(data)}
177+
columns = {"data{}".format(i): np.ravel(comp) for i, comp in enumerate(data)}
178178
columns["block"] = labels
179179
blocked = pd.DataFrame(columns).groupby("block").aggregate(reduction)
180180
blocked_data = tuple(
181-
blocked["data{}".format(i)].values.ravel() for i, _ in enumerate(data)
181+
np.ravel(blocked["data{}".format(i)]) for i, _ in enumerate(data)
182182
)
183183
blocked_coords = self._block_coordinates(coordinates, blocks, labels)
184184
if len(blocked_data) == 1:
@@ -228,7 +228,7 @@ def _block_coordinates(self, coordinates, block_coordinates, labels):
228228
if self.drop_coords:
229229
coordinates = coordinates[:2]
230230
coords = {
231-
"coordinate{}".format(i): coord.ravel()
231+
"coordinate{}".format(i): np.ravel(coord)
232232
for i, coord in enumerate(coordinates)
233233
}
234234
coords["block"] = labels
@@ -237,7 +237,7 @@ def _block_coordinates(self, coordinates, block_coordinates, labels):
237237
if self.center_coordinates:
238238
unique = np.unique(labels)
239239
for i, block_coord in enumerate(block_coordinates[:2]):
240-
grouped["coordinate{}".format(i)] = block_coord[unique].ravel()
240+
grouped["coordinate{}".format(i)] = np.ravel(block_coord[unique])
241241
return tuple(
242242
grouped["coordinate{}".format(i)].values for i in range(len(coordinates))
243243
)
@@ -414,23 +414,21 @@ def filter(self, coordinates, data, weights=None): # noqa: A003
414414
region=self.region,
415415
)
416416
ncomps = len(data)
417-
columns = {"data{}".format(i): comp.ravel() for i, comp in enumerate(data)}
417+
columns = {"data{}".format(i): np.ravel(comp) for i, comp in enumerate(data)}
418418
columns["block"] = labels
419419
if any(w is None for w in weights):
420420
mean, variance = self._blocked_mean_variance(pd.DataFrame(columns), ncomps)
421421
else:
422422
columns.update(
423-
{"weight{}".format(i): comp.ravel() for i, comp in enumerate(weights)}
423+
{"weight{}".format(i): np.ravel(comp) for i, comp in enumerate(weights)}
424424
)
425425
table = pd.DataFrame(columns)
426426
if self.uncertainty:
427427
mean, variance = self._blocked_mean_uncertainty(table, ncomps)
428428
else:
429429
mean, variance = self._blocked_mean_variance_weighted(table, ncomps)
430-
blocked_data = tuple(comp.values.ravel() for comp in mean)
431-
blocked_weights = tuple(
432-
variance_to_weights(var.values.ravel()) for var in variance
433-
)
430+
blocked_data = tuple(np.ravel(comp) for comp in mean)
431+
blocked_weights = tuple(variance_to_weights(np.ravel(var)) for var in variance)
434432
blocked_coords = self._block_coordinates(coordinates, blocks, labels)
435433
if ncomps == 1:
436434
return blocked_coords, blocked_data[0], blocked_weights[0]

verde/model_selection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,4 +817,4 @@ def select(arrays, index):
817817
"""
818818
if arrays is None or any(i is None for i in arrays):
819819
return arrays
820-
return tuple(i.ravel()[index] for i in arrays)
820+
return tuple(np.ravel(i)[index] for i in arrays)

0 commit comments

Comments
 (0)