def test_hhgq_va(data_answers_name, cons_name): ## Get the constraint, data with answers and schema c, data_answers, schema = ctu.getConstraintDataAnswersSchema( schemaname, data_answers_name, ctu.getAllImplementedInvariantNames(schemaname), cons_name) # Check sign and right hand side assert c.sign == "le" if cons_name == 'hhgq_va_ub' else "ge" assert np.array_equal(c.rhs, data_answers[cons_name]) if c.sign == "le": assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) <= c.rhs) else: assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) >= c.rhs) # Check Kronecker factors # Each column of the HHGQ matrix selects that GQ type (so only one True value -- on the diagonal) ctu.assertKronFactorDiagonal(c, CC.ATTR_HHGQ, schema) # For non-voting, select first AGECAT_VA_CUT categories, for voting select the other categories ctu.assertKronFactor( c, CC.ATTR_AGECAT, np.array([[True] * AGECAT_VA_CUT + [False] * (ctu.dimSch(CC.ATTR_AGECAT, schema) - AGECAT_VA_CUT), [False] * AGECAT_VA_CUT + [True] * (ctu.dimSch(CC.ATTR_AGECAT, schema) - AGECAT_VA_CUT)]), schema) # Selecting everyone on all other axes for dimname in (CC.ATTR_SEX, CC.ATTR_HISP, CC.ATTR_CENRACE): ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_no_kids_over_89(data_answers_name): cons_name = 'no_kids_over_89' # Get the constraint, data with answers and schema c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, ctu.getAllImplementedInvariantNames(schemaname), cons_name) assert c.sign == "=" assert c.rhs[0] == 0 assert ctu.lhsFromMatrix(c, data_answers["hist"][0])[0] == data_answers[cons_name] # Selecting relatives # 2 'Biological Son/Daughter', # 3 'Adopted Son/Daughter', # 4 'Stepson/Stepdaughter', # 9 'Son/Daughter-in-law', ctu.assertKronFactor(c, CC.ATTR_REL, np.array([[False, False, True, True, True] + [False] * 4 + [True] + [False] * 33]), schema) # # This one also includes 7 for "Grandchild" # ctu.assertKronFactor(c, CC.ATTR_HHGQ, np.array([[False, False, True, True, True] + [False, False, True, False, True] + [False] * 33]), schema) # Selecting people over 89 (First 90 False, the rest is True) ctu.assertKronFactor(c, CC.ATTR_AGE, np.array([[False] * 90 + [True] * 26]), schema) # Everyone on all other axes for dimname in ctu.excludeAx((CC.ATTR_REL, CC.ATTR_AGE), schema): ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_no_parents_under_30(data_answers_name): cons_name = 'no_parents_under_30' # Get the constraint, data with answers and schema c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, ctu.getAllImplementedInvariantNames(schemaname), cons_name) assert c.sign == "=" assert c.rhs[0] == 0 assert ctu.lhsFromMatrix(c, data_answers["hist"][0])[0] == data_answers[cons_name] # Selecting relatives "6" (parents) and "8" (parents-in-law) ctu.assertKronFactor(c, CC.ATTR_REL, np.array([[False] * 6 + [True, False, True] + [False] * 34]), schema) # Selecting people under 30 (First 30 True, the rest is False) ctu.assertKronFactor(c, CC.ATTR_AGE, np.array([[True] * 30 + [False] * 86]), schema) # Everyone on all other axes for dimname in ctu.excludeAx((CC.ATTR_REL, CC.ATTR_AGE), schema): ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_no_vacant(data_answers_name, schemaname): cons_name = 'no_vacant' # Get the constraint, data with answers and schema c, data_answers, schema = ctu.getConstraintDataAnswersSchema( schemaname, data_answers_name, ctu.getAllImplementedInvariantNames(schemaname), cons_name) assert c.sign == "=" assert c.rhs[0] == 0 assert ctu.lhsFromMatrix( c, data_answers["hist"][0])[0] == data_answers[cons_name] # Select only size 0 ctu.assertKronFactor(c, SIZE, np.array([[True] + [False] * 7]), schema) # Everyone on all other axes for dimname in ctu.excludeAx((SIZE, ), schema): ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_parents_ub(cons_name_rel_index, data_answers_name): cons_name, rel_index = cons_name_rel_index inv_names = list(ctu.getAllImplementedInvariantNames(schemaname)) c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, inv_names, cons_name) # Check right hand side assert np.all(c.rhs == data_answers[cons_name]) # Check left hand side assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) == data_answers[cons_name + "_lhs"]) # Check sign assert c.sign == "le" assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) <= c.rhs) # Parents are 10, parent-in-law are 12 ctu.assertKronFactor(c, CC.ATTR_RELGQ, np.array([[False] * rel_index + [True] + [False] * (42-rel_index-1)]), schema) # Selecting everyone on all other axes for dimname in ctu.excludeAx((CC.ATTR_RELGQ, ), schema): ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_no_refhh_under_15(data_answers_name): cons_name = 'no_refhh_under_15' # Get the constraint, data with answers and schema c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, ctu.getAllImplementedInvariantNames(schemaname), cons_name) assert c.sign == "=" assert c.rhs[0] == 0 assert ctu.lhsFromMatrix(c, data_answers["hist"][0])[0] == data_answers[cons_name] # Selecting relatives # 'Householder (0)', 'Husband/Wife (1)', 'Father/Mother (6)', 'Parent-in-law (8)', 'Son/Daughter-in-law (9)', 'Housemate, Roommate (12)', # 'Unmarried Partner (13)' age_cut = 15 ans = np.array([[False] * ctu.dimSch(CC.ATTR_REL, schema)]) ans[0, [0, 1, 6, 8, 9, 12, 13]] = True ctu.assertKronFactor(c, CC.ATTR_REL, ans, schema) # Selecting people under age_cut (First age_cut True, the rest are False) ctu.assertKronFactor(c, CC.ATTR_AGE, np.array([[True] * age_cut + [False] * (ctu.dimSch(CC.ATTR_AGE, schema) - age_cut)]), schema) # Everyone on all other axes for dimname in ctu.excludeAx((CC.ATTR_REL, CC.ATTR_AGE), schema): ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_over100(data_answers_name): cons_name = "people100Plus_ub" inv_names = list(ctu.getAllImplementedInvariantNames(schemaname)) c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, inv_names, cons_name) # Check right hand side assert np.all(c.rhs == data_answers[cons_name]) # Check left hand side assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) == data_answers[cons_name + "_lhs"]) # Check sign assert c.sign == "le" assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) <= c.rhs) # People in households (REL part, up to 18) ctu.assertKronFactor(c, CC.ATTR_RELGQ, np.array([[True] * 18 + [False] * 24]), schema) # People over 100 years old ctu.assertKronFactor(c, CC.ATTR_AGE, np.array([[False] * 100 + [True] * 16]), schema) # Selecting everyone on all other axes for dimname in ctu.excludeAx((CC.ATTR_RELGQ, CC.ATTR_AGE), schema): ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_living_alone(data_answers_name, schemaname): cons_name = 'living_alone' # Get the constraint, data with answers and schema cs, data_answers, schema = ctu.getConstraintsDataAnswersSchema( schemaname, data_answers_name, ctu.getAllImplementedInvariantNames(schemaname), (cons_name, )) # Check size 1, and all rhs being zero for c in cs.values(): assert c.sign == "=" assert c.rhs[0] == 0 # Select only size 1 ctu.assertKronFactor(c, SIZE, np.array([[False] + [True] + [False] * 6]), schema) name = "living_alone_gt1" assert ctu.lhsFromMatrix(cs[name], data_answers["hist"][0])[0] == data_answers[name] # Select hhtypes implying > 1 person ans = [True] * 24 ans[18] = False ctu.assertKronFactor(cs[name], HHTYPE, np.array([ans]), schema) # Everyone on all other axes for dimname in ctu.excludeAx((SIZE, HHTYPE), schema): ctu.assertKronFactorSelectAll(cs[name], dimname, schema) name = "living_alone_multi" assert ctu.lhsFromMatrix(cs[name], data_answers["hist"][0])[0] == data_answers[name] # Select multi=1 ctu.assertKronFactor(cs[name], MULTI, np.array([[False, True]]), schema) # Everyone on all other axes for dimname in ctu.excludeAx((SIZE, MULTI), schema): ctu.assertKronFactorSelectAll(cs[name], dimname, schema) for eld in [0, 1, 2, 3]: name = f"living_alone_eld{eld}" assert ctu.lhsFromMatrix( cs[name], data_answers["hist"][0])[0] == data_answers[name] # Select multi=0 ctu.assertKronFactor(cs[name], MULTI, np.array([[True, False]]), schema) # Select hhtypes implying 1 person ans = [False] * 24 ans[18] = True ctu.assertKronFactor(cs[name], HHTYPE, np.array([ans]), schema) # select elderly not "eld" ctu.assertKronFactor( cs[name], ELDERLY, np.array([[True] * eld + [False] + [True] * (3 - eld)]), schema) # select age under 60, 60-65, 65-75 or over 75 ctu.assertKronFactor( cs[name], HHAGE, np.array([[not (bool(eld))] * 5 + list(np.array([1, 2, 3]) == eld) + [eld == 3]]), schema) # Everyone on all other axes for dimname in ctu.excludeAx((SIZE, HHTYPE, MULTI, HHAGE, ELDERLY), schema): ctu.assertKronFactorSelectAll(cs[name], dimname, schema)