9
9
import base64
10
10
import platform
11
11
12
+ from numpy .lib import recfunctions as rfn
12
13
from numpy .testing import assert_array_equal , assert_array_almost_equal
13
14
14
15
from matplotlib import cbook , cm
@@ -1891,7 +1892,7 @@ def test_close_error_name():
1891
1892
matplotlib .colormaps ["grays" ]
1892
1893
1893
1894
1894
- def test_multi_norm ():
1895
+ def test_multi_norm_creation ():
1895
1896
# tests for mcolors.MultiNorm
1896
1897
1897
1898
# test wrong input
@@ -1911,13 +1912,24 @@ def test_multi_norm():
1911
1912
match = "Invalid norm str name" ):
1912
1913
mcolors .MultiNorm (["None" ])
1913
1914
1915
+ norm = mpl .colors .MultiNorm (['linear' , 'linear' ])
1916
+
1917
+
1918
+ def test_multi_norm_call_vmin_vmax ():
1914
1919
# test get vmin, vmax
1915
1920
norm = mpl .colors .MultiNorm (['linear' , 'log' ])
1916
1921
norm .vmin = 1
1917
1922
norm .vmax = 2
1918
1923
assert norm .vmin == (1 , 1 )
1919
1924
assert norm .vmax == (2 , 2 )
1920
1925
1926
+
1927
+ def test_multi_norm_call_clip_inverse ():
1928
+ # test get vmin, vmax
1929
+ norm = mpl .colors .MultiNorm (['linear' , 'log' ])
1930
+ norm .vmin = 1
1931
+ norm .vmax = 2
1932
+
1921
1933
# test call with clip
1922
1934
assert_array_equal (norm ([3 , 3 ], clip = False ), [2.0 , 1.584962500721156 ])
1923
1935
assert_array_equal (norm ([3 , 3 ], clip = True ), [1.0 , 1.0 ])
@@ -1933,6 +1945,9 @@ def test_multi_norm():
1933
1945
# test inverse
1934
1946
assert_array_almost_equal (norm .inverse ([0.5 , 0.5849625007211562 ]), [1.5 , 1.5 ])
1935
1947
1948
+
1949
+ def test_multi_norm_autoscale ():
1950
+ norm = mpl .colors .MultiNorm (['linear' , 'log' ])
1936
1951
# test autoscale
1937
1952
norm .autoscale ([[0 , 1 , 2 , 3 ], [0.1 , 1 , 2 , 3 ]])
1938
1953
assert_array_equal (norm .vmin , [0 , 0.1 ])
@@ -1945,3 +1960,109 @@ def test_multi_norm():
1945
1960
assert_array_equal (norm ([5 , 0 ]), [1 , 0.5 ])
1946
1961
assert_array_equal (norm .vmin , (0 , - 50 ))
1947
1962
assert_array_equal (norm .vmax , (5 , 50 ))
1963
+
1964
+
1965
+ def test_mult_norm_call_types ():
1966
+ mn = mpl .colors .MultiNorm (['linear' , 'linear' ])
1967
+ mn .vmin = - 2
1968
+ mn .vmax = 2
1969
+
1970
+ vals = np .arange (6 ).reshape ((3 ,2 ))
1971
+ target = np .ma .array ([(0.5 , 0.75 ),
1972
+ (1. , 1.25 ),
1973
+ (1.5 , 1.75 )])
1974
+
1975
+ # test structured array as input
1976
+ structured_target = rfn .unstructured_to_structured (target )
1977
+ from_mn = mn (rfn .unstructured_to_structured (vals ))
1978
+ assert from_mn .dtype == structured_target .dtype
1979
+ assert_array_almost_equal (rfn .structured_to_unstructured (from_mn ),
1980
+ rfn .structured_to_unstructured (structured_target ))
1981
+
1982
+ # test list of arrays as input
1983
+ assert_array_almost_equal (mn (list (vals .T )),
1984
+ list (target .T ))
1985
+ # test list of floats as input
1986
+ assert_array_almost_equal (mn (list (vals [0 ])),
1987
+ list (target [0 ]))
1988
+ # test tuple of arrays as input
1989
+ assert_array_almost_equal (mn (tuple (vals .T )),
1990
+ list (target .T ))
1991
+
1992
+
1993
+ # test setting structured_output true/false:
1994
+ # structured input, structured output
1995
+ from_mn = mn (rfn .unstructured_to_structured (vals ), structured_output = True )
1996
+ assert from_mn .dtype == structured_target .dtype
1997
+ assert_array_almost_equal (rfn .structured_to_unstructured (from_mn ),
1998
+ rfn .structured_to_unstructured (structured_target ))
1999
+ # structured input, list as output
2000
+ from_mn = mn (rfn .unstructured_to_structured (vals ), structured_output = False )
2001
+ assert_array_almost_equal (from_mn ,
2002
+ list (target .T ))
2003
+ # list as input, structured output
2004
+ from_mn = mn (list (vals .T ), structured_output = True )
2005
+ assert from_mn .dtype == structured_target .dtype
2006
+ assert_array_almost_equal (rfn .structured_to_unstructured (from_mn ),
2007
+ rfn .structured_to_unstructured (structured_target ))
2008
+ # list as input, list as output
2009
+ from_mn = mn (list (vals .T ), structured_output = False )
2010
+ assert_array_almost_equal (from_mn ,
2011
+ list (target .T ))
2012
+
2013
+ # test with NoNorm, list as input
2014
+ mn_no_norm = mpl .colors .MultiNorm (['linear' , mcolors .NoNorm ()])
2015
+ no_norm_out = mn_no_norm (list (vals .T ))
2016
+ assert_array_almost_equal (no_norm_out ,
2017
+ [[0. , 0.5 , 1. ],
2018
+ [1 , 3 , 5 ]])
2019
+ assert no_norm_out [0 ].dtype == np .dtype ('float64' )
2020
+ assert no_norm_out [1 ].dtype == np .dtype ('int64' )
2021
+
2022
+ # test with NoNorm, structured array as input
2023
+ mn_no_norm = mpl .colors .MultiNorm (['linear' , mcolors .NoNorm ()])
2024
+ no_norm_out = mn_no_norm (rfn .unstructured_to_structured (vals ))
2025
+ assert_array_almost_equal (rfn .structured_to_unstructured (no_norm_out ),
2026
+ np .array (\
2027
+ [[0. , 0.5 , 1. ],
2028
+ [1 , 3 , 5 ]]).T )
2029
+ assert no_norm_out .dtype ['f0' ] == np .dtype ('float64' )
2030
+ assert no_norm_out .dtype ['f1' ] == np .dtype ('int64' )
2031
+
2032
+ # test single int as input
2033
+ with pytest .raises (ValueError ,
2034
+ match = "Input of type <class 'int'> is not supported" ):
2035
+ mn (1 )
2036
+
2037
+ # test list of incompatible size
2038
+ with pytest .raises (ValueError ,
2039
+ match = "A <class 'list'> of length 3 is not compatible" ):
2040
+ mn ([3 , 2 , 1 ])
2041
+
2042
+ # np.arrays of shapes that can be converted:
2043
+ for data in [np .zeros (2 ), np .zeros ((2 ,3 )), np .zeros ((2 ,3 ,3 ))]:
2044
+ with pytest .raises (ValueError ,
2045
+ match = r"You can use `list\(data\)` to convert" ):
2046
+ mn (data )
2047
+
2048
+ for data in [np .zeros ((3 , 2 )), np .zeros ((3 , 3 , 2 ))]:
2049
+ with pytest .raises (ValueError ,
2050
+ match = r"You can use `rfn.unstructured_to_structured" ):
2051
+ mn (data )
2052
+
2053
+ # np.ndarray that can be converted, but unclear if first or last axis
2054
+ for data in [np .zeros ((2 , 2 )), np .zeros ((2 , 3 , 2 ))]:
2055
+ with pytest .raises (ValueError ,
2056
+ match = "An np.ndarray of shape" ):
2057
+ mn (data )
2058
+
2059
+ # incompatible arrays where no relevant axis matches
2060
+ for data in [np .zeros (3 ), np .zeros ((3 , 2 , 3 ))]:
2061
+ with pytest .raises (ValueError ,
2062
+ match = r"An np.ndarray of shape" ):
2063
+ mn (data )
2064
+
2065
+ # test incompatible class
2066
+ with pytest .raises (ValueError ,
2067
+ match = "Input of type <class 'str'> is not supported" ):
2068
+ mn ("An object of incompatible class" )
0 commit comments