-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_order_generator.py
165 lines (132 loc) · 4.82 KB
/
2_order_generator.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import pandas as pd
import argparse
import json
import parameters
import os.path
import random
from sklearn.metrics.pairwise import cosine_similarity
### Setting variables ####################################################################
parser = argparse.ArgumentParser(description="Main")
parser.add_argument(
"--nb_clusters",
type=int,
default=4,
help="Number of clusters, default to 4",
)
parser.add_argument(
"--scenario",
type=str,
default="task_based",
help="Other choice : task_free. Default to task_based.",
)
args = parser.parse_args()
nb_clusters = args.nb_clusters
scenario = args.scenario
bench = parameters.benchmark()
datasets = bench.datasets
workdir = bench.workdir
### Functions #############################################################################
def cluster_count(nb_clusters: int, workdir: str):
"""Count the real number of clusters that were created for this dataset.
Args:
nb_clusters (int): number of clusters that was used to generate tasks clusters
Returns:
real_nb_cluster (int): real number of clusters
"""
real_nb_cluster = 0
for i in range(nb_clusters):
if os.path.isfile(
workdir + "Eval_set/{}".format(k_data) + "_{}".format(i) + "_eval_set.csv"
):
real_nb_cluster += 1
return real_nb_cluster
def get_clusters(real_nb_cluster: int, k_data: str, workdir: str, scenario: str):
"""Generates a cluster containing the clusters data.
Args:
real_nb_cluster (int): real number of clusters
k_data (str): name of the dataset
Returns :
clusters (dict) : dictionnary containing the data of each clusters.
"""
clusters = dict()
for i in range(real_nb_cluster):
if os.path.isfile(
workdir
+ "Clusters/{}".format(k_data)
+ "_cluster_{}_".format(i)
+ "{}.csv".format(scenario)
):
clusters[i] = pd.read_csv(
workdir
+ "Clusters/{}".format(k_data)
+ "_cluster_{}_".format(i)
+ "{}.csv".format(scenario)
)
return clusters
def get_centroids(clusters: dict):
"""Generates a dictionnary containing the centroid of each cluster.
Args:
clusters (dict): dictionnary containing the data of each cluster.
Returns:
centroids (dict): dictionnary containing the centroid of each cluster.
"""
centroids = dict()
for k, v in clusters.items():
centroids[k] = clusters[k].mean().to_numpy().reshape(1, -1)
return centroids
def get_similarities(centroids: dict):
"""Generates a dictionnary containing the cosine similarities between the cluster centroids.
Args:
centroids (dict): dictionnary containing the centroids.
Returns:
similarities (dict): dictionnary containing the similarities between centroids.
"""
similarities = dict()
for k, v in centroids.items():
similarities[k] = []
for key, value in centroids.items():
if key != k:
similarities[k].append(cosine_similarity(centroids[k], centroids[key]))
else:
similarities[k].append(0)
return similarities
def get_random_order(real_nb_cluster: int, workdir: str, scenario: str):
"""Generates the csv file containing the cluster order for a random ordering.
Args:
real_nb_cluster (int): real number of clusters
"""
first_order_items = []
final_order_items = []
for i in range(real_nb_cluster):
first_order_items.append(i)
random.shuffle(first_order_items)
for i in range(2):
for j in first_order_items:
final_order_items.append(j)
order_idx = dict()
j = 0
for i in final_order_items:
order_idx[j] = i
j += 1
print("Random order :")
print(order_idx)
with open(
workdir + "Orders/{}".format(k_data) + "_random_{}.json".format(scenario),
"w",
encoding="utf8",
) as outfile:
json.dump(order_idx, outfile)
###########################################################################################
############### Main ######################################################################
###########################################################################################
for k_data, v_data in datasets.items():
# Count number of clusers
real_nb_cluster = cluster_count(nb_clusters, workdir)
# Getting the clusters
clusters = get_clusters(real_nb_cluster, k_data, workdir, scenario)
# Getting the centroids of the clusters
centroids = get_centroids(clusters)
# Determining the cosine similarities between the centroids of the clusters
similarities = get_similarities(centroids)
# Generating the cluster orders
get_random_order(real_nb_cluster, workdir, scenario)