-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunner.py
172 lines (112 loc) · 4.03 KB
/
runner.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
166
167
168
169
170
171
172
"""
Run algorithms vs. datasets in parallel
"""
from concurrent import futures
import csv
import importlib
import itertools
import os
from pathlib import Path
import sys
import time
import numpy as np
from sklearn.cluster import KMeans
from dataset import Dataset
from initialisations.base import InitialisationException
from metrics import ari
# Run-specific config
args = sys.argv
ALGORITHMS = [args[1]]
WHICH_SETS = args[2]
N_RUNS = int(args[3])
DATASETS = './datasets/' + WHICH_SETS + '/'
DIR_OUTPUT = '_output/'
def find_datasets(directory):
"""Find all datasets in a given directory"""
return [d for d in os.listdir(directory)
if os.path.isdir(os.path.join(directory, d))]
def load_dataset(datadir, which):
"""Load a dataset from disk"""
datafile = datadir + which + '/data.csv'
labelfile = datadir + which + '/labels.csv'
return Dataset(
which,
np.loadtxt(datafile, delimiter=','),
np.loadtxt(labelfile, delimiter=','),
)
def run_kmeans(dataset, algorithm, dsname, ctrstr):
"""Run the initialisation algorithm follower by k-means"""
num_clusters = dataset.num_clusters()
centroids = algorithm.generate(dataset.data, num_clusters)
est = KMeans(n_clusters=num_clusters, init=centroids, n_init=1)
est.fit(dataset.data)
return est.labels_, est.inertia_, est.n_iter_
def make_output_dir(output_dir):
"""Create empty directory for output if needed"""
Path(output_dir).mkdir(parents=True, exist_ok=True)
def save_log_file(outdir, info, ctrstr):
"""Write info to disk"""
info.append(ctrstr)
infofile = outdir + 'output-' + ctrstr + '.csv'
# print('Saving info to:', infofile)
with open(infofile, 'w+') as my_csv:
csvwriter = csv.writer(my_csv, delimiter=',')
csvwriter.writerows([info])
def save_label_file(outdir, labels, ctrstr):
"""Write discovered clustering to disk"""
labelfile = outdir + 'labels-' + ctrstr + '.csv'
# print('Saving labels to:', labelfile)
with open(labelfile, 'w+') as my_csv:
csvwriter = csv.writer(my_csv, delimiter=',')
csvwriter.writerow(labels)
def save_error_file(outdir, exc, ctrstr):
"""Save a log of exceptions"""
errorfile = outdir + 'exceptions-' + ctrstr + '.csv'
print('Saving errors to:', errorfile)
with open(errorfile, 'w+') as my_errors:
my_errors.write(exc.__class__.__name__ + ": " + str(exc) + "\n")
def handler(config):
"""Main processing method"""
algname = config[0]
datadir = config[1]
ctr = config[2]
ctr_str = f"{ctr:03d}"
start = time.perf_counter()
log = []
log.append(algname)
if WHICH_SETS == 'realworld':
log.append(datadir)
else:
# synthetic ones have a very specific naming convention incorporating
# their various characteristics
log.extend(datadir.split('_'))
print(log)
my_out_dir = DIR_OUTPUT + algname + '/' + WHICH_SETS + '/' + datadir + '/'
make_output_dir(my_out_dir)
# print("Called with:", my_output_dir)
dset = load_dataset(DATASETS, datadir)
try:
my_init = importlib.import_module('initialisations.' + algname)
labels, inertia, iterations = run_kmeans(dset, my_init, datadir, ctr_str)
except Exception as initexcept:
save_error_file(my_out_dir, initexcept, ctr_str)
return
log.append(inertia)
# print(labels)
ari_score = ari.score(dset.target, labels)
log.append(ari_score)
log.append(iterations)
# add time taken
end = time.perf_counter()
log.append(end - start)
print(log)
save_log_file(my_out_dir, log, ctr_str)
save_label_file(my_out_dir, labels, ctr_str)
# Main loop -------------------------------------------------------------------
if __name__ == '__main__':
all_sets = find_datasets(DATASETS)
configs = itertools.product(ALGORITHMS, all_sets, range(0, N_RUNS))
with futures.ProcessPoolExecutor() as executor:
res = executor.map(handler, configs)
# Force errors to be printed
print(len(list(res)))