示例#1
0
def test_sumover_query(data, add_over_margins, subset):
    """ Tests that SumoverQuery answers queries correctly """
    q1 = querybase.SumoverQuery(data.shape,
                                subset=subset,
                                add_over_margins=add_over_margins)
    # check for (1) correct answer, (2) answer matches the matrix representation
    if subset is None:
        answer = data.sum(axis=add_over_margins).flatten()
    else:
        answer = data[np.ix_(*subset)].sum(axis=add_over_margins).flatten()
    assert compare_arrays(q1.answer(data), answer)
    assert compare_arrays(q1.matrixRep() * data.flatten(), answer)
    # check that the query sizes are correct
    assert q1.domainSize() == data.size
    assert q1.domainSize() == q1.matrixRep().shape[1]
    assert q1.numAnswers() == q1.answer(
        data).size, "numAnswers is: {} but should be: {}".format(
            q1.numAnswers(),
            q1.answer(data).size)
    # check sensitivities
    assert q1.unboundedDPSensitivity() == 1
    assert np.abs(
        q1.matrixRep()).sum(axis=0).max() == q1.unboundedDPSensitivity()
    # make sure it is counting query
    assert q1.isIntegerQuery()
示例#2
0
def test_stackedquery1(data):
    """ we will stack a SumoverQuery and a SparseKronQuery """
    q1 = querybase.SumoverQuery(data.shape, add_over_margins=(1, ))
    matrices1 = [ss.eye(x) for x in data.shape]
    matrices1[0] = np.ones(data.shape[0])
    matrices1[-1] = ss.csr_matrix(np.ones(data.shape[-1]))
    q2 = querybase.SparseKronQuery(matrices1)
    q3 = q1
    # now stack
    q4 = querybase.StackedQuery([q1, q2, q3])
    answer = q4.answer(data)
    expected = np.concatenate(
        [q1.answer(data), q2.answer(data),
         q3.answer(data)])
    assert compare_arrays(answer, expected)
    # check sensitivities
    assert np.abs(
        q1.matrixRep()).sum(axis=0).max() == q1.unboundedDPSensitivity()
    assert np.abs(
        q2.matrixRep()).sum(axis=0).max() == q2.unboundedDPSensitivity()
    assert np.abs(
        q3.matrixRep()).sum(axis=0).max() == q3.unboundedDPSensitivity()
    assert np.abs(
        q4.matrixRep()).sum(axis=0).max() == q4.unboundedDPSensitivity()
    # make sure it is counting query
    assert q1.isIntegerQuery()
    assert q2.isIntegerQuery()
    assert q3.isIntegerQuery()
    assert q4.isIntegerQuery()
    # check shape
    assert q4.domainSize() == q4.matrixRep().shape[1]
    assert q4.domainSize() == data.size
    assert q4.numAnswers() == q4.matrixRep().shape[0]
    assert q4.numAnswers(
    ) == q1.numAnswers() + q2.numAnswers() + q3.numAnswers()
示例#3
0
def test_eq(constraint, query, data, samequery, samerhs, samesign, samename,
            rhstype):
    """ Test __eq__ constraint comparison"""
    if not samequery:
        query = querybase.SumoverQuery(data.shape, add_over_margins=(0, 1, 2))

    rhs = (query.answer(data) + 1e-14).astype(rhstype)
    constraint.rhs = constraint.rhs.astype(rhstype)
    if not samerhs:
        rhs = rhs + 1

    sign = '=' if samesign else 'le'
    name = 'teststandard' if samename else 'othername'

    c2 = Constraint(query, rhs, sign, name)

    if samequery and samerhs and samesign and samename:
        assert c2 == constraint
    else:
        assert c2 != constraint
示例#4
0
def test_addValueErrorsRaising(constraint, data, wrongattr):
    """ Test raising errors when trying to add Constraints with different queries, signs or names"""
    err_msg = f" cannot be added: addends have different {wrongattr}"
    argdict = {
        'query': constraint.query,
        'sign': constraint.sign,
        'name': constraint.name,
    }
    wrongargdict = {
        'query': querybase.SumoverQuery(data.shape,
                                        add_over_margins=(0, 1, 2)),
        'sign': 'le',
        'name': 'othername',
    }
    argdict[wrongattr] = wrongargdict[wrongattr]
    argdict['rhs'] = argdict['query'].answer(data)
    c2 = Constraint(**argdict)
    with pytest.raises(IncompatibleAddendsError) as err:
        cons_sum = constraint + c2
    assert err_msg in str(err.value)
示例#5
0
def test_queryfactory_without_collapse(data):
    shape = data.shape
    subset = ([0, shape[0] - 1], range(0, shape[1], 2), [0, shape[2] - 1])
    add_over_margins = (1, )
    q3 = querybase.QueryFactory.makeTabularQuery(
        shape, subset=subset, add_over_margins=add_over_margins)
    q4 = querybase.SumoverQuery(shape,
                                subset=subset,
                                add_over_margins=add_over_margins)
    assert compare_arrays(q3.answer(data), q4.answer(data))
    # check sensitivities
    assert np.abs(
        q3.matrixRep()).sum(axis=0).max() == q3.unboundedDPSensitivity()
    assert np.abs(
        q4.matrixRep()).sum(axis=0).max() == q4.unboundedDPSensitivity()
    # make sure it is counting query
    assert q3.isIntegerQuery()
    assert q4.isIntegerQuery()
    # check shape
    assert q3.domainSize() == q3.matrixRep().shape[1]
    assert q3.numAnswers() == q3.answer(data).size
    assert q4.domainSize() == q4.matrixRep().shape[1]
    assert q4.numAnswers() == q4.answer(data).size
示例#6
0
def test_repeated_subset_error(data):
    with pytest.raises(AssertionError):
        querybase.SumoverQuery(data.shape,
                               subset=([2, 2], range(0, 4, 2), [0, 4]),
                               add_over_margins=(1, ))
示例#7
0
def query(data):
    """ make a query """
    return querybase.SumoverQuery(data.shape, add_over_margins=(0, 2))