Esempio n. 1
0
 def test_user_function_str(self):
     u_function_1 = user_function(self.foo, "aligned", "aligned")
     u_function_2 = user_function(self.bar, "aligned", "pairwise_distances")
     self.assertEqual(
         str(u_function_1),
         "user_function(name='foo', module='test_composable')")
     self.assertEqual(
         str(u_function_2),
         "user_function(name='bar', module='test_composable')")
Esempio n. 2
0
 def test_user_function_str(self):
     u_function_1 = user_function(self.foo, "aligned", "aligned")
     u_function_2 = user_function(self.bar, "aligned", "pairwise_distances")
     self.assertEqual(
         str(u_function_1),
         "user_function(name='foo', module='test_composable')")
     self.assertEqual(
         str(u_function_2),
         "user_function(name='bar', module='test_composable')")
     # added into a composable func
     loader = io_app.load_aligned()
     proc = loader + u_function_1
     got = str(proc)
     self.assertTrue(got.startswith("load_aligned"))
Esempio n. 3
0
    def test_user_function_multiple(self):
        """user defined composable functions should not interfere with each other"""
        from cogent3 import make_aligned_seqs
        from cogent3.core.alignment import Alignment

        u_function_1 = user_function(self.foo, "aligned", "aligned")
        u_function_2 = user_function(self.bar, "aligned", "pairwise_distances")

        aln_1 = make_aligned_seqs(data=[("a", "GCAAGCGTTTAT"), ("b", "GCTTTTGTCAAT")])
        data = dict([("s1", "ACGTACGTA"), ("s2", "GTGTACGTA")])
        aln_2 = Alignment(data=data, moltype="dna")

        got_1 = u_function_1(aln_1)
        got_2 = u_function_2(aln_2)
        self.assertEqual(got_1.to_dict(), {"a": "GCAA", "b": "GCTT"})
        self.assertEqual(got_2, {("s1", "s2"): 2.0, ("s2", "s1"): 2.0})
Esempio n. 4
0
 def test_user_function_custom_variables(self):
     # not sure what this is meant to be testing
     demo = user_function(_demo, ("aligned", "serialisable"),
                          ("aligned", "serialisable"))
     frame_start = 2
     demo.frame_start = frame_start
     self.assertTrue(demo(demo, 2))
Esempio n. 5
0
 def test_user_function_custom_variables(self):
     demo = user_function(self._demo, ("aligned", "serialisable"),
                          ("aligned", "serialisable"))
     foo = demo
     frame_start = 2
     foo.frame_start = frame_start
     foo(frame_start)
Esempio n. 6
0
    def test_user_function_with_args_kwargs(self):
        """correctly handles definition with args, kwargs"""
        from math import log

        def product(val, multiplier, take_log=False):
            result = val * multiplier
            if take_log:
                result = log(result)

            return result

        # without defining any args, kwargs
        ufunc = user_function(
            product,
            SERIALISABLE_TYPE,
            SERIALISABLE_TYPE,
        )
        self.assertEqual(ufunc(2, 2), 4)
        self.assertEqual(ufunc(2, 2, take_log=True), log(4))

        # defining default arg2
        ufunc = user_function(
            product,
            SERIALISABLE_TYPE,
            SERIALISABLE_TYPE,
            2,
        )
        self.assertEqual(ufunc(2), 4)
        self.assertEqual(ufunc(2, take_log=True), log(4))

        # defining default kwarg only
        ufunc = user_function(product,
                              SERIALISABLE_TYPE,
                              SERIALISABLE_TYPE,
                              take_log=True)
        self.assertEqual(ufunc(2, 2), log(4))
        self.assertEqual(ufunc(2, 2, take_log=False), 4)

        # defining default arg and kwarg
        ufunc = user_function(product,
                              SERIALISABLE_TYPE,
                              SERIALISABLE_TYPE,
                              2,
                              take_log=True)
        self.assertEqual(ufunc(2), log(4))
Esempio n. 7
0
    def test_user_function(self):
        """composable functions should be user definable"""
        from cogent3 import make_aligned_seqs

        u_function = user_function(self.foo, "aligned", "aligned")

        aln = make_aligned_seqs(data=[("a", "GCAAGCGTTTAT"), ("b", "GCTTTTGTCAAT")])
        got = u_function(aln)

        self.assertEqual(got.to_dict(), {"a": "GCAA", "b": "GCTT"})