import itertools import math import unittest from collections import Counter from fractions import Fraction from identifiability import grouping_results, expected_bins, histogram, results, sample_law, total_variation def partitions(total, minimum=1): if total == 0: yield () for first in range(minimum, total+1): for rest in partitions(total-first, first): yield (first,) + rest def allocation_law(counts, size): weighted = Counter() for allocation in itertools.product(*(range(c+1) for c in counts)): if sum(allocation) == size: key = tuple(sorted(Counter(n for n in allocation if n).items())) weighted[key] += math.prod(math.comb(n, k) for n, k in zip(counts, allocation)) return {key: Fraction(n, math.comb(sum(counts), size)) for key, n in weighted.items()} class IdentifiabilityTests(unittest.TestCase): def test_two_position_laws_are_identical(self): expected = {((1, 2),): Fraction(4, 5), ((2, 1),): Fraction(1, 5)} self.assertEqual(sample_law((3, 1, 1, 1), 2), expected) self.assertEqual(sample_law((2, 2, 2), 2), expected) def test_grouped_reads_change_expected_bins(self): data = grouping_results() designs = data["designs"] self.assertEqual(data["marginal_position_inclusion"], "1/2") self.assertEqual(designs["uniform_two_positions"]["expected_singletons"], "4/3") self.assertEqual(designs["uniform_two_positions"]["expected_pairs"], "1/3") self.assertEqual(designs["copies_together"]["expected_singletons"], "0") self.assertEqual(designs["copies_together"]["expected_pairs"], "1") self.assertEqual(designs["copies_separated"]["expected_singletons"], "2") self.assertEqual(designs["copies_separated"]["expected_pairs"], "0") for groups in data["layouts"].values(): self.assertEqual(Counter(itertools.chain.from_iterable(groups)), Counter({"A": 2, "B": 2})) self.assertEqual([len(group) for group in groups], [2, 2]) def test_ground_truth_differs(self): data = results()["populations"] self.assertEqual(data["triple_and_singletons"]["saved_payload_fraction"], "1/3") self.assertEqual(data["three_pairs"]["saved_payload_fraction"], "1/2") def test_three_position_control(self): left, right = sample_law((3, 1, 1, 1), 3), sample_law((2, 2, 2), 3) self.assertEqual(left[((3, 1),)], Fraction(1, 20)) self.assertNotIn(((3, 1),), right) self.assertEqual(total_variation(left, right), Fraction(3, 20)) def test_independent_counting_and_hypergeometric_means(self): self.assertEqual(len(list(partitions(6))), 11) for counts in partitions(6): for size in range(7): with self.subTest(counts=counts, size=size): law = sample_law(counts, size) self.assertEqual(law, allocation_law(counts, size)) self.assertEqual(sum(law.values()), 1) for i, mean in expected_bins(counts, size).items(): self.assertEqual(sum(p*dict(key).get(i, 0) for key, p in law.items()), mean) for key in law: self.assertEqual(sum(k*v for k, v in key), size) def test_census_and_empty(self): for counts in [(3, 1, 1, 1), (2, 2, 2)]: self.assertEqual(sample_law(counts, 0), {(): Fraction(1)}) self.assertEqual(sample_law(counts, 6), {tuple(sorted(Counter(counts).items())): Fraction(1)}) def test_input_contract(self): for counts in [(), (0,), (-1,), (True,), (1.5,), (13,)]: with self.subTest(counts=counts), self.assertRaises(ValueError): sample_law(counts, 0) for size in [-1, 7, True, 1.5]: with self.subTest(size=size), self.assertRaises(ValueError): sample_law((3, 1, 1, 1), size) def test_permutation_and_relabeling(self): self.assertEqual(sample_law((3, 1, 1, 1), 2), sample_law((1, 3, 1, 1), 2)) self.assertEqual(histogram("AAABCD"), histogram([9, 9, 9, 4, 7, 8])) def test_naive_saved_fraction(self): for counts in [(3, 1, 1, 1), (2, 2, 2)]: mean = sum(p*(1-Fraction(sum(dict(key).values()), 2)) for key, p in sample_law(counts, 2).items()) self.assertEqual(mean, Fraction(1, 10)) if __name__ == "__main__": unittest.main()