source: qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_colour_scheme_interval_builder.py @ 6089

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

Imroved the colour bar code so that a legend colour bar can be used without specifying any intervals.

Line 
1'''
2Created on 30 Nov 2009
3
4@author: pnorton
5'''
6
7import geoplot
8from geoplot.colour_scheme_intervals import ColourSchemeIntervalBuilder
9import matplotlib
10
11from mock import patch, patch_object, Mock, sentinel
12import nose.tools as nt
13from geoplot.range import Range
14import nose
15import geoplot.tests.test_utils as test_utils
16
17class Test_ContinuousColourSchemeIntervalBuilder(object):
18   
19    def setUp(self):
20        self.builder = ColourSchemeIntervalBuilder()
21           
22    def test_001_buildIntervalsFromStrings(self):
23       
24        boundsString = "10,20,30,40"
25        labelsString = "a,b,c"
26       
27        cbInterval = self.builder.buildCSInterval( Range(0,100), boundsString, labelsString)
28       
29        expectedBounds = [10.0, 20.0, 30.0, 40.0]
30        expectedMidpoints = geoplot.utils.getMidpoints(expectedBounds) 
31        expectedLabels = ['a','b','c']
32       
33        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.bounds, expectedBounds), 
34                       "bounds %s didn't match expected %s" % (cbInterval.bounds, expectedBounds))
35
36        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.midpoints, expectedMidpoints), 
37                       "midpoints %s didn't match expected %s" % (cbInterval.midpoints, expectedMidpoints))
38       
39        nt.assert_equal(expectedLabels, cbInterval.labels)
40
41    @patch('geoplot.colour_scheme_intervals.DEFAULT_NUM_INTERVALS',5)
42    def test_002_buildIntervalsFromRange(self):
43        cbInterval = self.builder.buildCSInterval(Range(0,100), None, None)
44
45        expectedBounds = [0.0, 20.0, 40.0, 60.0, 80.0, 100.0]
46        expectedMidpoints = geoplot.utils.getMidpoints(expectedBounds)
47        expectedLabels = ['0.0 - 20.0','20.0 - 40.0','40.0 - 60.0', '60.0 - 80.0', '80.0 - 100.0']
48       
49        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.bounds, expectedBounds), 
50                       "bounds %s didn't match expected %s" % (cbInterval.bounds, expectedBounds))
51
52        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.midpoints, expectedMidpoints), 
53                       "midpoints %s didn't match expected %s" % (cbInterval.midpoints, expectedMidpoints))
54       
55        nt.assert_equal(expectedLabels, cbInterval.labels)
56
57    def test_003_filterBoundsUsingRange(self):
58        boundsString = "10,20,30,40,50"
59
60        cbInterval = self.builder.buildCSInterval(Range(25,45), boundsString, None)
61
62        expectedBounds = [25.0, 30.0, 40.0, 45]
63        expectedMidpoints = geoplot.utils.getMidpoints(expectedBounds)
64       
65
66        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.bounds, expectedBounds), 
67                       "bounds %s didn't match expected %s" % (cbInterval.bounds, expectedBounds))
68
69        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.midpoints, expectedMidpoints), 
70                       "midpoints %s didn't match expected %s" % (cbInterval.midpoints, expectedMidpoints))
71       
72   
73    def test_004_useDefaultLabelsWhenWrongGiven(self):
74        boundsString = "10,20,30,40"
75        labelsString = "a,b,c,d,e,f,g"
76       
77        expectedLabels = ['10.0 - 20.0','20.0 - 30.0','30.0 - 40.0']
78       
79        cbInterval = self.builder.buildCSInterval( Range(0,100), boundsString, labelsString)
80       
81        nt.assert_equal(expectedLabels, cbInterval.labels)
82   
83    def test_005_buildSpecificNumberOfIntervals(self):
84        cbInterval = self.builder.buildCSInterval(Range(0,100), None, None, 11)
85       
86        nt.assert_equal(len(cbInterval.midpoints), 11)
87       
88    def test_006_notEnoughValuesInBoundsString(self):
89        boundsString = "10,"
90        #if the bounds string is incorrect should use the default intervals.
91        cbInterval = self.builder.buildCSInterval(Range(0,100), boundsString, None, 11)
92
93        nt.assert_equal(len(cbInterval.midpoints), 11)
94       
95    def test_007_boundsEntierlyOutsideRangeAreIgnored(self):
96        boundsString = "150,160,"
97        #if the bounds are outside the range then the default should be used
98        cbInterval = self.builder.buildCSInterval(Range(0,100), boundsString, None, 11)
99
100        nt.assert_equal(len(cbInterval.midpoints), 11)
101
102        boundsString = "-10,-4"
103       
104        # if the bounds string is entierly outside the range then should use
105        # default intervals
106        cbInterval = self.builder.buildCSInterval(Range(0,100), boundsString, None, 11)
107
108        nt.assert_equal(len(cbInterval.midpoints), 11)           
109
110    def test_008_boundsPartiallyOutsideRangeAreCorrected(self):
111        boundsString = "10,160,"
112        cbInterval = self.builder.buildCSInterval(Range(0,100), boundsString, None, 11)
113        expectedBounds = [10.0,100.0]
114       
115        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.bounds, expectedBounds), 
116                       "bounds %s didn't match expected %s" % (cbInterval.bounds, expectedBounds))
117
118        boundsString = "-10,50"
119        cbInterval = self.builder.buildCSInterval(Range(0,100), boundsString, None, 11)
120        expectedBounds = [0.0,50.0]
121       
122        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.bounds, expectedBounds), 
123                       "bounds %s didn't match expected %s" % (cbInterval.bounds, expectedBounds)) 
124
125    def test_009_boundsInWrongOrderAreCorrected(self):
126        boundsString = "10,,5,100,"
127        cbInterval = self.builder.buildCSInterval(Range(0,100), boundsString, None, 11)
128        expectedBounds = [5.0,10.0,100.0]
129
130        nt.assert_true(test_utils._areNumericListsEqual(cbInterval.bounds, expectedBounds), 
131                       "bounds %s didn't match expected %s" % (cbInterval.bounds, expectedBounds))
132
133   
134if __name__ == '__main__':
135    print "Running tests in", __file__
136    nose.runmodule()
137   
138       
139   
Note: See TracBrowser for help on using the repository browser.