def test_to_dict_roundtrip(self): """roundtrip of DictArray.to_dict() should produce same order.""" d1 = dict(a=dict(k=1, l=2, m=3), b=dict(k=4, l=5, m=6)) darr1 = DictArray(d1) d2 = darr1.to_dict() darr2 = DictArray(d2) self.assertEqual(d1, d2) d3 = DictArray(d2) self.assertEqual(d1, d3)
def __init__(self, observed, expected=None): """Parameters ------------- observed a DictArray instance, or something that can be converted to one. Values must be integers. expected provide in the case where you know the prior proportions, otherwise calculated from marginal frequencies """ if not isinstance(observed, DictArray): observed = DictArray(observed) # make sure values are int observed.array = _astype(observed.array, int) if observed.array.sum() == 0: raise ValueError("at least one value must be > 0") if observed.array.min() < 0: raise ValueError("negative values encountered") if observed.array.ndim > 2: raise NotImplementedError("not designed for >2D") self._observed = observed self.expected = expected self._residuals = None self._df = None self.shape = observed.shape
def test_inputs_from_dict_array(self): """inputs_from_dict_array makes an array object and PhyloNode list""" twod = { "1": { "1": 0, "2": 0.86, "3": 0.92 }, "2": { "1": 0.86, "2": 0, "3": 0.67 }, "3": { "1": 0.92, "2": 0.67, "3": 0 }, } matrix_d2d = DictArray(twod) matrix_array, PhyloNode_order = inputs_from_dict_array(matrix_d2d) self.assertEqual(PhyloNode_order[0].name, "1") self.assertEqual(PhyloNode_order[2].name, "3") assert_allclose(matrix_array[0][2], 0.92) assert_allclose(matrix_array[1][0], 0.86)
def __init__(self, observed, expected=None): """Parameters ------------- observed a DictArray instance, or something that can be converted to one expected provide in the case where you know the prior proportions, otherwise calculated from marginal frequencies """ if not isinstance(observed, DictArray): observed = DictArray(observed) if observed.array.sum() == 0: raise ValueError("at least one value must be > 0") if expected: expected = observed.template.wrap(expected) if observed.array.min() < 0 or expected and expected.array.min() < 0: raise ValueError("negative values encountered") if expected: assert_allclose(observed.array.sum(), expected.array.sum() ), "unequal totals of observed and expected" self._observed = observed self._expected = expected self._residuals = None self._df = None self.shape = observed.shape
def test_valid_setitem(self): """tabular_result works when set correct item type""" tr = tabular_result("null") tr["result"] = make_table(data={"A": [0, 1]}) darr = DictArray({"A": [0, 1]}) tr["result2"] = darr js = tr.to_json() self.assertIsInstance(js, str)
def test_direct_construction(self): """directly construct a dict array""" b = DictArrayTemplate("abc", "ABC").wrap(self.a) data_types = ( [[245, 599]], dict(a=dict(b=4, c=5)), {("a", "b"): 4, ("a", "c"): 5}, dict(a=0, b=35, c=45), b, ) for data in data_types: g = DictArray(data)
def test_to_dict_1d(self): """should successfully produce a 1D dict""" data = { "ABAYE2984": { "ABAYE2984": 0, "Atu3667": None, "Avin_42730": 0.6381173875591908, "BAA10469": None, }, "Atu3667": { "ABAYE2984": None, "Atu3667": 0, "Avin_42730": 2.3682377869318993, "BAA10469": None, }, "Avin_42730": { "ABAYE2984": 0.6381173875591908, "Atu3667": 2.3682377869318993, "Avin_42730": 0, "BAA10469": 1.8515731266342546, }, "BAA10469": { "ABAYE2984": None, "Atu3667": None, "Avin_42730": 1.8515731266342546, "BAA10469": 0, }, } darr = DictArray(data, dtype="O") expect = { (n1, n2): darr[n1, n2] for n1 in darr.template.names[0] for n2 in darr.template.names[1] } self.assertEqual(darr.to_dict(flatten=True), expect) darr = DictArrayTemplate(["s1", "s2"], ["s1", "s2"]).wrap( [[0.0, 0.25], [0.25, 0.0]] ) self.assertEqual( darr.to_dict(flatten=True), { ("s1", "s2"): 0.25, ("s2", "s1"): 0.25, ("s1", "s1"): 0.0, ("s2", "s2"): 0.0, }, )
def upgma(pairwise_distances): """Uses the UPGMA algorithm to cluster sequences pairwise_distances: a dictionary with pair tuples mapped to a distance returns a PhyloNode object of the UPGMA cluster """ darr = DictArray(pairwise_distances) matrix_a, node_order = inputs_from_dict_array(darr) tree = UPGMA_cluster(matrix_a, node_order, BIG_NUM) index = 0 for node in tree.traverse(): if not node.parent: node.name = "root" elif not node.name: node.name = "edge." + str(index) index += 1 return tree
def row_totals(self): """returns totalled row values""" row_sums = self.array.sum(axis=1) template = DictArray(1, row_sums.shape[0]) return template.wrap(row_sums)