Commit 37c1d389 authored by Paulo Medeiros's avatar Paulo Medeiros
Browse files

Fix crash in commands when no stations accepted.

parent db67a1f3
......@@ -585,13 +585,16 @@ def report_clustering_results(df):
df (pandas.DataFrame): Dataframe with clustering info.
"""
if len(df.index) == 0:
logger.info("Dataframe has no rows. No results to report.")
return
n_obs = len(df.index)
noise_data_df = df[df["cluster_label"] < 0]
n_noise_clusters = len(noise_data_df["cluster_label"].unique())
noise_count = len(noise_data_df)
n_clusters = len(df["cluster_label"].unique()) - n_noise_clusters
n_accepted = n_obs - noise_count
silhouette_score = df["silhouette_score"].mean(skipna=True)
logger.info("Number of obs passed to the clustering routine: %d", n_obs)
logger.info("Estimated number of clusters: %d", n_clusters)
......@@ -601,4 +604,8 @@ def report_clustering_results(df):
noise_count,
100.0 * noise_count / n_obs,
)
logger.info("Mean silhouette score: %.3f", silhouette_score)
try:
silhouette_score = df["silhouette_score"].mean(skipna=True)
logger.info("Mean silhouette score: %.3f", silhouette_score)
except KeyError:
pass
......@@ -287,6 +287,16 @@ def select_stations(args):
len(df_unique_moving),
)
if len(df_accepted.index) == 0:
logger.info(
"%sSelection finished. No stations were accepted. "
"%d stations were rejected.%s",
logcolor.cyan,
len(df_rejected.index),
logcolor.reset,
)
return
##########################################################################
# Trim accepted obs to (a possibly coarser version of) the domain's grid #
##########################################################################
......@@ -308,13 +318,14 @@ def select_stations(args):
n_preliminary_accepted = len(df_accepted.index)
# (a) Add rejection rate info
@np.vectorize
def rej_rate(stat_id):
def rej_rate_non_vec(stat_id):
try:
return id2rejection_rate[stat_id]
except KeyError:
return 0.0
rej_rate = np.vectorize(rej_rate_non_vec, otypes=[float])
df_accepted["rejection_rate"] = rej_rate(df_accepted["id"])
# (b) Sort by rejection rate (lower to higher)
......
......@@ -554,6 +554,8 @@ class Domain:
pandas.Dataframe: Trimmed dataframe.
"""
if len(df.index) == 0:
return df
trimmed_df = df[self.contains_lonlat(df.lon, df.lat)].copy()
trimmed_df = trimmed_df.reset_index(drop=True)
return trimmed_df
......
......@@ -75,6 +75,11 @@ class DataFrameWithDtgMetadata(pd.DataFrame):
common_keys = set()
for dic in md_dicts_to_combine:
if dic == {}:
# If any dict is empty, then there will be no
# common keys at all.
common_keys = set()
break
common_keys = common_keys.union(set(dic))
combined_md_dict = {}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment