예제 #1
0
 def test_continuous_attribute_check_with_secret_index(self):
     dataset = ObliviousDataset.create(Sample([s(0), s(1), s(1)], s(0)),
                                       Sample([s(1), s(2), s(1)], s(1)),
                                       continuous=[False, True, False])
     self.assertFalse(reveal(dataset.is_continuous(s(0))))
     self.assertTrue(reveal(dataset.is_continuous(s(1))))
     self.assertFalse(reveal(dataset.is_continuous(s(2))))
예제 #2
0
 def test_column_with_secret_index(self):
     dataset = ObliviousDataset.create(sample(s(0), s(1), s(2)),
                                       sample(s(10), s(11), s(12)),
                                       sample(s(20), s(21), s(22)))
     self.assertEqual(reveal(dataset.column(s(0))), [0, 10, 20])
     self.assertEqual(reveal(dataset.column(s(1))), [1, 11, 21])
     self.assertEqual(reveal(dataset.column(s(2))), [2, 12, 22])
예제 #3
0
 def test_select_best_attribute(self):
     samples = ObliviousDataset.create(
         Sample([s(0), s(1), s(1), s(0)], s(1)),
         Sample([s(1), s(0), s(1), s(1)], s(1)),
         Sample([s(0), s(0), s(0), s(1)], s(0)))
     (best_attribute, threshold) = select_best_attribute(samples)
     self.assertEqual(reveal(best_attribute), 2)
     self.assertEqual(reveal(threshold), 0)
예제 #4
0
 def test_gini_gain_mpc(self):
     numerator, denominator = gini_gain_quotient(
         s(2), s(2), s(1), s(1), s(1), s(1))
     numerator = reveal(numerator)
     denominator = reveal(denominator)
     total = 4
     gain = (1 / total) * float(numerator / denominator)
     self.assertEqual(gain, 0.5)
예제 #5
0
 def test_partition_on_binary_attribute(self):
     data = ObliviousDataset.create(sample(s(0), s(1), s(1), s(0)),
                                    sample(s(1), s(0), s(1), s(1)),
                                    sample(s(0), s(0), s(0), s(1)))
     left, right = partition_binary(data, attribute_index=s(2))
     self.assertEqual(reveal(left), [Sample([0, 0, 0, 1], 0)])
     self.assertEqual(reveal(right),
                      [Sample([0, 1, 1, 0], 0),
                       Sample([1, 0, 1, 1], 0)])
예제 #6
0
 def test_select_best_attribute_from_continuous_and_binary(self):
     samples = ObliviousDataset.create(Sample([s(0), s(1)], s(0)),
                                       Sample([s(0), s(2)], s(0)),
                                       Sample([s(1), s(3)], s(0)),
                                       Sample([s(1), s(4)], s(1)),
                                       Sample([s(1), s(5)], s(1)),
                                       continuous=[False, True])
     (best_attribute, threshold) = select_best_attribute(samples)
     self.assertEqual(reveal(best_attribute), 1)
     self.assertEqual(reveal(threshold), 3)
예제 #7
0
 def test_partition_on_continuous_attribute(self):
     data = ObliviousDataset.create(sample(s(1), s(3), s(3), s(1)),
                                    sample(s(3), s(1), s(3), s(3)),
                                    sample(s(1), s(1), s(1), s(3)))
     left, right = partition_continuous(data,
                                        attribute_index=s(2),
                                        threshold=s(2))
     self.assertEqual(reveal(left), [Sample([1, 1, 1, 3], 0)])
     self.assertEqual(reveal(right),
                      [Sample([1, 3, 3, 1], 0),
                       Sample([3, 1, 3, 3], 0)])
예제 #8
0
 def test_sorts_column_and_outcomes_of_array(self):
     dataset = ObliviousDataset.create(
         Sample([s(2)], s(5)),
         Sample([s(1)], s(6)),
         Sample([s(3)], s(7)),
         Sample([s(4)], s(8))
     )
     column = dataset.column(s(0))
     outcomes = dataset.outcomes
     sorted_column, sorted_outcomes = sort(column, outcomes)
     self.assertEqual(reveal(sorted_column), [1, 2, 3, 4])
     self.assertEqual(reveal(sorted_outcomes), [6, 5, 7, 8])
