Slice an array or list of arrays. This takes an array-like, or a list of array-likes, and outputs: - arrays[start:stop] if `arrays` is an array-like - [x[start:stop] for x in arrays] if `arrays` is a list Can also work on list/array of indices: `slice_arrays(x, indices)` Argum
(arrays, start=None, stop=None)
| 495 | |
| 496 | |
| 497 | def slice_arrays(arrays, start=None, stop=None): |
| 498 | """Slice an array or list of arrays. |
| 499 | |
| 500 | This takes an array-like, or a list of |
| 501 | array-likes, and outputs: |
| 502 | - arrays[start:stop] if `arrays` is an array-like |
| 503 | - [x[start:stop] for x in arrays] if `arrays` is a list |
| 504 | |
| 505 | Can also work on list/array of indices: `slice_arrays(x, indices)` |
| 506 | |
| 507 | Arguments: |
| 508 | arrays: Single array or list of arrays. |
| 509 | start: can be an integer index (start index) |
| 510 | or a list/array of indices |
| 511 | stop: integer (stop index); should be None if |
| 512 | `start` was a list. |
| 513 | |
| 514 | Returns: |
| 515 | A slice of the array(s). |
| 516 | |
| 517 | Raises: |
| 518 | ValueError: If the value of start is a list and stop is not None. |
| 519 | """ |
| 520 | if arrays is None: |
| 521 | return [None] |
| 522 | if isinstance(start, list) and stop is not None: |
| 523 | raise ValueError('The stop argument has to be None if the value of start ' |
| 524 | 'is a list.') |
| 525 | elif isinstance(arrays, list): |
| 526 | if hasattr(start, '__len__'): |
| 527 | # hdf5 datasets only support list objects as indices |
| 528 | if hasattr(start, 'shape'): |
| 529 | start = start.tolist() |
| 530 | return [None if x is None else x[start] for x in arrays] |
| 531 | return [ |
| 532 | None if x is None else |
| 533 | None if not hasattr(x, '__getitem__') else x[start:stop] for x in arrays |
| 534 | ] |
| 535 | else: |
| 536 | if hasattr(start, '__len__'): |
| 537 | if hasattr(start, 'shape'): |
| 538 | start = start.tolist() |
| 539 | return arrays[start] |
| 540 | if hasattr(start, '__getitem__'): |
| 541 | return arrays[start:stop] |
| 542 | return [None] |
| 543 | |
| 544 | |
| 545 | def to_list(x): |
no outgoing calls
no test coverage detected