scipy-yli/tests/test_fmt_pvalues.py

89 lines
2.8 KiB
Python
Raw Normal View History

2022-10-15 23:11:22 +11:00
# scipy-yli: Helpful SciPy utilities and recipes
# Copyright © 2022 Lee Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# 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/>.
import yli
from yli.config import config
from yli.utils import fmt_p
def test_fmt_pvalues_ord():
"""Test formatting of p values requiring no special handling"""
# Default config
config.pvalue_min_dps = 2
config.pvalue_max_dps = 3
config.pvalue_leading_zero = True
config.alpha = 0.05
assert fmt_p(0.0096, html=False) == '= 0.01*'
assert fmt_p(0.01, html=False) == '= 0.01*'
assert fmt_p(0.04, html=False) == '= 0.04*'
assert fmt_p(0.11, html=False) == '= 0.11'
assert fmt_p(0.55, html=False) == '= 0.55'
def test_fmt_pvalues_ord_noleadingzero():
"""Test formatting of p values requiring no special handling, no leading zero"""
# Default config
config.pvalue_min_dps = 2
config.pvalue_max_dps = 3
config.pvalue_leading_zero = False
config.alpha = 0.05
assert fmt_p(0.0096, html=False) == '= .01*'
assert fmt_p(0.01, html=False) == '= .01*'
assert fmt_p(0.04, html=False) == '= .04*'
assert fmt_p(0.11, html=False) == '= .11'
assert fmt_p(0.55, html=False) == '= .55'
def test_fmt_pvalues_small():
"""Test formatting of small p values requiring extra decimal points to represent"""
# Default config
config.pvalue_min_dps = 2
config.pvalue_max_dps = 3
config.pvalue_leading_zero = True
config.alpha = 0.05
assert fmt_p(0.009, html=False) == '= 0.009*'
def test_fmt_pvalues_ambiguous():
"""Test formatting of p values requiring extra decimal points to avoid ambiguity"""
# Default config
config.pvalue_min_dps = 2
config.pvalue_max_dps = 3
config.pvalue_leading_zero = True
config.alpha = 0.05
assert fmt_p(0.048, html=False) == '= 0.048*'
assert fmt_p(0.052, html=False) == '= 0.052'
# Special rounding rules
assert fmt_p(0.04999, html=False) == '= 0.049*'
assert fmt_p(0.05001, html=False) == '= 0.051'
def test_fmt_pvalues_extreme():
"""Test formatting of p values too small or large to be represented"""
# Default config
config.pvalue_min_dps = 2
config.pvalue_max_dps = 3
config.pvalue_leading_zero = True
config.alpha = 0.05
assert fmt_p(0.0009, html=False) == '< 0.001*'
assert fmt_p(0.999, html=False) == '> 0.99'