source: qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_grid_builder_lat_lon_2.py @ 5835

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

Improved the performance of the getUniqueValues method on the grid, also cached the result to avoid the cost of re-calculation.

Line 
1#/urs/bin/env python
2"""
3test_grid_builder_base.py
4"""
5import logging
6
7import nose
8import cdms2 as cdms
9import numpy
10from mock import Mock
11
12import nose.tools as nt
13
14from geoplot.grid_builder_lat_lon import GridBuilderLatLon
15
16
17class Test_GridBuidlerLatLon_ResizeVar(object):
18   
19    # Need to test the re-sizing of the axis works
20
21    def setUp(self):
22        self.lonVals = numpy.linspace(-6, 2, 17, endpoint=True)
23        self.latVals = numpy.linspace(-45, 60, 15, endpoint=True)
24
25        self.xLimits = (-4,-1.5)
26        self.yLimits = (-40, 53)
27       
28        self.subsetLonVals = numpy.linspace(-4, 1.5, 12, endpoint=True)
29        self.subsetLatVals = numpy.linspace(-45, 52.5, 14, endpoint=True)
30   
31    def buildMockSubsetVar(self):
32        #because mock dosen't mock magic methods creating transient axis
33        self.lonAxis = cdms.axis.TransientAxis(self.lonVals)
34        self.lonAxis.isCircular = lambda : True
35        #print self.lonAxis.isCircular()
36        self.latAxis = cdms.axis.TransientAxis(self.latVals)
37       
38
39        self.subsetLonAxis = cdms.axis.TransientAxis(self.subsetLonVals)
40        self.subsetLonAxis.isCircular = lambda : True
41        self.subsetLatAxis = cdms.axis.TransientAxis(self.subsetLatVals)
42       
43        self.mockVariable = _buildMockCDMSVariable(self.lonVals, self.latVals)
44        self.mockSubsetVar = _buildMockCDMSVariable(self.subsetLonVals, self.subsetLatVals)
45        self.mockVariable._return_value = self.mockSubsetVar       
46               
47    def test_001_checkSelectionIsMade(self):
48        self.buildMockSubsetVar()
49        gridBuilder = GridBuilderLatLon(self.mockVariable)
50       
51        subsetVar = gridBuilder._resizeVar(self.xLimits, self.yLimits)
52       
53        nt.assert_true(self.mockVariable.called)
54        nt.assert_equal(self.mockVariable.call_count, 1)
55        nt.assert_equal(self.mockVariable.call_args, ((), {
56                'longitude':(self.xLimits[0], self.xLimits[1], 'cce'), 
57                'latitude' :(self.yLimits[0], self.yLimits[1], 'cce')}))
58       
59        #check that the subset var is returned
60        nt.assert_equal(id(subsetVar), id(self.mockSubsetVar))
61   
62    def test_002_selectionChnagesForHighToLowLongitude(self):
63       
64        # reverse the values
65        self.lonVals = self.lonVals[::-1] 
66       
67        self.buildMockSubsetVar()
68       
69        gridBuilder = GridBuilderLatLon(self.mockVariable)
70        subsetVar = gridBuilder._resizeVar(self.xLimits, self.yLimits)
71
72        nt.assert_equal(self.mockVariable.call_args, ((), {
73                'longitude':(self.xLimits[1], self.xLimits[0], 'cce'), 
74                'latitude' :(self.yLimits[0], self.yLimits[1], 'cce')}))
75
76    def test_003_selectionChnagesForHighToLowLatitude(self):
77       
78        # reverse the values
79        self.latVals = self.latVals[::-1] 
80       
81        self.buildMockSubsetVar()
82       
83        gridBuilder = GridBuilderLatLon(self.mockVariable)
84        subsetVar = gridBuilder._resizeVar(self.xLimits, self.yLimits)
85
86        nt.assert_equal(self.mockVariable.call_args, ((), {
87                'longitude':(self.xLimits[0], self.xLimits[1], 'cce'), 
88                'latitude' :(self.yLimits[1], self.yLimits[0], 'cce')}))
89
90    def test_004_selectionChnagesForHighToLowLonAndLat(self):
91       
92        # reverse the values
93        self.latVals = self.latVals[::-1]
94        self.lonVals = self.lonVals[::-1] 
95       
96        self.buildMockSubsetVar()
97       
98        gridBuilder = GridBuilderLatLon(self.mockVariable)
99        subsetVar = gridBuilder._resizeVar(self.xLimits, self.yLimits)
100
101        nt.assert_equal(self.mockVariable.call_args, ((), {
102                'longitude':(self.xLimits[1], self.xLimits[0], 'cce'), 
103                'latitude' :(self.yLimits[1], self.yLimits[0], 'cce')}))
104
105    def test_005_returnsOriginalVariableOnException(self):
106        self.buildMockSubsetVar()
107       
108        self.mockVariable.side_effect = Exception("Subsetting Exception")
109       
110        gridBuilder = GridBuilderLatLon(self.mockVariable)
111       
112        subsetVar = gridBuilder._resizeVar(self.xLimits, self.yLimits)
113       
114        #check that the original var is returned
115        nt.assert_equal(id(subsetVar), id(self.mockVariable))
116       
117class Test_GridBuidlerLatLon_isAxisOrderLonLat(object):
118   
119    def setUp(self):
120        self.lonVals = numpy.linspace(-6, 2, 17, endpoint=True)
121        self.latVals = numpy.linspace(-45, 60, 15, endpoint=True)
122       
123    def test_001_findsLatLonOrder(self):
124        self.mockVariable = _buildMockCDMSVariable(self.lonVals, self.latVals, 
125                                                   orderLonLat=True)
126       
127        nt.assert_false(GridBuilderLatLon._areAxisInOrderYX(self.mockVariable))
128       
129    def test_002_findsLonLatOrder(self):
130        self.mockVariable = _buildMockCDMSVariable(self.lonVals, self.latVals, 
131                                                   orderLonLat=False)
132       
133        nt.assert_true(GridBuilderLatLon._areAxisInOrderYX(self.mockVariable))       
134
135class Test_GridBuidlerLatLon_buildGridValues(object):
136
137    #try and test that the values masked array built by the grid is correct
138
139    def setUp(self):
140        self.lonVals = numpy.linspace(-6, 2, 17, endpoint=True)
141        self.latVals = numpy.linspace(-45, 60, 15, endpoint=True)
142       
143    def buildMockVariable(self, orderLonLat, getMissing=None):
144       
145        self.mockVariable = _buildMockCDMSVariable(self.lonVals, self.latVals,
146                                                   orderLonLat=orderLonLat)
147        #the variable data is set up in the order of the axis
148        if orderLonLat:
149            variableData = numpy.zeros((len(self.lonVals), len(self.latVals)))
150        else:
151            variableData = numpy.zeros((len(self.latVals), len(self.lonVals)))
152       
153        self.mockVariable.getValue._return_value = variableData
154        self.mockVariable.getMissing._return_value = getMissing       
155
156    def test_001_buildsDataForLatLon(self):
157       
158        orderLonLat = False
159        self.buildMockVariable(orderLonLat)
160               
161        builder = GridBuilderLatLon(self.mockVariable)
162       
163        #assuming no subsetting was needed so just pass the variable used in the constructor
164        data = builder._buildGridValues(self.mockVariable)
165       
166        # the data should be in the order lat/lon (this is needed for imshow to work)
167        nt.assert_equal(data.shape, (len(self.latVals), len(self.lonVals)))       
168       
169    def test_001_buildsDataForLonLat(self):
170       
171        orderLonLat = True
172        self.buildMockVariable(orderLonLat)
173                     
174        builder = GridBuilderLatLon(self.mockVariable)
175       
176        #assuming no subsetting was needed so just pass the variable used in the constructor
177        data = builder._buildGridValues(self.mockVariable)
178       
179        # the data should be in the order lat/lon (this is needed for imshow to work)
180        nt.assert_equal(data.shape, (len(self.latVals), len(self.lonVals)))
181   
182
183def _buildMockCDMSVariable(lonVals, latVals, orderLonLat=False):
184    mockVariable = Mock(spec=cdms.tvariable.TransientVariable)
185
186    lonAxis = cdms.axis.TransientAxis(lonVals)
187    lonAxis.isCircular = lambda : True
188    latAxis = cdms.axis.TransientAxis(latVals)
189
190    mockVariable.getLongitude = Mock(return_value=lonAxis)
191    mockVariable.getLatitude = Mock(return_value=latAxis)
192           
193    if orderLonLat:
194        mockVariable.getAxisList._return_value = [lonAxis, latAxis]
195        mockVariable.getAxisIds._return_value = ['longitude','latitude']
196    else:
197        mockVariable.getAxisList._return_value = [latAxis, lonAxis]
198        mockVariable.getAxisIds._return_value = ['latitude','longitude']
199       
200    def getAxisIndex(axis):
201        assert axis in [lonAxis, latAxis]
202
203        if orderLonLat:
204            lonIndex, latIndex = 0,1
205        else:
206            lonIndex, latIndex = 1,0
207           
208        if axis == lonAxis:
209            return lonIndex
210        else:
211            return latIndex
212       
213    mockVariable.getAxisIndex = Mock(side_effect=getAxisIndex)
214       
215    mockVariable.getLevel = Mock(return_value=None)
216    mockVariable.getTime = Mock(return_value=None)
217    mockVariable.id = 'var'
218
219    mockVariable.reset_mock()
220    return mockVariable       
221       
222if __name__ == '__main__':
223
224    nose.runmodule()
Note: See TracBrowser for help on using the repository browser.