Getitem function This function creates a copy of the desired selection for array-like inputs when the selection is smaller than half of the original array. This avoids excess memory usage when extracting a small portion from a large array. For more information, see https://numpy
(obj, index)
| 406 | |
| 407 | |
| 408 | def getitem(obj, index): |
| 409 | """Getitem function |
| 410 | |
| 411 | This function creates a copy of the desired selection for array-like |
| 412 | inputs when the selection is smaller than half of the original array. This |
| 413 | avoids excess memory usage when extracting a small portion from a large array. |
| 414 | For more information, see |
| 415 | https://numpy.org/doc/stable/reference/arrays.indexing.html#basic-slicing-and-indexing. |
| 416 | |
| 417 | Parameters |
| 418 | ---------- |
| 419 | obj: ndarray, string, tuple, list |
| 420 | Object to get item from. |
| 421 | index: int, list[int], slice() |
| 422 | Desired selection to extract from obj. |
| 423 | |
| 424 | Returns |
| 425 | ------- |
| 426 | Selection obj[index] |
| 427 | |
| 428 | """ |
| 429 | try: |
| 430 | result = obj[index] |
| 431 | except IndexError as e: |
| 432 | raise ValueError( |
| 433 | "Array chunk size or shape is unknown. " |
| 434 | "Possible solution with x.compute_chunk_sizes()" |
| 435 | ) from e |
| 436 | |
| 437 | try: |
| 438 | if not result.flags.owndata and obj.size >= 2 * result.size: |
| 439 | result = result.copy() |
| 440 | except AttributeError: |
| 441 | pass |
| 442 | |
| 443 | return result |