Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
better indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Feb 6, 2024
1 parent 0fd9c4f commit 4bec1d8
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/test_indexing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# (C) Copyright 2023 European Centre for Medium-Range Weather Forecasts.
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


import numpy as np

from ecml_tools.indexing import length_to_slices


def test_length_to_slices():
lengths = [5, 7, 11, 13]
datasets = [np.random.rand(n) for n in lengths]
total = sum(lengths)

combined = np.concatenate(datasets)

for start in range(total):
for stop in range(start, total):
for step in range(1, stop - start + 1):
index = slice(start, stop, step)
print(index)
slices = length_to_slices(index, lengths)
result = [d[i] for (d, i) in zip(datasets, slices) if i is not None]
result = np.concatenate(result)

if (combined[index].shape != result.shape) or not (
combined[index] == result
).all():
print(index)
print(combined[index])
print(result)
print(slices)
assert (combined[index] == result).all(), index


if __name__ == "__main__":
test_length_to_slices()

0 comments on commit 4bec1d8

Please sign in to comment.