source: qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_colour_bar.py @ 5403

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_colour_bar.py@5403
Revision 5403, 5.8 KB checked in by pnorton, 11 years ago (diff)

Moved QESDI tree from DCIP repository
 http://proj.badc.rl.ac.uk/svn/dcip/qesdi@3900.

Line 
1#/urs/bin/env python
2"""
3test_colour_bar.py
4"""
5
6import nose
7import matplotlib
8import matplotlib.ticker
9import matplotlib.colorbar
10import matplotlib.cm
11
12import geoplot.colour_bar
13import geoplot.utils
14import geoplot.tests.test_utils
15
16from geoplot.plot_font import PlotFont
17from geoplot.colour_bar import ColourBar
18
19import geoplot.tests.test_config as testConfig
20config = testConfig.getConfig()
21
22FONTS_SECTION = 'Fonts'
23
24class Test_ColourBar(object):
25
26    def setUp(self):
27        #replace the colour bar config
28        self.oldConfig = geoplot.colour_bar.config
29        geoplot.colour_bar.config = config
30       
31        self.colourBarLabel = "A label"
32        self.colourBarPosition = "horizontal"
33        self.colourBarRange = (25.0, 75.0)
34        self.cb = ColourBar(self.colourBarLabel, self.colourBarPosition, self.colourBarRange)
35
36        self.sm = matplotlib.cm.ScalarMappable(cmap=matplotlib.cm.prism)
37        self.sm.set_clim(15.5, 95.5)
38
39    def tearDown(self):
40        geoplot.colour_bar.config = self.oldConfig
41
42    def test_001_checkInitialisaion(self):
43        assert(self.cb.label == self.colourBarLabel)
44        assert(self.cb.position == self.colourBarPosition)
45        print self.colourBarRange
46        print self.cb.range
47        assert(self.cb.range == self.colourBarRange)
48
49    def test_002_checkLabelFont(self):
50        colourBarFont = config[FONTS_SECTION]['ColourBarLabel']
51        assert(self.cb.labelFont.getDict('small')  == colourBarFont.getDict('small'))
52        assert(self.cb.labelFont.getDict('medium') == colourBarFont.getDict('medium'))
53        assert(self.cb.labelFont.getDict('large')  == colourBarFont.getDict('large'))
54
55    def test_003_setsMaxLimit(self):
56        newMax = 100.52
57        self.cb.range = [None, newMax]
58        (minBefore, maxBefore) = self.sm.get_clim()
59        print self.sm.get_clim()
60        self.cb._setLimits(self.sm)
61        (minAfter, maxAfter) = self.sm.get_clim()
62        print self.sm.get_clim()
63        assert(minAfter == minBefore)
64        assert(maxAfter == newMax)
65
66    def test_004_setsMinLimit(self):
67        newMin = 10.5
68
69        self.cb.range = (newMin, None)
70        (minBefore, maxBefore) = self.sm.get_clim()
71        self.cb._setLimits(self.sm)
72        (minAfter, maxAfter) = self.sm.get_clim()
73        assert(minAfter == newMin)
74        assert(maxAfter == maxBefore )
75
76    def test_005_setsBothLimits(self):
77        newRange = (50.5, 75.0)
78        self.cb.range = newRange
79        self.cb._setLimits(self.sm)
80        (minAfter, maxAfter) = self.sm.get_clim()
81        assert(minAfter == newRange[0])
82        assert(maxAfter == newRange[1])
83
84    def test_006_setsNeitherLimit(self):
85        newRange = (None, None)
86        self.cb.range = newRange
87        (minBefore, maxBefore) = self.sm.get_clim()
88        self.cb._setLimits(self.sm)
89        (minAfter, maxAfter) = self.sm.get_clim()
90        assert(minBefore == minAfter)
91        assert(maxBefore == maxAfter)
92
93    def test_007_raisesErrorWhenMinGraterThanMax(self):
94        newRange = (10.0, 5.0)
95        nose.tools.assert_raises(ValueError, setattr, self.cb, 'range', newRange)
96
97    def test_008_raisesErrorWhenMinGraterThanMax(self):
98        newRange = (10.0, 5.0)
99        nose.tools.assert_raises(ValueError, setattr, self.cb, 'range', newRange)
100
101    def test_009_raisesErrorWhenMinEqualsMax(self):
102        newRange = (10.0, 10.0)
103        nose.tools.assert_raises(ValueError, setattr, self.cb, 'range', newRange)
104
105    def test_010_raisesErrorWhenRangeNotListOrTuple(self):
106        newRange = "1, 10"
107        nose.tools.assert_raises(ValueError, setattr, self.cb, 'range', newRange)
108
109    def test_011_raisesErrorWhenRangeNotLength2(self):
110        newRange = (10.0, 5.0, 2.5)
111        nose.tools.assert_raises(ValueError, setattr, self.cb, 'range', newRange)
112
113    #position ticks (if
114    def test_012_repositionsTicksToDiscreteCmapBounds(self):
115        #create a 6 element discreet colour map
116        cmap = geoplot.utils.generateDiscreteCmap(
117           [(0,0,0), (20,20,20), (40,40,40), (60,60,60), (80,80,80)], "temp")
118        #create a mock colour bar, decided not to use a real one as it would
119        #involve setting up a figure and an axis
120        mcb = MockColourBar(0,6,cmap)
121
122        #make sure the max ticks is 10
123        geoplot.colour_bar.MAX_CBAR_TICKS = 10
124
125        ColourBar._repositionColourBarTicks(mcb)
126
127        #check the formatter and the locator have been set correctly and that
128        #the .draw_all function has been called
129        assert(mcb.locator.__class__ == matplotlib.ticker.FixedLocator)
130        assert(mcb.formatter.__class__ == matplotlib.ticker.FormatStrFormatter)
131        assert(mcb.redrawn == True)
132        assert(geoplot.tests.test_utils._areNumericListsEqual(mcb.locator.locs,
133                                                [0.0, 1.2, 2.4, 3.6, 4.8, 6.0]))
134       
135
136    def test_013_raiseErrorWhenSettingInvalidPositionValue(self):
137        nose.tools.assert_raises(ValueError, setattr, self.cb, 'position', 'sideways')
138
139
140class MockColourBar:
141    def __init__(self, vmin, vmax, cmap):
142        self.vmin = vmin
143        self.vmax = vmax
144        self.cmap = cmap
145        self.locator = None
146        self.formatter = None
147        self.redrawn = False
148        self.outline = MockOutline()
149        self.ax = MockAxes()
150
151    def draw_all(self):
152        self.redrawn = True
153
154class MockAxes(object):
155    def __init__(self):
156        self.yaxis = MockAxis()
157        self.xaxis = MockAxis()
158
159class MockOutline(object):
160    def __init__(self):
161        self.linewidth = None
162       
163    def set_linewidth(self, value):
164        self.linewidth = value
165
166class MockAxis(object):
167    def __init__(self):
168        self._tickPosition = "default"
169   
170    def get_ticks_position(self):
171        return self._tickPosition
172
173    def set_ticks_position(self, val):
174        self._tickPosition = val
175if __name__ == '__main__':
176
177    import geoplot.log_util
178
179    geoplot.log_util.setGeoplotHandlerToStdOut()
180
181    nose.runmodule()
182   
Note: See TracBrowser for help on using the repository browser.