Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove non-centrality check (solved in scipy 1.11) #392

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ v0.5.4 (November 2023)

This is a minor release with several bugfixes and no new features. The new version is tested for Python 3.8 to 3.11 (but should also work with Python 3.12). See GitHub for the full changelog.

This release requires pandas≥1.5.
This release requires pandas≥1.5. We recommend scipy≥1.11.0.

*************

Expand Down
24 changes: 12 additions & 12 deletions notebooks/00_QuickStart.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pingouin/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,9 +1113,9 @@ def replace_pval(x):

if stars:
# Replace p-values by stars
mat_upper = mat_upper.map(replace_pval)
mat_upper = mat_upper.applymap(replace_pval)
else:
mat_upper = mat_upper.map(lambda x: ffp(x, precision=decimals))
mat_upper = mat_upper.applymap(lambda x: ffp(x, precision=decimals))

# Replace upper triangle by p-values or n
mat.to_numpy()[tif(mat, k=1)] = mat_upper.to_numpy()[tif(mat, k=1)]
Expand Down
4 changes: 2 additions & 2 deletions pingouin/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,9 @@ def replace_pval(x):

if stars:
# Replace p-values by stars
mat_upper = mat_upper.map(replace_pval)
mat_upper = mat_upper.applymap(replace_pval)
else:
mat_upper = mat_upper.map(lambda x: ffp(x, precision=decimals))
mat_upper = mat_upper.applymap(lambda x: ffp(x, precision=decimals))

# Replace upper triangle by p-values
mat.to_numpy()[tif(mat, k=1)] = mat_upper.to_numpy()[tif(mat, k=1)]
Expand Down
34 changes: 0 additions & 34 deletions pingouin/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,6 @@
]


def _check_nc(dist, nc):
"""Check if non-centrality parameter is too large for the given distribution.
This is a workaround for scipy/scipy#17916 which can hopefully be removed at some point.
"""
if dist is stats.ncx2 and nc >= float(2**32 - 1):
warnings.warn("Non-centrality parameter is too large for the ncx2 distribution.")
return False
if dist is stats.ncf and nc >= float(2**32):
warnings.warn("Non-centrality parameter is too large for the ncf distribution.")
return False
if dist is stats.nct and (nc <= float(-(2**16)) or nc >= float(2**16)):
warnings.warn("Non-centrality parameter is too large for the nct distribution.")
return False
return True


def power_ttest(
d=None, n=None, power=None, alpha=0.05, contrast="two-samples", alternative="two-sided"
):
Expand Down Expand Up @@ -165,8 +149,6 @@ def func(d, n, power, alpha):
dof = (n - 1) * tsample
nc = d * np.sqrt(n / tsample)
tcrit = stats.t.ppf(alpha / tside, dof)
if not _check_nc(stats.nct, nc):
return np.nan
return stats.nct.cdf(tcrit, dof, nc)

elif alternative == "two-sided":
Expand All @@ -175,8 +157,6 @@ def func(d, n, power, alpha):
dof = (n - 1) * tsample
nc = d * np.sqrt(n / tsample)
tcrit = stats.t.ppf(1 - alpha / tside, dof)
if not _check_nc(stats.nct, nc):
return np.nan
return stats.nct.sf(tcrit, dof, nc) + stats.nct.cdf(-tcrit, dof, nc)

else: # Alternative = 'greater'
Expand All @@ -185,8 +165,6 @@ def func(d, n, power, alpha):
dof = (n - 1) * tsample
nc = d * np.sqrt(n / tsample)
tcrit = stats.t.ppf(1 - alpha / tside, dof)
if not _check_nc(stats.nct, nc):
return np.nan
return stats.nct.sf(tcrit, dof, nc)

# Evaluate missing variable
Expand Down Expand Up @@ -338,8 +316,6 @@ def func(d, nx, ny, power, alpha):
dof = nx + ny - 2
nc = d * (1 / np.sqrt(1 / nx + 1 / ny))
tcrit = stats.t.ppf(alpha / tside, dof)
if not _check_nc(stats.nct, nc):
return np.nan
return stats.nct.cdf(tcrit, dof, nc)

elif alternative == "two-sided":
Expand All @@ -348,8 +324,6 @@ def func(d, nx, ny, power, alpha):
dof = nx + ny - 2
nc = d * (1 / np.sqrt(1 / nx + 1 / ny))
tcrit = stats.t.ppf(1 - alpha / tside, dof)
if not _check_nc(stats.nct, nc):
return np.nan
return stats.nct.sf(tcrit, dof, nc) + stats.nct.cdf(-tcrit, dof, nc)

