@@ -867,99 +867,69 @@ def test_array_dtype_shape(self):
867
867
z [...] = a
868
868
assert_array_equal (a , z [...])
869
869
870
- def test_structured_array (self ):
871
-
872
- # setup some data
873
- d = np .array ([(b'aaa' , 1 , 4.2 ),
874
- (b'bbb' , 2 , 8.4 ),
875
- (b'ccc' , 3 , 12.6 )],
876
- dtype = [('foo' , 'S3' ), ('bar' , 'i4' ), ('baz' , 'f8' )])
870
+ def check_structured_array (self , d , fill_values ):
877
871
for a in (d , d [:0 ]):
878
- for fill_value in None , b'' , ( b'zzz' , 42 , 16.8 ) :
872
+ for fill_value in fill_values :
879
873
z = self .create_array (shape = a .shape , chunks = 2 , dtype = a .dtype , fill_value = fill_value )
880
874
assert len (a ) == len (z )
875
+ assert a .shape == z .shape
876
+ assert a .dtype == z .dtype
877
+
878
+ # check use of fill value before array is initialised with data
881
879
if fill_value is not None :
882
880
if fill_value == b'' :
883
881
# numpy 1.14 compatibility
884
882
np_fill_value = np .array (fill_value , dtype = a .dtype .str ).view (a .dtype )[()]
885
883
else :
886
884
np_fill_value = np .array (fill_value , dtype = a .dtype )[()]
887
885
assert np_fill_value == z .fill_value
888
- if len (z ):
886
+ if len (a ):
889
887
assert np_fill_value == z [0 ]
890
888
assert np_fill_value == z [- 1 ]
889
+ empty = np .empty_like (a )
890
+ empty [:] = np_fill_value
891
+ assert empty [0 ] == z [0 ]
892
+ assert_array_equal (empty [0 :2 ], z [0 :2 ])
893
+ assert_array_equal (empty , z [...])
894
+ for f in a .dtype .names :
895
+ assert_array_equal (empty [f ], z [f ])
896
+
897
+ # store data in array
891
898
z [...] = a
899
+
900
+ # check stored data
892
901
if len (a ):
893
902
assert a [0 ] == z [0 ]
894
- assert_array_equal (a , z [...])
895
- assert_array_equal (a ['foo' ], z ['foo' ])
896
- assert_array_equal (a ['bar' ], z ['bar' ])
897
- assert_array_equal (a ['baz' ], z ['baz' ])
903
+ assert a [- 1 ] == z [- 1 ]
904
+ assert_array_equal (a [0 :2 ], z [0 :2 ])
905
+ assert_array_equal (a , z [...])
906
+ for f in a .dtype .names :
907
+ assert_array_equal (a [f ], z [f ])
898
908
899
- def test_structured_array_subshapes (self ):
909
+ def test_structured_array (self ):
910
+ d = np .array ([(b'aaa' , 1 , 4.2 ),
911
+ (b'bbb' , 2 , 8.4 ),
912
+ (b'ccc' , 3 , 12.6 )],
913
+ dtype = [('foo' , 'S3' ), ('bar' , 'i4' ), ('baz' , 'f8' )])
914
+ fill_values = None , b'' , (b'zzz' , 42 , 16.8 )
915
+ self .check_structured_array (d , fill_values )
900
916
901
- # setup some data
917
+ def test_structured_array_subshapes ( self ):
902
918
d = np .array ([(0 , ((0 , 1 , 2 ), (1 , 2 , 3 )), b'aaa' ),
903
919
(1 , ((1 , 2 , 3 ), (2 , 3 , 4 )), b'bbb' ),
904
920
(2 , ((2 , 3 , 4 ), (3 , 4 , 5 )), b'ccc' )],
905
921
dtype = [('foo' , 'i8' ), ('bar' , '(2, 3)f4' ), ('baz' , 'S3' )])
906
- for a in (d , d [:0 ]):
907
- for fill_value in None , b'' , (0 , ((0 , 0 , 0 ), (1 , 1 , 1 )), b'zzz' ):
908
- z = self .create_array (shape = a .shape , chunks = 2 , dtype = a .dtype , fill_value = fill_value )
909
- assert len (a ) == len (z )
910
- if fill_value is not None :
911
- if fill_value == b'' :
912
- # numpy 1.14 compatibility
913
- np_fill_value = np .array (fill_value , dtype = a .dtype .str ).view (a .dtype )[()]
914
- else :
915
- np_fill_value = np .array (fill_value , dtype = a .dtype )[()]
916
- assert np_fill_value == z .fill_value
917
- if len (z ):
918
- assert np_fill_value == z [0 ]
919
- assert np_fill_value == z [- 1 ]
920
- z [...] = a
921
- if len (a ):
922
- assert a [0 ] == z [0 ]
923
- assert_array_equal (a , z [...])
924
- assert_array_equal (a ['foo' ], z ['foo' ])
925
- assert_array_equal (a ['bar' ], z ['bar' ])
926
- assert_array_equal (a ['baz' ], z ['baz' ])
927
- else :
928
- # workaround for numpy bug https://www.github.com/numpy/numpy/issues/11946
929
- assert a .tobytes () == z [...].tobytes ()
922
+ fill_values = None , b'' , (0 , ((0 , 0 , 0 ), (1 , 1 , 1 )), b'zzz' )
923
+ self .check_structured_array (d , fill_values )
930
924
931
925
def test_structured_array_nested (self ):
932
-
933
- # setup some data
934
926
d = np .array ([(0 , (0 , ((0 , 1 ), (1 , 2 ), (2 , 3 )), 0 ), b'aaa' ),
935
927
(1 , (1 , ((1 , 2 ), (2 , 3 ), (3 , 4 )), 1 ), b'bbb' ),
936
928
(2 , (2 , ((2 , 3 ), (3 , 4 ), (4 , 5 )), 2 ), b'ccc' )],
937
929
dtype = [('foo' , 'i8' ), ('bar' , [('foo' , 'i4' ), ('bar' , '(3, 2)f4' ),
938
930
('baz' , 'u1' )]), ('baz' , 'S3' )])
939
- for a in (d , d [:0 ]):
940
- for fill_value in None , b'' , (0 , (0 , ((0 , 0 ), (1 , 1 ), (2 , 2 )), 0 ), b'zzz' ):
941
- z = self .create_array (shape = a .shape , chunks = 2 , dtype = a .dtype , fill_value = fill_value )
942
- assert len (a ) == len (z )
943
- if fill_value is not None :
944
- if fill_value == b'' :
945
- # numpy 1.14 compatibility
946
- np_fill_value = np .array (fill_value , dtype = a .dtype .str ).view (a .dtype )[()]
947
- else :
948
- np_fill_value = np .array (fill_value , dtype = a .dtype )[()]
949
- assert np_fill_value == z .fill_value
950
- if len (z ):
951
- assert np_fill_value == z [0 ]
952
- assert np_fill_value == z [- 1 ]
953
- z [...] = a
954
- if len (a ):
955
- assert a [0 ] == z [0 ]
956
- assert_array_equal (a , z [...])
957
- assert_array_equal (a ['foo' ], z ['foo' ])
958
- assert_array_equal (a ['bar' ], z ['bar' ])
959
- assert_array_equal (a ['baz' ], z ['baz' ])
960
- else :
961
- # workaround for numpy bug https://www.github.com/numpy/numpy/issues/11946
962
- assert a .tobytes () == z [...].tobytes ()
931
+ fill_values = None , b'' , (0 , (0 , ((0 , 0 ), (1 , 1 ), (2 , 2 )), 0 ), b'zzz' )
932
+ self .check_structured_array (d , fill_values )
963
933
964
934
def test_dtypes (self ):
965
935
0 commit comments