Exemplo n.º 1
0
    def test_index_with_float_array_raises(self, multi_indexing):
        X = np.zeros(10)
        i = np.arange(3, 0.5)
        with pytest.raises(IndexError) as exc:
            multi_indexing(X, i)

        assert exc.value.args[0] == (
            "arrays used as indices must be of integer (or boolean) type")
Exemplo n.º 2
0
    def test_index_with_float_array_raises(self, multi_indexing):
        X = np.zeros(10)
        i = np.arange(3, 0.5)
        with pytest.raises(IndexError) as exc:
            multi_indexing(X, i)

        assert exc.value.args[0] == (
            "arrays used as indices must be of integer (or boolean) type")
Exemplo n.º 3
0
    def test_index_with_float_array_raises(self, multi_indexing):
        # sklearn < 0.22 raises IndexError with msg0
        # sklearn >= 0.22 raises ValueError with msg1
        X = np.zeros(10)
        i = np.arange(3, 0.5)

        with pytest.raises((IndexError, ValueError)) as exc:
            multi_indexing(X, i)

        msg0 = "arrays used as indices must be of integer (or boolean) type"
        msg1 = ("No valid specification of the columns. Only a scalar, list or "
                "slice of all integers or all strings, or boolean mask is allowed")

        result = exc.value.args[0]
        assert result in (msg0, msg1)
Exemplo n.º 4
0
    def test_boolean_index_2d_with_torch_tensor(self, multi_indexing):
        X = torch.LongTensor(np.arange(9).reshape(3, 3))
        i = np.eye(3).astype(bool)

        res = multi_indexing(X, i)
        expected = torch.LongTensor([0, 4, 8])
        assert all(res == expected)
Exemplo n.º 5
0
    def test_boolean_index_2d_with_torch_tensor(self, multi_indexing):
        X = torch.LongTensor(np.arange(9).reshape(3, 3))
        i = np.eye(3).astype(bool)

        res = multi_indexing(X, i)
        expected = torch.LongTensor([0, 4, 8])
        assert all(res == expected)
Exemplo n.º 6
0
 def test_list_of_dataframe_and_series_slice(self, multi_indexing, pd):
     data = [
         pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0]),
         pd.Series(data=[0, 1, 2], index=[2, 1, 0]),
     ]
     result = multi_indexing(data, np.s_[:2])
     assert result[0].equals(
         pd.DataFrame({'a': [0, 1], 'b': [3, 4]}, index=[2, 1]))
     assert result[1].equals(pd.Series(data=[0, 1], index=[2, 1]))
Exemplo n.º 7
0
 def test_list_of_dataframe_and_series_slice(self, multi_indexing, pd):
     data = [
         pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0]),
         pd.Series(data=[0, 1, 2], index=[2, 1, 0]),
     ]
     result = multi_indexing(data, np.s_[:2])
     assert result[0].equals(
         pd.DataFrame({'a': [0, 1], 'b': [3, 4]}, index=[2, 1]))
     assert result[1].equals(pd.Series(data=[0, 1], index=[2, 1]))
Exemplo n.º 8
0
 def test_dict_of_torch_tensors(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert result.keys() == expected.keys()
     for k in result:
         try:
             val = result[k].long().numpy()
         except AttributeError:
             val = result[k]
         assert np.allclose(val, expected[k])
Exemplo n.º 9
0
 def test_list_of_dataframe_and_series(self, multi_indexing, pd):
     data = [
         pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0]),
         pd.Series(data=[0, 1, 2], index=[2, 1, 0]),
     ]
     result = multi_indexing(data, 0)
     assert result[0].equals(
         pd.Series(data=[0, 3], index=['a', 'b'], name=2))
     assert result[1] == 0
Exemplo n.º 10
0
 def test_list_of_dataframe_and_series(self, multi_indexing, pd):
     data = [
         pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0]),
         pd.Series(data=[0, 1, 2], index=[2, 1, 0]),
     ]
     result = multi_indexing(data, 0)
     assert result[0].equals(
         pd.Series(data=[0, 3], index=['a', 'b'], name=2))
     assert result[1] == 0
