Beispiel #1
0
 def test_many(self):
     for a in self.randombitarrays(start=1):
         b = a.copy()
         # we set one random bit in b to 1, so a is always a subset of b
         b[randint(0, len(a) - 1)] = 1
         self.assertTrue(subset(a, b))
         # but b in not always a subset of a
         self.assertEqual(subset(b, a), self.subset_simple(b, a))
         # we set all bits in a, which ensures that b is a subset of a
         a.setall(1)
         self.assertTrue(subset(b, a))
Beispiel #2
0
 def test_subset(self):
     a = frozenbitarray('0101')
     b = bitarray('0111')
     self.assertTrue(subset(a, b))
     self.assertFalse(subset(b, a))
     self.assertRaises(TypeError, subset)
     self.assertRaises(TypeError, subset, a, '')
     self.assertRaises(TypeError, subset, '1', b)
     self.assertRaises(TypeError, subset, a, 4)
     b.append(1)
     self.assertRaises(ValueError, subset, a, b)
Beispiel #3
0
    def add(self, new_cut, new_cut_specification, min_size):
        """
        Check if new_cut can be added to the current orientation

        Parameters
        ----------
        new_cut: bitarray
            The bipartition that we need to add as bitarray
        new_cut_specification: dict of bool
            The specification of new_cut
        min_size:
            Minimum triplet size that we accept for it to be a tangle

        Returns
        -------
        new_specification: Specification or None
            If it is possible to add we return the new specification otherwise we return None
        """

        cuts = deepcopy(self.cuts)
        core = deepcopy(self.core)
        specification = deepcopy(self.specification)

        i_to_remove = []
        for i, core_cut in enumerate(core):
            if subset(core_cut, new_cut):
                cuts.append(new_cut)
                specification.update(new_cut_specification)
                return Tangle(cuts, core, specification)
            if subset(new_cut, core_cut):
                i_to_remove.append(i)

        for i in i_to_remove[::-1]:
            del core[i]

        if len(core) == 0:
            if new_cut.count() < min_size:
                return None
        elif len(core) == 1:
            if (core[0] & new_cut).count() < min_size:
                return None
        else:
            for core1, core2 in combinations(core, 2):
                if (core1 & core2 & new_cut).count() < min_size:
                    return None

        cuts.append(new_cut)
        core.append(new_cut)
        specification.update(new_cut_specification)

        return Tangle(cuts, core, specification)
Beispiel #4
0
 def test_subset_True(self):
     for a, b in [('', ''), ('0', '1'), ('0', '0'), ('1', '1'),
                  ('000', '111'), ('0101', '0111'),
                  ('000010111', '010011111')]:
         a, b = bitarray(a), bitarray(b)
         self.assertTrue(subset(a, b) is True)
         self.assertTrue(self.subset_simple(a, b) is True)
Beispiel #5
0
    def complete_closure(self, gen_idx, bit_extension, part_closure):
        """
        :param gen_idx:
        :param bit_extension:
        :param closure_prefix:
        :return:

        >>> table = [[0, 1, 0, 1],
        ...          [1, 1, 1, 0],
        ...          [1, 0, 1, 0],
        ...          [0, 1, 0, 1]]
        >>> ctx = Context.from_tab(table)
        >>> clo = bitarray('1000')
        >>> ctx.complete_closure(0, bitarray('0110'), clo)
        2
        >>> clo
        bitarray('1010')
        >>> clo = bitarray('1110')
        >>> ctx.complete_closure(1, bitarray('0100'), clo)
        4
        >>> clo
        bitarray('1110')
        """
        n = len(part_closure)
        crit_idx = n
        for j in range(gen_idx + 1, n):
            # TODO: for the moment put guard out because it seems faster, but this could change with
            #       numba and/or be different for different datasets
            # and len(extension) <= len(self.extents[j])
            if not part_closure[j] and subset(bit_extension, self.bit_extents[j]):
                crit_idx = min(crit_idx, j)
                part_closure[j] = True

        return crit_idx
Beispiel #6
0
 def find_small_crit_index(self, gen_idx, bit_extension, part_closure):
     """
     >>> table = [[0, 1, 0, 1],
     ...          [1, 1, 1, 0],
     ...          [1, 0, 1, 0],
     ...          [0, 1, 0, 1]]
     >>> ctx = Context.from_tab(table)
     >>> root = Node([], bitarray('0000'), array([0,1,2,3]), bitarray('1111'), -1, -4, 1, inf)
     >>> ctx.find_small_crit_index(0, bitarray('0110'), bitarray('1000'))
     4
     >>> ctx.find_small_crit_index(1, bitarray('1101'), bitarray('0100'))
     4
     >>> ctx.find_small_crit_index(2, bitarray('0110'), bitarray('0010'))
     0
     >>> ctx.find_small_crit_index(3, bitarray('1001'), bitarray('0001'))
     1
     >>> ctx.find_small_crit_index(3, bitarray('1001'), bitarray('0101'))
     4
     """
     for j in range(0, gen_idx):
         # and len(extension) <= len(self.extents[j])
         if not part_closure[j] and subset(bit_extension, self.bit_extents[j]):
             return j
     return len(part_closure)