Skip to content

Commit

Permalink
Merge pull request scipy#11573 from pvanmulbregt/warnings_stats
Browse files Browse the repository at this point in the history
MAINT: Removed unused imports; fixed unused assignments in scipy/stats.
  • Loading branch information
ev-br authored Feb 23, 2020
2 parents 4cff36f + afc20d6 commit 5b5446f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 81 deletions.
3 changes: 1 addition & 2 deletions scipy/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@
from __future__ import division, print_function, absolute_import

import warnings
import sys
import math
from math import gcd
from collections import namedtuple
Expand Down Expand Up @@ -5874,7 +5873,7 @@ def _compute_prob_inside_method(m, n, g, h):
for i in range(1, m + 1):
# Generate the next column.
# First calculate the sliding window
lastminj, lastmaxj, lastlen = minj, maxj, curlen
lastminj, lastlen = minj, curlen
minj = max(int(np.floor((ng * i - h) / mg)) + 1, 0)
minj = min(minj, n)
maxj = min(int(np.ceil((ng * i + h) / mg)), n + 1)
Expand Down
126 changes: 63 additions & 63 deletions scipy/stats/tests/test_contingency.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,33 @@ def test_chi2_contingency_trivial():
def test_chi2_contingency_R():
# Some test cases that were computed independently, using R.

