Split the dataset into N partitions based on the given class labels. It can make sure the same ratio of classes in every partition. Others are same as :py:class:`monai.data.partition_dataset`. Args: data: input dataset to split, expect a list of data. classes: a lis
(
data: Sequence,
classes: Sequence[int],
ratios: Sequence[float] | None = None,
num_partitions: int | None = None,
shuffle: bool = False,
seed: int = 0,
drop_last: bool = False,
even_divisible: bool = False,
)
| 1240 | |
| 1241 | |
| 1242 | def partition_dataset_classes( |
| 1243 | data: Sequence, |
| 1244 | classes: Sequence[int], |
| 1245 | ratios: Sequence[float] | None = None, |
| 1246 | num_partitions: int | None = None, |
| 1247 | shuffle: bool = False, |
| 1248 | seed: int = 0, |
| 1249 | drop_last: bool = False, |
| 1250 | even_divisible: bool = False, |
| 1251 | ): |
| 1252 | """ |
| 1253 | Split the dataset into N partitions based on the given class labels. |
| 1254 | It can make sure the same ratio of classes in every partition. |
| 1255 | Others are same as :py:class:`monai.data.partition_dataset`. |
| 1256 | |
| 1257 | Args: |
| 1258 | data: input dataset to split, expect a list of data. |
| 1259 | classes: a list of labels to help split the data, the length must match the length of data. |
| 1260 | ratios: a list of ratio number to split the dataset, like [8, 1, 1]. |
| 1261 | num_partitions: expected number of the partitions to evenly split, only works when no `ratios`. |
| 1262 | shuffle: whether to shuffle the original dataset before splitting. |
| 1263 | seed: random seed to shuffle the dataset, only works when `shuffle` is True. |
| 1264 | drop_last: only works when `even_divisible` is False and no ratios specified. |
| 1265 | if True, will drop the tail of the data to make it evenly divisible across partitions. |
| 1266 | if False, will add extra indices to make the data evenly divisible across partitions. |
| 1267 | even_divisible: if True, guarantee every partition has same length. |
| 1268 | |
| 1269 | Examples:: |
| 1270 | |
| 1271 | >>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
| 1272 | >>> classes = [2, 0, 2, 1, 3, 2, 2, 0, 2, 0, 3, 3, 1, 3] |
| 1273 | >>> partition_dataset_classes(data, classes, shuffle=False, ratios=[2, 1]) |
| 1274 | [[2, 8, 4, 1, 3, 6, 5, 11, 12], [10, 13, 7, 9, 14]] |
| 1275 | |
| 1276 | """ |
| 1277 | if not issequenceiterable(classes) or len(classes) != len(data): |
| 1278 | raise ValueError(f"length of classes {classes} must match the dataset length {len(data)}.") |
| 1279 | datasets = [] |
| 1280 | class_indices = defaultdict(list) |
| 1281 | for i, c in enumerate(classes): |
| 1282 | class_indices[c].append(i) |
| 1283 | |
| 1284 | class_partition_indices: list[Sequence] = [] |
| 1285 | for _, per_class_indices in sorted(class_indices.items()): |
| 1286 | per_class_partition_indices = partition_dataset( |
| 1287 | data=per_class_indices, |
| 1288 | ratios=ratios, |
| 1289 | num_partitions=num_partitions, |
| 1290 | shuffle=shuffle, |
| 1291 | seed=seed, |
| 1292 | drop_last=drop_last, |
| 1293 | even_divisible=even_divisible, |
| 1294 | ) |
| 1295 | if not class_partition_indices: |
| 1296 | class_partition_indices = per_class_partition_indices |
| 1297 | else: |
| 1298 | for part, data_indices in zip(class_partition_indices, per_class_partition_indices): |
| 1299 | part += data_indices |
searching dependent graphs…