source: qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_utils.py @ 5612

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

Have added some code that allows the grid drawer to decide if it should use imshow or pcolormesh to render the grid. This was done to get arround the problem of the grid cells having outlines when pcolormesh is used.

Also implemented a map function that chooses which basemap resolution to use if one isn't set.

Line 
1#/urs/bin/env python
2"""
3test_utils.py
4"""
5
6# Import python modules
7
8# Import third-party software
9import nose
10import nose.tools as nt
11import matplotlib
12
13import geoplot.utils
14
15class Test_ScaleRGBList(object):
16
17    def setUp(self):
18        pass
19
20    def tearDown(self):
21        pass
22
23    def testScalesRGBListOfInts(self):
24        l = [(63,255,0), (85,85,85), (120,1,14), (0,0,0)]
25        scaledList = geoplot.utils._scaleRGBList(l)
26        expectedResults4db = [(0.2471, 1.0000, 0.0000),
27                              (0.3333, 0.3333, 0.3333),
28                              (0.4706, 0.0039, 0.0549),
29                              (0.0000, 0.0000, 0.0000)]
30        for i in range(len(l)):
31            print scaledList[i], " - ", expectedResults4db[i]
32            assert (round(scaledList[i][0],4) == expectedResults4db[i][0]) # red
33            assert (round(scaledList[i][1],4) == expectedResults4db[i][1]) # green
34            assert (round(scaledList[i][2],4) == expectedResults4db[i][2]) # blue
35
36    def testRaisesExceptionWhenRGBValueLargetThan255(self):
37        l = [(63,255,0), (85,85,85), (120,320,14), (0,0,0)]
38        nose.tools.assert_raises(Exception, geoplot.utils._scaleRGBList, l)
39
40class TestGenerateDiscreteCmap(object):
41
42    def setUp(self):
43        self.l = [(63,255,0), (85,85,85), (120,1,14), (0,0,0)]
44        self.name = 'cmap_name'
45        self.cmap = geoplot.utils.generateDiscreteCmap(self.l, self.name)
46
47    def tearDown(self):
48        self.cmap = None
49        self.name = None
50        self.l = None
51
52    def testCreatesCmapAttributes(self):
53        assert(self.cmap.__class__ == matplotlib.colors.ListedColormap)
54        assert(self.cmap.name == self.name)
55        assert(self.cmap._isinit == False)
56        assert(self.cmap.N == len(self.l))
57
58    def testCreatedCmapColors(self):
59        scaledList = geoplot.utils._scaleRGBList(self.l)
60        assert(scaledList == self.cmap.colors)
61       
62class TestRoundToN(object):
63
64    def setUp(self):
65        pass
66
67    def tearDown(self):
68        pass
69
70    def testRoundToN(self):
71        n = 4
72        for value, answer in [(1.23451, 1.235), 
73                              (111111111.0, 111100000.0),
74                              (0.0000000012321, 0.000000001232),
75                               ]:
76            recieved = geoplot.utils.round_to_n(value, n)
77            nt.assert_equal(answer, recieved)
78
79
80# Magic to run tests if executed as a script
81if __name__ == '__main__':
82
83    nose.runmodule()
Note: See TracBrowser for help on using the repository browser.