File: test_struct_pipeline.py

package info (click to toggle)
pybel 0.15.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,504 kB
  • sloc: python: 29,392; javascript: 246; makefile: 226; sh: 20
file content (136 lines) | stat: -rw-r--r-- 4,076 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-

import logging
import unittest
from io import StringIO

from pybel import BELGraph
from pybel.examples.egf_example import egf_graph
from pybel.struct.mutation import enrich_protein_and_rna_origins
from pybel.struct.pipeline import Pipeline
from pybel.struct.pipeline.decorators import get_transformation, mapped
from pybel.struct.pipeline.exc import MetaValueError, MissingPipelineFunctionError

log = logging.getLogger(__name__)
log.setLevel(10)


class TestEgfExample(unittest.TestCase):
    """Random test for mutation functions"""

    def setUp(self):
        self.graph = egf_graph.copy()
        self.original_number_nodes = self.graph.number_of_nodes()
        self.original_number_edges = self.graph.number_of_edges()

    def check_original_unchanged(self):
        self.assertEqual(
            self.original_number_nodes,
            self.graph.number_of_nodes(),
            msg="original graph nodes should remain unchanged",
        )
        self.assertEqual(
            self.original_number_edges,
            self.graph.number_of_edges(),
            msg="original graph edges should remain unchanged",
        )


class TestPipelineFailures(unittest.TestCase):
    def test_assert_failure(self):
        with self.assertRaises(MissingPipelineFunctionError):
            get_transformation("missing function")

    def test_assert_success(self):
        m = list(mapped)
        self.assertLess(0, len(m))
        m = m[0]
        f = get_transformation(m)
        self.assertIsNotNone(f)

    def test_append_invalid(self):
        """Test when an invalid type is given to a :class:`pybel.struct.Pipeline`."""
        p = Pipeline()
        with self.assertRaises(TypeError):
            p.append(4)

    def test_get_function_failure(self):
        p = Pipeline()

        with self.assertRaises(MissingPipelineFunctionError):
            p._get_function("nonsense name")

    def test_build_meta_failure(self):
        p1, p2 = Pipeline(), Pipeline()

        p = Pipeline._build_meta("wrong", [p1, p2])

        with self.assertRaises(MetaValueError):
            p(BELGraph())

    def test_fail_add(self):
        pipeline = Pipeline()
        with self.assertRaises(MissingPipelineFunctionError):
            pipeline.append("missing function")


class TestPipeline(TestEgfExample):
    def test_append(self):
        pipeline = Pipeline()
        self.assertEqual(0, len(pipeline))

        pipeline.append("enrich_protein_and_rna_origins")
        self.assertEqual(1, len(pipeline))

    def test_extend(self):
        p1 = Pipeline.from_functions(["enrich_protein_and_rna_origins"])
        self.assertEqual(1, len(p1))

        p2 = Pipeline.from_functions(["remove_pathologies"])
        p1.extend(p2)

        self.assertEqual(2, len(p1))

    def test_serialize_string(self):
        p = Pipeline.from_functions(["enrich_protein_and_rna_origins"])
        s = p.dumps()
        p_reconstituted = Pipeline.loads(s)
        self.assertEqual(p.protocol, p_reconstituted.protocol)

    def test_serialize_file(self):
        p = Pipeline.from_functions(["enrich_protein_and_rna_origins"])
        sio = StringIO()
        p.dump(sio)
        sio.seek(0)
        p_reconstituted = Pipeline.load(sio)
        self.assertEqual(p.protocol, p_reconstituted.protocol)

    def test_pipeline_by_string(self):
        pipeline = Pipeline.from_functions(
            [
                "enrich_protein_and_rna_origins",
            ]
        )
        result = pipeline(self.graph)

        self.assertEqual(32, result.number_of_nodes())

        for node in self.graph:
            self.assertIn(node, result)

        self.check_original_unchanged()

    def test_pipeline_by_function(self):
        pipeline = Pipeline.from_functions(
            [
                enrich_protein_and_rna_origins,
            ]
        )
        result = pipeline(self.graph)

        self.assertEqual(32, result.number_of_nodes())

        for node in self.graph:
            self.assertIn(node, result)

        self.check_original_unchanged()