Add option to make __repr__ print the summary

This commit is contained in:
RunasSudo 2022-10-18 18:44:04 +11:00
parent 57189990d6
commit a484b6205c
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 64 additions and 0 deletions

View File

@ -10,3 +10,5 @@ Global options
.. autoattribute:: yli.config.Config.pvalue_max_dps .. autoattribute:: yli.config.Config.pvalue_max_dps
.. autoattribute:: yli.config.Config.pvalue_min_dps .. autoattribute:: yli.config.Config.pvalue_min_dps
.. autoattribute:: yli.config.Config.repr_is_summary

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from .config import config
class BayesFactor: class BayesFactor:
"""A Bayes factor""" """A Bayes factor"""
@ -30,6 +32,11 @@ class BayesFactor:
#: Description of the hypothesis in the denominator (*str*) #: Description of the hypothesis in the denominator (*str*)
self.denom_desc = denom_desc self.denom_desc = denom_desc
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return 'BF<sub>{0}{1}</sub> = {2:.2f}, {5}<br>H<sub>{0}</sub>: {3}<br>H<sub>{1}</sub>: {4}'.format(self.num_symbol, self.denom_symbol, self.factor, self.num_desc, self.denom_desc, self.interpret_lw(html=True)) return 'BF<sub>{0}{1}</sub> = {2:.2f}, {5}<br>H<sub>{0}</sub>: {3}<br>H<sub>{1}</sub>: {4}'.format(self.num_symbol, self.denom_symbol, self.factor, self.num_desc, self.denom_desc, self.interpret_lw(html=True))

View File

@ -18,6 +18,8 @@ class Config:
"""Global configuration for the library""" """Global configuration for the library"""
def __init__(self): 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*) #: Display at least this many decimal places for *p* values (*int*)
self.pvalue_min_dps = 2 self.pvalue_min_dps = 2
#: Display at most this many decimal places for *p* values (*int*) #: 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*) #: Alpha level for significance tests, confidence intervals (*float*)
self.alpha = 0.05 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""" """Global configuration singleton"""
config = Config() config = Config()

View File

@ -83,6 +83,11 @@ class LikelihoodRatioTestResult:
#: *p* value for the likelihood ratio test (*float*) #: *p* value for the likelihood ratio test (*float*)
self.pvalue = pvalue self.pvalue = pvalue
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return 'LR({}) = {:.2f}; <i>p</i> {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=True)) return 'LR({}) = {:.2f}; <i>p</i> {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=True))
@ -261,6 +266,11 @@ class RegressionResult:
return left_col, right_col return left_col, right_col
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
# Render header table # Render header table
left_col, right_col = self._header_table(html=True) left_col, right_col = self._header_table(html=True)

View File

@ -47,6 +47,11 @@ class TTestResult:
#: Description of the direction of the effect (*str*) #: Description of the direction of the effect (*str*)
self.delta_direction = delta_direction self.delta_direction = delta_direction
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return '<i>t</i>({:.0f}) = {:.2f}; <i>p</i> {}<br>Δ<i>μ</i> ({:g}% CI) = {}, {}'.format(self.dof, self.statistic, fmt_p(self.pvalue, html=True), (1-config.alpha)*100, self.delta.summary(), self.delta_direction) return '<i>t</i>({:.0f}) = {:.2f}; <i>p</i> {}<br>Δ<i>μ</i> ({: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*) #: *p* value for the *F* statistic (*float*)
self.pvalue = pvalue self.pvalue = pvalue
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return '<i>F</i>({}, {}) = {:.2f}; <i>p</i> {}'.format(self.dof1, self.dof2, self.statistic, fmt_p(self.pvalue, html=True)) return '<i>F</i>({}, {}) = {:.2f}; <i>p</i> {}'.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 #: :class:`BrunnerMunzelResult` on the same data, or *None* if N/A
self.brunnermunzel = brunnermunzel self.brunnermunzel = brunnermunzel
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
line1 = '<i>U</i> = {:.1f}; <i>p</i> {}<br><i>r</i> = {:.2f}, {}'.format(self.statistic, fmt_p(self.pvalue, html=True), self.rank_biserial, self.direction) line1 = '<i>U</i> = {:.1f}; <i>p</i> {}<br><i>r</i> = {:.2f}, {}'.format(self.statistic, fmt_p(self.pvalue, html=True), self.rank_biserial, self.direction)
if self.brunnermunzel: if self.brunnermunzel:
@ -218,6 +233,11 @@ class BrunnerMunzelResult:
#: *p* value for the *W* statistic (*float*) #: *p* value for the *W* statistic (*float*)
self.pvalue = pvalue self.pvalue = pvalue
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return '<i>W</i> = {:.1f}; <i>p</i> {}'.format(self.statistic, fmt_p(self.pvalue, html=True)) return '<i>W</i> = {:.1f}; <i>p</i> {}'.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) #: Risk ratio (*float*; *None* if not a 2×2 table)
self.riskratio = riskratio self.riskratio = riskratio
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
if self.oddsratio is not None: if self.oddsratio is not None:
return '{0}<br><i>χ</i><sup>2</sup>({1}) = {2:.2f}; <i>p</i> {3}<br>OR ({4:g}% CI) = {5}<br>RR ({4:g}% CI) = {6}'.format( return '{0}<br><i>χ</i><sup>2</sup>({1}) = {2:.2f}; <i>p</i> {3}<br>OR ({4:g}% CI) = {5}<br>RR ({4:g}% CI) = {6}'.format(
@ -404,6 +429,11 @@ class PearsonRResult:
#: *p* value for the *r* statistic (*float*) #: *p* value for the *r* statistic (*float*)
self.pvalue = pvalue self.pvalue = pvalue
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return '<i>r</i> ({:g}% CI) = {}; <i>p</i> {}'.format((1-config.alpha)*100, self.statistic.summary(), fmt_p(self.pvalue, html=True)) return '<i>r</i> ({:g}% CI) = {}; <i>p</i> {}'.format((1-config.alpha)*100, self.statistic.summary(), fmt_p(self.pvalue, html=True))

View File

@ -148,6 +148,11 @@ class ConfidenceInterval:
#: Upper confidence limit (*float*) #: Upper confidence limit (*float*)
self.upper = upper self.upper = upper
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return self.summary() return self.summary()
@ -171,6 +176,11 @@ class Estimate:
#: Upper confidence limit (*float*) #: Upper confidence limit (*float*)
self.ci_upper = ci_upper self.ci_upper = ci_upper
def __repr__(self):
if config.repr_is_summary:
return self.summary()
return super().__repr__()
def _repr_html_(self): def _repr_html_(self):
return self.summary() return self.summary()