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)
示例#2
0
def test_householder_ub(data_answers_name):
    cons_name = "householder_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)

    dim_hhgq = ctu.dimSch(CC.ATTR_RELGQ, schema)
    kf_hhgq = c.query.queries[0].kronFactors()[ctu.indSch(CC.ATTR_RELGQ, schema)]
    assert kf_hhgq.shape == (1, dim_hhgq)

    # True for 0,1 -- householder living alone and householder not living alone
    assert np.array_equal(kf_hhgq[0, :], np.array([True] * 2 + [False] * (dim_hhgq - 2)))

    # Selecting everyone on all other axes
    for dimname in ctu.excludeAx((CC.ATTR_RELGQ,), schema):
        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_nurse_nva_0(data_answers_name):
    cons_name = 'nurse_nva_0'

    # 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 which is zero (in constraint) since it's a structural zero.
    assert c.sign == "="
    assert c.rhs[0] == 0
    # If constraint query matrix is applied to the data it doesn't have to be zero
    assert ctu.lhsFromMatrix(
        c, data_answers["hist"][0])[0] == data_answers[cons_name]

    # Check Kronecker factors
    # Selecting only nursing homes (index=3)
    ctu.assertKronFactorSingle(c, CC.ATTR_HHGQ, 3, schema)

    # Selecting only non-voting age (index=0)
    ctu.assertKronFactorSingle(c, CC.ATTR_VOTING, 0, schema)

    # Selecting everyone on other axes
    for dimname in ctu.excludeAx((CC.ATTR_HHGQ, CC.ATTR_VOTING), schema):
        ctu.assertKronFactorSelectAll(c, dimname, schema)
示例#5
0
def test_struct_zero(cons_name, data_answers_name):
    # 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]
def test_struct_zero(cons_name, data_answers_name):
    cs, data_answers, schema = ctu.getConstraintsDataAnswersSchema(
        schemaname, data_answers_name,
        ctu.getAllImplementedInvariantNames(schemaname), (cons_name, ))

    for cname, c in cs.items():
        assert c.sign == '='
        assert c.rhs[0] == 0
        assert ctu.lhsFromMatrix(
            c, data_answers['hist'][0])[0] == data_answers[cname]
def test_hhgq_total(data_answers_name, tot_inv, cons_name):

    # Create invariant names, remove 'tot' if checking without total
    inv_names = list(ctu.getAllImplementedInvariantNames(schemaname))
    if not tot_inv:
        inv_names.remove('tot')

    # Get the constraint, data with answers and schema
    c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, inv_names, cons_name)

    # Check sign, <= for upper bound, >= for lower
    assert c.sign == "le" if cons_name == 'hhgq_total_ub' else "ge"

    # Check right hand side
    if tot_inv:
        assert np.array_equal(c.rhs, data_answers[cons_name])
    else:
        assert np.array_equal(c.rhs, data_answers[cons_name + "_no_tot"])

    # Check right hand side from matrix application to the data
    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)

    # Should get 1 + (schDim(HHGQ) - NUM_HH_CATS) rows x schDim(HHGQ) columns
    dim_hhgq = ctu.dimSch(CC.ATTR_REL, schema)
    dim_gq = dim_hhgq - NUM_HH_CATS

    kf_hhgq = c.query.queries[0].kronFactors()[ctu.indSch(CC.ATTR_REL, schema)]
    assert kf_hhgq.shape == (dim_gq + 1, dim_hhgq)
    # First row is selecting first NUM_HH_CATS, and dropping all the GQs, so first NUM_HH_CATS values True, then False
    assert np.array_equal(kf_hhgq[0, :], np.array([True] * NUM_HH_CATS + [False] * dim_gq))
    # The rest rows drop first NUM_HH_CATS values
    assert np.array_equal(kf_hhgq[1:, :NUM_HH_CATS],  np.broadcast_to(np.array([False]), (dim_gq, NUM_HH_CATS)))
    # and select only the corresponding GQ type, leaving a diagonal of True in lower right corner of the matrix
    assert np.array_equal(kf_hhgq[1:, NUM_HH_CATS:], np.diag([True]*dim_gq))

    # Selecting everyone on all other axes
    for dimname in ctu.excludeAx((CC.ATTR_REL, ), schema):
        ctu.assertKronFactorSelectAll(c, dimname, schema)
示例#8
0
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)
示例#9
0
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_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)
示例#11
0
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_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_voting_age(data_answers_name):
    cons_name = 'voting_age'

    ## 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, in constraint and from query matrix applied to the data
    assert c.sign == "="
    assert c.rhs[0] == data_answers[cons_name]
    assert ctu.lhsFromMatrix(
        c, data_answers["hist"][0])[0] == data_answers[cons_name]

    # Check Kronecker factors
    # The values below AGECAT_VA_CUT are non-voting, selecting the other ones
    ctu.assertKronFactorMiltiple(
        c, CC.ATTR_AGECAT,
        range(AGECAT_VA_CUT, ctu.dimSch(CC.ATTR_AGECAT, schema)), schema)

    # Selecting everyone on other axes
    for dimname in (CC.ATTR_HHGQ, CC.ATTR_SEX, CC.ATTR_HISP, CC.ATTR_CENRACE):
        ctu.assertKronFactorSelectAll(c, dimname, schema)
示例#14
0
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)