Skip to content

Commit

Permalink
Merge pull request #30 from NREL/boolean_masks
Browse files Browse the repository at this point in the history
Enable slicing with boolean masks
  • Loading branch information
MRossol committed Aug 27, 2020
2 parents 7b3ca08 + 2d91b41 commit 52c5f3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions rex/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def _check_slice(ds_slice):
ds_idx = None
if isinstance(ds_slice, (list, np.ndarray)):
in_slice = np.array(ds_slice)
if np.issubdtype(in_slice.dtype, np.dtype(bool)):
in_slice = np.where(in_slice)[0]

s = in_slice.min()
e = in_slice.max() + 1
ds_slice = slice(s, e, None)
Expand Down
2 changes: 1 addition & 1 deletion rex/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""rex Version number"""

__version__ = "0.2.12"
__version__ = "0.2.13"
6 changes: 6 additions & 0 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def check_dset(res_cls, ds_name):
assert isinstance(ds, np.ndarray)
assert ds.shape == (100,)
assert np.allclose(arr[times, 0], ds)
# boolean mask
mask = res_cls.time_index.month == 7
ds = res_cls[ds_name, mask]
assert isinstance(ds, np.ndarray)
assert ds.shape == (mask.sum(), ds_shape[1])
assert np.allclose(arr[mask], ds)
# time and site lists
with pytest.raises(IndexError):
assert res_cls[ds_name, times, sites]
Expand Down

0 comments on commit 52c5f3d

Please sign in to comment.