GitHub issue #1258: interpolation was failing with numpy 1.7 pre-release.
()
| 261 | |
| 262 | |
| 263 | def test_BoundaryNorm(): |
| 264 | """ |
| 265 | GitHub issue #1258: interpolation was failing with numpy |
| 266 | 1.7 pre-release. |
| 267 | """ |
| 268 | |
| 269 | boundaries = [0, 1.1, 2.2] |
| 270 | vals = [-1, 0, 1, 2, 2.2, 4] |
| 271 | |
| 272 | # Without interpolation |
| 273 | expected = [-1, 0, 0, 1, 2, 2] |
| 274 | ncolors = len(boundaries) - 1 |
| 275 | bn = mcolors.BoundaryNorm(boundaries, ncolors) |
| 276 | assert_array_equal(bn(vals), expected) |
| 277 | |
| 278 | # ncolors != len(boundaries) - 1 triggers interpolation |
| 279 | expected = [-1, 0, 0, 2, 3, 3] |
| 280 | ncolors = len(boundaries) |
| 281 | bn = mcolors.BoundaryNorm(boundaries, ncolors) |
| 282 | assert_array_equal(bn(vals), expected) |
| 283 | |
| 284 | # with a single region and interpolation |
| 285 | expected = [-1, 1, 1, 1, 3, 3] |
| 286 | bn = mcolors.BoundaryNorm([0, 2.2], ncolors) |
| 287 | assert_array_equal(bn(vals), expected) |
| 288 | |
| 289 | # more boundaries for a third color |
| 290 | boundaries = [0, 1, 2, 3] |
| 291 | vals = [-1, 0.1, 1.1, 2.2, 4] |
| 292 | ncolors = 5 |
| 293 | expected = [-1, 0, 2, 4, 5] |
| 294 | bn = mcolors.BoundaryNorm(boundaries, ncolors) |
| 295 | assert_array_equal(bn(vals), expected) |
| 296 | |
| 297 | # a scalar as input should not trigger an error and should return a scalar |
| 298 | boundaries = [0, 1, 2] |
| 299 | vals = [-1, 0.1, 1.1, 2.2] |
| 300 | bn = mcolors.BoundaryNorm(boundaries, 2) |
| 301 | expected = [-1, 0, 1, 2] |
| 302 | for v, ex in zip(vals, expected): |
| 303 | ret = bn(v) |
| 304 | assert isinstance(ret, int) |
| 305 | assert_array_equal(ret, ex) |
| 306 | assert_array_equal(bn([v]), ex) |
| 307 | |
| 308 | # same with interp |
| 309 | bn = mcolors.BoundaryNorm(boundaries, 3) |
| 310 | expected = [-1, 0, 2, 3] |
| 311 | for v, ex in zip(vals, expected): |
| 312 | ret = bn(v) |
| 313 | assert isinstance(ret, int) |
| 314 | assert_array_equal(ret, ex) |
| 315 | assert_array_equal(bn([v]), ex) |
| 316 | |
| 317 | # Clipping |
| 318 | bn = mcolors.BoundaryNorm(boundaries, 3, clip=True) |
| 319 | expected = [0, 0, 2, 2] |
| 320 | for v, ex in zip(vals, expected): |