From a484b6205c2bcfb301341af366fe27ac2a74085e Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Tue, 18 Oct 2022 18:44:04 +1100 Subject: [PATCH] Add option to make __repr__ print the summary --- docs/global.rst | 2 ++ yli/bayes_factors.py | 7 +++++++ yli/config.py | 5 +++++ yli/regress.py | 10 ++++++++++ yli/sig_tests.py | 30 ++++++++++++++++++++++++++++++ yli/utils.py | 10 ++++++++++ 6 files changed, 64 insertions(+) diff --git a/docs/global.rst b/docs/global.rst index d03331a..a26263f 100644 --- a/docs/global.rst +++ b/docs/global.rst @@ -10,3 +10,5 @@ Global options .. autoattribute:: yli.config.Config.pvalue_max_dps .. autoattribute:: yli.config.Config.pvalue_min_dps + + .. autoattribute:: yli.config.Config.repr_is_summary diff --git a/yli/bayes_factors.py b/yli/bayes_factors.py index 8b2851f..e787a4d 100644 --- a/yli/bayes_factors.py +++ b/yli/bayes_factors.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from .config import config + class BayesFactor: """A Bayes factor""" @@ -30,6 +32,11 @@ class BayesFactor: #: Description of the hypothesis in the denominator (*str*) self.denom_desc = denom_desc + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return 'BF{0}{1} = {2:.2f}, {5}
H{0}: {3}
H{1}: {4}'.format(self.num_symbol, self.denom_symbol, self.factor, self.num_desc, self.denom_desc, self.interpret_lw(html=True)) diff --git a/yli/config.py b/yli/config.py index 6741f3a..7c282bb 100644 --- a/yli/config.py +++ b/yli/config.py @@ -18,6 +18,8 @@ class Config: """Global configuration for the library""" def __init__(self): + # NOTE: If add any attributes here, must also change docs/global.rst + #: Display at least this many decimal places for *p* values (*int*) self.pvalue_min_dps = 2 #: Display at most this many decimal places for *p* values (*int*) @@ -27,6 +29,9 @@ class Config: #: Alpha level for significance tests, confidence intervals (*float*) self.alpha = 0.05 + + #: If enabled, `__repr__` on test results, etc. directly calls the ``summary`` function (*bool*) + self.repr_is_summary = True """Global configuration singleton""" config = Config() diff --git a/yli/regress.py b/yli/regress.py index f6e4382..2b78bc6 100644 --- a/yli/regress.py +++ b/yli/regress.py @@ -83,6 +83,11 @@ class LikelihoodRatioTestResult: #: *p* value for the likelihood ratio test (*float*) self.pvalue = pvalue + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return 'LR({}) = {:.2f}; p {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=True)) @@ -261,6 +266,11 @@ class RegressionResult: return left_col, right_col + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): # Render header table left_col, right_col = self._header_table(html=True) diff --git a/yli/sig_tests.py b/yli/sig_tests.py index 1f3e917..f49e473 100644 --- a/yli/sig_tests.py +++ b/yli/sig_tests.py @@ -47,6 +47,11 @@ class TTestResult: #: Description of the direction of the effect (*str*) self.delta_direction = delta_direction + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return 't({:.0f}) = {:.2f}; p {}
Δμ ({:g}% CI) = {}, {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=True), (1-config.alpha)*100, self.delta.summary(), self.delta_direction) @@ -118,6 +123,11 @@ class FTestResult: #: *p* value for the *F* statistic (*float*) self.pvalue = pvalue + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return 'F({}, {}) = {:.2f}; p {}'.format(self.dof1, self.dof2, self.statistic, fmt_p(self.pvalue, html=True)) @@ -183,6 +193,11 @@ class MannWhitneyResult: #: :class:`BrunnerMunzelResult` on the same data, or *None* if N/A self.brunnermunzel = brunnermunzel + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): line1 = 'U = {:.1f}; p {}
r = {:.2f}, {}'.format(self.statistic, fmt_p(self.pvalue, html=True), self.rank_biserial, self.direction) if self.brunnermunzel: @@ -218,6 +233,11 @@ class BrunnerMunzelResult: #: *p* value for the *W* statistic (*float*) self.pvalue = pvalue + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return 'W = {:.1f}; p {}'.format(self.statistic, fmt_p(self.pvalue, html=True)) @@ -311,6 +331,11 @@ class PearsonChiSquaredResult: #: Risk ratio (*float*; *None* if not a 2×2 table) self.riskratio = riskratio + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): if self.oddsratio is not None: return '{0}
χ2({1}) = {2:.2f}; p {3}
OR ({4:g}% CI) = {5}
RR ({4:g}% CI) = {6}'.format( @@ -404,6 +429,11 @@ class PearsonRResult: #: *p* value for the *r* statistic (*float*) self.pvalue = pvalue + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return 'r ({:g}% CI) = {}; p {}'.format((1-config.alpha)*100, self.statistic.summary(), fmt_p(self.pvalue, html=True)) diff --git a/yli/utils.py b/yli/utils.py index c58dd9d..fb40365 100644 --- a/yli/utils.py +++ b/yli/utils.py @@ -148,6 +148,11 @@ class ConfidenceInterval: #: Upper confidence limit (*float*) self.upper = upper + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return self.summary() @@ -171,6 +176,11 @@ class Estimate: #: Upper confidence limit (*float*) self.ci_upper = ci_upper + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + def _repr_html_(self): return self.summary()