@@ -75,6 +75,60 @@ def test_colormap_endian():
75
75
assert_array_equal (cmap (anative ), cmap (aforeign ))
76
76
77
77
78
+ def test_colormap_invalid ():
79
+ """
80
+ Github issue #9892: Handling of nan's were getting mapped to under
81
+ rather than bad. This tests to make sure all invalid values
82
+ (-inf, nan, inf) are mapped respectively to (under, bad, over).
83
+ """
84
+ cmap = cm .get_cmap ("plasma" )
85
+ x = np .array ([- np .inf , - 1 , 0 , np .nan , .7 , 2 , np .inf ])
86
+
87
+ expected = np .array ([[0.050383 , 0.029803 , 0.527975 , 1. ],
88
+ [0.050383 , 0.029803 , 0.527975 , 1. ],
89
+ [0.050383 , 0.029803 , 0.527975 , 1. ],
90
+ [0. , 0. , 0. , 0. ],
91
+ [0.949217 , 0.517763 , 0.295662 , 1. ],
92
+ [0.940015 , 0.975158 , 0.131326 , 1. ],
93
+ [0.940015 , 0.975158 , 0.131326 , 1. ]])
94
+ assert_array_equal (cmap (x ), expected )
95
+
96
+ # Test masked representation (-inf, inf) are now masked
97
+ expected = np .array ([[0. , 0. , 0. , 0. ],
98
+ [0.050383 , 0.029803 , 0.527975 , 1. ],
99
+ [0.050383 , 0.029803 , 0.527975 , 1. ],
100
+ [0. , 0. , 0. , 0. ],
101
+ [0.949217 , 0.517763 , 0.295662 , 1. ],
102
+ [0.940015 , 0.975158 , 0.131326 , 1. ],
103
+ [0. , 0. , 0. , 0. ]])
104
+ assert_array_equal (cmap (np .ma .masked_invalid (x )), expected )
105
+
106
+ # Test scalar representations
107
+ assert_array_equal (cmap (- np .inf ), cmap (0 ))
108
+ assert_array_equal (cmap (np .inf ), cmap (1.0 ))
109
+ assert_array_equal (cmap (np .nan ), np .array ([0. , 0. , 0. , 0. ]))
110
+
111
+
112
+ def test_colormap_return_types ():
113
+ """
114
+ Make sure that tuples are returned for scalar input and
115
+ that the proper shapes are returned for ndarrays.
116
+ """
117
+ cmap = cm .get_cmap ("plasma" )
118
+ # Test return types and shapes
119
+ # scalar input needs to return a tuple of length 4
120
+ assert isinstance (cmap (0.5 ), tuple )
121
+ assert len (cmap (0.5 )) == 4
122
+
123
+ # input array returns an ndarray of shape x.shape + (4,)
124
+ x = np .ones (4 )
125
+ assert cmap (x ).shape == x .shape + (4 ,)
126
+
127
+ # multi-dimensional array input
128
+ x2d = np .zeros ((2 , 2 ))
129
+ assert cmap (x2d ).shape == x2d .shape + (4 ,)
130
+
131
+
78
132
def test_BoundaryNorm ():
79
133
"""
80
134
Github issue #1258: interpolation was failing with numpy
0 commit comments