Example #1
0
def test_inverted_bidict(bi_and_mapping):
    """:func:`bidict.inverted` should yield the inverse items of an ordered bidict."""
    bi, mapping = bi_and_mapping
    mapping_inv = {v: k for (k, v) in mapping.items()}
    assert all(i == j for (i, j) in zip(inverted(bi), mapping_inv.items()))
    assert all(i == j
               for (i, j) in zip(inverted(inverted(bi)), mapping.items()))
Example #2
0
def test_inverted_bidict(bi_and_cmp_dict):
    """:func:`bidict.inverted` should yield the inverse items of a bidict."""
    bi, cmp_dict = bi_and_cmp_dict
    cmp_dict_inv = OrderedDict((v, k) for (k, v) in cmp_dict.items())
    assert set(inverted(bi)) == cmp_dict_inv.items() == bi.inv.items()
    assert set(inverted(
        inverted(bi))) == cmp_dict.items() == bi.inv.inv.items()
Example #3
0
def test_inverted_orderedbidict(ob_and_od):
    """:func:`bidict.inverted` should yield the inverse items of an ordered bidict."""
    ob, od = ob_and_od
    od_inv = OrderedDict((v, k) for (k, v) in iteritems(od))
    assert all(i == j for (i, j) in izip(inverted(ob), iteritems(od_inv)))
    assert all(i == j
               for (i, j) in izip(inverted(inverted(ob)), iteritems(od)))
Example #4
0
def test_inverted(bi_cls, init_items):
    """Test that :func:`bidict.inverted` works correctly."""
    inv_items = [(v, k) for (k, v) in init_items]
    assert list(inverted(init_items)) == inv_items
    assert list(inverted(inverted(init_items))) == init_items
    some_bidict = bi_cls(init_items)
    inv_bidict = bi_cls(inv_items)
    assert some_bidict.inv == inv_bidict
    assert set(inverted(some_bidict)) == set(inv_items)
    assert bi_cls(inverted(inv_bidict)) == some_bidict
Example #5
0
 def get_rotations(self, nescient):
     """returns list of directions nescient can rotate."""
     drctns = []
     nbdr = nescient.body['right'].location
     for direction, _ in inverted(self.grid.directions):
         body = self.make_body(nbdr, direction)
         if self.can_move_nescient(body, nescient):
             drctns.append(direction)  # might want to return body as well.
     return drctns
Example #6
0
def save_stats(stats, figname, mappings):
    import matplotlib.pyplot as plt
    plt.rcParams.update({'figure.autolayout': True})

    fig, axes = plt.subplots(figsize=(7, 5), dpi=100)
    plt.bar(stats['rels'].keys(), height=stats['rels'].values())
    plt.xticks(rotation=90)
    fig.savefig(figname)
    with open(mappings + ".json", "w") as fd:
        json.dump(dict(stats['mapping']), fd)
    with open(mappings + ".inv.json", "w") as fd:
        json.dump(dict(inverted(stats['mapping'])), fd)
Example #7
0
 def training_prep(self, seq: Sequence[State]):
     """ Preparation for training the chain on a sequence. """
     print("Learning...")
     seq = self._ensure_seq(seq)
     self.dim = len(seq[0])
     # Determine the set of possible values for each dimension
     val_sets = [{x[i] for x in seq} for i in range(self.dim)]
     # Create a mapping from values to indices
     self.ind_maps = [
         bidict(inverted(enumerate(sorted(s)))) for s in val_sets
     ]
     # which, together with the order, gives us the shape of the transition matrix
     shape = self._subshape * (self.order + 1)
     print("Attempting to allocate transition matrix of shape",
           shape,
           end="... ")
     self.tr_matrix = np.zeros(shape)
     print("successful!")
Example #8
0
def test_inverted_pairs(pairs):
    """:func:`bidict.inverted` should yield the inverses of a list of pairs."""
    inv = [(v, k) for (k, v) in pairs]
    assert list(inverted(pairs)) == inv
    assert list(inverted(inverted(pairs))) == pairs
Example #9
0
 def inverted_vectors(cls):
     if cls._inverted_vectors is not None:
         return cls._inverted_vectors
     cls._inverted_vectors = bidict(inverted(cls.vectors))
     return cls._inverted_vectors
Example #10
0
def test_inverted_orderedbidict(ob_and_od):
    """:func:`bidict.inverted` should yield the inverse items of an ordered bidict."""
    ob, od = ob_and_od
    od_inv = OrderedDict((v, k) for (k, v) in iteritems(od))
    assert all(i == j for (i, j) in izip(inverted(ob), iteritems(od_inv)))
    assert all(i == j for (i, j) in izip(inverted(inverted(ob)), iteritems(od)))
Example #11
0
def test_inverted_bidict(bi_and_cmp_dict):
    """:func:`bidict.inverted` should yield the inverse items of a bidict."""
    bi, cmp_dict = bi_and_cmp_dict
    cmp_dict_inv = OrderedDict((v, k) for (k, v) in iteritems(cmp_dict))
    assert set(inverted(bi)) == viewitems(cmp_dict_inv) == viewitems(bi.inv)
    assert set(inverted(inverted(bi))) == viewitems(cmp_dict) == viewitems(bi.inv.inv)
Example #12
0
def test_inverted_pairs(pairs):
    """:func:`bidict.inverted` should yield the inverses of a list of pairs."""
    inv = [(v, k) for (k, v) in pairs]
    assert list(inverted(pairs)) == inv
    assert list(inverted(inverted(pairs))) == pairs
Example #13
0
def test_inverse_02(lst=strs_to_ints):
    assert lst == list(B.inverted(B.inverted(lst)))
Example #14
0
def test_inverse_01(lst=ints_to_strs):
    assert lst == list(B.inverted(B.inverted(lst)))
Example #15
0
 def test_inverted(self):
     self.assertEqual(Grid.inverted_vectors, bidict(inverted(Grid.vectors)))