test_weighted_grid.py 4.76 KB
Newer Older
1
2
3
4
5
6
7
#!/usr/bin/env python

import os, sys
import unittest

sys.path.append('../../')

8
from myplotlib import EmptyFig
9
from myplotlib.mpl_toolbox import WeightedGrid, Ax2dPlots
10
11
12
13

class WeightedGridTestCase(unittest.TestCase):

    def setUp(self):
14
        self.rawdata = [-1., 1.]
15
16
17
18
19
        self.foo_fig = EmptyFig()
        self.foo_grid = WeightedGrid()
        self.foo_grid2 = WeightedGrid()
        self.foo_axes = Ax2dPlots(self.foo_fig)
        self.foo_axes2 = Ax2dPlots(self.foo_fig)
20
21
22
23
24
25
26
27
28
29
30

    def test_default_constructor(self):
        wgrid = WeightedGrid()
        self.assertEqual(wgrid.weights, [])

    def test_constructor_with_weights(self):
        wgrid = WeightedGrid(weights=[0.2, 0.2, 0.4, 0.15, 0.05])
        self.assertEqual(wgrid.weights, [0.2, 0.2, 0.4, 0.15, 0.05])
        
    def test_append_axes(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05)
31
32
        self.foo_fig.set_grid(wgrid)
        wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata)
33
        self.assertEqual(wgrid.weights, [1.])
34
        wgrid.append_axes(self.foo_axes2, 'foo_axes2', self.rawdata)
35
36
37
38
        self.assertEqual(wgrid.weights, [1., 1.])

    def test_append_axes_with_weight(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05)
39
40
        self.foo_fig.set_grid(wgrid)
        wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata, weight=0.6)
41
        self.assertEqual(wgrid.weights, [0.6])
42
        wgrid.append_axes(self.foo_axes2, 'foo_axes2', self.rawdata, weight=0.4)
43
44
45
46
        self.assertEqual(wgrid.weights, [0.6, 0.4])
        
    def test_append_axes_with_weights(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05)
47
48
        self.foo_fig.set_grid(wgrid)
        wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata)
49
        self.assertEqual(wgrid.weights, [1.])
50
        wgrid.append_axes(self.foo_axes2, 'foo_axes2', self.rawdata, weights=[0.4, 0.6])
51
52
53
54
        self.assertEqual(wgrid.weights, [0.4, 0.6])

    def test_append_grid(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05)
55
56
        self.foo_fig.set_grid(wgrid)
        wgrid.append_grid(self.foo_grid)
57
        self.assertEqual(wgrid.weights, [1.])
58
        wgrid.append_grid(self.foo_grid2)
59
60
61
62
        self.assertEqual(wgrid.weights, [1., 1.])

    def test_append_grid_with_weight(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05)
63
64
        self.foo_fig.set_grid(wgrid)
        wgrid.append_grid(self.foo_grid, weight=0.6)
65
        self.assertEqual(wgrid.weights, [0.6])
66
        wgrid.append_grid(self.foo_grid2, weight=0.4)
67
68
69
70
        self.assertEqual(wgrid.weights, [0.6, 0.4])        

    def test_append_grid_with_weights(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05)
71
72
        self.foo_fig.set_grid(wgrid)
        wgrid.append_grid(self.foo_grid)
73
        self.assertEqual(wgrid.weights, [1.])
74
        wgrid.append_grid(self.foo_grid2, weights=[0.4, 0.6])
75
76
77
78
        self.assertEqual(wgrid.weights, [0.4, 0.6])        

    def test_get_item_weight(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05, weights=[0.5, 0.3, 0.6])
79
        self.assertEqual(wgrid.get_item_weight(1), 0.3)
80
81
82

    def test_get_weights(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05, weights=[0.5, 0.3, 0.6])
83
        self.assertEqual(wgrid.get_weights(), [0.5, 0.3, 0.6])
84
85
86
87
88

    def test_looping_grids(self):
        wgrid = WeightedGrid(margin=0.15, padding=0.05)
        wgrid2 = WeightedGrid(margin=0.15, padding=0.05)

89
        self.foo_fig.set_grid(wgrid)
90
        
91
92
        wgrid.append_grid(wgrid2)
        wgrid2.append_grid(wgrid)
93
94

        with self.assertRaises(RuntimeError):
95
            self.foo_fig.layout()      
96
97
98
99
        
class HorizontalWeightedGridTestCase(unittest.TestCase):

    def setUp(self):
100
        self.rawdata = [-1., 1.]
101
102
103
        self.foo_fig = EmptyFig()
        self.foo_grid = HorizontalWeightedGrid()
        self.foo_axes = Ax2dPlots(self.foo_fig)
104
105
106
        
        self.wgrid = HorizontalWeightedGrid(margin=0.15, padding=0.05)
        
107
        self.foo_fig.set_grid(wgrid)
108
        
109
110
        self.wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata, weight=0.6)
        self.wgrid.append_axes(self.foo_grid, weight=0.4)
111
112

    def test_layout(self):
113
        self.foo_fig.layout()
114
115
116
117
    
class VerticalWeightedGridTestCase(unittest.TestCase):

    def setUp(self):
118
        self.rawdata = [-1., 1.]
119
120
121
        self.foo_fig = EmptyFig()
        self.foo_grid = VerticalWeightedGrid()
        self.foo_axes = Ax2dPlots(self.foo_fig)
122
123
124
        
        self.wgrid = VerticalWeightedGrid(margin=0.15, padding=0.05)
        
125
        self.foo_fig.set_grid(wgrid)
126
        
127
128
        self.wgrid.append_axes(self.foo_axes, 'foo_axes', self.rawdata, weight=0.6)
        self.wgrid.append_axes(self.foo_grid, weight=0.4)
129
130

    def test_layout(self):
131
        self.foo_fig.layout()
132
133
134
        
if __name__ == '__main__':
    unittest.main()