How to use the cogent3.util.dict2d.Dict2D function in cogent3

To help you get started, we’ve selected a few cogent3 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_init(self):
        """Dict2D init should work as expected"""
        # NOTE: currently only tests init from dict of dicts. Other initializers
        # are tested in the test_guess_input* and test_from* methods

        # should compare equal to the relevant dict
        for d in [self.empty, self.single_same, self.single_diff, self.dense,
                  self.sparse]:
            d2d = Dict2D(d)
            self.assertEqual(d2d, d)
            self.assertEqual(d2d.__class__, Dict2D)
        # spot-check values
        d2d = Dict2D(self.sparse)
        self.assertEqual(d2d['a']['c'], 3)
        self.assertEqual(d2d['d']['b'], 2)
        self.assertEqual(len(d2d), 2)
        self.assertEqual(len(d2d['a']), 2)
        self.assertRaises(KeyError, d2d.__getitem__, 'c')
        # check truth values
        assert not Dict2D(self.empty)
        assert Dict2D(self.single_same)
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_item_indices_if(self):
        """Dict2D item_indices_if should return indices when f(item) is True"""
        lt_5 = lambda x: x < 5
        d = Dict2D(self.square)
        d.RowOrder = d.ColOrder = 'abc'
        self.assertEqual(d.item_indices_if(lt_5),
                         [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('c', 'a')])
        self.assertEqual(d.item_indices_if(lt_5, negate=True),
                         [('b', 'c'), ('c', 'b'), ('c', 'c')])
        d = Dict2D(self.top_triangle)
        d.RowOrder = d.ColOrder = 'abc'
        self.assertEqual(d.item_indices_if(lt_5),
                         [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b')])
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_cols(self):
        """Dict2D Cols property should return list in correct order"""
        # should work with no data
        self.assertEqual(list(Dict2D(self.empty).Cols), [])
        # should work with square matrix
        sq = Dict2D(self.square, RowOrder='abc', ColOrder='abc')
        self.assertEqual(list(sq.Cols), [[1, 2, 3], [2, 4, 6], [3, 6, 9]])
        # check that it works when we change the row and col order
        sq.RowOrder = 'ba'
        sq.ColOrder = 'ccb'
        self.assertEqual(list(sq.Cols), [[6, 3], [6, 3], [4, 2]])
        # check that it _does_ raise an error on sparse matrices...
        sp = Dict2D(self.sparse)
        self.assertRaises(Dict2DSparseError, list, sp.Cols)
        #...especially if self.RowOrder and self.ColOrder are set...
        sp.RowOrder = 'ad'
        sp.ColOrder = 'abc'
        self.assertRaises(Dict2DSparseError, list, sp.Cols)
        #...and then, only if self.Pad is not set
        sp.Pad = True
        sp.Default = 'xxx'
        self.assertEqual(list(sp.Cols), [[1, 'xxx'], ['xxx', 2], [3, 'xxx']])
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_fromLists(self):
        """Dict2D.fromLists should construct from list of lists"""
        # Note that this only works for dense matrices, not sparse ones
        orig = Dict2D(self.dense)
        new = Dict2D(self.dense)  # will overwrite this data
        self.assertEqual(orig, new)
        assert orig is not new
        new.RowOrder = ['b', 'a']
        new.ColOrder = ['c', 'a', 'b']
        new.fromLists([[3, 6, 9], [1, 3, 5]])
        self.assertNotEqual(orig, new)
        test = Dict2D({'b': {'c': 3, 'a': 6, 'b': 9},
                      'a': {'c': 1, 'a': 3, 'b': 5}})
        self.assertEqual(new, test)
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_fromDicts(self):
        """Dict2D.fromDicts should construct from dict of dicts"""
        d2d = Dict2D()
        d2d.fromDicts(self.sparse)
        self.assertEqual(d2d['a']['c'], 3)
        self.assertEqual(d2d['d']['b'], 2)
        self.assertEqual(len(d2d), 2)
        self.assertEqual(len(d2d['a']), 2)
        self.assertRaises(KeyError, d2d.__getitem__, 'c')
        self.assertRaises(Dict2DInitError, d2d.fromDicts, [1, 2, 3])
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_transpose(self):
        """Dict2D transpose should work on both dense and sparse matrices, in place"""
        # should do nothing to empty matrix
        d = Dict2D(self.empty)
        d.transpose()
        self.assertEqual(d, {})
        # should do nothing to single-element square matrix
        d = Dict2D(self.single_same)
        d.transpose()
        self.assertEqual(d, {'a': {'a': 2}})
        # should reverse single-element non-square matrix
        d = Dict2D(self.single_diff)
        d.transpose()
        self.assertEqual(d, {'b': {'a': 3}})
        # should work on sparse matrix
        d = Dict2D(self.sparse)
        d.transpose()
        self.assertEqual(d, {'a': {'a': 1}, 'c': {'a': 3}, 'b': {'d': 2}})
        # should reverse row and col order
        d = Dict2D(self.dense)
        d.RowOrder = 'ab'
        d.ColOrder = 'abc'
        d.transpose()
        self.assertEqual(d,
                         {'a': {'a': 1, 'b': 2}, 'b': {'a': 2, 'b': 4}, 'c': {'a': 3, 'b': 6}})
        self.assertEqual(d.ColOrder, 'ab')
        self.assertEqual(d.RowOrder, 'abc')
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_rowKeys(self):
        """Dict2D rowKeys should find all the keys of component rows"""
        self.assertEqual(Dict2D(self.empty).rowKeys(), [])
        self.assertEqual(Dict2D(self.single_diff).rowKeys(), ['a'])
        # note that keys will be returned in arbitrary order
        self.assertEqualItems(Dict2D(self.dense).rowKeys(), ['a', 'b', ])
        self.assertEqualItems(Dict2D(self.square).rowKeys(), ['a', 'b', 'c'])
        self.assertEqualItems(Dict2D(self.sparse).rowKeys(), ['a', 'd'])
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_cols(self):
        """Dict2D Cols property should return list in correct order"""
        # should work with no data
        self.assertEqual(list(Dict2D(self.empty).Cols), [])
        # should work with square matrix
        sq = Dict2D(self.square, RowOrder='abc', ColOrder='abc')
        self.assertEqual(list(sq.Cols), [[1, 2, 3], [2, 4, 6], [3, 6, 9]])
        # check that it works when we change the row and col order
        sq.RowOrder = 'ba'
        sq.ColOrder = 'ccb'
        self.assertEqual(list(sq.Cols), [[6, 3], [6, 3], [4, 2]])
        # check that it _does_ raise an error on sparse matrices...
        sp = Dict2D(self.sparse)
        self.assertRaises(Dict2DSparseError, list, sp.Cols)
        #...especially if self.RowOrder and self.ColOrder are set...
        sp.RowOrder = 'ad'
        sp.ColOrder = 'abc'
        self.assertRaises(Dict2DSparseError, list, sp.Cols)
        #...and then, only if self.Pad is not set
        sp.Pad = True
        sp.Default = 'xxx'
        self.assertEqual(list(sp.Cols), [[1, 'xxx'], ['xxx', 2], [3, 'xxx']])
github cogent3 / cogent3 / tests / test_util / test_dict2d.py View on Github external
def test_rowKeys(self):
        """Dict2D rowKeys should find all the keys of component rows"""
        self.assertEqual(Dict2D(self.empty).rowKeys(), [])
        self.assertEqual(Dict2D(self.single_diff).rowKeys(), ['a'])
        # note that keys will be returned in arbitrary order
        self.assertEqualItems(Dict2D(self.dense).rowKeys(), ['a', 'b', ])
        self.assertEqualItems(Dict2D(self.square).rowKeys(), ['a', 'b', 'c'])
        self.assertEqualItems(Dict2D(self.sparse).rowKeys(), ['a', 'd'])
github cogent3 / cogent3 / cogent3 / maths / matrix / distance.py View on Github external
possible elements based on RowOrder and ColOrder
                RowConstructor = constructor to use when building inner
                    objects, default dict
                info = the AAIndexRecord object

            Power = Power the original matrix has been raised to yield current
              matrix
        """
        if RowOrder is not None:
            self.RowOrder = RowOrder
        if ColOrder is not None:
            self.ColOrder = ColOrder
        if Pad is not None:
            self.Pad = Pad
        # Initialize super class attributes
        Dict2D.__init__(self, data=data, RowOrder=self.RowOrder,
                        ColOrder=self.ColOrder, Default=Default, Pad=self.Pad,
                        RowConstructor=RowConstructor)
        Delegator.__init__(self, info)

        # The power to which the original data has been raised to give
        # the current data, starts at 1., modified by elementPow()
        # accessed as self.Power
        self.__dict__['Power'] = 1.