@@ -912,96 +912,113 @@ def test_rgb_density():
912
912
assert tf ._rgb_density (data ) == 1.0
913
913
data = np .zeros ((4 , 4 ), dtype = 'uint32' )
914
914
assert tf ._rgb_density (data ) == np .inf
915
- data [2 , 2 ] = b
915
+ data [3 , 3 ] = b
916
916
assert tf ._rgb_density (data ) == 0
917
- data [2 , 1 ] = data [1 , 2 ] = data [1 , 1 ] = b
918
- assert np .allclose (tf ._rgb_density (data ), 3. / 8. )
917
+ data [2 , 0 ] = data [0 , 2 ] = data [1 , 1 ] = b
918
+ assert np .allclose (tf ._rgb_density (data ), 0.75 )
919
+ assert np .allclose (tf ._rgb_density (data , 3 ), 1 )
919
920
920
921
def test_int_array_density ():
921
922
data = np .ones ((4 , 4 ), dtype = 'uint32' )
922
923
assert tf ._array_density (data , float_type = False ) == 1.0
923
924
data = np .zeros ((4 , 4 ), dtype = 'uint32' )
924
925
assert tf ._array_density (data , float_type = False ) == np .inf
925
- data [2 , 2 ] = 1
926
+ data [3 , 3 ] = 1
926
927
assert tf ._array_density (data , float_type = False ) == 0
927
- data [2 , 1 ] = data [1 , 2 ] = data [1 , 1 ] = 1
928
- assert np .allclose (tf ._array_density (data , float_type = False ), 3. / 8. )
928
+ data [2 , 0 ] = data [0 , 2 ] = data [1 , 1 ] = 1
929
+ assert np .allclose (tf ._array_density (data , float_type = False ), 0.75 )
930
+ assert np .allclose (tf ._array_density (data , float_type = False , px = 3 ), 1 )
929
931
932
+
930
933
def test_float_array_density ():
931
934
data = np .ones ((4 , 4 ), dtype = 'float32' )
932
935
assert tf ._array_density (data , float_type = True ) == 1.0
933
936
data = np .full ((4 , 4 ), np .nan , dtype = 'float32' )
934
937
assert tf ._array_density (data , float_type = True ) == np .inf
935
- data [2 , 2 ] = 1
938
+ data [3 , 3 ] = 1
936
939
assert tf ._array_density (data , float_type = True ) == 0
937
- data [2 , 1 ] = data [1 , 2 ] = data [1 , 1 ] = 1
938
- assert np .allclose (tf ._array_density (data , float_type = True ), 3. / 8. )
939
-
940
+ data [2 , 0 ] = data [0 , 2 ] = data [1 , 1 ] = 1
941
+ assert np .allclose (tf ._array_density (data , float_type = True ), 0.75 )
942
+ assert np .allclose (tf ._array_density (data , float_type = True , px = 3 ), 1 )
943
+
940
944
941
945
def test_rgb_dynspread ():
942
946
b = 0xffff0000
947
+ coords = [np .arange (5 ), np .arange (5 )]
943
948
data = np .array ([[b , b , 0 , 0 , 0 ],
944
949
[b , b , 0 , 0 , 0 ],
945
950
[0 , 0 , 0 , 0 , 0 ],
946
951
[0 , 0 , 0 , b , 0 ],
947
952
[0 , 0 , 0 , 0 , 0 ]], dtype = 'uint32' )
948
- coords = [np .arange (5 ), np .arange (5 )]
949
953
img = tf .Image (data , coords = coords , dims = dims )
950
- assert tf .dynspread (img ).equals (tf .spread (img , 1 ))
951
- assert tf .dynspread (img , threshold = 0.9 ).equals (tf .spread (img , 2 ))
952
- assert tf .dynspread (img , threshold = 0 ).equals (img )
954
+ assert tf .dynspread (img ).equals (img )
955
+ data = np .array ([[b , 0 , 0 , 0 , 0 ],
956
+ [0 , 0 , 0 , 0 , 0 ],
957
+ [b , 0 , 0 , 0 , b ],
958
+ [0 , 0 , 0 , 0 , 0 ],
959
+ [0 , 0 , 0 , 0 , 0 ]], dtype = 'uint32' )
960
+ img = tf .Image (data , coords = coords , dims = dims )
961
+ assert tf .dynspread (img , threshold = 0.4 ).equals (tf .spread (img , 0 ))
962
+ assert tf .dynspread (img , threshold = 0.7 ).equals (tf .spread (img , 1 ))
963
+ assert tf .dynspread (img , threshold = 1.0 ).equals (tf .spread (img , 3 ))
953
964
assert tf .dynspread (img , max_px = 0 ).equals (img )
954
965
955
966
pytest .raises (ValueError , lambda : tf .dynspread (img , threshold = 1.1 ))
956
967
pytest .raises (ValueError , lambda : tf .dynspread (img , max_px = - 1 ))
957
968
958
969
def test_array_dynspread ():
970
+ coords = [np .arange (5 ), np .arange (5 )]
959
971
data = np .array ([[1 , 1 , 0 , 0 , 0 ],
960
972
[1 , 1 , 0 , 0 , 0 ],
961
973
[0 , 0 , 0 , 0 , 0 ],
962
974
[0 , 0 , 0 , 1 , 0 ],
963
975
[0 , 0 , 0 , 0 , 0 ]], dtype = 'uint32' )
964
- coords = [np .arange (5 ), np .arange (5 )]
965
976
arr = xr .DataArray (data , coords = coords , dims = dims )
966
- assert tf .dynspread (arr ).equals (tf .spread (arr , 1 ))
967
- assert tf .dynspread (arr , threshold = 0.9 ).equals (tf .spread (arr , 2 ))
968
- assert tf .dynspread (arr , threshold = 0 ).equals (arr )
977
+ assert tf .dynspread (arr ).equals (arr )
978
+ data = np .array ([[1 , 0 , 0 , 0 , 0 ],
979
+ [0 , 0 , 0 , 0 , 0 ],
980
+ [1 , 0 , 0 , 0 , 1 ],
981
+ [0 , 0 , 0 , 0 , 0 ],
982
+ [0 , 0 , 0 , 0 , 0 ]], dtype = 'uint32' )
983
+ arr = xr .DataArray (data , coords = coords , dims = dims )
984
+ assert tf .dynspread (arr , threshold = 0.4 ).equals (tf .spread (arr , 0 ))
985
+ assert tf .dynspread (arr , threshold = 0.7 ).equals (tf .spread (arr , 1 ))
986
+ assert tf .dynspread (arr , threshold = 1.0 ).equals (tf .spread (arr , 3 ))
969
987
assert tf .dynspread (arr , max_px = 0 ).equals (arr )
970
988
971
989
pytest .raises (ValueError , lambda : tf .dynspread (arr , threshold = 1.1 ))
972
990
pytest .raises (ValueError , lambda : tf .dynspread (arr , max_px = - 1 ))
973
991
974
992
975
993
def test_categorical_dynspread ():
976
- a_data = np .array ([[0 , 1 , 0 , 0 , 0 ],
994
+ a_data = np .array ([[1 , 0 , 0 , 0 , 0 ],
977
995
[0 , 0 , 0 , 0 , 0 ],
978
996
[0 , 0 , 0 , 0 , 0 ],
979
997
[0 , 0 , 0 , 0 , 0 ],
980
998
[0 , 0 , 0 , 0 , 0 ]], dtype = 'int32' )
981
999
982
1000
b_data = np .array ([[0 , 0 , 0 , 0 , 0 ],
983
- [0 , 1 , 0 , 0 , 0 ],
984
1001
[0 , 0 , 0 , 0 , 0 ],
1002
+ [1 , 0 , 0 , 0 , 0 ],
985
1003
[0 , 0 , 0 , 0 , 0 ],
986
1004
[0 , 0 , 0 , 0 , 0 ]], dtype = 'int32' )
987
1005
988
- c_data = np .array ([[1 , 0 , 0 , 0 , 0 ],
989
- [1 , 0 , 0 , 0 , 0 ],
1006
+ c_data = np .array ([[0 , 0 , 0 , 0 , 0 ],
1007
+ [0 , 0 , 0 , 0 , 0 ],
1008
+ [0 , 0 , 0 , 0 , 1 ],
990
1009
[0 , 0 , 0 , 0 , 0 ],
991
- [0 , 0 , 0 , 1 , 0 ],
992
1010
[0 , 0 , 0 , 0 , 0 ]], dtype = 'int32' )
993
1011
994
1012
data = np .dstack ([a_data , b_data , c_data ])
995
1013
coords = [np .arange (5 ), np .arange (5 )]
996
1014
arr = xr .DataArray (data , coords = coords + [['a' , 'b' , 'c' ]],
997
1015
dims = dims + ['cat' ])
998
- assert tf .dynspread (arr ).equals (tf .spread (arr , 1 ))
999
- assert tf .dynspread (arr , threshold = 0.9 ).equals (tf .spread (arr , 2 ))
1000
- assert tf .dynspread (arr , threshold = 0 ).equals (arr )
1016
+ assert tf .dynspread (arr , threshold = 0.4 ).equals (tf .spread (arr , 0 ))
1017
+ assert tf .dynspread (arr , threshold = 0.7 ).equals (tf .spread (arr , 1 ))
1018
+ assert tf .dynspread (arr , threshold = 1. 0 ).equals (tf . spread ( arr , 3 ) )
1001
1019
assert tf .dynspread (arr , max_px = 0 ).equals (arr )
1002
1020
1003
1021
1004
-
1005
1022
def check_eq_hist_cdf_slope (eq ):
1006
1023
# Check that the slope of the cdf is ~1
1007
1024
# Adapted from scikit-image's test for the same function
0 commit comments