Skip to content

Commit ad0e6ea

Browse files
authored
Fixes following pandas 2 (#327)
* Remove deprecated append method, use concat instead * Division by zero returns `-inf`, `nan`, or `inf` depending on the numerator, instead of raising * Fix DeprecationWarnings from scipy.ndimage * Concatenate once * Suppress pandas' SettingWithCopyWarning
1 parent 16d9920 commit ad0e6ea

File tree

11 files changed

+61
-51
lines changed

11 files changed

+61
-51
lines changed

pysteps/blending/steps.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import time
4747

4848
import numpy as np
49-
import scipy.ndimage
49+
from scipy.ndimage import binary_dilation, generate_binary_structure, iterate_structure
5050

5151
from pysteps import cascade
5252
from pysteps import extrapolation
@@ -1599,13 +1599,13 @@ def _compute_incremental_mask(Rbin, kr, r):
15991599

16001600
# buffer observation mask
16011601
Rbin = np.ndarray.astype(Rbin.copy(), "uint8")
1602-
Rd = scipy.ndimage.morphology.binary_dilation(Rbin, kr)
1602+
Rd = binary_dilation(Rbin, kr)
16031603

16041604
# add grayscale rim
1605-
kr1 = scipy.ndimage.generate_binary_structure(2, 1)
1605+
kr1 = generate_binary_structure(2, 1)
16061606
mask = Rd.astype(float)
16071607
for n in range(r):
1608-
Rd = scipy.ndimage.morphology.binary_dilation(Rd, kr1)
1608+
Rd = binary_dilation(Rd, kr1)
16091609
mask += Rd
16101610
# normalize between 0 and 1
16111611
return mask / mask.max()
@@ -1995,10 +1995,10 @@ def _prepare_forecast_loop(
19951995
mask_rim = mask_kwargs.get("mask_rim", 10)
19961996
mask_f = mask_kwargs.get("mask_f", 1.0)
19971997
# initialize the structuring element
1998-
struct = scipy.ndimage.generate_binary_structure(2, 1)
1998+
struct = generate_binary_structure(2, 1)
19991999
# iterate it to expand it nxn
20002000
n = mask_f * timestep / kmperpixel
2001-
struct = scipy.ndimage.iterate_structure(struct, int((n - 1) / 2.0))
2001+
struct = iterate_structure(struct, int((n - 1) / 2.0))
20022002
else:
20032003
mask_rim, struct = None, None
20042004

pysteps/extrapolation/semilagrangian.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import warnings
1616

1717
import numpy as np
18-
import scipy.ndimage.interpolation as ip
18+
from scipy.ndimage import map_coordinates
1919

2020

2121
def extrapolate(
@@ -182,10 +182,10 @@ def interpolate_motion(displacement, velocity_inc, td):
182182
coords_warped = xy_coords + displacement
183183
coords_warped = [coords_warped[1, :, :], coords_warped[0, :, :]]
184184

185-
velocity_inc_x = ip.map_coordinates(
185+
velocity_inc_x = map_coordinates(
186186
velocity[0, :, :], coords_warped, mode="nearest", order=1, prefilter=False
187187
)
188-
velocity_inc_y = ip.map_coordinates(
188+
velocity_inc_y = map_coordinates(
189189
velocity[1, :, :], coords_warped, mode="nearest", order=1, prefilter=False
190190
)
191191

@@ -222,7 +222,7 @@ def interpolate_motion(displacement, velocity_inc, td):
222222
coords_warped = [coords_warped[1, :, :], coords_warped[0, :, :]]
223223

224224
if precip is not None:
225-
precip_warped = ip.map_coordinates(
225+
precip_warped = map_coordinates(
226226
precip,
227227
coords_warped,
228228
mode=map_coordinates_mode,
@@ -232,7 +232,7 @@ def interpolate_motion(displacement, velocity_inc, td):
232232
)
233233

234234
if interp_order > 1:
235-
mask_warped = ip.map_coordinates(
235+
mask_warped = map_coordinates(
236236
mask_min,
237237
coords_warped,
238238
mode=map_coordinates_mode,
@@ -242,7 +242,7 @@ def interpolate_motion(displacement, velocity_inc, td):
242242
)
243243
precip_warped[mask_warped < 0.5] = minval
244244

245-
mask_warped = ip.map_coordinates(
245+
mask_warped = map_coordinates(
246246
mask_finite,
247247
coords_warped,
248248
mode=map_coordinates_mode,

pysteps/feature/tstorm.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -233,26 +233,33 @@ def get_profile(areas, binary, ref, loc_max, time, minref):
233233
cells = areas * binary
234234
cell_labels = cells[loc_max]
235235
labels = np.zeros(cells.shape)
236+
cells_id = []
237+
for n, cell_label in enumerate(cell_labels):
238+
this_id = n + 1
239+
x = np.where(cells == cell_label)[1]
240+
y = np.where(cells == cell_label)[0]
241+
cell_unique = np.zeros(cells.shape)
242+
cell_unique[cells == cell_label] = 1
243+
maxref = np.nanmax(ref[y, x])
244+
contours = skime.find_contours(cell_unique, 0.8)
245+
cells_id.append(
246+
{
247+
"ID": this_id,
248+
"time": time,
249+
"x": x,
250+
"y": y,
251+
"cen_x": np.round(np.nanmean(x)).astype(int),
252+
"cen_y": np.round(np.nanmean(y)).astype(int),
253+
"max_ref": maxref,
254+
"cont": contours,
255+
"area": len(x),
256+
}
257+
)
258+
labels[cells == cell_labels[n]] = this_id
236259
cells_id = pd.DataFrame(
237-
data=None,
260+
data=cells_id,
238261
index=range(len(cell_labels)),
239262
columns=["ID", "time", "x", "y", "cen_x", "cen_y", "max_ref", "cont", "area"],
240263
)
241-
cells_id.time = time
242-
for n in range(len(cell_labels)):
243-
ID = n + 1
244-
cells_id.ID.iloc[n] = ID
245-
cells_id.x.iloc[n] = np.where(cells == cell_labels[n])[1]
246-
cells_id.y.iloc[n] = np.where(cells == cell_labels[n])[0]
247-
cell_unique = np.zeros(cells.shape)
248-
cell_unique[cells == cell_labels[n]] = 1
249-
maxref = np.nanmax(ref[cells_id.y[n], cells_id.x[n]])
250-
contours = skime.find_contours(cell_unique, 0.8)
251-
cells_id.cont.iloc[n] = contours
252-
cells_id.cen_x.iloc[n] = np.round(np.nanmean(cells_id.x[n])).astype(int)
253-
cells_id.cen_y.iloc[n] = np.round(np.nanmean(cells_id.y[n])).astype(int)
254-
cells_id.max_ref.iloc[n] = maxref
255-
cells_id.area.iloc[n] = len(cells_id.x.iloc[n])
256-
labels[cells == cell_labels[n]] = ID
257264

258265
return cells_id, labels

pysteps/motion/constant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"""
1414

1515
import numpy as np
16-
import scipy.ndimage.interpolation as ip
1716
import scipy.optimize as op
17+
from scipy.ndimage import map_coordinates
1818

1919

2020
def constant(R, **kwargs):
@@ -40,7 +40,7 @@ def constant(R, **kwargs):
4040

4141
def f(v):
4242
XYW = [Y + v[1], X + v[0]]
43-
R_w = ip.map_coordinates(
43+
R_w = map_coordinates(
4444
R[-2, :, :], XYW, mode="constant", cval=np.nan, order=0, prefilter=False
4545
)
4646

pysteps/motion/vet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
import numpy
3838
from numpy.ma.core import MaskedArray
39-
from scipy.ndimage.interpolation import zoom
39+
from scipy.ndimage import zoom
4040
from scipy.optimize import minimize
4141

4242
from pysteps.decorators import check_input_frames

pysteps/nowcasts/sseps.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"""
2020

2121
import numpy as np
22-
import scipy.ndimage
2322
import time
23+
from scipy.ndimage import generate_binary_structure, iterate_structure
24+
2425

2526
from pysteps import cascade
2627
from pysteps import extrapolation
@@ -342,10 +343,10 @@ def forecast(
342343
mask_rim = mask_kwargs.get("mask_rim", 10)
343344
mask_f = mask_kwargs.get("mask_f", 1.0)
344345
# initialize the structuring element
345-
struct = scipy.ndimage.generate_binary_structure(2, 1)
346+
struct = generate_binary_structure(2, 1)
346347
# iterate it to expand it nxn
347348
n = mask_f * timestep / kmperpixel
348-
struct = scipy.ndimage.iterate_structure(struct, int((n - 1) / 2.0))
349+
struct = iterate_structure(struct, int((n - 1) / 2.0))
349350

350351
noise_kwargs.update(
351352
{

pysteps/nowcasts/steps.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414
import numpy as np
15-
import scipy.ndimage
15+
from scipy.ndimage import generate_binary_structure, iterate_structure
1616
import time
1717

1818
from pysteps import cascade
@@ -598,10 +598,10 @@ def f(precip, i):
598598
mask_rim = mask_kwargs.get("mask_rim", 10)
599599
mask_f = mask_kwargs.get("mask_f", 1.0)
600600
# initialize the structuring element
601-
struct = scipy.ndimage.generate_binary_structure(2, 1)
601+
struct = generate_binary_structure(2, 1)
602602
# iterate it to expand it nxn
603603
n = mask_f * timestep / kmperpixel
604-
struct = scipy.ndimage.iterate_structure(struct, int((n - 1) / 2.0))
604+
struct = iterate_structure(struct, int((n - 1) / 2.0))
605605
# initialize precip mask for each member
606606
mask_prec = nowcast_utils.compute_dilated_mask(mask_prec, struct, mask_rim)
607607
mask_prec = [mask_prec.copy() for _ in range(n_ens_members)]

pysteps/nowcasts/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import time
2020
import numpy as np
21-
import scipy.ndimage
21+
from scipy.ndimage import binary_dilation, generate_binary_structure
22+
2223
from pysteps import extrapolation
2324

2425

@@ -85,13 +86,13 @@ def compute_dilated_mask(input_mask, kr, r):
8586
"""
8687
# buffer the input mask
8788
input_mask = np.ndarray.astype(input_mask.copy(), "uint8")
88-
mask_dilated = scipy.ndimage.morphology.binary_dilation(input_mask, kr)
89+
mask_dilated = binary_dilation(input_mask, kr)
8990

9091
# add grayscale rim
91-
kr1 = scipy.ndimage.generate_binary_structure(2, 1)
92+
kr1 = generate_binary_structure(2, 1)
9293
mask = mask_dilated.astype(float)
9394
for _ in range(r):
94-
mask_dilated = scipy.ndimage.morphology.binary_dilation(mask_dilated, kr1)
95+
mask_dilated = binary_dilation(mask_dilated, kr1)
9596
mask += mask_dilated
9697

9798
# normalize between 0 and 1

pysteps/tracking/tdating.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def tracking(cells_id, cells_id_prev, labels, V1, max_ID):
200200
cells_ad = advect(cells_id_prev, labels, V1)
201201
cells_ov, labels = match(cells_ad, labels)
202202
newlabels = np.zeros(labels.shape)
203-
for ID, cell in cells_id_new.iterrows():
203+
for index, cell in cells_id_new.iterrows():
204204
if cell.ID == 0 or np.isnan(cell.ID):
205205
continue
206206
new_ID = cells_ov[cells_ov.t_ID == cell.ID].ID.values
@@ -211,12 +211,12 @@ def tracking(cells_id, cells_id_prev, labels, V1, max_ID):
211211
size.append(len(x))
212212
biggest = np.argmax(size)
213213
new_ID = new_ID[biggest]
214-
cells_id_new.ID.iloc[ID] = new_ID
214+
cells_id_new.loc[index, "ID"] = new_ID
215215
else:
216216
max_ID += 1
217217
new_ID = max_ID
218-
cells_id_new.ID.iloc[ID] = new_ID
219-
newlabels[labels == ID + 1] = new_ID
218+
cells_id_new.loc[index, "ID"] = new_ID
219+
newlabels[labels == index + 1] = new_ID
220220
del new_ID
221221
return cells_id_new, max_ID, newlabels
222222

@@ -308,10 +308,11 @@ def couple_track(cell_list, max_ID, mintrack):
308308
index=None,
309309
columns=["ID", "time", "x", "y", "cen_x", "cen_y", "max_ref", "cont"],
310310
)
311+
cell_track = []
311312
for t in range(len(cell_list)):
312313
mytime = cell_list[t]
313-
mycell = mytime[mytime.ID == n]
314-
cell_track = cell_track.append(mycell)
314+
cell_track.append(mytime[mytime.ID == n])
315+
cell_track = pd.concat(cell_track, axis=0)
315316

316317
if len(cell_track) < mintrack:
317318
continue

pysteps/verification/salscores.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from math import sqrt, hypot
1717

1818
import numpy as np
19-
from scipy.ndimage.measurements import center_of_mass
19+
from scipy.ndimage import center_of_mass
2020

2121
from pysteps.exceptions import MissingOptionalDependency
2222
from pysteps.feature import tstorm as tstorm_detect
@@ -159,7 +159,7 @@ def sal_structure(
159159
observation_volume = _sal_scaled_volume(observation_objects).sum()
160160
nom = prediction_volume - observation_volume
161161
denom = prediction_volume + observation_volume
162-
return nom / (0.5 * denom)
162+
return np.divide(nom, (0.5 * denom))
163163

164164

165165
def sal_amplitude(prediction, observation):

0 commit comments

Comments
 (0)