当前位置: 首页>>代码示例>>Python>>正文


Python Profile.reduce方法代码示例

本文整理汇总了Python中cogent.core.profile.Profile.reduce方法的典型用法代码示例。如果您正苦于以下问题:Python Profile.reduce方法的具体用法?Python Profile.reduce怎么用?Python Profile.reduce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cogent.core.profile.Profile的用法示例。


在下文中一共展示了Profile.reduce方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_reduce_operators

# 需要导入模块: from cogent.core.profile import Profile [as 别名]
# 或者: from cogent.core.profile.Profile import reduce [as 别名]
    def test_reduce_operators(self):
        """reduce: should work fine with different operators
        """
        # different operators, normalize input, don't normalize output
        p1 = Profile(array([[1, 0, 0], [0, 1, 0]]), Alphabet="ABC")
        p2 = Profile(array([[1, 0, 0], [0, 0, 1]]), Alphabet="ABC")

        self.assertEqual(p1.reduce(p2).Data, array([[1, 0, 0], [0, 0.5, 0.5]]))
        self.assertEqual(
            p1.reduce(p2, add, normalize_input=True, normalize_output=False).Data, array([[2, 0, 0], [0, 1, 1]])
        )
        self.assertEqual(
            p1.reduce(p2, subtract, normalize_input=True, normalize_output=False).Data, array([[0, 0, 0], [0, 1, -1]])
        )
        self.assertEqual(
            p1.reduce(p2, multiply, normalize_input=True, normalize_output=False).Data, array([[1, 0, 0], [0, 0, 0]])
        )

        self.assertRaises(ProfileError, p1.reduce, p2, divide, normalize_input=True, normalize_output=False)

        # don't normalize and normalize only input
        p3 = Profile(array([[1, 2], [3, 4]]), Alphabet="AB")
        p4 = Profile(array([[4, 3], [2, 1]]), Alphabet="AB")

        self.assertEqual(
            p3.reduce(p4, add, normalize_input=False, normalize_output=False).Data, array([[5, 5], [5, 5]])
        )
        self.assertFloatEqual(
            p3.reduce(p4, add, normalize_input=True, normalize_output=False).Data,
            array([[19 / 21, 23 / 21], [23 / 21, 19 / 21]]),
        )

        # normalize input and output
        p5 = Profile(array([[1, 1, 0, 0], [1, 1, 1, 1]]), Alphabet="ABCD")
        p6 = Profile(array([[1, 0, 0, 0], [1, 0, 0, 1]]), Alphabet="ABCD")

        self.assertEqual(
            p5.reduce(p6, add, normalize_input=True, normalize_output=True).Data,
            array([[0.75, 0.25, 0, 0], [0.375, 0.125, 0.125, 0.375]]),
        )

        # it can collapse empty profiles when normalizing is turned off
        self.assertEqual(
            self.empty.reduce(self.empty, normalize_input=False, normalize_output=False).Data.tolist(), [[]]
        )
开发者ID:blankenberg,项目名称:pycogent,代码行数:47,代码来源:test_profile.py

示例2: ProfileTests

# 需要导入模块: from cogent.core.profile import Profile [as 别名]
# 或者: from cogent.core.profile.Profile import reduce [as 别名]

#.........这里部分代码省略.........

        # it will still print with invalid data (e.g if len(CharOrder)
        # doesn't match the data
        p = self.full.copy()
        p.CharOrder = "ABC"

        self.assertEqual(p.prettyPrint(include_header=True), "A\tB\tC\n2\t4\t \n3\t5\t \n4\t8\t ")
        # it will truncate the CharOrder if data is transposed
        # and CharOrder is longer then the number of rows in the
        # transposed data
        self.assertEqual(p.prettyPrint(include_header=True, transpose_data=True), "A\t2\t3\t4\nB\t4\t5\t8")

    def test_prettyPrint_four_cases(self):
        """prettyPrint: with/without header/transpose/limit"""
        p = self.full
        p = self.pp
        self.assertEqual(p.prettyPrint(), "1\t 2\t 3\t 4\n5\t 6\t 7\t 8\n9\t10\t11\t12")
        self.assertEqual(p.prettyPrint(column_limit=3), "1\t 2\t 3\n5\t 6\t 7\n9\t10\t11")
        self.assertEqual(
            p.prettyPrint(column_limit=3, include_header=True), "A\t B\t C\n1\t 2\t 3\n5\t 6\t 7\n9\t10\t11"
        )
        self.assertEqual(
            p.prettyPrint(column_limit=3, include_header=False, transpose_data=True),
            "1\t5\t 9\n2\t6\t10\n3\t7\t11\n4\t8\t12",
        )
        self.assertEqual(
            p.prettyPrint(column_limit=2, include_header=False, transpose_data=True), "1\t5\n2\t6\n3\t7\n4\t8"
        )
        self.assertEqual(
            p.prettyPrint(column_limit=3, include_header=True, transpose_data=True),
            "A\t1\t5\nB\t2\t6\nC\t3\t7\nD\t4\t8",
        )

    def test_reduce_wrong_size(self):
        """reduce: should fail when profiles have different sizes"""
        p1 = Profile(array([[1, 0], [0, 1]]), Alphabet="AB")
        p2 = Profile(array([[1, 0, 0], [1, 0, 0]]), Alphabet="ABC")
        self.assertRaises(ProfileError, p1.reduce, p2)

    def test_reduce_normalization_error(self):
        """reduce: fails when input or output can't be normalized"""
        # Will raise errors when input data can't be normalized
        self.assertRaises(ProfileError, self.empty.reduce, self.empty, add)
        self.assertRaises(ProfileError, self.full.reduce, self.empty_row, add)

        # don't normalize input, but do normalize output
        # fails when one row adds up to zero
        p1 = Profile(array([[3, 3], [4, 4]]), "AB")
        p2 = Profile(array([[3, 3], [-4, -4]]), "AB")
        self.assertRaises(ProfileError, p1.reduce, p2, add, False, True)

    def test_reduce_operators(self):
        """reduce: should work fine with different operators
        """
        # different operators, normalize input, don't normalize output
        p1 = Profile(array([[1, 0, 0], [0, 1, 0]]), Alphabet="ABC")
        p2 = Profile(array([[1, 0, 0], [0, 0, 1]]), Alphabet="ABC")

        self.assertEqual(p1.reduce(p2).Data, array([[1, 0, 0], [0, 0.5, 0.5]]))
        self.assertEqual(
            p1.reduce(p2, add, normalize_input=True, normalize_output=False).Data, array([[2, 0, 0], [0, 1, 1]])
        )
        self.assertEqual(
            p1.reduce(p2, subtract, normalize_input=True, normalize_output=False).Data, array([[0, 0, 0], [0, 1, -1]])
        )
        self.assertEqual(
开发者ID:blankenberg,项目名称:pycogent,代码行数:70,代码来源:test_profile.py


注:本文中的cogent.core.profile.Profile.reduce方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。