Skip to content
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

Return total scaled volume in SAL metric #345

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Changes from 2 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
35 changes: 20 additions & 15 deletions pysteps/verification/salscores.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ def sal_structure(
observation_objects = _sal_detect_objects(
observation, thr_factor, thr_quantile, tstorm_kwargs
)
prediction_volume = _sal_scaled_volume(prediction_objects).sum()
observation_volume = _sal_scaled_volume(observation_objects).sum()
prediction_volume = sal_scaled_volume(prediction_objects)
dnerini marked this conversation as resolved.
Show resolved Hide resolved
observation_volume = sal_scaled_volume(observation_objects)
nom = prediction_volume - observation_volume
denom = prediction_volume + observation_volume
return np.divide(nom, (0.5 * denom))
return nom / (0.5 * denom)
dnerini marked this conversation as resolved.
Show resolved Hide resolved


def sal_amplitude(prediction, observation):
Expand Down Expand Up @@ -360,10 +360,10 @@ def _sal_detect_objects(precip, thr_factor, thr_quantile, tstorm_kwargs):
}
_, labels = tstorm_detect.detection(precip, **tstorm_kwargs)
labels = labels.astype(int)
precip_objects = regionprops_table(
labels, intensity_image=precip, properties=REGIONPROPS
precip_objects = pd.DataFrame(
regionprops_table(labels, intensity_image=precip, properties=REGIONPROPS)
)
return pd.DataFrame(precip_objects)
return precip_objects


def _sal_scaled_volume(precip_objects):
Expand All @@ -379,8 +379,8 @@ def _sal_scaled_volume(precip_objects):

Returns
-------
object_volume: pd.Series
A pandas Series with the scaled volume of each precipitation object.
total_scaled_volum: float
The total scaled volume of precipitation objects.
"""
if not PANDAS_IMPORTED:
raise MissingOptionalDependency(
Expand All @@ -389,13 +389,18 @@ def _sal_scaled_volume(precip_objects):
)
objects_volume_scaled = []
for _, precip_object in precip_objects.iterrows():
intensity_sum = precip_object.intensity_image.sum()
intensity_sum = np.nansum(precip_object.intensity_image)
max_intensity = precip_object.max_intensity
volume_scaled = intensity_sum / max_intensity
objects_volume_scaled.append(volume_scaled)
return pd.Series(
data=objects_volume_scaled, index=precip_objects.label, name="scaled_volume"
tot_vol = intensity_sum * volume_scaled
objects_volume_scaled.append(
{"intensity_vol": tot_vol, "intensity_sum_obj": intensity_sum}
)
df_vols = pd.DataFrame(objects_volume_scaled)
total_scaled_volum = (np.nansum(df_vols.intensity_vol)) / (
np.nansum(df_vols.intensity_sum_obj)
)
return total_scaled_volum


def _sal_weighted_distance(precip, thr_factor, thr_quantile, tstorm_kwargs):
Expand Down Expand Up @@ -443,10 +448,10 @@ def _sal_weighted_distance(precip, thr_factor, thr_quantile, tstorm_kwargs):
yd = (precip_objects["weighted_centroid-0"][i] - centroid_total[0]) ** 2

dst = sqrt(xd + yd)
sumr = (precip_objects.intensity_image[i].sum()) * dst
sumr = (np.nansum(precip_objects.intensity_image[i])) * dst

sump = precip_objects.intensity_image[i].sum()
sump = np.nansum(precip_objects.intensity_image[i])

r.append({"sum_dist": sumr, "sum_p": sump})
rr = pd.DataFrame(r)
return rr.sum_dist.sum() / (rr.sum_p.sum())
return (np.nansum(rr.sum_dist)) / (np.nansum(rr.sum_p))
Loading