Documentation for Nebuladataset Module¶
NebulaDataset
¶
Source code in nebula/core/datasets/nebuladataset.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 |
|
balanced_iid_partition(dataset)
¶
Partition the dataset into balanced and IID (Independent and Identically Distributed) subsets for each client.
This function divides a dataset into a specified number of subsets (federated clients), where each subset has an equal class distribution. This makes the partition suitable for simulating IID data scenarios in federated learning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
list
|
The dataset to partition. It should be a list of tuples where each tuple represents a data sample and its corresponding label. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary where keys are client IDs (ranging from 0 to partitions_number-1) and values are lists of indices corresponding to the samples assigned to each client. |
The function ensures that each class is represented equally in each subset. The partitioning process involves iterating over each class, shuffling the indices of that class, and then splitting them equally among the clients. The function does not print the class distribution in each subset.
Example usage
federated_data = balanced_iid_partition(my_dataset)
This creates federated data subsets with equal class distributions.¶
Source code in nebula/core/datasets/nebuladataset.py
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
|
clear()
¶
Clear the dataset. This should remove or reset the dataset.
Source code in nebula/core/datasets/nebuladataset.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
data_partitioning(plot=False)
¶
Perform the data partitioning.
Source code in nebula/core/datasets/nebuladataset.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
dirichlet_partition(dataset, alpha=0.5, min_samples_size=50, balanced=False, max_iter=100, verbose=True)
¶
Partition the dataset among clients using a Dirichlet distribution. This function ensures each client gets at least min_samples_size samples.
Parameters¶
dataset : Dataset The dataset to partition. Must have a 'targets' attribute. alpha : float, default=0.5 Concentration parameter for the Dirichlet distribution. min_samples_size : int, default=50 Minimum number of samples required in each partition. balanced : bool, default=False If True, distribute each class evenly among clients. Otherwise, allocate according to a Dirichlet distribution. max_iter : int, default=100 Maximum number of iterations to try finding a valid partition. verbose : bool, default=True If True, print debug information per iteration.
Returns¶
partitions : dict[int, list[int]] Dictionary mapping each client index to a list of sample indices.
Source code in nebula/core/datasets/nebuladataset.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
|
generate_iid_map(dataset, plot=False)
abstractmethod
¶
Create an iid map of the dataset.
Source code in nebula/core/datasets/nebuladataset.py
470 471 472 473 474 475 |
|
generate_non_iid_map(dataset, partition='dirichlet', plot=False)
abstractmethod
¶
Create a non-iid map of the dataset.
Source code in nebula/core/datasets/nebuladataset.py
463 464 465 466 467 468 |
|
get_local_test_indices_map()
¶
Get the indices of the local test set for each participant. Indices whose labels are the same as the training set are selected.
Returns:
Type | Description |
---|---|
A dictionary mapping participant_id to a list of indices. |
Source code in nebula/core/datasets/nebuladataset.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
|
get_test_indices_map()
¶
Get the indices of the test set for each participant.
Returns:
Type | Description |
---|---|
A dictionary mapping participant_id to a list of indices. |
Source code in nebula/core/datasets/nebuladataset.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
|
homo_partition(dataset)
¶
Homogeneously partition the dataset into multiple subsets.
This function divides a dataset into a specified number of subsets, where each subset is intended to have a roughly equal number of samples. This method aims to ensure a homogeneous distribution of data across all subsets. It's particularly useful in scenarios where a uniform distribution of data is desired among all federated learning clients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Dataset
|
The dataset to partition. It should have 'data' and 'targets' attributes. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary where keys are subset indices (ranging from 0 to partitions_number-1) and values are lists of indices corresponding to the samples in each subset. |
The function randomly shuffles the entire dataset and then splits it into the number
of subsets specified by partitions_number
. It ensures that each subset has a similar number
of samples. The function also prints the class distribution in each subset for reference.
Example usage
federated_data = homo_partition(my_dataset)
This creates federated data subsets with homogeneous distribution.¶
Source code in nebula/core/datasets/nebuladataset.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
initialize_dataset()
abstractmethod
¶
Initialize the dataset. This should load or create the dataset.
Source code in nebula/core/datasets/nebuladataset.py
320 321 322 323 324 325 |
|
percentage_partition(dataset, percentage=20)
¶
Partition a dataset into multiple subsets with a specified level of non-IID-ness.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Dataset
|
The dataset to partition. It should have 'data' and 'targets' attributes. |
required |
percentage
|
int
|
A value between 0 and 100 that specifies the desired level of non-IID-ness for the labels of the federated data. This percentage controls the imbalance in the class distribution across different subsets. |
20
|
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary where keys are subset indices (ranging from 0 to partitions_number-1) and values are lists of indices corresponding to the samples in each subset. |
Example usage
federated_data = percentage_partition(my_dataset, percentage=20)
This creates federated data subsets with varying class distributions based on¶
a percentage of 20.¶
Source code in nebula/core/datasets/nebuladataset.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 |
|
plot_all_data_distribution(phase, dataset, partitions_map)
¶
Plot all of the data distribution of the dataset according to the partitions map provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
The dataset to plot (torch.utils.data.Dataset). |
required | |
partitions_map
|
The map of the dataset partitions. |
required |
Source code in nebula/core/datasets/nebuladataset.py
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 |
|
plot_data_distribution(phase, dataset, partitions_map)
¶
Plot the data distribution of the dataset.
Plot the data distribution of the dataset according to the partitions map provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
phase
|
The phase of the dataset (train, test, local_test). |
required | |
dataset
|
The dataset to plot (torch.utils.data.Dataset). |
required | |
partitions_map
|
The map of the dataset partitions. |
required |
Source code in nebula/core/datasets/nebuladataset.py
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
|
postprocess_partition(partition, y_data, min_samples_per_class=10)
¶
Post-process a partition to remove (and reassign) classes with too few samples per client.
For each class: - For clients with a count > 0 but below min_samples_per_class, remove those samples. - Reassign the removed samples to the client that already has the maximum count for that class.
Parameters¶
partition : dict[int, list[int]] The initial partition mapping client indices to sample indices. y_data : np.ndarray The array of labels corresponding to the dataset samples. min_samples_per_class : int, default=10 The minimum acceptable number of samples per class for each client.
Returns¶
new_partition : dict[int, list[int]] The updated partition.
Source code in nebula/core/datasets/nebuladataset.py
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 |
|
save_partitions()
¶
Save each partition data (train, test, and local test) to separate pickle files. The controller saves one file per partition for each data split.
Source code in nebula/core/datasets/nebuladataset.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
|
unbalanced_iid_partition(dataset, imbalance_factor=2)
¶
Partition the dataset into multiple IID (Independent and Identically Distributed) subsets with different size.
This function divides a dataset into a specified number of IID subsets (federated clients), where each subset has a different number of samples. The number of samples in each subset is determined by an imbalance factor, making the partition suitable for simulating imbalanced data scenarios in federated learning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
list
|
The dataset to partition. It should be a list of tuples where each tuple represents a data sample and its corresponding label. |
required |
imbalance_factor
|
float
|
The factor to determine the degree of imbalance among the subsets. A lower imbalance factor leads to more imbalanced partitions. |
2
|
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary where keys are client IDs (ranging from 0 to partitions_number-1) and values are lists of indices corresponding to the samples assigned to each client. |
The function ensures that each class is represented in each subset but with varying proportions. The partitioning process involves iterating over each class, shuffling the indices of that class, and then splitting them according to the calculated subset sizes. The function does not print the class distribution in each subset.
Example usage
federated_data = unbalanced_iid_partition(my_dataset, imbalance_factor=2)
This creates federated data subsets with varying number of samples based on¶
an imbalance factor of 2.¶
Source code in nebula/core/datasets/nebuladataset.py
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 |
|
NebulaPartition
¶
A class to handle the partitioning of datasets for federated learning.
Source code in nebula/core/datasets/nebuladataset.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
get_local_test_indices()
¶
Get the indices of the local test set based on the indices map.
Source code in nebula/core/datasets/nebuladataset.py
185 186 187 188 189 190 191 |
|
get_local_test_labels()
¶
Get the labels of the test set based on the indices map.
Source code in nebula/core/datasets/nebuladataset.py
209 210 211 212 213 214 215 |
|
get_test_indices()
¶
Get the indices of the test set based on the indices map.
Source code in nebula/core/datasets/nebuladataset.py
177 178 179 180 181 182 183 |
|
get_test_labels()
¶
Get the labels of the test set based on the indices map.
Source code in nebula/core/datasets/nebuladataset.py
201 202 203 204 205 206 207 |
|
get_train_indices()
¶
Get the indices of the training set based on the indices map.
Source code in nebula/core/datasets/nebuladataset.py
169 170 171 172 173 174 175 |
|
get_train_labels()
¶
Get the labels of the training set based on the indices map.
Source code in nebula/core/datasets/nebuladataset.py
193 194 195 196 197 198 199 |
|
load_partition()
¶
Load only the partition data corresponding to the current node. The node loads its train, test, and local test partition data from HDF5 files.
Source code in nebula/core/datasets/nebuladataset.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
set_local_test_indices()
¶
Set the local test indices for the current node.
Source code in nebula/core/datasets/nebuladataset.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
NebulaPartitionHandler
¶
Bases: Dataset
, ABC
A class to handle the loading of datasets from HDF5 files.
Source code in nebula/core/datasets/nebuladataset.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
set_data(data, targets, data_opt=None, targets_opt=None)
¶
Set the data and targets for the dataset.
Source code in nebula/core/datasets/nebuladataset.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
wait_for_file(file_path)
¶
Wait until the given file exists, polling every 'interval' seconds.
Source code in nebula/core/datasets/nebuladataset.py
27 28 29 30 31 |
|