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

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

Fixed a problem with imshow drawing a grid when there was no data to draw. Also fixed some of the tests.

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