2
2
from __future__ import absolute_import , division , print_function
3
3
4
4
import os
5
+ import re
5
6
import sys
6
7
7
8
import pytest
30
31
# Test Value Strings #
31
32
# ================== #
32
33
34
+
33
35
def test_value_string_split ():
34
36
assert_array_equal (_to_ticks ('M,F' ), np .asarray (['M' , 'F' ]))
35
37
assert_array_equal (_to_ticks ('M, F' ), np .asarray (['M' , 'F' ]))
@@ -721,7 +723,7 @@ def test_getitem_abstract_positional(array):
721
723
array [X .age .i [2 , 3 ], X .age .i [1 , 5 ]]
722
724
723
725
724
- def test_getitem_bool_larray_key ():
726
+ def test_getitem_bool_larray_key_arr_whout_bool_axis ():
725
727
arr = ndtest ((3 , 2 , 4 ))
726
728
raw = arr .data
727
729
@@ -732,8 +734,8 @@ def test_getitem_bool_larray_key():
732
734
assert_array_equal (res , raw [raw < 5 ])
733
735
734
736
# missing dimension
735
- filt = arr ['b1' ] % 5 == 0
736
- res = arr [filt ]
737
+ filter_ = arr ['b1' ] % 5 == 0
738
+ res = arr [filter_ ]
737
739
assert isinstance (res , LArray )
738
740
assert res .ndim == 2
739
741
assert res .shape == (3 , 2 )
@@ -752,6 +754,26 @@ def test_getitem_bool_larray_key():
752
754
assert_array_equal (res , raw [:, :2 ])
753
755
754
756
757
+ def test_getitem_bool_larray_key_arr_wh_bool_axis ():
758
+ gender = Axis ([False , True ], 'gender' )
759
+ arr = LArray ([0.1 , 0.2 ], gender )
760
+ id_axis = Axis ('id=0..3' )
761
+ key = LArray ([True , False , True , True ], id_axis )
762
+ expected = LArray ([0.2 , 0.1 , 0.2 , 0.2 ], id_axis )
763
+
764
+ # LGroup using the real axis
765
+ assert_larray_equal (arr [gender [key ]], expected )
766
+
767
+ # LGroup using an AxisReference
768
+ assert_larray_equal (arr [X .gender [key ]], expected )
769
+
770
+ # this test checks that the current behavior does not change unintentionally...
771
+ # ... but I am unsure the current behavior is what we actually want
772
+ msg = re .escape ("boolean subset key contains more axes ({id}) than array ({gender})" )
773
+ with pytest .raises (ValueError , match = msg ):
774
+ arr [key ]
775
+
776
+
755
777
def test_getitem_bool_larray_and_group_key ():
756
778
arr = ndtest ((3 , 6 , 4 )).set_labels ('b' , '0..5' )
757
779
@@ -769,14 +791,34 @@ def test_getitem_bool_larray_and_group_key():
769
791
assert_array_equal (res , expected )
770
792
771
793
772
- def test_getitem_bool_ndarray_key (array ):
794
+ def test_getitem_bool_ndarray_key_arr_whout_bool_axis (array ):
773
795
raw = array .data
774
796
res = array [raw < 5 ]
775
797
assert isinstance (res , LArray )
776
798
assert res .ndim == 1
777
799
assert_array_equal (res , raw [raw < 5 ])
778
800
779
801
802
+ def test_getitem_bool_ndarray_key_arr_wh_bool_axis ():
803
+ gender = Axis ([False , True ], 'gender' )
804
+ arr = LArray ([0.1 , 0.2 ], gender )
805
+ key = np .array ([True , False , True , True ])
806
+ expected = arr .i [[1 , 0 , 1 , 1 ]]
807
+
808
+ # LGroup using the real axis
809
+ assert_larray_equal (arr [gender [key ]], expected )
810
+
811
+ # LGroup using an AxisReference
812
+ assert_larray_equal (arr [X .gender [key ]], expected )
813
+
814
+ # raw key => ???
815
+ # this test checks that the current behavior does not change unintentionally...
816
+ # ... but I am unsure the current behavior is what we actually want
817
+ msg = re .escape ("boolean key with a different shape ((4,)) than array ((2,))" )
818
+ with pytest .raises (ValueError , match = msg ):
819
+ arr [key ]
820
+
821
+
780
822
def test_getitem_bool_anonymous_axes ():
781
823
a = ndtest ([Axis (2 ), Axis (3 ), Axis (4 ), Axis (5 )])
782
824
mask = ones (a .axes [1 , 3 ], dtype = bool )
0 commit comments