Exemplo n.º 11
0
 def test_dict_of_torch_tensors(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert result.keys() == expected.keys()
     for k in result:
         try:
             val = result[k].long().numpy()
         except AttributeError:
             val = result[k]
         assert np.allclose(val, expected[k])
Exemplo n.º 12
0
 def test_mixed_data(self, multi_indexing):
     data = [
         [1, 2, 3],
         np.arange(3),
         torch.arange(3, 6),
         {'a': [4, 5, 6], 'b': [7, 8, 9]},
     ]
     result = multi_indexing(data, 0)
     expected = [1, 0, 3, {'a': 4, 'b': 7}]
     assert result == expected
Exemplo n.º 13
0
 def test_mixed_data(self, multi_indexing):
     data = [
         [1, 2, 3],
         np.arange(3),
         torch.arange(3, 6),
         {'a': [4, 5, 6], 'b': [7, 8, 9]},
     ]
     result = multi_indexing(data, 0)
     expected = [1, 0, 3, {'a': 4, 'b': 7}]
     assert result == expected
Exemplo n.º 14
0
 def test_mixed_data_slice(self, multi_indexing):
     data = [
         [1, 2, 3],
         np.arange(3),
         torch.arange(3, 6),
         {'a': [4, 5, 6], 'b': [7, 8, 9]},
     ]
     result = multi_indexing(data, np.s_[:2])
     assert result[0] == [1, 2]
     assert np.allclose(result[1], np.arange(2))
     assert np.allclose(result[2].long().numpy(), np.arange(3, 5))
     assert result[3] == {'a': [4, 5], 'b': [7, 8]}
Exemplo n.º 15
0
 def test_mixed_data_slice(self, multi_indexing):
     data = [
         [1, 2, 3],
         np.arange(3),
         torch.arange(3, 6),
         {'a': [4, 5, 6], 'b': [7, 8, 9]},
     ]
     result = multi_indexing(data, np.s_[:2])
     assert result[0] == [1, 2]
     assert np.allclose(result[1], np.arange(2))
     assert np.allclose(result[2].long().numpy(), np.arange(3, 5))
     assert result[3] == {'a': [4, 5], 'b': [7, 8]}
Exemplo n.º 16
0
 def test_pandas_dataframe_slice(self, multi_indexing, pd):
     import pandas as pd
     df = pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0])
     result = multi_indexing(df, np.s_[:2])
     expected = pd.DataFrame({'a': [0, 1], 'b': [3, 4]}, index=[2, 1])
     assert result.equals(expected)
Exemplo n.º 17
0
 def test_pandas_dataframe(self, multi_indexing, pd):
     df = pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0])
     result = multi_indexing(df, 0)
     # Note: taking one row of a DataFrame returns a Series
     expected = pd.Series(data=[0, 3], index=['a', 'b'], name=2)
     assert result.equals(expected)
Exemplo n.º 18
0
 def test_pandas_series(self, multi_indexing, pd):
     series = pd.Series(data=[0, 1, 2], index=[2, 1, 0])
     result = multi_indexing(series, 0)
     assert result == 0
Exemplo n.º 19
0
 def test_index_torch_tensor_with_numpy_int_array(self, multi_indexing):
     X = torch.zeros((1000, 10))
     i = np.arange(100)
     result = multi_indexing(X, i)
     assert (result == X[:100]).all()
Exemplo n.º 20
0
 def test_pandas_dataframe_slice(self, multi_indexing, pd):
     import pandas as pd
     df = pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0])
     result = multi_indexing(df, np.s_[:2])
     expected = pd.DataFrame({'a': [0, 1], 'b': [3, 4]}, index=[2, 1])
     assert result.equals(expected)
