def test_empty_household(): sp.logger.info("Test creating an empty household.") household = sp.Household() def check_phrase(key, value, passed): print( f"Check {'passed' if passed else 'failed'}. household {key} for an empty household is {value}." ) assert passed check_phrase('hhid', household['hhid'], passed=household['hhid'] is None) check_phrase('reference_uid', household['reference_uid'], passed=household['reference_uid'] is None) check_phrase('reference_age', household['reference_age'], passed=household['reference_age'] is None) np.testing.assert_array_equal( household['member_uids'], np.array([], dtype=int), err_msg="Check failed: empty array not found for member_uids.", verbose=True) check_phrase('member_uids', household['member_uids'], passed=True) household['member_uids'] = np.array([0, 1]) empty_age_by_uid = [] np.testing.assert_array_equal( household.member_ages(empty_age_by_uid), np.array([], dtype=int), err_msg="Check failed: empty array not found for member_ages.", verbose=True) check_phrase('member_ages', household.member_ages(empty_age_by_uid), passed=True) print('Checks passed for an empty household.')
def test_add_household(): sp.logger.info( "Test creating a sp.Household object and adding it to an empty sp.Households class after generation." ) home = [1, 2, 3] age_by_uid = {1: 88, 2: 45, 3: 47} household = sp.Household(member_uids=home, reference_uid=home[0], reference_age=age_by_uid[home[0]], hhid=0) assert isinstance(household, sp.Household), 'Check failed. Not a sp.Household object.' pop = sc.prettyobj() pop.households = [] sp.add_household(pop, household) assert isinstance( pop.households[0], sp.Household ), 'Check failed. Did not add a sp.Household object to the list of households.' print( 'Check passed. Added a sp.Household object to an sp.Households object.' )
def test_make_household(): sp.logger.info("Test creating a household after the fact.") pop = sp.Pop(**pars) household = sp.Household() household.set_layer_group(member_uids=pop.homes_by_uids[0], reference_uid=min(pop.homes_by_uids[0]), reference_age=pop.age_by_uid[min( pop.homes_by_uids[0])], hhid=0) assert household[ 'hhid'] == 0, f"Check failed. household hhid is {household['hhid']}." print('Check passed. household hhid is 0.') assert len(household['member_uids']) == len( household.member_ages(pop.age_by_uid) ), 'Check failed: member_uids and member_ages have different lengths.' print( f"Check passed. household member_uids and member_ages have the same length ({len(household['member_uids'])})." ) assert isinstance( household['member_uids'], np.ndarray), 'Check failed: member_uids is not a np.array.' print("Check passed. household member_uids is a np.array.") assert len(household) == len( household['member_uids'] ), 'Check failed: len(household) does not return the household size, i.e. the number of household members.' print( 'Check passed. len(household) returns the number of household members.' ) assert isinstance( household.member_ages(pop.age_by_uid), np.ndarray), 'Check failed: member_ages is not a np.array.' print('Check passed. household member_ages is a np.array.') assert household[ 'reference_uid'] is not None, 'Check failed. household reference_uid is None.' print( f"Check passed. household reference_uid is not None and instead is {household['reference_uid']}." ) assert household[ 'reference_age'] is not None, 'Check failed. household reference_age is None.' print( f"Check passed. household reference_age is not None and instead is {household['reference_age']}." ) assert len(household), f"Check failed. household size is {len(household)}." print(f"Check passed. household household size is {len(household)}.") household_2 = sp.Household() household_2['member_uids'] = ['a'] with pytest.raises(TypeError): household_2.validate() print( 'Check passed. A synthpops household must have member_uids that are passable as a np.array of ints.' ) household_2 = sp.Household() household_2['hhid'] = 'b' with pytest.raises(TypeError): household_2.validate() print( 'Check passed. A synthpops household must have hhid that is an int or None.' )