Skip to content

Commit

Permalink
Merge pull request #78 from awst-austria/fix_metaplots
Browse files Browse the repository at this point in the history
Fix bugs with metadata plots
  • Loading branch information
wpreimes authored Jun 27, 2024
2 parents e6d377c + 3c2e2f8 commit a77c280
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/qa4sm_reader/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _box_caption(Var,
ds_parts = []
id, meta = mds_meta
if tc:
id, meta = other_meta
id, meta = other_meta # show name of the OTHER dataset
if short_caption:
ds_parts.append(
f"{id}-{meta['pretty_name']} ({meta['pretty_version']})")
Expand All @@ -117,10 +117,8 @@ def _box_caption(Var,
id, meta['pretty_name'], meta['pretty_version'],
meta['pretty_variable'], meta['mu']))
capt = '\n and \n'.join(ds_parts)

if tc:
capt = 'Other Data:\n' + capt

return capt

@staticmethod
Expand Down Expand Up @@ -513,7 +511,7 @@ def boxplot_basic(self,
if not values:
return None
# put all Variables in the same dataframe
values = pd.concat(values)
values = pd.concat(values, axis=1)
# create plot
fig, ax = self._boxplot_definition(metric=metric,
df=values,
Expand Down Expand Up @@ -909,6 +907,18 @@ def meta_single(self,
mu = globals._metric_description[metric].format(
globals.get_metric_units(unit_ref))

# For tca with > 3 datasets, it can be that the same metric comes
# from different combinations of datasets, in that case take average
# the different versions
for col in np.unique(values.columns):
loc = np.where(values.columns == col)[0]
if len(loc) == 1:
continue
else:
colmean = values.pop(col).mean(axis=1, skipna=True)
values[col] = colmean


out = plm.boxplot_metadata(df=values,
metadata_values=meta_values,
ax_label=Var.Metric.pretty_name + mu,
Expand Down
15 changes: 9 additions & 6 deletions src/qa4sm_reader/plotting_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def bin_discrete(
group.columns = ["values", meta_key]
group["Dataset"] = col
groups.append(group)
grouped = pd.concat(groups)
grouped = pd.concat(groups, axis=0)
formatted = []
for meta, meta_df in grouped.groupby(meta_key).__iter__():
if meta_df["values"].count() < min_size:
Expand Down Expand Up @@ -1238,17 +1238,20 @@ def _dict2df(to_plot_dict: dict, meta_key: str) -> pd.DataFrame:
values_ds["Dataset"] = ds
values_ds[meta_key] = "\n[".join(range.split(" ["))
range_grouped.append(values_ds)
range_grouped = pd.concat(range_grouped)
range_grouped = pd.concat(range_grouped, axis=0)
to_plot_df.append(range_grouped)
to_plot_df = pd.concat(to_plot_df)
to_plot_df = pd.concat(to_plot_df, axis=0)

return to_plot_df


def add_cat_info(to_plot: pd.DataFrame, metadata_name: str) -> pd.DataFrame:
"""Add info (N, median value) to metadata category labels"""
groups = to_plot.groupby(metadata_name)["values"]
counts = groups.count()
groups = to_plot.groupby(metadata_name)["values"]#
counts = {}
for name, group in groups:
counts[name] = group[~group.index.duplicated(keep='first')].index.size

to_plot[metadata_name] = to_plot[metadata_name].apply(
lambda x: x + "\nN: {}".format(counts[x]))

Expand Down Expand Up @@ -1296,7 +1299,7 @@ def bplot_catplot(to_plot,
x=x,
y=y,
hue="Dataset",
data=to_plot,
data=to_plot.set_index(np.arange(to_plot.index.size)),
palette="Set2",
ax=axis,
showfliers=False,
Expand Down

0 comments on commit a77c280

Please sign in to comment.