Exemplo n.º 21
0
 def test_list(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert np.allclose(result, expected)
Exemplo n.º 22
0
 def test_pandas_dataframe(self, multi_indexing, pd):
     df = pd.DataFrame({'a': [0, 1, 2], 'b': [3, 4, 5]}, index=[2, 1, 0])
     result = multi_indexing(df, 0)
     # Note: taking one row of a DataFrame returns a Series
     expected = pd.Series(data=[0, 3], index=['a', 'b'], name=2)
     assert result.equals(expected)
Exemplo n.º 23
0
 def test_index_torch_tensor_with_numpy_int_array(self, multi_indexing):
     X = torch.zeros((1000, 10))
     i = np.arange(100)
     result = multi_indexing(X, i)
     assert (result == X[:100]).all()
Exemplo n.º 24
0
 def test_dict_of_lists(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert result == expected
Exemplo n.º 25
0
 def test_boolean_index_2d(self, multi_indexing):
     X = np.arange(9).reshape(3, 3)
     i = np.eye(3).astype(bool)
     result = multi_indexing(X, i)
     expected = np.asarray([0, 4, 8])
     assert np.allclose(result, expected)
Exemplo n.º 26
0
 def test_dict_of_arrays(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert result.keys() == expected.keys()
     for k in result:
         assert np.allclose(result[k], expected[k])
Exemplo n.º 27
0
 def test_index_torch_tensor_with_numpy_bool_array(self, multi_indexing):
     X = torch.zeros((1000, 10))
     i = np.asarray([True] * 100 + [False] * 900)
     result = multi_indexing(X, i)
     assert (result == X[:100]).all()
Exemplo n.º 28
0
 def test_pandas_series(self, multi_indexing, pd):
     series = pd.Series(data=[0, 1, 2], index=[2, 1, 0])
     result = multi_indexing(series, 0)
     assert result == 0
Exemplo n.º 29
0
 def test_pandas_series_slice(self, multi_indexing, pd):
     series = pd.Series(data=[0, 1, 2], index=[2, 1, 0])
     result = multi_indexing(series, np.s_[:2])
     expected = pd.Series(data=[0, 1], index=[2, 1])
     assert result.equals(expected)
Exemplo n.º 30
0
 def test_dict_of_lists(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert result == expected
Exemplo n.º 31
0
 def test_dict_of_arrays(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert result.keys() == expected.keys()
     for k in result:
         assert np.allclose(result[k], expected[k])
Exemplo n.º 32
0
 def test_pandas_series_slice(self, multi_indexing, pd):
     series = pd.Series(data=[0, 1, 2], index=[2, 1, 0])
     result = multi_indexing(series, np.s_[:2])
     expected = pd.Series(data=[0, 1], index=[2, 1])
     assert result.equals(expected)
Exemplo n.º 33
0
 def test_index_torch_tensor_with_numpy_bool_array(self, multi_indexing):
     X = torch.zeros((1000, 10))
     i = np.asarray([True] * 100 + [False] * 900)
     result = multi_indexing(X, i)
     assert (result == X[:100]).all()
Exemplo n.º 34
0
 def test_list(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i)
     assert np.allclose(result, expected)
Exemplo n.º 35
0
 def test_boolean_index_2d(self, multi_indexing):
     X = np.arange(9).reshape(3, 3)
     i = np.eye(3).astype(bool)
     result = multi_indexing(X, i)
     expected = np.asarray([0, 4, 8])
     assert np.allclose(result, expected)
Exemplo n.º 36
0
 def test_torch_tensor(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i).long().numpy()
     assert np.allclose(result, expected)
Exemplo n.º 37
0
 def test_sparse_csr_matrix(self, multi_indexing, data, i, expected):
     data = sparse.csr_matrix(data)
     result = multi_indexing(data, i).toarray()
     assert np.allclose(result, expected)
Exemplo n.º 38
0
 def test_torch_tensor(self, multi_indexing, data, i, expected):
     result = multi_indexing(data, i).long().numpy()
     assert np.allclose(result, expected)