-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_dataset.py
60 lines (50 loc) · 2.51 KB
/
test_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
import numpy as np
import argparse
from tangle.lab import Dataset
from tangle.lab.config.lab_configuration import LabConfiguration
def main(data_dir, num_clusters, num_classes=10):
labConfig = LabConfiguration()
labConfig.model_data_dir = './data/' + data_dir
dataset = Dataset(labConfig, None)
cluster_clients = {}
for i in range(num_clusters):
cluster_clients[i] = []
for (client, cluster_id) in dataset.clients:
cluster_clients[cluster_id].append(client)
print(f'Number of Clients: {len(dataset.clients)}, cluster distribution: {[len(cluster_clients[i]) for i in range(num_clusters)]}')
cluster_train_data = {cluster: np.concatenate([dataset.train_data[client]['y'] for client in clients]) for (cluster, clients) in cluster_clients.items()}
print('Train data:')
for cluster in range(num_clusters):
hist, _ = np.histogram(cluster_train_data[cluster], bins=range(num_classes+1))
print(hist)
for cluster in range(num_clusters):
dataset_sizes = [len(dataset.train_data[client]['y']) for client in cluster_clients[cluster]]
mean_size = np.mean(dataset_sizes)
std_size = np.std(dataset_sizes)
print(f'Cluster {cluster}: data size per client: {mean_size} +/- {std_size}')
cluster_test_data = {cluster: np.concatenate([dataset.test_data[client]['y'] for client in clients]) for (cluster, clients) in cluster_clients.items()}
print('\nTest data:')
for cluster in range(num_clusters):
hist, _ = np.histogram(cluster_test_data[cluster], bins=range(num_classes+1))
print(hist)
for cluster in range(num_clusters):
dataset_sizes = [len(dataset.test_data[client]['y']) for client in cluster_clients[cluster]]
mean_size = np.mean(dataset_sizes)
std_size = np.std(dataset_sizes)
print(f'Cluster {cluster}: data size per client: {mean_size} +/- {std_size}')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data-dir',
help='dir for data ./data/x',
type=str,
required=True)
parser.add_argument('--num-clusters',
help='number of clusters',
type=int,
default=3)
parser.add_argument('--num-classes',
help='number of classes',
type=int,
default=10)
args = parser.parse_args()
main(args.data_dir, args.num_clusters, args.num_classes)