Rcode = \
"""
# Data vector.
data <- c(
12, 34, 23, 4, 47, 11,
35, 31, 11, 34, 10, 18,
12, 32, 9, 18, 13, 19,
12, 12, 14, 9, 33, 25
)
# Create factor tags:r=rows, c=columns, t=tiers
r <- factor(gl(4, 2*3, 2*3*4, labels=c("r1", "r2", "r3", "r4")))
c <- factor(gl(3, 1, 2*3*4, labels=c("c1", "c2", "c3")))
t <- factor(gl(2, 3, 2*3*4, labels=c("t1", "t2")))
# 3-way Chi squared test of independence
s = summary(xtabs(data~r+c+t))
print(s)
"""
Routput = \
"""
Call: xtabs(formula = data ~ r + c + t)
Number of cases in table: 478
Number of factors: 3
Test for independence of all factors:
Chisq = 102.17, df = 17, p-value = 3.514e-14
"""
# Rcode = \
# """
# # Data vector.
# data <- c(
# 12, 34, 23, 4, 47, 11,
# 35, 31, 11, 34, 10, 18,
# 12, 32, 9, 18, 13, 19,
# 12, 12, 14, 9, 33, 25
# )
#
# # Create factor tags:r=rows, c=columns, t=tiers
# r <- factor(gl(4, 2*3, 2*3*4, labels=c("r1", "r2", "r3", "r4")))
# c <- factor(gl(3, 1, 2*3*4, labels=c("c1", "c2", "c3")))
# t <- factor(gl(2, 3, 2*3*4, labels=c("t1", "t2")))
#
# # 3-way Chi squared test of independence
# s = summary(xtabs(data~r+c+t))
# print(s)
# """
# Routput = \
# """
# Call: xtabs(formula = data ~ r + c + t)
# Number of cases in table: 478
# Number of factors: 3
# Test for independence of all factors:
# Chisq = 102.17, df = 17, p-value = 3.514e-14
# """
obs = np.array(
[[[12, 34, 23],
[35, 31, 11],
Expand All @@ -117,42 +117,42 @@ def test_chi2_contingency_R():
assert_approx_equal(p, 3.514e-14, significant=4)
assert_equal(dof, 17)

Rcode = \
"""
# Data vector.
data <- c(
#
12, 17,
11, 16,
#
11, 12,
15, 16,
#
23, 15,
30, 22,
#
14, 17,
15, 16
)
# Create factor tags:r=rows, c=columns, d=depths(?), t=tiers
r <- factor(gl(2, 2, 2*2*2*2, labels=c("r1", "r2")))
c <- factor(gl(2, 1, 2*2*2*2, labels=c("c1", "c2")))
d <- factor(gl(2, 4, 2*2*2*2, labels=c("d1", "d2")))
t <- factor(gl(2, 8, 2*2*2*2, labels=c("t1", "t2")))
# 4-way Chi squared test of independence
s = summary(xtabs(data~r+c+d+t))
print(s)
"""
Routput = \
"""
Call: xtabs(formula = data ~ r + c + d + t)
Number of cases in table: 262
Number of factors: 4
Test for independence of all factors:
Chisq = 8.758, df = 11, p-value = 0.6442
"""
# Rcode = \
# """
# # Data vector.
# data <- c(
# #
# 12, 17,
# 11, 16,
# #
# 11, 12,
# 15, 16,
# #
# 23, 15,
# 30, 22,
# #
# 14, 17,
# 15, 16
# )
#
# # Create factor tags:r=rows, c=columns, d=depths(?), t=tiers
# r <- factor(gl(2, 2, 2*2*2*2, labels=c("r1", "r2")))
# c <- factor(gl(2, 1, 2*2*2*2, labels=c("c1", "c2")))
# d <- factor(gl(2, 4, 2*2*2*2, labels=c("d1", "d2")))
# t <- factor(gl(2, 8, 2*2*2*2, labels=c("t1", "t2")))
#
# # 4-way Chi squared test of independence
# s = summary(xtabs(data~r+c+d+t))
# print(s)
# """
# Routput = \
# """
# Call: xtabs(formula = data ~ r + c + d + t)
# Number of cases in table: 262
# Number of factors: 4
# Test for independence of all factors:
# Chisq = 8.758, df = 11, p-value = 0.6442
# """
obs = np.array(
[[[[12, 17],
[11, 16]],
Expand Down
3 changes: 1 addition & 2 deletions scipy/stats/tests/test_morestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from numpy.random import RandomState
from numpy.testing import (assert_array_equal,
assert_almost_equal, assert_array_less, assert_array_almost_equal,
assert_, assert_allclose, assert_equal, assert_warns, suppress_warnings)
assert_, assert_allclose, assert_equal, suppress_warnings)
import pytest
from pytest import raises as assert_raises

Expand Down Expand Up @@ -1510,7 +1510,6 @@ def test_empty(self):
def test_array_like(self):
np.random.seed(54321)
x = stats.norm.rvs(size=100, loc=0)
lmbda = 1.5
xt1, _ = stats.yeojohnson(x)
xt2, _ = stats.yeojohnson(list(x))
assert_allclose(xt1, xt2, rtol=1e-12)
Expand Down
2 changes: 1 addition & 1 deletion scipy/stats/tests/test_mstats_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def test_mode_modifies_input(self):
im[:50, :] += 1
im[:, :50] += 1
cp = im.copy()
a = mstats.mode(im, None)
mstats.mode(im, None)
assert_equal(im, cp)


Expand Down
4 changes: 2 additions & 2 deletions scipy/stats/tests/test_multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,8 @@ class TestRandomCorrelation(object):
def test_reproducibility(self):
np.random.seed(514)
eigs = (.5, .8, 1.2, 1.5)
x = random_correlation.rvs((.5, .8, 1.2, 1.5))
x2 = random_correlation.rvs((.5, .8, 1.2, 1.5), random_state=514)
x = random_correlation.rvs(eigs)
x2 = random_correlation.rvs(eigs, random_state=514)
expected = np.array([[1., -0.20387311, 0.18366501, -0.04953711],
[-0.20387311, 1., -0.24351129, 0.06703474],
[0.18366501, -0.24351129, 1., 0.38530195],
Expand Down
19 changes: 8 additions & 11 deletions scipy/stats/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from __future__ import division, print_function, absolute_import

import os
import sys
import warnings
from collections import namedtuple
import multiprocessing
Expand All @@ -28,9 +27,7 @@
import scipy.stats.mstats as mstats
import scipy.stats.mstats_basic as mstats_basic
from .common_tests import check_named_results
from scipy.special import kv
from scipy.sparse.sputils import matrix
from scipy.integrate import quad

""" Numbers in docstrings beginning with 'W' refer to the section numbers
and headings found in the STATISTICS QUIZ of Leland Wilkinson. These are
Expand Down Expand Up @@ -89,7 +86,7 @@ def test_tvar(self):
assert_approx_equal(y, 4.666666666666667, significant=self.dprec)

with suppress_warnings() as sup:
r = sup.record(RuntimeWarning, "Degrees of freedom <= 0 for slice.")
sup.record(RuntimeWarning, "Degrees of freedom <= 0 for slice.")

# Limiting some values along one axis
y = stats.tvar(x_2d, limits=(1, 5), axis=1, inclusive=(True, True))
Expand Down Expand Up @@ -123,7 +120,7 @@ def test_tmin(self):
x = np.arange(10.)
x[9] = np.nan
with suppress_warnings() as sup:
r = sup.record(RuntimeWarning, "invalid value*")
sup.record(RuntimeWarning, "invalid value*")
assert_equal(stats.tmin(x), np.nan)
assert_equal(stats.tmin(x, nan_policy='omit'), 0.)
assert_raises(ValueError, stats.tmin, x, nan_policy='raise')
Expand All @@ -148,7 +145,7 @@ def test_tmax(self):
x = np.arange(10.)
x[6] = np.nan
with suppress_warnings() as sup:
r = sup.record(RuntimeWarning, "invalid value*")
sup.record(RuntimeWarning, "invalid value*")
assert_equal(stats.tmax(x), np.nan)
assert_equal(stats.tmax(x, nan_policy='omit'), 9.)
assert_raises(ValueError, stats.tmax, x, nan_policy='raise')
Expand Down Expand Up @@ -2092,13 +2089,13 @@ def test_nanpolicy(self):

# Yes NaNs
x[1, 2] = np.nan
with warnings.catch_warnings(record=True) as w:
with warnings.catch_warnings(record=True):
warnings.simplefilter("always")
assert_equal(stats.iqr(x, nan_policy='propagate'), np.nan)
assert_equal(stats.iqr(x, axis=0, nan_policy='propagate'), [5, 5, np.nan, 5, 5])
assert_equal(stats.iqr(x, axis=1, nan_policy='propagate'), [2, np.nan, 2])

with warnings.catch_warnings(record=True) as w:
with warnings.catch_warnings(record=True):
warnings.simplefilter("always")
assert_equal(stats.iqr(x, nan_policy='omit'), 7.5)
assert_equal(stats.iqr(x, axis=0, nan_policy='omit'), np.full(5, 5))
Expand All @@ -2121,7 +2118,7 @@ def test_scale(self):

# Yes NaNs
x[1, 2] = np.nan
with warnings.catch_warnings(record=True) as w:
with warnings.catch_warnings(record=True):
warnings.simplefilter("always")
assert_equal(stats.iqr(x, scale='raw', nan_policy='propagate'), np.nan)
assert_equal(stats.iqr(x, scale='normal', nan_policy='propagate'), np.nan)
Expand Down Expand Up @@ -4711,7 +4708,7 @@ def test_inf_values(self):
stats.wasserstein_distance([1, -np.inf, np.inf], [1, 1]),
np.inf)
with suppress_warnings() as sup:
r = sup.record(RuntimeWarning, "invalid value*")
sup.record(RuntimeWarning, "invalid value*")
assert_equal(
stats.wasserstein_distance([1, 2, np.inf], [np.inf, 1]),
np.nan)
Expand Down Expand Up @@ -4778,7 +4775,7 @@ def test_inf_values(self):
stats.energy_distance([1, -np.inf, np.inf], [1, 1]),
np.inf)
with suppress_warnings() as sup:
r = sup.record(RuntimeWarning, "invalid value*")
sup.record(RuntimeWarning, "invalid value*")
assert_equal(
stats.energy_distance([1, 2, np.inf], [np.inf, 1]),
np.nan)
Expand Down

0 comments on commit 5b5446f

Please sign in to comment.