From 05bba7fe49130108bcf527e83aed8a9f12ebf254 Mon Sep 17 00:00:00 2001 From: Sebastien Wieckowski Date: Thu, 12 Sep 2024 13:53:14 +0200 Subject: [PATCH] fix to `print_summary(decimals=x)` in lifelines.statistics --- lifelines/statistics.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lifelines/statistics.py b/lifelines/statistics.py index e7b227876..1b18cb2e3 100644 --- a/lifelines/statistics.py +++ b/lifelines/statistics.py @@ -77,7 +77,7 @@ def __init__(self, p_value, test_statistic, name=None, test_name=None, **kwargs) kwargs["test_name"] = test_name self._kwargs = kwargs - def _print_specific_style(self, style, decimals=2, **kwargs): + def _print_specific_style(self, style, **kwargs): """ Parameters ----------- @@ -87,11 +87,11 @@ def _print_specific_style(self, style, decimals=2, **kwargs): """ if style == "html": - return self._html_print(decimals=decimals, **kwargs) + return self._html_print(**kwargs) elif style == "ascii": - return self._ascii_print(decimals=decimals, **kwargs) + return self._ascii_print(**kwargs) elif style == "latex": - return self._latex_print(decimals=decimals, **kwargs) + return self._latex_print(**kwargs) else: raise ValueError("style not available.") @@ -110,6 +110,7 @@ def print_summary(self, decimals=2, style=None, **kwargs): multiple outputs. """ + self.decimals=decimals if style is not None: self._print_specific_style(style) else: @@ -120,16 +121,16 @@ def print_summary(self, decimals=2, style=None, **kwargs): except ImportError: self._ascii_print() - def _html_print(self, decimals=2, **kwargs): - print(self.to_html(decimals, **kwargs)) + def _html_print(self, **kwargs): + print(self.to_html(**kwargs)) - def _latex_print(self, decimals=2, **kwargs): - print(self.to_latex(decimals, **kwargs)) + def _latex_print(self, **kwargs): + print(self.to_latex(**kwargs)) - def _ascii_print(self, decimals=2, **kwargs): - print(self.to_ascii(decimals, **kwargs)) + def _ascii_print(self, **kwargs): + print(self.to_ascii(**kwargs)) - def to_html(self, decimals=2, **kwargs): + def to_html(self, **kwargs): extra_kwargs = dict(list(self._kwargs.items()) + list(kwargs.items())) summary_df = self.summary @@ -140,14 +141,14 @@ def to_html(self, decimals=2, **kwargs): header_df = pd.DataFrame.from_records(headers).set_index(0) header_html = header_df.to_html(header=False, notebook=True, index_names=False) - summary_html = summary_df.to_html(float_format=format_floats(decimals), formatters={**{"p": format_p_value(decimals)}}) + summary_html = summary_df.to_html(float_format=format_floats(self.decimals), formatters={**{"p": format_p_value(self.decimals)}}) return header_html + summary_html - def to_latex(self, decimals=2, **kwargs): + def to_latex(self, **kwargs): # This is using the new Style object in Pandas. Previously df.to_latex was giving a warning. s = self.summary.style - s = s.format(precision=decimals) + s = s.format(precision=self.decimals) return s.to_latex() @property @@ -172,7 +173,7 @@ def summary(self): df["-log2(p)"] = -utils.quiet_log2(df["p"]) return df - def to_ascii(self, decimals=2, **kwargs): + def to_ascii(self, **kwargs): extra_kwargs = dict(list(self._kwargs.items()) + list(kwargs.items())) meta_data = self._stringify_meta_data(extra_kwargs) @@ -182,7 +183,7 @@ def to_ascii(self, decimals=2, **kwargs): s += "\n" + meta_data + "\n" s += "---\n" s += df.to_string( - float_format=format_floats(decimals), index=self.name is not None, formatters={"p": format_p_value(decimals)} + float_format=format_floats(self.decimals), index=self.name is not None, formatters={"p": format_p_value(self.decimals)} ) return s