MCPcopy Index your code
hub / github.com/dask/dask / _intersect_1d

Function _intersect_1d

dask/array/rechunk.py:60–156  ·  view source on GitHub ↗

Internal utility to intersect chunks for 1d after preprocessing. >>> new = cumdims_label(((2, 3), (2, 2, 1)), 'n') >>> old = cumdims_label(((2, 2, 1), (5,)), 'o') >>> _intersect_1d(_breakpoints(old[0], new[0])) # doctest: +NORMALIZE_WHITESPACE [[(0, slice(0, 2, None))],

(breaks)

Source from the content-addressed store, hash-verified

58
59
60def _intersect_1d(breaks):
61 """
62 Internal utility to intersect chunks for 1d after preprocessing.
63
64 >>> new = cumdims_label(((2, 3), (2, 2, 1)), 'n')
65 >>> old = cumdims_label(((2, 2, 1), (5,)), 'o')
66
67 >>> _intersect_1d(_breakpoints(old[0], new[0])) # doctest: +NORMALIZE_WHITESPACE
68 [[(0, slice(0, 2, None))],
69 [(1, slice(0, 2, None)), (2, slice(0, 1, None))]]
70 >>> _intersect_1d(_breakpoints(old[1], new[1])) # doctest: +NORMALIZE_WHITESPACE
71 [[(0, slice(0, 2, None))],
72 [(0, slice(2, 4, None))],
73 [(0, slice(4, 5, None))]]
74
75 Parameters
76 ----------
77
78 breaks: list of tuples
79 Each tuple is ('o', 8) or ('n', 8)
80 These are pairs of 'o' old or new 'n'
81 indicator with a corresponding cumulative sum,
82 or breakpoint (a position along the chunking axis).
83 The list of pairs is already ordered by breakpoint.
84 Note that an 'o' pair always occurs BEFORE
85 an 'n' pair if both share the same breakpoint.
86 Uses 'o' and 'n' to make new tuples of slices for
87 the new block crosswalk to old blocks.
88 """
89 # EXPLANATION:
90 # We know each new chunk is obtained from the old chunks, but
91 # from which ones and how? This function provides the answer.
92 # On return, each new chunk is represented as a list of slices
93 # of the old chunks. Therefore, paired with each slice is the
94 # index of the old chunk to which that slice refers.
95 # NOTE: if any nonzero-size new chunks extend beyond the total
96 # span of the old chunks, then those new chunks are assumed
97 # to be obtained from an imaginary old chunk that extends
98 # from the end of that total span to infinity. The chunk-
99 # index of this imaginary chunk follows in consecutive order
100 # from the chunk-indices of the actual old chunks.
101
102 # First, let us determine the index of the last old_chunk:
103 o_pairs = [pair for pair in breaks if pair[0] == "o"]
104 last_old_chunk_idx = len(o_pairs) - 2
105 last_o_br = o_pairs[-1][1] # end of range spanning all old chunks
106
107 start = 0 # start of a slice of an old chunk
108 last_end = 0
109 old_idx = 0 # index of old chunk
110 last_o_end = 0
111 ret = [] # will hold the list of new chunks
112 ret_next = [] # will hold the list of slices comprising one new chunk
113 for idx in range(1, len(breaks)): # Note start from the 2nd pair
114 # the interval between any two consecutive breakpoints is a potential
115 # new chunk:
116 label, br = breaks[idx]
117 last_label, last_br = breaks[idx - 1]

Callers 2

test_rechunk_internals_1Function · 0.90
old_to_newFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_rechunk_internals_1Function · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…