def test_extra_field_names_is_optional(self): Square = _make_tuple_bunch('Square', ['width', 'height']) sq = Square(width=1, height=2) assert_equal(sq.width, 1) assert_equal(sq.height, 2) s = repr(sq) assert_equal(s, 'Square(width=1, height=2)')
def test_at_least_one_field_name_required(self): with pytest.raises(ValueError, match='at least one name'): _make_tuple_bunch('Qwerty', [], ['a', 'b'])
def test_keyword_not_allowed_in_fields(self, args): with pytest.raises(ValueError, match='keyword'): _make_tuple_bunch(*args)
def test_leading_underscore_not_allowed(self, args): with pytest.raises(ValueError, match='underscore'): _make_tuple_bunch(*args)
def test_repeated_field_names(self, args): with pytest.raises(ValueError, match='Duplicate'): _make_tuple_bunch(*args)
def test_identifiers_not_allowed(self, args): with pytest.raises(ValueError, match='identifiers'): _make_tuple_bunch(*args)
def test_explicit_module(self): m = 'some.module.name' Foo = _make_tuple_bunch('Foo', ['x'], ['a', 'b'], module=m) foo = Foo(x=1, a=355, b=113) assert_equal(Foo.__module__, m) assert_equal(foo.__module__, m)
def test_tuple_like(self): Tup = _make_tuple_bunch('Tup', ['a', 'b']) tu = Tup(a=1, b=2) assert isinstance(tu, tuple) assert isinstance(tu + (1, ), tuple)
import pytest import pickle from numpy.testing import assert_equal from scipy._lib._bunch import _make_tuple_bunch # `Result` is defined at the top level of the module so it can be # used to test pickling. Result = _make_tuple_bunch('Result', ['x', 'y', 'z'], ['w', 'beta']) class TestMakeTupleBunch: # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Tests with Result # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def setup(self): # Set up an instance of Result. self.result = Result(x=1, y=2, z=3, w=99, beta=0.5) def test_attribute_access(self): assert_equal(self.result.x, 1) assert_equal(self.result.y, 2) assert_equal(self.result.z, 3) assert_equal(self.result.w, 99) assert_equal(self.result.beta, 0.5) def test_indexing(self): assert_equal(self.result[0], 1) assert_equal(self.result[1], 2) assert_equal(self.result[2], 3)
# computations may overflow, so we first switch to floating point. observed = np.asarray(observed, dtype=np.float64) # Create a list of the marginal sums. margsums = margins(observed) # Create the array of expected frequencies. The shapes of the # marginal sums returned by apply_over_axes() are just what we # need for broadcasting in the following product. d = observed.ndim expected = reduce(np.multiply, margsums) / observed.sum()**(d - 1) return expected Chi2ContingencyResult = _make_tuple_bunch( 'Chi2ContingencyResult', ['statistic', 'pvalue', 'dof', 'expected_freq'], []) def chi2_contingency(observed, correction=True, lambda_=None): """Chi-square test of independence of variables in a contingency table. This function computes the chi-square statistic and p-value for the hypothesis test of independence of the observed frequencies in the contingency table [1]_ `observed`. The expected frequencies are computed based on the marginal sums under the assumption of independence; see `scipy.stats.contingency.expected_freq`. The number of degrees of freedom is (expressed using numpy functions and attributes):: dof = observed.size - sum(observed.shape) + observed.ndim - 1