Skip to content

Commit

Permalink
Add decimal percentages for MAPE and wMAPE in the model summary output.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 725366893
  • Loading branch information
yeonjaepark authored and The Meridian Authors committed Feb 11, 2025
1 parent 79ab867 commit f0f8f92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
18 changes: 18 additions & 0 deletions meridian/analysis/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ def bar_chart_width(num_bars: int) -> int:
return (c.BAR_SIZE + c.PADDING_20) * num_bars


def format_percent(percent: float) -> str:
"""Formats a percentage value into a string format.
Percentage values between 0 and 1 are formatted with 1 decimal place.
Percentage values greater than 1 are formatted with 0 decimal places.
Args:
percent: The percentage value to format.
Returns:
A formatted string.
"""
if percent >= 0.01:
return '{:.0%}'.format(percent)
else:
return '{:.1g}%'.format(percent * 100)


def compact_number(n: float, precision: int = 0, currency: str = '') -> str:
"""Formats a number into a compact notation to the specified precision.
Expand Down
10 changes: 10 additions & 0 deletions meridian/analysis/formatter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def test_bar_chart_width(self):
width = formatter.bar_chart_width(num_bars)
self.assertEqual(width, 186)

@parameterized.named_parameters(
('zero_percent', 0.0, '0%'),
('less_than_one_percent', 0.0005, '0.05%'),
('one_percent', 0.01, '1%'),
('greater_than_one_percent', 0.4257, '43%'),
)
def test_format_percent_correct(self, percent, expected):
formatted_percent = formatter.format_percent(percent)
self.assertEqual(formatted_percent, expected)

def test_compact_number_expr_default(self):
expr = formatter.compact_number_expr()
self.assertEqual(expr, "replace(format(datum.value, '.3~s'), 'G', 'B')")
Expand Down
4 changes: 2 additions & 2 deletions meridian/analysis/summarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ def _slice_table_by_evaluation_set(eval_set: str) -> Sequence[str]:
]
row_values = [
'{:.2f}'.format(sliced_table_by_eval_set[c.R_SQUARED].item()),
'{:.0%}'.format(sliced_table_by_eval_set[c.MAPE].item()),
'{:.0%}'.format(sliced_table_by_eval_set[c.WMAPE].item()),
formatter.format_percent(sliced_table_by_eval_set[c.MAPE].item()),
formatter.format_percent(sliced_table_by_eval_set[c.WMAPE].item()),
]
return row_values

Expand Down

0 comments on commit f0f8f92

Please sign in to comment.