class TestMultiHistQuery: data1 = np.arange(12).reshape((3, 4)) data2 = np.arange(12, 24).reshape((3, 2, 2)) q1 = querybase.SumOverGroupedQuery(data1.shape, groupings={1: [[0, 1]]}) q2 = querybase.SumOverGroupedQuery(data2.shape, groupings={ 1: [[0, 1]], 2: [[0, 1]] }) def test___init__(self): querybase.MultiHistQuery((self.q1, self.q2)) def test___init__fail(self): q1 = querybase.SumOverGroupedQuery(self.data1.shape, groupings={1: [[0, 1]]}, name='q1') q2 = querybase.SumOverGroupedQuery(self.data2.shape, groupings={ 0: [[0, 1]], 2: [[0, 1]] }, name='q2') with pytest.raises(ValueError): querybase.MultiHistQuery((q1, q2)) def test_numAnswers(self): q = querybase.MultiHistQuery((self.q1, self.q2)) assert q.numAnswers() == 3 @pytest.mark.parametrize("coeff, ans", [((1, 1), [55, 79, 103]), ((1, -1), [-53, -61, -69])]) def test_answerByMem(self, coeff, ans): q = querybase.MultiHistQuery((self.q1, self.q2), coeff) #assert np.array_equal(q.answer((self.data1, self.data2)), np.array(ans)) assert np.array_equal( q.answer(np.hstack((self.data1.ravel(), self.data2.ravel()))), np.array(ans)) @pytest.mark.parametrize("coeff, ans", [((1, 1), [55, 79, 103]), ((1, -1), [-53, -61, -69])]) def test_answer(self, coeff, ans): q = querybase.MultiHistQuery((self.q1, self.q2), coeff) assert np.array_equal( q.answerByMatRep( np.hstack((self.data1.ravel(), self.data2.ravel()))), np.array(ans)) def test_matrix_rep(self): self.q1.matrixRep().toarray().dot( self.data1.ravel()) + self.q2.matrixRep().toarray().dot( self.data2.ravel()) q = querybase.MultiHistQuery((self.q1, self.q2)) from_m_r = q.matrixRep().toarray().dot( np.hstack((self.data1.ravel(), self.data2.ravel()))) from_indiv = self.q1.matrixRep().toarray().dot( self.data1.ravel()) + self.q2.matrixRep().toarray().dot( self.data2.ravel()) assert np.array_equal(from_indiv, from_m_r)
def test___init__fail(self): q1 = querybase.SumOverGroupedQuery(self.data1.shape, groupings={1: [[0, 1]]}, name='q1') q2 = querybase.SumOverGroupedQuery(self.data2.shape, groupings={ 0: [[0, 1]], 2: [[0, 1]] }, name='q2') with pytest.raises(ValueError): querybase.MultiHistQuery((q1, q2))
def test_sumover_grouped_query(data, add_over_margins, subset): """ Tests that SumOverGroupedQuery answers queries correctly """ # these are the same as in test_sumover_query if subset is None: q1 = querybase.SumOverGroupedQuery(data.shape, add_over_margins=add_over_margins) answer = data.sum(axis=add_over_margins).flatten() else: q1 = querybase.SumoverQuery(data.shape, subset=subset, add_over_margins=add_over_margins) answer = data[np.ix_(*subset)].sum(axis=add_over_margins).flatten() # check for (1) correct answer, (2) answer matches the matrix representation assert compare_arrays(q1.answer(data), answer) assert compare_arrays(q1.matrixRep() * data.flatten(), answer) assert q1.domainSize() == data.size 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()
def test_makeTabularGroupQuery(data, groupings, add_over_margins): q5 = querybase.QueryFactory.makeTabularGroupQuery( data.shape, groupings=groupings, add_over_margins=add_over_margins) q6 = querybase.SumOverGroupedQuery(data.shape, groupings=groupings, add_over_margins=add_over_margins) assert compare_arrays(q5.answer(data), q6.answer(data)) # check sensitivities assert np.abs( q5.matrixRep()).sum(axis=0).max() == q5.unboundedDPSensitivity() assert np.abs( q6.matrixRep()).sum(axis=0).max() == q6.unboundedDPSensitivity() # make sure it is counting query assert q5.isIntegerQuery() assert q6.isIntegerQuery()
def test_error_not_disjoint(data): with pytest.raises(AssertionError): querybase.SumOverGroupedQuery(data.shape, groupings={0: [0, 1]}, add_over_margins=(0, 1))
def test_sumover_grouped_query2(data): """ Tests that SumOverGroupedQuery answers queries correctly """ # these are queries that use groupings s = data.shape # the same as a subset groupings1 = { 0: [ [0], ] } # mutliple groups groupings2 = {0: [[0, 1], [2]]} # multiple dimensions groupings3 = {0: [[0, 1], [2]], 1: [[0], [1]]} q0 = querybase.SumOverGroupedQuery(data.shape, add_over_margins=(0, 1, 2)) q1 = querybase.SumOverGroupedQuery(data.shape, groupings=groupings1) q2 = querybase.SumOverGroupedQuery(data.shape, groupings=groupings2) q3 = querybase.SumOverGroupedQuery(data.shape, groupings=groupings3) q5 = querybase.SumOverGroupedQuery(data.shape, groupings=groupings3, add_over_margins=(2, )) # check for (1) correct answer, (2) answer matches the matrix representation assert compare_arrays(q0.answer(data), data.sum().flatten()) assert compare_arrays(q0.matrixRep() * data.flatten(), data.sum().flatten()) # check for (1) correct answer, (2) answer matches the matrix representation assert compare_arrays(q1.answer(data), data[0, 0:s[1], 0:s[2]].flatten()) assert compare_arrays(q1.matrixRep() * data.flatten(), data[0, 0:s[1], 0:s[2]].flatten()) # check for (1) correct answer, (2) answer matches the matrix representation right_answer = np.stack([ data[0:2, 0:s[1], 0:s[2]].sum(0, keepdims=False), data[2, 0:s[1], 0:s[2]] ], axis=0).flatten() assert compare_arrays(q2.answer(data), right_answer) assert compare_arrays(q2.matrixRep() * data.flatten(), right_answer) # check for (1) correct answer, (2) answer matches the matrix representation right_answer = np.stack( [data[0:2, 0:2, 0:s[2]].sum(0, keepdims=False), data[2, 0:2, 0:s[2]]], axis=0).flatten() assert compare_arrays(q3.answer(data), right_answer) assert compare_arrays(q3.matrixRep() * data.flatten(), right_answer) # check for (1) correct answer, (2) answer matches the matrix representation right_answer = np.stack( [data[0:2, 0:2, 0:s[2]].sum(0, keepdims=False), data[2, 0:2, 0:s[2]]], axis=0).sum(2).flatten() assert compare_arrays(q5.answer(data), right_answer) assert compare_arrays(q5.matrixRep() * data.flatten(), right_answer) # check that the query sizes are correct assert q1.domainSize() == data.size assert q1.numAnswers() == q1.answer( data).size, "numAnswers is: {} but should be: {}".format( q1.numAnswers(), q1.answer(data).size) # make a query with sensitivty 2 groupings4 = {0: [[0, 1], [0, 1]], 1: [[0], [1]]} q4 = querybase.SumOverGroupedQuery(data.shape, groupings=groupings4) # check sensitivities assert q1.unboundedDPSensitivity() == 1 assert np.abs( q1.matrixRep()).sum(axis=0).max() == q1.unboundedDPSensitivity() assert q2.unboundedDPSensitivity() == 1 assert np.abs( q2.matrixRep()).sum(axis=0).max() == q2.unboundedDPSensitivity() assert q3.unboundedDPSensitivity() == 1 assert np.abs( q3.matrixRep()).sum(axis=0).max() == q3.unboundedDPSensitivity() assert q4.unboundedDPSensitivity() == 2 assert np.abs( q4.matrixRep()).sum(axis=0).max() == q4.unboundedDPSensitivity() with pytest.raises(ValueError, match="only supported when equal to 1"): q4.unboundedL2Sensitivity() # make a query with sensitivity groupings6 = { 0: [[1], [2]], 1: [[1, 2], [2, 3]], 2: [[1, 2], [2, 3]], } q6 = querybase.SumOverGroupedQuery(data.shape, groupings=groupings6) assert q6.unboundedDPSensitivity() == 4 assert np.abs( q6.matrixRep()).sum(axis=0).max() == q6.unboundedDPSensitivity() # make sure it is counting query assert q1.isIntegerQuery() assert q2.isIntegerQuery() assert q3.isIntegerQuery()