Ejemplo n.º 1
0
 def test_apply_kind_on_data_slice_multiple_axis_0_skip(self):
     data = np.array([list(range(0, 20)) + [100.0]] * 3)
     kinds = [DiffPrivStatisticKind.all, None, DiffPrivStatisticKind.all]
     expected_results = [
         self.calculate_stats(data[0]),
         None,
         self.calculate_stats(data[2]),
     ]
     data = np.transpose(data)
     self.set_seed()
     results = DiffPrivStatistics.apply_kind_on_data_slice(
         data, kinds, self.epsilon, axis=0
     )
     self.assertEqual(len(results), len(expected_results))
     for index in range(len(expected_results)):
         result = results[index]
         expected_result = expected_results[index]
         if expected_result:
             self.assertEqual(len(result), len(expected_result))
             for key, value in expected_result.items():
                 value = result[key]
                 expected_value = expected_result[key]
                 self.assertAlmostEqual(value, expected_value, self.decimal_places)
         else:
             self.assertEqual(result, expected_result)
Ejemplo n.º 2
0
 def test_proportion_multiple(self):
     data = np.array([[True, False, True, True] * 10] * 3)
     expected_values = np.count_nonzero(data, axis=1)
     expected_values = np.divide(expected_values, np.size(data, axis=1))
     self.set_seed()
     value = DiffPrivStatistics.proportion(data, self.epsilon, axis=1)
     np.testing.assert_almost_equal(value, expected_values, self.decimal_places)
Ejemplo n.º 3
0
 def test_proportion_single(self):
     data = np.array([True, False, True, True] * 10)
     expected_value = np.count_nonzero(data)
     expected_value = np.divide(expected_value, np.size(data))
     self.set_seed()
     value = DiffPrivStatistics.proportion(data, self.epsilon)
     self.assertAlmostEqual(value, expected_value, self.decimal_places)
Ejemplo n.º 4
0
    def test_median_multiple(self):
        data = np.array([list(range(0, 20)) + [100.0]] * 3)
        expected_values = np.median(data, axis=1)

        self.set_seed()
        value = DiffPrivStatistics.median(data, self.epsilon, axis=1)
        np.testing.assert_almost_equal(value, expected_values, self.decimal_places)
Ejemplo n.º 5
0
 def test_min_single(self):
     data = list(range(3, 20))
     data.reverse()
     data = np.array(data)
     expected_value = np.min(data)
     self.set_seed()
     value = DiffPrivStatistics.min(data, self.epsilon)
     self.assertAlmostEqual(value, expected_value, self.decimal_places)
Ejemplo n.º 6
0
 def test_min_multiple(self):
     data = list(range(3, 20))
     data.reverse()
     data = np.array([data] * 3)
     expected_values = np.min(data, axis=1)
     self.set_seed()
     value = DiffPrivStatistics.min(data, self.epsilon, axis=1)
     np.testing.assert_almost_equal(value, expected_values, self.decimal_places)
Ejemplo n.º 7
0
    def test_count_single_with_condition(self):
        data = np.array([0.1, -0.1, 0.1, 0.1] * 10)
        expected_value = np.count_nonzero(data >= 0)

        def condition(data):
            return data >= 0.0

        self.set_seed()
        value = DiffPrivStatistics.count(data, self.epsilon, condition=condition)
        self.assertAlmostEqual(value, expected_value, self.decimal_places)
Ejemplo n.º 8
0
 def test_apply_kind_on_data_slice_single_skip(self):
     data = np.array(list(range(0, 20)) + [100.0])
     kinds = None
     expected_results = [None]
     self.set_seed()
     results = DiffPrivStatistics.apply_kind_on_data_slice(data, kinds, self.epsilon)
     self.assertEqual(len(results), len(expected_results))
     for index in range(len(expected_results)):
         result = results[index]
         expected_result = expected_results[index]
         self.assertEqual(result, expected_result)
Ejemplo n.º 9
0
    def test_count_multiple_with_condition(self):
        data = np.array([[0.1, -0.1, 0.1, 0.1] * 10] * 3)
        expected_values = np.count_nonzero(data >= 0, axis=1)

        def condition(data):
            return data >= 0.0

        self.set_seed()
        value = DiffPrivStatistics.count(
            data, self.epsilon, condition=condition, axis=1
        )
        np.testing.assert_almost_equal(value, expected_values, self.decimal_places)
Ejemplo n.º 10
0
 def test_apply_kind_on_data_slice_single(self):
     data = np.array(list(range(0, 20)) + [100.0])
     kinds = DiffPrivStatisticKind.all
     expected_results = [self.calculate_stats(data)]
     self.set_seed()
     results = DiffPrivStatistics.apply_kind_on_data_slice(data, kinds, self.epsilon)
     self.assertEqual(len(results), len(expected_results))
     for index in range(len(expected_results)):
         result = results[index]
         expected_result = expected_results[index]
         self.assertEqual(len(result), len(expected_result))
         for key, value in expected_result.items():
             value = result[key]
             expected_value = expected_result[key]
             self.assertAlmostEqual(value, expected_value, self.decimal_places)
Ejemplo n.º 11
0
 def test_count_single(self):
     data = np.array([True, False, True, True] * 10)
     expected_value = np.count_nonzero(data)
     self.set_seed()
     value = DiffPrivStatistics.count(data, self.epsilon)
     self.assertAlmostEqual(value, expected_value, self.decimal_places)
Ejemplo n.º 12
0
 def test_apply_kind_on_data_slice_size_mismatch_error(self):
     data = np.array(list(range(0, 20)) + [100.0])
     kinds = [DiffPrivStatisticKind.all, DiffPrivStatisticKind.all]
     self.set_seed()
     with self.assertRaises(DiffPrivSizeMismatch):
         DiffPrivStatistics.apply_kind_on_data_slice(data, kinds, self.epsilon)
Ejemplo n.º 13
0
 def test_apply_kind_on_data_slice_invalid_dimension_error(self):
     data = np.array([[list(range(0, 20)) + [100.0]]])
     kinds = DiffPrivStatisticKind.all
     self.set_seed()
     with self.assertRaises(DiffPrivInvalidDimensions):
         DiffPrivStatistics.apply_kind_on_data_slice(data, kinds, self.epsilon)
Ejemplo n.º 14
0
 def test_variance_single(self):
     data = np.array(list(range(0, 20)) + [100.0])
     expected_value = np.var(data)
     self.set_seed()
     value = DiffPrivStatistics.variance(data, self.epsilon)
     self.assertAlmostEqual(value, expected_value, self.decimal_places)