Skip to content

Commit

Permalink
Added proper formatting to tables
Browse files Browse the repository at this point in the history
  • Loading branch information
AKuederle committed Nov 14, 2024
1 parent 8ff57ec commit d825aa1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
1 change: 1 addition & 0 deletions mobgap/utils/df_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def apply_transformations( # noqa: C901, PLR0912
"This is likely due to an unexpected return type of a custom function."
"Please ensure that the return type is a pandas Series for all custom functions."
) from e
column_names = [col_name if isinstance(col_name, tuple) else (col_name,) for col_name in column_names]
try:
transformation_results.columns = pd.MultiIndex.from_tuples(column_names)
except ValueError as e:
Expand Down
40 changes: 40 additions & 0 deletions mobgap/utils/tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Tools to format pandas tables for documentation and publication."""

import pandas as pd


def value_with_range(df: pd.DataFrame, value_col: str, range_col: str, precision: int = 2) -> pd.Series:
"""Combine a value column (float) and a range column tuple(float, float) into one string.
Parameters
----------
df
The DataFrame containing the columns.
value_col
The name of the column containing the value.
range_col
The name of the column containing the range.
precision
The precision to use for the value and range.
"""
return df.apply(
lambda x: f"{x[value_col]:.{precision}f} [{x[range_col][0]:.{precision}f}, {x[range_col][1]:.{precision}f}]",
axis=1,
)


class FormatTransformer:
"""Formatting functions that can be applied to a DataFrame using :func:`~mobgap.utils.df_operations.apply_transformations`.
These functions can be applied to individual columns or to multiple columns combined
by using :class:`~mobgap.utils.df_operations.CustomOperation`.
If you want to perform styling per value, usually the built-in pandas styling functions are more appropriate.
Attributes
----------
value_with_range
Combine a value column (float) and a range column tuple(float, float) into one string.
""" # noqa: E501

value_with_range = value_with_range
59 changes: 53 additions & 6 deletions revalidation/gait_sequences/_01_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@
from functools import partial

from mobgap.pipeline.evaluation import CustomErrorAggregations as A
from mobgap.utils.df_operations import CustomOperation, apply_aggregations
from mobgap.utils.df_operations import (
CustomOperation,
apply_aggregations,
apply_transformations,
)
from mobgap.utils.tables import FormatTransformer as F

custom_aggs = [
CustomOperation(
Expand Down Expand Up @@ -155,10 +160,52 @@
),
]

format_transforms = [
*(
CustomOperation(
identifier=None,
function=partial(
F.value_with_range,
value_col=("mean", c),
range_col=("conf_intervals", c),
),
column_name=c,
)
for c in [
"recall",
"precision",
"f1_score",
"accuracy",
"specificity",
"reference_gs_duration_s",
"detected_gs_duration_s",
"gs_absolute_duration_error_s",
]
),
CustomOperation(
identifier=None,
function=partial(
F.value_with_range,
value_col=("mean", "gs_duration_error_s"),
range_col=("loa", "gs_duration_error_s"),
),
column_name="gs_duration_error_s",
),
CustomOperation(
identifier=None,
function=partial(
F.value_with_range,
value_col=("icc", "gs_duration_s"),
range_col=("icc_ci", "gs_duration_s"),
),
column_name="icc",
),
]

perf_metrics_all = results.groupby(["algo", "version"]).apply(
apply_aggregations, custom_aggs
)
perf_metrics_all.T
perf_metrics_all.pipe(apply_transformations, format_transforms)

# %%
# Per Cohort
Expand All @@ -172,10 +219,10 @@
perf_metrics_per_cohort = (
results.groupby(["cohort", "algo", "version"])
.apply(apply_aggregations, custom_aggs)
.swaplevel(axis=1)
.pipe(apply_transformations, format_transforms)
.loc[cohort_order]
)
perf_metrics_per_cohort.T
perf_metrics_per_cohort

# %%
# Per relevant cohort
Expand Down Expand Up @@ -214,7 +261,7 @@
# %%
perf_metrics_per_cohort.loc[
pd.IndexSlice[low_impairment_cohorts, low_impairment_algo], :
].reset_index("algo", drop=True).T
].reset_index("algo", drop=True)

# %%
high_impairment_algo = "GsdIonescu"
Expand Down Expand Up @@ -246,4 +293,4 @@
# %%
perf_metrics_per_cohort.loc[
pd.IndexSlice[high_impairment_cohorts, high_impairment_algo], :
].reset_index("algo", drop=True).T
].reset_index("algo", drop=True)

0 comments on commit d825aa1

Please sign in to comment.