Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support features indexer with length 0 #246

Merged
merged 1 commit into from
Sep 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions chainer_chemistry/dataset/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,24 @@ def _extract_feature(self, data_index, j):
res = [self.extract_feature(i, j) for i in
six.moves.range(current, stop, step)]
elif isinstance(data_index, (list, numpy.ndarray)):
if isinstance(data_index[0], (bool, numpy.bool, numpy.bool_)):
# Access by bool flag list
if len(data_index) != self.dataset_length:
raise ValueError('Feature index wrong length {} instead of'
' {}'.format(len(data_index),
self.dataset_length))
data_index = numpy.argwhere(data_index).ravel()

res = [self.extract_feature(i, j) for i in data_index]
if len(data_index) == 0:
try:
# HACKING
return self.extract_feature_by_slice(slice(0, 0, 1), j)
except ExtractBySliceNotSupportedError:
res = []
else:
if isinstance(data_index[0], (bool, numpy.bool, numpy.bool_)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR. Maybe we need to consider when data_index[0] is numpy.int.

# Access by bool flag list
if len(data_index) != self.dataset_length:
raise ValueError('Feature index wrong length {} instead of'
' {}'.format(len(data_index),
self.dataset_length))
data_index = numpy.argwhere(data_index).ravel()

res = [self.extract_feature(i, j) for i in data_index]
else:
# `data_index` is expected to be `int`
return self.extract_feature(data_index, j)
try:
feature = numpy.asarray(res)
Expand Down
18 changes: 15 additions & 3 deletions tests/dataset_tests/test_numpy_tuple_feature_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,32 @@ class TestNumpyTupleDatasetFeatureIndexer(object):
def test_feature_length(self, indexer):
assert indexer.features_length() == 3

@pytest.mark.parametrize('slice_index', [0, 1, slice(0, 2, None)])
@pytest.mark.parametrize('slice_index', [
0, 1, slice(0, 2, None), slice(0, 0, None)])
@pytest.mark.parametrize('j', [0, 1])
def test_extract_feature_by_slice(self, indexer, data, slice_index, j):
numpy.testing.assert_array_equal(
indexer.extract_feature_by_slice(slice_index, j),
data[j][slice_index])
# indexer's __getitem__ should call `extract_feature_by_slice` method,
# result should be same with above.
numpy.testing.assert_array_equal(
indexer[slice_index, j],
data[j][slice_index])

@pytest.mark.parametrize('ndarray_index', [numpy.asarray([0, 1]),
numpy.asarray([1])])
@pytest.mark.parametrize('ndarray_index', [
numpy.asarray([0, 1]), numpy.asarray([1]),
numpy.asarray([], dtype=numpy.int32)])
@pytest.mark.parametrize('j', [0, 1])
def test_extract_feature_by_ndarray(self, indexer, data, ndarray_index, j):
numpy.testing.assert_array_equal(
indexer.extract_feature_by_slice(ndarray_index, j),
data[j][ndarray_index])
# indexer's __getitem__ should call `extract_feature_by_slice` method,
# result should be same with above.
numpy.testing.assert_array_equal(
indexer[ndarray_index, j],
data[j][ndarray_index])


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions tests/datasets_tests/test_numpy_tuple_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def test_get_item_slice_index(self, data, index):
for a, e in six.moves.zip(tuple_a, tuple_e):
numpy.testing.assert_array_equal(a, e)

@pytest.mark.parametrize('index', [numpy.asarray([2, 0]),
numpy.asarray([1])])
@pytest.mark.parametrize('index', [
numpy.asarray([2, 0]), numpy.asarray([1]),
numpy.asarray([], dtype=numpy.int32)])
def test_get_item_ndarray_index(self, long_data, index):
dataset = NumpyTupleDataset(*long_data)
actual = dataset[index]
Expand Down