else: # Alternative = 'greater'
Expand All @@ -358,8 +332,6 @@ def func(d, nx, ny, power, alpha):
dof = nx + ny - 2
nc = d * (1 / np.sqrt(1 / nx + 1 / ny))
tcrit = stats.t.ppf(1 - alpha / tside, dof)
if not _check_nc(stats.nct, nc):
return np.nan
return stats.nct.sf(tcrit, dof, nc)

# Evaluate missing variable
Expand Down Expand Up @@ -515,8 +487,6 @@ def func(f_sq, k, n, power, alpha):
dof1 = k - 1
dof2 = (n * k) - k
fcrit = stats.f.ppf(1 - alpha, dof1, dof2)
if not _check_nc(stats.ncf, nc):
return np.nan
return stats.ncf.sf(fcrit, dof1, dof2, nc)

# Evaluate missing variable
Expand Down Expand Up @@ -745,8 +715,6 @@ def func(f_sq, m, n, power, alpha, corr):
dof2 = (n - 1) * dof1
nc = (f_sq * n * m * epsilon) / (1 - corr)
fcrit = stats.f.ppf(1 - alpha, dof1, dof2)
if not _check_nc(stats.ncf, nc):
return np.nan
return stats.ncf.sf(fcrit, dof1, dof2, nc)

# Evaluate missing variable
Expand Down Expand Up @@ -1069,8 +1037,6 @@ def power_chi2(dof, w=None, n=None, power=None, alpha=0.05):
def func(w, n, power, alpha):
k = stats.chi2.ppf(1 - alpha, dof)
nc = n * w**2
if not _check_nc(stats.ncx2, nc):
return np.nan
return stats.ncx2.sf(k, dof, nc)

# Evaluate missing variable
Expand Down
2 changes: 1 addition & 1 deletion pingouin/tests/test_nonparametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_mad(self):
def test_madmedianrule(self):
"""Test function madmedianrule."""
a = [1.2, 3, 4.5, 2.4, 5, 12.7, 0.4]
assert np.alltrue(madmedianrule(a) == [False, False, False, False, False, True, False])
assert np.all(madmedianrule(a) == [False, False, False, False, False, True, False])

def test_mwu(self):
"""Test function mwu"""
Expand Down
23 changes: 0 additions & 23 deletions pingouin/tests/test_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ def test_power_ttest(self):
# Error
with pytest.raises(ValueError):
power_ttest(d=0.5)
# Too high values of nc
with pytest.warns(UserWarning, match="Non-centrality parameter"):
assert np.isnan(power_ttest(d=2**16, n=20))
assert np.isnan(power_ttest(d=-(2**16), n=20))
assert np.isnan(power_ttest(d=5000.0, n=1500))
assert np.isnan(power_ttest(d=5000.0, n=1500, alternative="less"))
assert np.isnan(power_ttest(d=5000.0, n=1500, alternative="greater"))

def test_power_ttest2n(self):
"""Test function power_ttest2n.
Expand Down Expand Up @@ -138,12 +131,6 @@ def test_power_ttest2n(self):
# Error
with pytest.raises(ValueError):
power_ttest2n(nx=20, ny=20)
# Too high values of nc
with pytest.warns(UserWarning, match="Non-centrality parameter"):
assert np.isnan(power_ttest2n(nx=1500, ny=450, d=5000.0))
assert np.isnan(power_ttest2n(nx=1500, ny=450, d=5000.0, alternative="less"))
assert np.isnan(power_ttest2n(nx=1500, ny=450, d=5000.0, alternative="greater"))
assert np.isnan(power_ttest2n(nx=5, ny=7, d=float(-(2**16))))

def test_power_anova(self):
"""Test function power_anova.
Expand All @@ -160,10 +147,6 @@ def test_power_anova(self):
# Error
with pytest.raises(ValueError):
power_anova(eta_squared=eta, k=2)
# Too high values of nc
with pytest.warns(UserWarning, match="Non-centrality parameter"):
assert np.isnan(power_anova(eta_squared=0.9999, k=40, n=100000))
assert np.isnan(power_anova(eta_squared=0.9999, k=10, n=50000))

def test_power_rm_anova(self):
"""Test function power_rm_anova.
Expand Down Expand Up @@ -217,9 +200,6 @@ def test_power_rm_anova(self):
# Error
with pytest.raises(ValueError):
power_rm_anova(eta_squared=eta, m=2)
# Too high values of nc
with pytest.warns(UserWarning, match="Non-centrality parameter"):
assert np.isnan(power_rm_anova(eta_squared=0.999, m=50, n=100000))

def test_power_corr(self):
"""Test function power_corr.
Expand Down Expand Up @@ -275,6 +255,3 @@ def test_power_chi2(self):
# Error
with pytest.raises(ValueError):
power_chi2(1, w=0.3)
# Too high values of nc
with pytest.warns(UserWarning, match="Non-centrality parameter"):
assert np.isnan(power_chi2(dof=10, w=0.999, n=10000000000))
Loading