1-
21import numpy as np
32import numpy .ma as ma
43from numpy .lib .recfunctions import (
3130
3231class TestRecFunctions :
3332 # Misc tests
34-
35- def setup_method ( self ):
33+ def test_zip_descr ( self ):
34+ # Test zip_descr
3635 x = np .array ([1 , 2 , ])
3736 y = np .array ([10 , 20 , 30 ])
3837 z = np .array ([('A' , 1. ), ('B' , 2. )],
3938 dtype = [('A' , '|S3' ), ('B' , float )])
4039 w = np .array ([(1 , (2 , 3.0 )), (4 , (5 , 6.0 ))],
4140 dtype = [('a' , int ), ('b' , [('ba' , float ), ('bb' , int )])])
42- self .data = (w , x , y , z )
43-
44- def test_zip_descr (self ):
45- # Test zip_descr
46- (w , x , y , z ) = self .data
4741
4842 # Std array
4943 test = zip_descr ((x , x ), flatten = True )
@@ -448,19 +442,19 @@ def test_masked_flexible(self):
448442class TestMergeArrays :
449443 # Test merge_arrays
450444
451- def setup_method (self ):
445+ def _create_arrays (self ):
452446 x = np .array ([1 , 2 , ])
453447 y = np .array ([10 , 20 , 30 ])
454448 z = np .array (
455449 [('A' , 1. ), ('B' , 2. )], dtype = [('A' , '|S3' ), ('B' , float )])
456450 w = np .array (
457451 [(1 , (2 , 3.0 , ())), (4 , (5 , 6.0 , ()))],
458452 dtype = [('a' , int ), ('b' , [('ba' , float ), ('bb' , int ), ('bc' , [])])])
459- self . data = ( w , x , y , z )
453+ return w , x , y , z
460454
461455 def test_solo (self ):
462456 # Test merge_arrays on a single array.
463- ( _ , x , _ , z ) = self .data
457+ _ , x , _ , z = self ._create_arrays ()
464458
465459 test = merge_arrays (x )
466460 control = np .array ([(1 ,), (2 ,)], dtype = [('f0' , int )])
@@ -475,7 +469,7 @@ def test_solo(self):
475469
476470 def test_solo_w_flatten (self ):
477471 # Test merge_arrays on a single array w & w/o flattening
478- w = self .data [0 ]
472+ w = self ._create_arrays () [0 ]
479473 test = merge_arrays (w , flatten = False )
480474 assert_equal (test , w )
481475
@@ -487,7 +481,7 @@ def test_solo_w_flatten(self):
487481 def test_standard (self ):
488482 # Test standard & standard
489483 # Test merge arrays
490- ( _ , x , y , _ ) = self .data
484+ _ , x , y , _ = self ._create_arrays ()
491485 test = merge_arrays ((x , y ), usemask = False )
492486 control = np .array ([(1 , 10 ), (2 , 20 ), (- 1 , 30 )],
493487 dtype = [('f0' , int ), ('f1' , int )])
@@ -502,7 +496,7 @@ def test_standard(self):
502496
503497 def test_flatten (self ):
504498 # Test standard & flexible
505- ( _ , x , _ , z ) = self .data
499+ _ , x , _ , z = self ._create_arrays ()
506500 test = merge_arrays ((x , z ), flatten = True )
507501 control = np .array ([(1 , 'A' , 1. ), (2 , 'B' , 2. )],
508502 dtype = [('f0' , int ), ('A' , '|S3' ), ('B' , float )])
@@ -516,7 +510,7 @@ def test_flatten(self):
516510
517511 def test_flatten_wflexible (self ):
518512 # Test flatten standard & nested
519- ( w , x , _ , _ ) = self .data
513+ w , x , _ , _ = self ._create_arrays ()
520514 test = merge_arrays ((x , w ), flatten = True )
521515 control = np .array ([(1 , 1 , 2 , 3.0 ), (2 , 4 , 5 , 6.0 )],
522516 dtype = [('f0' , int ),
@@ -532,7 +526,7 @@ def test_flatten_wflexible(self):
532526
533527 def test_wmasked_arrays (self ):
534528 # Test merge_arrays masked arrays
535- ( _ , x , _ , _ ) = self .data
529+ x = self ._create_arrays ()[ 1 ]
536530 mx = ma .array ([1 , 2 , 3 ], mask = [1 , 0 , 0 ])
537531 test = merge_arrays ((x , mx ), usemask = True )
538532 control = ma .array ([(1 , 1 ), (2 , 2 ), (- 1 , 3 )],
@@ -554,7 +548,7 @@ def test_w_singlefield(self):
554548
555549 def test_w_shorter_flex (self ):
556550 # Test merge_arrays w/ a shorter flexndarray.
557- z = self .data [- 1 ]
551+ z = self ._create_arrays () [- 1 ]
558552
559553 # Fixme, this test looks incomplete and broken
560554 #test = merge_arrays((z, np.array([10, 20, 30]).view([('C', int)])))
@@ -567,7 +561,7 @@ def test_w_shorter_flex(self):
567561 dtype = [('A' , '|S3' ), ('B' , float ), ('C' , int )])
568562
569563 def test_singlerecord (self ):
570- ( _ , x , y , z ) = self .data
564+ _ , x , y , z = self ._create_arrays ()
571565 test = merge_arrays ((x [0 ], y [0 ], z [0 ]), usemask = False )
572566 control = np .array ([(1 , 10 , ('A' , 1 ))],
573567 dtype = [('f0' , int ),
@@ -579,18 +573,18 @@ def test_singlerecord(self):
579573class TestAppendFields :
580574 # Test append_fields
581575
582- def setup_method (self ):
576+ def _create_arrays (self ):
583577 x = np .array ([1 , 2 , ])
584578 y = np .array ([10 , 20 , 30 ])
585579 z = np .array (
586580 [('A' , 1. ), ('B' , 2. )], dtype = [('A' , '|S3' ), ('B' , float )])
587581 w = np .array ([(1 , (2 , 3.0 )), (4 , (5 , 6.0 ))],
588582 dtype = [('a' , int ), ('b' , [('ba' , float ), ('bb' , int )])])
589- self . data = ( w , x , y , z )
583+ return w , x , y , z
590584
591585 def test_append_single (self ):
592586 # Test simple case
593- ( _ , x , _ , _ ) = self .data
587+ x = self ._create_arrays ()[ 1 ]
594588 test = append_fields (x , 'A' , data = [10 , 20 , 30 ])
595589 control = ma .array ([(1 , 10 ), (2 , 20 ), (- 1 , 30 )],
596590 mask = [(0 , 0 ), (0 , 0 ), (1 , 0 )],
@@ -599,7 +593,7 @@ def test_append_single(self):
599593
600594 def test_append_double (self ):
601595 # Test simple case
602- ( _ , x , _ , _ ) = self .data
596+ x = self ._create_arrays ()[ 1 ]
603597 test = append_fields (x , ('A' , 'B' ), data = [[10 , 20 , 30 ], [100 , 200 ]])
604598 control = ma .array ([(1 , 10 , 100 ), (2 , 20 , 200 ), (- 1 , 30 , - 1 )],
605599 mask = [(0 , 0 , 0 ), (0 , 0 , 0 ), (1 , 0 , 1 )],
@@ -608,7 +602,7 @@ def test_append_double(self):
608602
609603 def test_append_on_flex (self ):
610604 # Test append_fields on flexible type arrays
611- z = self .data [- 1 ]
605+ z = self ._create_arrays () [- 1 ]
612606 test = append_fields (z , 'C' , data = [10 , 20 , 30 ])
613607 control = ma .array ([('A' , 1. , 10 ), ('B' , 2. , 20 ), (- 1 , - 1. , 30 )],
614608 mask = [(0 , 0 , 0 ), (0 , 0 , 0 ), (1 , 1 , 0 )],
@@ -617,7 +611,7 @@ def test_append_on_flex(self):
617611
618612 def test_append_on_nested (self ):
619613 # Test append_fields on nested fields
620- w = self .data [0 ]
614+ w = self ._create_arrays () [0 ]
621615 test = append_fields (w , 'C' , data = [10 , 20 , 30 ])
622616 control = ma .array ([(1 , (2 , 3.0 ), 10 ),
623617 (4 , (5 , 6.0 ), 20 ),
@@ -632,18 +626,18 @@ def test_append_on_nested(self):
632626
633627class TestStackArrays :
634628 # Test stack_arrays
635- def setup_method (self ):
629+ def _create_arrays (self ):
636630 x = np .array ([1 , 2 , ])
637631 y = np .array ([10 , 20 , 30 ])
638632 z = np .array (
639633 [('A' , 1. ), ('B' , 2. )], dtype = [('A' , '|S3' ), ('B' , float )])
640634 w = np .array ([(1 , (2 , 3.0 )), (4 , (5 , 6.0 ))],
641635 dtype = [('a' , int ), ('b' , [('ba' , float ), ('bb' , int )])])
642- self . data = ( w , x , y , z )
636+ return w , x , y , z
643637
644638 def test_solo (self ):
645639 # Test stack_arrays on single arrays
646- ( _ , x , _ , _ ) = self .data
640+ x = self ._create_arrays ()[ 1 ]
647641 test = stack_arrays ((x ,))
648642 assert_equal (test , x )
649643 assert_ (test is x )
@@ -654,7 +648,7 @@ def test_solo(self):
654648
655649 def test_unnamed_fields (self ):
656650 # Tests combinations of arrays w/o named fields
657- ( _ , x , y , _ ) = self .data
651+ _ , x , y , _ = self ._create_arrays ()
658652
659653 test = stack_arrays ((x , x ), usemask = False )
660654 control = np .array ([1 , 2 , 1 , 2 ])
@@ -670,7 +664,7 @@ def test_unnamed_fields(self):
670664
671665 def test_unnamed_and_named_fields (self ):
672666 # Test combination of arrays w/ & w/o named fields
673- ( _ , x , _ , z ) = self .data
667+ _ , x , _ , z = self ._create_arrays ()
674668
675669 test = stack_arrays ((x , z ))
676670 control = ma .array ([(1 , - 1 , - 1 ), (2 , - 1 , - 1 ),
@@ -702,7 +696,7 @@ def test_unnamed_and_named_fields(self):
702696
703697 def test_matching_named_fields (self ):
704698 # Test combination of arrays w/ matching field names
705- ( _ , x , _ , z ) = self .data
699+ _ , x , _ , z = self ._create_arrays ()
706700 zz = np .array ([('a' , 10. , 100. ), ('b' , 20. , 200. ), ('c' , 30. , 300. )],
707701 dtype = [('A' , '|S3' ), ('B' , float ), ('C' , float )])
708702 test = stack_arrays ((z , zz ))
@@ -730,7 +724,7 @@ def test_matching_named_fields(self):
730724
731725 def test_defaults (self ):
732726 # Test defaults: no exception raised if keys of defaults are not fields.
733- ( _ , _ , _ , z ) = self .data
727+ z = self ._create_arrays ()[ - 1 ]
734728 zz = np .array ([('a' , 10. , 100. ), ('b' , 20. , 200. ), ('c' , 30. , 300. )],
735729 dtype = [('A' , '|S3' ), ('B' , float ), ('C' , float )])
736730 defaults = {'A' : '???' , 'B' : - 999. , 'C' : - 9999. , 'D' : - 99999. }
@@ -802,18 +796,18 @@ def test_subdtype(self):
802796
803797
804798class TestJoinBy :
805- def setup_method (self ):
806- self . a = np .array (list (zip (np .arange (10 ), np .arange (50 , 60 ),
799+ def _create_arrays (self ):
800+ a = np .array (list (zip (np .arange (10 ), np .arange (50 , 60 ),
807801 np .arange (100 , 110 ))),
808802 dtype = [('a' , int ), ('b' , int ), ('c' , int )])
809- self . b = np .array (list (zip (np .arange (5 , 15 ), np .arange (65 , 75 ),
803+ b = np .array (list (zip (np .arange (5 , 15 ), np .arange (65 , 75 ),
810804 np .arange (100 , 110 ))),
811805 dtype = [('a' , int ), ('b' , int ), ('d' , int )])
806+ return a , b
812807
813808 def test_inner_join (self ):
814809 # Basic test of join_by
815- a , b = self .a , self .b
816-
810+ a , b = self ._create_arrays ()
817811 test = join_by ('a' , a , b , jointype = 'inner' )
818812 control = np .array ([(5 , 55 , 65 , 105 , 100 ), (6 , 56 , 66 , 106 , 101 ),
819813 (7 , 57 , 67 , 107 , 102 ), (8 , 58 , 68 , 108 , 103 ),
@@ -823,8 +817,7 @@ def test_inner_join(self):
823817 assert_equal (test , control )
824818
825819 def test_join (self ):
826- a , b = self .a , self .b
827-
820+ a , b = self ._create_arrays ()
828821 # Fixme, this test is broken
829822 #test = join_by(('a', 'b'), a, b)
830823 #control = np.array([(5, 55, 105, 100), (6, 56, 106, 101),
@@ -833,7 +826,6 @@ def test_join(self):
833826 # dtype=[('a', int), ('b', int),
834827 # ('c', int), ('d', int)])
835828 #assert_equal(test, control)
836-
837829 join_by (('a' , 'b' ), a , b )
838830 np .array ([(5 , 55 , 105 , 100 ), (6 , 56 , 106 , 101 ),
839831 (7 , 57 , 107 , 102 ), (8 , 58 , 108 , 103 ),
@@ -851,8 +843,7 @@ def test_join_subdtype(self):
851843 assert_equal (res , bar .view (ma .MaskedArray ))
852844
853845 def test_outer_join (self ):
854- a , b = self .a , self .b
855-
846+ a , b = self ._create_arrays ()
856847 test = join_by (('a' , 'b' ), a , b , 'outer' )
857848 control = ma .array ([(0 , 50 , 100 , - 1 ), (1 , 51 , 101 , - 1 ),
858849 (2 , 52 , 102 , - 1 ), (3 , 53 , 103 , - 1 ),
@@ -879,8 +870,7 @@ def test_outer_join(self):
879870 assert_equal (test , control )
880871
881872 def test_leftouter_join (self ):
882- a , b = self .a , self .b
883-
873+ a , b = self ._create_arrays ()
884874 test = join_by (('a' , 'b' ), a , b , 'leftouter' )
885875 control = ma .array ([(0 , 50 , 100 , - 1 ), (1 , 51 , 101 , - 1 ),
886876 (2 , 52 , 102 , - 1 ), (3 , 53 , 103 , - 1 ),
@@ -1029,19 +1019,17 @@ def test_two_keys_two_vars(self):
10291019 assert_equal (test .dtype , control .dtype )
10301020 assert_equal (test , control )
10311021
1022+
10321023class TestAppendFieldsObj :
10331024 """
10341025 Test append_fields with arrays containing objects
10351026 """
10361027 # https://github.com/numpy/numpy/issues/2346
10371028
1038- def setup_method (self ):
1039- from datetime import date
1040- self .data = {'obj' : date (2000 , 1 , 1 )}
1041-
10421029 def test_append_to_objects (self ):
10431030 "Test append_fields when the base array contains objects"
1044- obj = self .data ['obj' ]
1031+ from datetime import date
1032+ obj = date (2000 , 1 , 1 )
10451033 x = np .array ([(obj , 1. ), (obj , 2. )],
10461034 dtype = [('A' , object ), ('B' , float )])
10471035 y = np .array ([10 , 20 ], dtype = int )
0 commit comments