예제 #9
0
 def test_outcomes(self):
     dataset = ObliviousDataset.create(
         Sample([s(0), s(1), s(2)], outcome=s(60)),
         Sample([s(10), s(11), s(12)], outcome=s(70)),
         Sample([s(20), s(21), s(22)],
                outcome=s(80))).select([s(1), s(0), s(1)])
     self.assertEqual(reveal(dataset.outcomes), [60, 80])
예제 #10
0
 def test_select_best_attribute_with_gini_denominator_zero(self):
     samples = ObliviousDataset.create(
         Sample([s(0), s(0), s(1), s(0)], s(1)),
         Sample([s(0), s(0), s(1), s(0)], s(1)),
         Sample([s(0), s(0), s(0), s(0)], s(0)))
     (best_attribute, _) = select_best_attribute(samples)
     self.assertEqual(reveal(best_attribute), 2)
예제 #11
0
 def test_random_sample(self):
     dataset = ObliviousDataset.create(
         Sample([s(1), s(2), s(3)], s(4)),
         Sample([s(11), s(12), s(13)], s(14)))
     randomSamples = [reveal(dataset.choice()) for _ in range(100)]
     self.assertIn(Sample([1, 2, 3], 4), randomSamples)
     self.assertIn(Sample([11, 12, 13], 14), randomSamples)
예제 #12
0
 async def test_single_sample_with_some_depth(self):
     samples = ObliviousDataset.create(Sample([s(1)], s(1)))
     self.assertEqual(
         reveal(await train(samples, depth=2)),
         Branch(0,
                threshold=0,
                left=Branch(0, threshold=0, left=pruned(), right=pruned()),
                right=Branch(0, threshold=0, left=pruned(), right=leaf(1))))
예제 #13
0
 def test_select_best_attribute_using_subset(self):
     samples = ObliviousDataset.create(
         Sample([s(0), s(1), s(1), s(0)], s(1)),
         Sample([s(1), s(0), s(1), s(1)], s(1)),
         Sample([s(42), s(43), s(44), s(45)], s(46)),
         Sample([s(0), s(0), s(0), s(1)], s(0)),
     ).select([s(0), s(1), s(0), s(1)])
     (best_attribute, _) = select_best_attribute(samples)
     self.assertEqual(reveal(best_attribute), 2)
예제 #14
0
 def test_calculate_gains_for_thresholds_ignores_duplicates(self):
     samples = ObliviousDataset.create(Sample([s(0)], s(0)),
                                       Sample([s(0)], s(0)),
                                       Sample([s(0)], s(0)),
                                       continuous=[True])
     column = samples.column(0)
     outcomes = samples.outcomes
     gains = calculate_gains_for_thresholds(column, outcomes)
     plain_gains = reveal(gains)
     self.assertEqual(len(plain_gains), 1)
예제 #15
0
 def test_reveal_branches(self):
     tree = Branch(s(0),
                   threshold=s(10),
                   left=Leaf(s(1), pruned=s(False)),
                   right=Leaf(s(2), pruned=s(True)))
     expected_output = Branch(0,
                              threshold=10,
                              left=Leaf(1, pruned=False),
                              right=Leaf(2, pruned=True))
     self.assertEqual(reveal(tree), expected_output)
예제 #16
0
 async def test_continuous_attribute_with_some_depth(self):
     samples = ObliviousDataset.create(Sample([s(1)], s(0)),
                                       Sample([s(2)], s(0)),
                                       Sample([s(3)], s(1)),
                                       Sample([s(4)], s(1)),
                                       Sample([s(5)], s(0)),
                                       continuous=[True])
     tree = reveal(await train(samples, depth=2))
     self.assertEqual(tree.attribute, 0)
     self.assertEqual(tree.threshold, 2)
     self.assertTrue(isinstance(tree.left, Branch))
     self.assertTrue(isinstance(tree.right, Branch))
     self.assertEqual(tree.right.attribute, 0)
     self.assertEqual(tree.right.threshold, 4)
