Skip to content

Commit 0ad9ffd

Browse files
fix(data): take back excess items when partition ratios round up
I take items back from the partitions rounded up the most instead of clipping the last partition. I added both reported splits and a check on the rounded sizes across dataset lengths. Assisted-by: Claude Assisted-by: GPT-6 Astra Signed-off-by: mottopanikeiku <176798723+mottopanikeiku@users.noreply.github.com>
1 parent f4d2247 commit 0ad9ffd

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎monai/data/utils.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,9 @@ def partition_dataset(
11811181
11821182
Args:
11831183
data: input dataset to split, expect a list of data.
1184-
ratios: a list of ratio number to split the dataset, like [8, 1, 1]. If rounding the partition sizes
1185-
leaves some items over, they are added to the partitions that were rounded down the most.
1184+
ratios: relative partition sizes, e.g., [8, 1, 1]. If the rounded sizes fall short of the dataset
1185+
length, add one item to the partitions rounded down the most. If they exceed it, take one item
1186+
back from the partitions rounded up the most. Earlier partitions get the extra item on ties.
11861187
num_partitions: expected number of the partitions to evenly split, only works when `ratios` not specified.
11871188
shuffle: whether to shuffle the original dataset before splitting.
11881189
seed: random seed to shuffle the dataset, only works when `shuffle` is True.
@@ -1220,11 +1221,16 @@ def partition_dataset(
12201221
rsum = sum(ratios)
12211222
exact = [r / rsum * data_len for r in ratios]
12221223
sizes = [int(e + 0.5) for e in exact]
1223-
# rounding each size on its own can leave items without a partition,
1224-
# give them to the partitions whose sizes were rounded down the most
1225-
missing = max(data_len - sum(sizes), 0)
1226-
for j in sorted(range(len(sizes)), key=lambda k: sizes[k] - exact[k])[:missing]:
1227-
sizes[j] += 1
1224+
remainder = data_len - sum(sizes)
1225+
if remainder:
1226+
# A stable sort lets earlier partitions gain an item first and later partitions lose one first.
1227+
by_error = sorted(range(len(sizes)), key=lambda i: sizes[i] - exact[i])
1228+
if remainder > 0:
1229+
to_adjust, step = by_error[:remainder], 1
1230+
else:
1231+
to_adjust, step = by_error[remainder:], -1
1232+
for i in to_adjust:
1233+
sizes[i] += step
12281234
for size in sizes:
12291235
start_idx = next_idx
12301236
next_idx = min(start_idx + size, data_len)

‎tests/data/test_partition_dataset.py‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,41 @@ def test_ratios_rounding(self, input_param, result):
141141
"""
142142
self.assertListEqual(partition_dataset(**input_param), result)
143143

144-
@parameterized.expand([(63, [0.8, 0.1, 0.1]), (303, [0.7, 0.15, 0.15]), (32, [1, 1, 1, 1, 1])])
145-
def test_ratios_assign_every_item(self, data_len, ratios):
146-
"""Every item lands in exactly one partition when the data is shuffled.
144+
@parameterized.expand([(52, [3, 1, 3, 8], [10, 4, 10, 28]), (80, [9, 9, 3, 9, 2], [23, 23, 7, 22, 5])])
145+
def test_ratios_rounding_sizes(self, data_len, ratios, expected_sizes):
146+
"""When rounding gives too few or too many items, adjust the partitions with the largest errors.
147147
148148
Args:
149149
data_len: number of items to partition.
150150
ratios: the partition ratios.
151+
expected_sizes: partition sizes after correcting the rounded total.
151152
"""
152-
data = list(range(data_len))
153-
partitions = partition_dataset(data, ratios=ratios, shuffle=True, seed=0)
154-
self.assertListEqual(sorted(i for part in partitions for i in part), data)
153+
partitions = partition_dataset(range(data_len), ratios=ratios)
154+
self.assertListEqual([len(part) for part in partitions], expected_sizes)
155+
156+
@parameterized.expand(
157+
[(63, [0.8, 0.1, 0.1]), (303, [0.7, 0.15, 0.15]), (32, [1, 1, 1, 1, 1]), (80, [0, 9, 9, 3, 9, 2, 0])]
158+
)
159+
def test_ratios_assign_every_item(self, max_len, ratios):
160+
"""Check coverage and rounding across dataset lengths, including splits with a zero ratio.
161+
162+
Args:
163+
max_len: largest number of items to partition.
164+
ratios: the partition ratios.
165+
"""
166+
total_ratio = sum(ratios)
167+
for data_len in range(max_len + 1):
168+
with self.subTest(data_len=data_len):
169+
data = list(range(data_len))
170+
partitions = partition_dataset(data, ratios=ratios, shuffle=True, seed=0)
171+
self.assertListEqual(sorted(i for part in partitions for i in part), data)
172+
sizes = [len(part) for part in partitions]
173+
exact = [r / total_ratio * data_len for r in ratios]
174+
for size, share in zip(sizes, exact, strict=True):
175+
self.assertLess(abs(size - share), 1)
176+
rounded = [int(share + 0.5) for share in exact]
177+
if sum(rounded) == data_len:
178+
self.assertListEqual(sizes, rounded)
155179

156180

157181
if __name__ == "__main__":

0 commit comments

Comments
 (0)