def test_tuple_integer_limits(row_limits, col_limits, weights, prob, seed): """Verify that `crossover` produces a valid individual where the lower and upper column limits are tuple and integer respectively.""" distributions = [Gamma, Normal, Poisson] families = [Family(distribution) for distribution in distributions] random_state = np.random.RandomState(seed) parents = [ create_individual( row_limits, col_limits, families, weights, random_state ) for _ in [0, 1] ] individual = crossover(*parents, col_limits, families, random_state, prob) _common_asserts(individual, *parents) for family, lower_limit in zip(families, col_limits[0]): count = sum(pdf.family is family for pdf in individual.metadata) assert count >= lower_limit
def test_tuple_integer_limits(row_limits, col_limits, weights, seed): """Create an individual with tuple lower limits and integer upper limits on the columns. Verify the individual is valid and of a reasonable shape and does not exceed the lower bounds.""" distributions = [Gamma, Normal, Poisson] families = [Family(distribution) for distribution in distributions] state = np.random.RandomState(seed) individual = create_individual( row_limits, col_limits, families, weights, state ) _common_asserts(individual, state, families) dataframe, metadata = individual assert row_limits[0] <= dataframe.shape[0] <= row_limits[1] assert sum(col_limits[0]) <= dataframe.shape[1] <= col_limits[1] for family, lower_limit in zip(families, col_limits[0]): count = sum([isinstance(pdf, family.distribution) for pdf in metadata]) assert count >= lower_limit
def test_tuple_limits(row_limits, col_limits, weights, seed): """Create an individual with tuple column limits. Verify the individual is valid and of a reasonable shape and does not exceed either of the column bounds.""" distributions = [Gamma, Normal, Poisson] families = [Family(distribution) for distribution in distributions] state = np.random.RandomState(seed) individual = create_individual( row_limits, col_limits, families, weights, state ) _common_asserts(individual, state, families) dataframe, metadata = individual assert row_limits[0] <= dataframe.shape[0] <= row_limits[1] assert sum(col_limits[0]) <= dataframe.shape[1] <= sum(col_limits[1]) for i, family in enumerate(families): count = sum([isinstance(pdf, family.distribution) for pdf in metadata]) assert col_limits[0][i] <= count <= col_limits[1][i]
def test_load(distribution): """ Test that a family can be created from a cache. """ family = Family(distribution) family.add_subtype() subtype = family.subtypes[0] family.save(".testcache") pickled = Family.load(distribution, root=".testcache") pickled_subtype = pickled.subtypes[0] assert isinstance(pickled, Family) assert pickled.distribution is distribution assert pickled.subtype_id == 1 assert pickled.subtypes == {0: pickled_subtype} assert issubclass(pickled_subtype, distribution) assert pickled_subtype.__name__ == subtype.__name__ assert pickled_subtype.name == subtype.name assert pickled_subtype.dtype == subtype.dtype assert pickled_subtype.subtype_id == 0 assert pickled_subtype.family is pickled assert pickled_subtype.hard_limits == subtype.hard_limits assert pickled_subtype.param_limits == subtype.param_limits assert pickled_subtype.__init__ is subtype.__init__ assert pickled_subtype.__repr__ is subtype.__repr__ assert pickled_subtype.sample is subtype.sample for fpart, ppart in zip(family.random_state.get_state(), pickled.random_state.get_state()): try: assert all(fpart == ppart) except TypeError: assert fpart == ppart os.system("rm -r .testcache")