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
@@ -1881,7 +1882,7 @@ def n_components(self):
1881
1882
axes [1 ,1 ].contourf (r , colorizer = colorizer )
1882
1883
1883
1884
1884
- def test_multi_norm ():
1885
+ def test_multi_norm_creation ():
1885
1886
# tests for mcolors.MultiNorm
1886
1887
1887
1888
# test wrong input
@@ -1901,13 +1902,24 @@ def test_multi_norm():
1901
1902
match = "Invalid norm str name" ):
1902
1903
mcolors .MultiNorm (["None" ])
1903
1904
1905
+ norm = mpl .colors .MultiNorm (['linear' , 'linear' ])
1906
+
1907
+
1908
+ def test_multi_norm_call_vmin_vmax ():
1904
1909
# test get vmin, vmax
1905
1910
norm = mpl .colors .MultiNorm (['linear' , 'log' ])
1906
1911
norm .vmin = 1
1907
1912
norm .vmax = 2
1908
1913
assert norm .vmin == (1 , 1 )
1909
1914
assert norm .vmax == (2 , 2 )
1910
1915
1916
+
1917
+ def test_multi_norm_call_clip_inverse ():
1918
+ # test get vmin, vmax
1919
+ norm = mpl .colors .MultiNorm (['linear' , 'log' ])
1920
+ norm .vmin = 1
1921
+ norm .vmax = 2
1922
+
1911
1923
# test call with clip
1912
1924
assert_array_equal (norm ([3 , 3 ], clip = False ), [2.0 , 1.584962500721156 ])
1913
1925
assert_array_equal (norm ([3 , 3 ], clip = True ), [1.0 , 1.0 ])
@@ -1923,6 +1935,9 @@ def test_multi_norm():
1923
1935
# test inverse
1924
1936
assert_array_almost_equal (norm .inverse ([0.5 , 0.5849625007211562 ]), [1.5 , 1.5 ])
1925
1937
1938
+
1939
+ def test_multi_norm_autoscale ():
1940
+ norm = mpl .colors .MultiNorm (['linear' , 'log' ])
1926
1941
# test autoscale
1927
1942
norm .autoscale ([[0 , 1 , 2 , 3 ], [0.1 , 1 , 2 , 3 ]])
1928
1943
assert_array_equal (norm .vmin , [0 , 0.1 ])
@@ -1935,3 +1950,109 @@ def test_multi_norm():
1935
1950
assert_array_equal (norm ([5 , 0 ]), [1 , 0.5 ])
1936
1951
assert_array_equal (norm .vmin , (0 , - 50 ))
1937
1952
assert_array_equal (norm .vmax , (5 , 50 ))
1953
+
1954
+
1955
+ def test_mult_norm_call_types ():
1956
+ mn = mpl .colors .MultiNorm (['linear' , 'linear' ])
1957
+ mn .vmin = - 2
1958
+ mn .vmax = 2
1959
+
1960
+ vals = np .arange (6 ).reshape ((3 ,2 ))
1961
+ target = np .ma .array ([(0.5 , 0.75 ),
1962
+ (1. , 1.25 ),
1963
+ (1.5 , 1.75 )])
1964
+
1965
+ # test structured array as input
1966
+ structured_target = rfn .unstructured_to_structured (target )
1967
+ from_mn = mn (rfn .unstructured_to_structured (vals ))
1968
+ assert from_mn .dtype == structured_target .dtype
1969
+ assert_array_almost_equal (rfn .structured_to_unstructured (from_mn ),
1970
+ rfn .structured_to_unstructured (structured_target ))
1971
+
1972
+ # test list of arrays as input
1973
+ assert_array_almost_equal (mn (list (vals .T )),
1974
+ list (target .T ))
1975
+ # test list of floats as input
1976
+ assert_array_almost_equal (mn (list (vals [0 ])),
1977
+ list (target [0 ]))
1978
+ # test tuple of arrays as input
1979
+ assert_array_almost_equal (mn (tuple (vals .T )),
1980
+ list (target .T ))
1981
+
1982
+
1983
+ # test setting structured_output true/false:
1984
+ # structured input, structured output
1985
+ from_mn = mn (rfn .unstructured_to_structured (vals ), structured_output = True )
1986
+ assert from_mn .dtype == structured_target .dtype
1987
+ assert_array_almost_equal (rfn .structured_to_unstructured (from_mn ),
1988
+ rfn .structured_to_unstructured (structured_target ))
1989
+ # structured input, list as output
1990
+ from_mn = mn (rfn .unstructured_to_structured (vals ), structured_output = False )
1991
+ assert_array_almost_equal (from_mn ,
1992
+ list (target .T ))
1993
+ # list as input, structured output
1994
+ from_mn = mn (list (vals .T ), structured_output = True )
1995
+ assert from_mn .dtype == structured_target .dtype
1996
+ assert_array_almost_equal (rfn .structured_to_unstructured (from_mn ),
1997
+ rfn .structured_to_unstructured (structured_target ))
1998
+ # list as input, list as output
1999
+ from_mn = mn (list (vals .T ), structured_output = False )
2000
+ assert_array_almost_equal (from_mn ,
2001
+ list (target .T ))
2002
+
2003
+ # test with NoNorm, list as input
2004
+ mn_no_norm = mpl .colors .MultiNorm (['linear' , mcolors .NoNorm ()])
2005
+ no_norm_out = mn_no_norm (list (vals .T ))
2006
+ assert_array_almost_equal (no_norm_out ,
2007
+ [[0. , 0.5 , 1. ],
2008
+ [1 , 3 , 5 ]])
2009
+ assert no_norm_out [0 ].dtype == np .dtype ('float64' )
2010
+ assert no_norm_out [1 ].dtype == np .dtype ('int64' )
2011
+
2012
+ # test with NoNorm, structured array as input
2013
+ mn_no_norm = mpl .colors .MultiNorm (['linear' , mcolors .NoNorm ()])
2014
+ no_norm_out = mn_no_norm (rfn .unstructured_to_structured (vals ))
2015
+ assert_array_almost_equal (rfn .structured_to_unstructured (no_norm_out ),
2016
+ np .array (\
2017
+ [[0. , 0.5 , 1. ],
2018
+ [1 , 3 , 5 ]]).T )
2019
+ assert no_norm_out .dtype ['f0' ] == np .dtype ('float64' )
2020
+ assert no_norm_out .dtype ['f1' ] == np .dtype ('int64' )
2021
+
2022
+ # test single int as input
2023
+ with pytest .raises (ValueError ,
2024
+ match = "Input of type <class 'int'> is not supported" ):
2025
+ mn (1 )
2026
+
2027
+ # test list of incompatible size
2028
+ with pytest .raises (ValueError ,
2029
+ match = "A <class 'list'> of length 3 is not compatible" ):
2030
+ mn ([3 , 2 , 1 ])
2031
+
2032
+ # np.arrays of shapes that can be converted:
2033
+ for data in [np .zeros (2 ), np .zeros ((2 ,3 )), np .zeros ((2 ,3 ,3 ))]:
2034
+ with pytest .raises (ValueError ,
2035
+ match = r"You can use `list\(data\)` to convert" ):
2036
+ mn (data )
2037
+
2038
+ for data in [np .zeros ((3 , 2 )), np .zeros ((3 , 3 , 2 ))]:
2039
+ with pytest .raises (ValueError ,
2040
+ match = r"You can use `rfn.unstructured_to_structured" ):
2041
+ mn (data )
2042
+
2043
+ # np.ndarray that can be converted, but unclear if first or last axis
2044
+ for data in [np .zeros ((2 , 2 )), np .zeros ((2 , 3 , 2 ))]:
2045
+ with pytest .raises (ValueError ,
2046
+ match = "An np.ndarray of shape" ):
2047
+ mn (data )
2048
+
2049
+ # incompatible arrays where no relevant axis matches
2050
+ for data in [np .zeros (3 ), np .zeros ((3 , 2 , 3 ))]:
2051
+ with pytest .raises (ValueError ,
2052
+ match = r"An np.ndarray of shape" ):
2053
+ mn (data )
2054
+
2055
+ # test incompatible class
2056
+ with pytest .raises (ValueError ,
2057
+ match = "Input of type <class 'str'> is not supported" ):
2058
+ mn ("An object of incompatible class" )
0 commit comments