Return `nb_chunks` slices of approximatively `nb_item / nb_chunks` each. Parameters ---------- nb_item : int Total number of items nb_chunks : int Number of chunks to return start_offset : int Shift start of slice by this amount Returns --
(nb_item: int, nb_chunks: int, start_offset=0)
| 7 | |
| 8 | |
| 9 | def chunk(nb_item: int, nb_chunks: int, start_offset=0) -> List[slice]: |
| 10 | """ |
| 11 | Return `nb_chunks` slices of approximatively `nb_item / nb_chunks` each. |
| 12 | |
| 13 | Parameters |
| 14 | ---------- |
| 15 | nb_item : int |
| 16 | Total number of items |
| 17 | |
| 18 | nb_chunks : int |
| 19 | Number of chunks to return |
| 20 | |
| 21 | start_offset : int |
| 22 | Shift start of slice by this amount |
| 23 | |
| 24 | Returns |
| 25 | ------- |
| 26 | A list of slices |
| 27 | |
| 28 | Examples |
| 29 | -------- |
| 30 | >>> chunks = chunk(103, 4) |
| 31 | >>> chunks |
| 32 | [slice(0, 26, None), slice(26, 52, None), slice(52, 78, None), slice(78, 103, None)] |
| 33 | """ |
| 34 | if nb_item <= nb_chunks: |
| 35 | return [slice(max(0, idx - start_offset), idx + 1) for idx in range(nb_item)] |
| 36 | |
| 37 | quotient = nb_item // nb_chunks |
| 38 | remainder = nb_item % nb_chunks |
| 39 | |
| 40 | quotients = [quotient] * nb_chunks |
| 41 | remainders = [1] * remainder + [0] * (nb_chunks - remainder) |
| 42 | |
| 43 | nb_elems_per_chunk = [ |
| 44 | quotient + remainder for quotient, remainder in zip(quotients, remainders) |
| 45 | ] |
| 46 | |
| 47 | accumulated = list(itertools.accumulate(nb_elems_per_chunk)) |
| 48 | shifted_accumulated = accumulated.copy() |
| 49 | shifted_accumulated.insert(0, 0) |
| 50 | shifted_accumulated.pop() |
| 51 | |
| 52 | return [ |
| 53 | slice(max(0, begin - start_offset), end) |
| 54 | for begin, end in zip(shifted_accumulated, accumulated) |
| 55 | ] |
| 56 | |
| 57 | |
| 58 | def df_indexed_like(df: DataFrame, axes: List[Index]) -> bool: |
no outgoing calls
no test coverage detected
searching dependent graphs…