Ignore:
Timestamp:
04/12/09 16:07:45 (11 years ago)
Author:
pnorton
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_colour_scheme_builder.py

    r5946 r6089  
    44""" 
    55 
     6import logging 
     7 
    68import nose 
    79import numpy 
     
    1012import nose.tools as nt 
    1113import matplotlib 
     14import geoplot 
    1215from geoplot.colour_scheme import ColourSchemeBuilder, _ConinuousSchemeBuilder, _IntervalSchemeBuidler 
     16from geoplot.colour_scheme_intervals import ColourSchemeInterval 
    1317from geoplot.grid import Grid 
    1418from geoplot.range import Range 
    1519import geoplot.colour_scheme 
     20 
     21log = logging.getLogger(__name__) 
    1622 
    1723class Test_ColourSchemeBuilder(object): 
     
    7379    @patch('geoplot.colour_scheme._ConinuousSchemeBuilder', spec=True) 
    7480    @patch('geoplot.colour_scheme._IntervalSchemeBuidler', spec=True) 
    75     def test_003_usesCorrectSchemeBuilders(self, mockIntBuilder, mockContBuilder): 
     81    def test_003_usesCorrectSchemeBuilders(self,  
     82                                        mockIntSchemeBuilder, mockContSchemeBuilder): 
    7683         
    7784        # make sure that the right scheme builder classes are used to  
     
    8491        self.builder.intervalNames = None 
    8592        mockGrid = Mock(spec=Grid) 
     93        mockIntBuilder = Mock(spec=geoplot.colour_scheme_intervals.ColourSchemeIntervalBuilder) 
     94        mockIntBuilder.buildCSInterval.return_value = Mock(spec=geoplot.colour_scheme_intervals.ColourSchemeInterval) 
     95         
     96        self.builder._intervalBuilder = mockIntBuilder  
    8697         
    8798        # when intervalColourbar is false should use the _ConinuousSchemeBuilder  
    8899        self.builder.intervalColourbar = False 
    89100         
    90         scheme = self.builder.buildScheme(mockGrid) 
    91          
    92         nt.assert_equal(mockContBuilder.call_count, 1) 
    93         nt.assert_equal(mockIntBuilder.call_count, 0) 
    94         nt.assert_equals(mockContBuilder.call_args, ((Range(10, 100), self.builder.colourBarScale, self.builder.cmap, ),{})) 
     101        scheme = self.builder.buildScheme('continuous', mockGrid) 
     102         
     103        nt.assert_equal(mockContSchemeBuilder.call_count, 1) 
     104        nt.assert_equal(mockIntSchemeBuilder.call_count, 0) 
     105        nt.assert_equals(mockContSchemeBuilder.call_args, ((Range(10, 100), self.builder.colourBarScale, self.builder.cmap, ),{})) 
    95106 
    96107        # check the build command was called on the object returned from the class 
    97         nt.assert_equal(mockContBuilder.return_value.buildScheme.call_count, 1) 
    98         nt.assert_equals(mockContBuilder.return_value.buildScheme.call_args, ((mockGrid, ),{})) 
     108        nt.assert_equal(mockContSchemeBuilder.return_value.buildScheme.call_count, 1) 
     109        nt.assert_equals(mockContSchemeBuilder.return_value.buildScheme.call_args, ((),{})) 
    99110         
    100111        # check the scheme is the result of this call 
    101         nt.assert_equal(mockContBuilder.return_value.buildScheme.return_value, scheme) 
    102          
    103         mockIntBuilder.reset_mock() 
    104         mockContBuilder.reset_mock() 
     112        nt.assert_equal(mockContSchemeBuilder.return_value.buildScheme.return_value, scheme) 
     113         
     114        mockIntSchemeBuilder.reset_mock() 
     115        mockContSchemeBuilder.reset_mock() 
    105116         
    106117        # when intervalColourbar is true use the _IntervalSchemeBuidler 
    107118         
    108         self.builder.intervals = "5,10,15" 
    109          
    110         scheme = self.builder.buildScheme(mockGrid) 
    111          
    112         nt.assert_equal(mockIntBuilder.call_count, 1) 
    113         nt.assert_equal(mockContBuilder.call_count, 0) 
    114         nt.assert_equals(mockIntBuilder.call_args, ((Range(10, 100), self.builder.colourBarScale, self.builder.cmap, self.builder.intervals, self.builder.intervalNames ),{})) 
     119        scheme = self.builder.buildScheme('legend',mockGrid) 
     120         
     121        nt.assert_equal(mockIntSchemeBuilder.call_count, 1) 
     122        nt.assert_equal(mockContSchemeBuilder.call_count, 0) 
     123        nt.assert_equals(mockIntSchemeBuilder.call_args,  
     124            ((Range(10, 100),  
     125              self.builder.colourBarScale,  
     126              self.builder.cmap,  
     127              mockIntBuilder.buildCSInterval.return_value ),{})) 
    115128 
    116129        # check the build command was called on the object returned 
    117         nt.assert_equal(mockIntBuilder.return_value.buildScheme.call_count, 1) 
    118         nt.assert_equals(mockIntBuilder.return_value.buildScheme.call_args, ((mockGrid, ),{})) 
     130        nt.assert_equal(mockIntSchemeBuilder.return_value.buildScheme.call_count, 1) 
     131        nt.assert_equals(mockIntSchemeBuilder.return_value.buildScheme.call_args, ((),{})) 
    119132         
    120133        # check the scheme is the result of this call 
    121         nt.assert_equal(mockIntBuilder.return_value.buildScheme.return_value, scheme) 
     134        nt.assert_equal(mockIntSchemeBuilder.return_value.buildScheme.return_value, scheme) 
    122135                 
    123136         
     
    152165        self.builder = _ConinuousSchemeBuilder(Range(10, 100), 'linear' , 
    153166                                               Mock(spec=matplotlib.colors.LinearSegmentedColormap)) 
    154         self.mockGrid = Mock(spec=Grid) 
    155167     
    156168    def test_001_returnedSchemeIsContinuouse(self): 
    157169         
    158         scheme = self.builder.buildScheme(self.mockGrid) 
     170        scheme = self.builder.buildScheme() 
    159171        nt.assert_true(isinstance(scheme, geoplot.colour_scheme.ContinuousColourScheme)) 
    160172     
     
    164176        # inital cmap 
    165177         
    166         scheme = self.builder.buildScheme(self.mockGrid) 
     178        scheme = self.builder.buildScheme() 
    167179        nt.assert_equal(scheme.colourMap, self.builder.initialCmap) 
    168180        nt.assert_equal(scheme.colourMap.method_calls, []) 
     
    173185        # min/max is set to None 
    174186         
    175         self.mockGrid.values = numpy.array([1.0,2.0,200.0]) 
    176         self.mockGrid.getMinValue.return_value = self.mockGrid.values.min() 
    177         self.mockGrid.getMaxValue.return_value = self.mockGrid.values.max() 
    178          
    179         for cbMin, cbMax in ( (None, None), (10,None), (None, 10), (10, 100)): 
    180             self.builder.cbRange = Range(cbMin, cbMax) 
    181             scheme = self.builder.buildScheme(self.mockGrid) 
     187 
     188        self.builder.cbRange = Range(5.0, 10.0) 
     189        scheme = self.builder.buildScheme() 
    182190             
    183             v_min = cbMin if cbMin != None else self.mockGrid.values.min() 
    184             v_max = cbMax if cbMax != None else self.mockGrid.values.max() 
    185              
    186             nt.assert_equal((scheme.norm.vmin, scheme.norm.vmax), (v_min, v_max))                    
     191        nt.assert_equal((scheme.norm.vmin, scheme.norm.vmax), (5.0, 10.0))                    
    187192         
    188193    def test_004_returnsLogNorm(self): 
    189194        self.builder.cbScale = 'log' 
    190         scheme = self.builder.buildScheme(self.mockGrid) 
     195        scheme = self.builder.buildScheme() 
    191196 
    192197        nt.assert_true(isinstance(scheme.norm, matplotlib.colors.LogNorm)) 
     
    195200     
    196201    def setUp(self): 
    197         self.oldMax = geoplot.colour_scheme.MAX_INTERVALS 
    198         geoplot.colour_scheme.MAX_INTERVALS = 20 
     202        interval = ColourSchemeInterval(numpy.array([25.0,35.0,45.0,55.0]),  
     203                                        numpy.array([20.0,30.0,40.0,50.0,60.0]),  
     204                                        ['a','b','c','d']) 
    199205        self.builder = _IntervalSchemeBuidler(Range(10.0, 100.0),  'linear' , 
    200206                                              Mock(spec=matplotlib.colors.LinearSegmentedColormap),  
    201                                               "20,30,40,50,60",  
    202                                               "a,b,c,d") 
    203      
    204     def tearDown(self): 
    205         geoplot.colour_scheme.MAX_INTERVALS = self.oldMax 
     207                                              interval) 
    206208     
    207209    def test_001_returnedCmapForNamedIntervals(self): 
    208210         
    209211        #check that the right number of colours appear in the listed colourmap 
    210         self.builder.intervalBoundsString = "20,30,40,50,60" 
    211         self.builder.cbRange = Range(None, None) 
    212212         
    213213        scheme = self.builder.buildScheme() 
     
    215215        nt.assert_equal(scheme.colourMap.N, 4) 
    216216        nt.assert_equal(scheme.colourMap.__class__, matplotlib.colors.ListedColormap) 
    217  
    218  
    219     def test_002_returnedNormForNamedIntervals(self): 
    220  
    221         #test that the normalisation is correct for the named bounds 
    222         self.builder.intervalLabelsString = None 
    223         self.builder.intervalBoundsString = "2.5,7.5,12.5,17.5,22.5" 
    224         self.builder.cbRange = Range(None, None) 
    225          
    226         scheme = self.builder.buildScheme() 
    227          
    228         expectedBounds = numpy.array([2.5, 7.5, 12.5, 17.5, 22.5]) 
    229         nt.assert_true((scheme.norm.boundaries ==  expectedBounds).all()) 
    230          
    231         #add in the cmap min/max 
    232          
    233         self.builder.cbRange = Range(7.5,13.0) 
    234          
    235         scheme = self.builder.buildScheme() 
    236         print scheme.norm.boundaries 
    237         expectedBounds = numpy.array([7.5, 12.5, 17.5]) 
    238         nt.assert_true((scheme.norm.boundaries ==  expectedBounds).all())         
    239  
    240     def test_003_returnIntervalCmapUntouched(self): 
     217    
     218 
     219    def test_002_returnIntervalCmapUntouched(self): 
    241220         
    242221        colours = [(0.4, 0.4, 0.5, 1.0), 
     
    250229        nt.assert_equal(scheme.colourMap.name, "test_colourmap") 
    251230     
    252     def test_004_returnedLabelsCorrect(self): 
     231    def test_003_returnedLabelsCorrect(self): 
    253232         
    254233        #labels for the unique values 
    255         self.builder.intervalLabelsString = None 
    256         self.builder.intervalBoundsString = None 
    257         self.builder.cbRange = Range(None, None) 
     234        self.builder.csInterval.bounds = numpy.array([5.0,15.0,25.0,35.0]) 
     235        self.builder.csInterval.midpoints = numpy.array([10.0,20.0,30.0]) 
     236        self.builder.csInterval.labels = ['5.0 - 15.0', '15.0 - 25.0', '25.0 - 35.0'] 
     237        self.builder.cbRange = Range(0, 100) 
    258238 
    259239        #labels for specified bounds 
    260         self.builder.intervalBoundsString = "5,15,25,35" 
    261         scheme = self.builder.buildScheme() 
    262          
     240        scheme = self.builder.buildScheme() 
    263241        nt.assert_equal(scheme.labels, ['5.0 - 15.0', '15.0 - 25.0', '25.0 - 35.0']) 
    264242 
    265243        #labels for given bounds 
    266         self.builder.intervalLabelsString = "one,two,three" 
     244        self.builder.csInterval.labels = ["one","two","three"] 
    267245        scheme = self.builder.buildScheme() 
    268246         
Note: See TracChangeset for help on using the changeset viewer.