Skip to content

Commit

Permalink
Add a check for same object partitioners
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-narozniak committed Oct 14, 2024
1 parent 2d91b4d commit 824e6c0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
19 changes: 19 additions & 0 deletions datasets/flwr_datasets/federated_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(
self._partitioners: Dict[str, Partitioner] = _instantiate_partitioners(
partitioners
)
self._check_partitioners_correctness()
self._shuffle = shuffle
self._seed = seed
# _dataset is prepared lazily on the first call to `load_partition`
Expand Down Expand Up @@ -336,3 +337,21 @@ def _check_if_no_split_keyword_possible(self) -> None:
"Please set the `split` argument. You can only omit the split keyword "
"if there is exactly one partitioner specified."
)

def _check_partitioners_correctness(self) -> None:
"""Check if the partitioners are correctly specified.
Check if the multiple partitioner objects are not the same Python object, which
is not allowed, as the partitioner objects should be independent (one
partitioner per split).
"""
partitioners_keys = list(self._partitioners.keys())
for i, first_split in enumerate(partitioners_keys):
for j in range(i + 1, len(partitioners_keys)):
second_split = partitioners_keys[j]
if self._partitioners[first_split] is self._partitioners[second_split]:
raise ValueError(
f"The same partitioner object is used for multiple splits: "
f"('{first_split}', '{second_split}'). "
"Each partitioner should be a separate object."
)
52 changes: 52 additions & 0 deletions datasets/flwr_datasets/federated_dataset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
_load_mocked_dataset_dict_by_partial_download,
)
from flwr_datasets.partitioner import IidPartitioner, NaturalIdPartitioner, Partitioner
from flwr_datasets.preprocessor.divider import Divider

mocked_datasets = ["cifar100", "svhn", "sentiment140", "speech_commands"]

Expand Down Expand Up @@ -568,6 +569,57 @@ def test_use_load_dataset_kwargs(self) -> None:
with self.assertRaises(ValueError):
_ = fds.load_partition(0)

def test_incorrect_two_partitioners(self) -> None:
"""Test if the method raises ValueError with incorrect partitioners."""
partitioner = IidPartitioner(num_partitions=10)
partitioners: dict[str, Partitioner | int] = {
"train": partitioner,
"test": partitioner,
}
first_split = "train"
second_split = "test"
with self.assertRaises(ValueError) as context:
FederatedDataset(
dataset="mnist",
partitioners=partitioners,
)
self.assertIn(
f"The same partitioner object is used for multiple splits: "
f"('{first_split}', '{second_split}'). "
"Each partitioner should be a separate object.",
str(context.exception),
)

def test_incorrect_three_partitioners(self) -> None:
"""Test if the method raises ValueError with incorrect partitioners."""
partitioner = IidPartitioner(num_partitions=10)
partitioners: dict[str, int | Partitioner] = {
"train1": partitioner,
"train2": 10,
"test": partitioner,
}
divider = Divider(
divide_config={
"train1": 0.5,
"train2": 0.5,
},
divide_split="train",
)

with self.assertRaises(
ValueError,
) as context:

FederatedDataset(
dataset="mnist", partitioners=partitioners, preprocessor=divider
)

self.assertIn(
"The same partitioner object is used for multiple splits: "
"('train1', 'test'). Each partitioner should be a separate object.",
str(context.exception),
)


def datasets_are_equal(ds1: Dataset, ds2: Dataset) -> bool:
"""Check if two Datasets have the same values."""
Expand Down

0 comments on commit 824e6c0

Please sign in to comment.