예제 #17
0
 async def test_multiple_samples_with_some_depth(self):
     samples = ObliviousDataset.create(Sample([s(0), s(1)], s(0)),
                                       Sample([s(1), s(0)], s(0)),
                                       Sample([s(1), s(0)], s(0)),
                                       Sample([s(1), s(1)], s(1)))
     self.assertEqual(
         reveal(await train(samples, depth=2)),
         Branch(
             1,
             threshold=0,
             left=Branch(
                 1,  # random, could have been zero as well
                 threshold=0,
                 left=leaf(0),
                 right=pruned()),
             right=Branch(0, threshold=0, left=leaf(0), right=leaf(1))))
 def test_classify_with_pruned_subtree(self):
     tree = Branch(s(1), s(0), Branch(s(0), s(0), pruned(), pruned()),
                   Branch(s(2), s(0), leaf(s(0)), leaf(s(1))))
     self.assertEqual(reveal(classify([s(0), s(0), s(1)], tree)), 1)
 def test_classify_with_a_branch(self):
     tree = Branch(s(1), s(0), leaf(s(1)), leaf(s(0)))
     self.assertEqual(reveal(classify([s(1), s(0), s(1)], tree)), 1)
     self.assertEqual(reveal(classify([s(1), s(1), s(1)], tree)), 0)
예제 #20
0
 def test_output_sec_int(self):
     self.assertEqual(reveal(s(42)), 42)
예제 #21
0
 def test_output_tuple_of_secrets(self):
     self.assertEqual(reveal((SomeSecret(), SomeSecret())), (42, 42))
예제 #22
0
 def test_output_list_of_secrets(self):
     self.assertEqual(reveal([SomeSecret(), SomeSecret()]), [42, 42])
예제 #23
0
 def test_output_secret(self):
     self.assertEqual(reveal(SomeSecret()), 42)
예제 #24
0
 def test_tuple(self):
     chosen = if_else(s(True), (s(1), s(2)), (s(3), s(4)))
     self.assertEqual(reveal(chosen), (1, 2))
예제 #25
0
 def test_add_samples(self):
     sample1 = Sample([s(1), s(2), s(3)], s(4))
     sample2 = Sample([s(5), s(6), s(7)], s(8))
     self.assertEqual(reveal(sample1 + sample2), Sample([6, 8, 10], 12))
예제 #26
0
 def test_determine_class_multiple_samples(self):
     dataset = ObliviousDataset.create(Sample([s(0)], s(0)),
                                       Sample([s(0)], s(1)),
                                       Sample([s(0)], s(1)))
     self.assertEqual(reveal(dataset.determine_class()), 1)
예제 #27
0
 def test_random_sample_with_one_sample(self):
     dataset = ObliviousDataset.create(Sample([s(1), s(2), s(3)], s(4)))
     self.assertEqual(reveal(dataset.choice()), Sample([1, 2, 3], 4))
 def test_classify_with_continuous_attribute(self):
     tree = Branch(s(1), s(2), leaf(s(1)), leaf(s(0)))
     self.assertEqual(reveal(classify([s(1), s(2), s(1)], tree)), 1)
     self.assertEqual(reveal(classify([s(1), s(3), s(1)], tree)), 0)
 def test_classify_with_only_leaf_node(self):
     sample = [s(1), s(0), s(1)]
     tree = leaf(s(1))
     self.assertEqual(reveal(classify(sample, tree)), 1)
예제 #30
0
 def test_column_of_subset_with_secret_index(self):
     dataset = ObliviousDataset.create(sample(s(0), s(1), s(2)),
                                       sample(s(10), s(11), s(12)),
                                       sample(s(20), s(21), s(22))).select(
                                           [s(1), s(0), s(1)])
     self.assertEqual(reveal(dataset.column(s(1))), [1, 21])