Skip to content

Commit be8eea4

Browse files
authored
Return total scaled volume in SAL metric (#345)
1 parent c2f8320 commit be8eea4

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

pysteps/verification/salscores.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ def sal_structure(
155155
observation_objects = _sal_detect_objects(
156156
observation, thr_factor, thr_quantile, tstorm_kwargs
157157
)
158-
prediction_volume = _sal_scaled_volume(prediction_objects).sum()
159-
observation_volume = _sal_scaled_volume(observation_objects).sum()
158+
prediction_volume = _sal_scaled_volume(prediction_objects)
159+
observation_volume = _sal_scaled_volume(observation_objects)
160160
nom = prediction_volume - observation_volume
161161
denom = prediction_volume + observation_volume
162162
return np.divide(nom, (0.5 * denom))
@@ -360,10 +360,10 @@ def _sal_detect_objects(precip, thr_factor, thr_quantile, tstorm_kwargs):
360360
}
361361
_, labels = tstorm_detect.detection(precip, **tstorm_kwargs)
362362
labels = labels.astype(int)
363-
precip_objects = regionprops_table(
364-
labels, intensity_image=precip, properties=REGIONPROPS
363+
precip_objects = pd.DataFrame(
364+
regionprops_table(labels, intensity_image=precip, properties=REGIONPROPS)
365365
)
366-
return pd.DataFrame(precip_objects)
366+
return precip_objects
367367

368368

369369
def _sal_scaled_volume(precip_objects):
@@ -379,8 +379,8 @@ def _sal_scaled_volume(precip_objects):
379379
380380
Returns
381381
-------
382-
object_volume: pd.Series
383-
A pandas Series with the scaled volume of each precipitation object.
382+
total_scaled_volum: float
383+
The total scaled volume of precipitation objects.
384384
"""
385385
if not PANDAS_IMPORTED:
386386
raise MissingOptionalDependency(
@@ -389,13 +389,27 @@ def _sal_scaled_volume(precip_objects):
389389
)
390390
objects_volume_scaled = []
391391
for _, precip_object in precip_objects.iterrows():
392-
intensity_sum = precip_object.intensity_image.sum()
392+
intensity_sum = np.nansum(precip_object.intensity_image)
393393
max_intensity = precip_object.max_intensity
394-
volume_scaled = intensity_sum / max_intensity
395-
objects_volume_scaled.append(volume_scaled)
396-
return pd.Series(
397-
data=objects_volume_scaled, index=precip_objects.label, name="scaled_volume"
398-
)
394+
if intensity_sum == 0:
395+
intensity_vol = 0
396+
else:
397+
volume_scaled = intensity_sum / max_intensity
398+
tot_vol = intensity_sum * volume_scaled
399+
intensity_vol = tot_vol
400+
401+
objects_volume_scaled.append(
402+
{"intensity_vol": intensity_vol, "intensity_sum_obj": intensity_sum}
403+
)
404+
df_vols = pd.DataFrame(objects_volume_scaled)
405+
406+
if df_vols.empty or (df_vols["intensity_sum_obj"] == 0).all():
407+
total_scaled_volum = 0
408+
else:
409+
total_scaled_volum = np.nansum(df_vols.intensity_vol) / np.nansum(
410+
df_vols.intensity_sum_obj
411+
)
412+
return total_scaled_volum
399413

400414

401415
def _sal_weighted_distance(precip, thr_factor, thr_quantile, tstorm_kwargs):
@@ -443,10 +457,10 @@ def _sal_weighted_distance(precip, thr_factor, thr_quantile, tstorm_kwargs):
443457
yd = (precip_objects["weighted_centroid-0"][i] - centroid_total[0]) ** 2
444458

445459
dst = sqrt(xd + yd)
446-
sumr = (precip_objects.intensity_image[i].sum()) * dst
460+
sumr = (np.nansum(precip_objects.intensity_image[i])) * dst
447461

448-
sump = precip_objects.intensity_image[i].sum()
462+
sump = np.nansum(precip_objects.intensity_image[i])
449463

450464
r.append({"sum_dist": sumr, "sum_p": sump})
451465
rr = pd.DataFrame(r)
452-
return rr.sum_dist.sum() / (rr.sum_p.sum())
466+
return (np.nansum(rr.sum_dist)) / (np.nansum(rr.sum_p))

0 commit comments

Comments
 (0)