-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_bt_clustering.py
474 lines (428 loc) · 24.7 KB
/
generate_bt_clustering.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
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
282
283
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
"""
Script for generating clusters using Butina-Taylor esque method with a specified distance function.
Handles large datasets; reads data into RAM in chunks and makes use of multithreading.
Output: two .dat files for cluster id assignment and leader index of cluster.
csv_file_or_dir: specifies a single file or path with format of csv files to be loaded. e.g: /path/iter_{}.csv or /path/iter_*.csv. Use {} instead of *.
output_dir: where to save the modified input csv files with cluster information added.
feature_name: specifies the column name for features in the csv file.
cutoff: instances within this cutoff distance belong to the same cluster.
dist_function: distance function to use.
process_count: number of processes to use when computing near neighbors.
Usage:
python generate_bt_clustering.py \
--csv_file_or_dir=../../datasets/file_{}.csv \
--output_dir=../../datasets/ \
--feature_name="Morgan FP_2_1024" \
--cutoff=0.4 \
--dist_function=tanimoto_dissimilarity \
--process_count=4 \
--process_batch_size=2**17 \
--dissimilarity_memmap_filename=../../datasets/dissimilarity_matrix_94857_94857.dat \
--index_name="Index ID"
"""
from __future__ import print_function
import argparse
import pandas as pd
import numpy as np
import glob
from multiprocessing import Process
import pathlib
import time
from data_utils import *
def get_n_instances(csv_files_list, process_batch_size):
# first get n_instances
instances_per_file = []
for f in csv_files_list:
for chunk in pd.read_csv(f, chunksize=process_batch_size):
instances_per_file.append(chunk.shape[0])
n_instances = np.sum(instances_per_file)
return n_instances
def get_features(csv_files_list, feature_name, index_name, tmp_dir, process_batch_size):
# first get n_instances
instances_per_file = []
for f in csv_files_list:
for chunk in pd.read_csv(f, chunksize=process_batch_size):
instances_per_file.append(chunk.shape[0])
n_features = len(chunk[feature_name].iloc[0])
n_instances = np.sum(instances_per_file)
X = np.memmap(tmp_dir+'/X.dat', dtype='float16', mode='w+', shape=(n_instances, n_features))
chunksize = process_batch_size
for i, f in enumerate(csv_files_list):
for chunk in pd.read_csv(f, chunksize=chunksize):
for batch_i in range(instances_per_file[i]//chunksize + 1):
row_start = batch_i*chunksize
row_end = min(instances_per_file[i], (batch_i+1)*chunksize)
if i > 0:
row_start = np.sum(instances_per_file[:i]) + batch_i*chunksize
row_end = min(np.sum(instances_per_file[:(i+1)]), np.sum(instances_per_file[:i]) + (batch_i+1)*chunksize)
if index_name is None:
X[row_start:row_end,:] = np.vstack([np.fromstring(x, 'u1') - ord('0') for x in chunk[feature_name]]).astype(float) # this is from: https://stackoverflow.com/a/29091970
else:
X[chunk[index_name].values.astype('int64'),:] = np.vstack([np.fromstring(x, 'u1') - ord('0') for x in chunk[feature_name]]).astype(float) # this is from: https://stackoverflow.com/a/29091970
X.flush()
return n_instances, n_features
"""
Function wrapper method for computing dissimilarity_matrix for a range of indices.
Used with multiprocessing.
"""
def compute_dissimilarity_matrix_wrapper(start_ind, end_ind,
n_instances, n_features,
tmp_dir, dist_func,
process_id, process_batch_size,
dissimilarity_memmap_filename):
X = np.memmap(tmp_dir+'/X.dat', dtype='float16', mode='r', shape=(n_instances, n_features))
if dissimilarity_memmap_filename is None:
dissimilarity_memmap_filename = tmp_dir+'/dissimilarity_matrix_{}_{}.dat'.format(n_instances, n_instances)
dissimilarity_matrix = np.memmap(dissimilarity_memmap_filename,
dtype='float16', mode='r+', shape=(n_instances, n_instances))
dissimilarity_process_matrix = np.load(tmp_dir+'/dissimilarity_process_matrix.npy')[start_ind:end_ind]
for i in range(end_ind-start_ind):
start_time = time.time()
row_start, row_end, col_start, col_end = dissimilarity_process_matrix[i,:]
X_cols = X[col_start:col_end]
X_rows = X[row_start:row_end]
dist_col_row = dist_func(X_cols, X_rows, X_batch_size=process_batch_size//2, Y_batch_size=process_batch_size//2)
dist_col_row = dist_col_row.reshape(X_cols.shape[0], X_rows.shape[0])
dissimilarity_matrix[row_start:row_end, col_start:col_end] = dist_col_row.T
dissimilarity_matrix[col_start:col_end, row_start:row_end] = dist_col_row
end_time = time.time()
print('pid: {}, at {} of {}. time {} seconds.'.format(process_id, i, (end_ind-start_ind), (end_time-start_time)))
del dissimilarity_matrix
"""
Function wrapper method for computing total number of nearest neigbors within cutoff for a set of instances.
Used with multiprocessing.
"""
def compute_nn_total_wrapper(start_ind, end_ind,
n_instances, cutoff,
tmp_dir,
process_id, process_batch_size,
dissimilarity_memmap_filename):
if dissimilarity_memmap_filename is None:
dissimilarity_memmap_filename = tmp_dir+'/dissimilarity_matrix_{}_{}.dat'.format(n_instances, n_instances)
dissimilarity_matrix = np.memmap(dissimilarity_memmap_filename,
dtype='float16', mode='r', shape=(n_instances, n_instances))
nn_total_vector = np.memmap(tmp_dir+'/nn_total_vector_{}.dat'.format(cutoff),
dtype='int32', mode='r+', shape=(n_instances,))
neighbor_matrix = np.memmap(tmp_dir+'/neighbor_matrix_{}_{}.dat'.format(n_instances, n_instances),
dtype='uint8', mode='r+', shape=(n_instances, n_instances))
row_batch_size = process_batch_size
n_cols = n_instances
for row in range(start_ind, end_ind):
row_total_neighbors = 0
for batch_i in range(n_cols//row_batch_size + 1):
col_start = batch_i*row_batch_size
col_end = (batch_i+1)*row_batch_size
dm_slice = dissimilarity_matrix[row, col_start:col_end]
row_total_neighbors += np.sum(dm_slice <= cutoff)
neighbor_idxs = col_start + np.where(dm_slice <= cutoff)[0]
neighbor_matrix[row, neighbor_idxs] = 1
neighbor_matrix[neighbor_idxs, row] = 1
nn_total_vector[row] = row_total_neighbors
del nn_total_vector
del neighbor_matrix
"""
Function wrapper method for clustering singletons.
Used with multiprocessing.
NOTE 1: output files cluster_assigment_vector_ and cluster_leader_idx_vector_ are 1D and assume same ordering of original data file.
NOTE 2: may update this in future to be 2D where first column is Index ID and second column is assignment value.
"""
def cluster_singletons_wrapper(start_ind, end_ind,
n_instances, cutoff,
output_dir, tmp_dir,
cluster_id_start,
process_id, process_batch_size,
dissimilarity_memmap_filename):
if dissimilarity_memmap_filename is None:
dissimilarity_memmap_filename = tmp_dir+'/dissimilarity_matrix_{}_{}.dat'.format(n_instances, n_instances)
dissimilarity_matrix = np.memmap(dissimilarity_memmap_filename,
dtype='float16', mode='r', shape=(n_instances, n_instances))
nn_total_vector = np.memmap(tmp_dir+'/nn_total_vector_{}.dat'.format(cutoff),
dtype='int32', mode='r', shape=(n_instances,))
cluster_assigment_vector = np.memmap(output_dir+'/cluster_assigment_vector_{}.dat'.format(cutoff),
dtype='int32', mode='r+', shape=(n_instances,))
cluster_leader_idx_vector = np.memmap(output_dir+'/cluster_leader_idx_vector_{}.dat'.format(cutoff),
dtype='int32', mode='r+', shape=(n_instances,))
row_batch_size = process_batch_size
n_cols = n_instances
for row in range(start_ind, end_ind):
if nn_total_vector[row] == 1:
row_total_neighbors = 0
closest_neighbor_idx = row
closest_neighbor_dist = 1.0
for batch_i in range(n_cols//row_batch_size + 1):
col_start = batch_i*row_batch_size
col_end = (batch_i+1)*row_batch_size
if col_start <= row and row < col_end:
dm_slice_1 = dissimilarity_matrix[row, col_start:row]
dm_slice_2 = dissimilarity_matrix[row, (row+1):col_end]
# process dm_slice_1
if dm_slice_1.shape[0] > 0:
min_idx_slice = np.argmin(dm_slice_1)
min_dist = dm_slice_1[min_idx_slice]
if min_dist <= cutoff:
leader_idx = cluster_leader_idx_vector[col_start + min_idx_slice]
cluster_leader_dist = dissimilarity_matrix[row, leader_idx]
if cluster_leader_dist < closest_neighbor_dist:
closest_neighbor_idx = leader_idx
closest_neighbor_dist = cluster_leader_dist
# process dm_slice_2
if dm_slice_2.shape[0] > 0:
min_idx_slice = np.argmin(dm_slice_2)
min_dist = dm_slice_2[min_idx_slice]
if min_dist <= cutoff:
leader_idx = cluster_leader_idx_vector[(row+1) + min_idx_slice]
cluster_leader_dist = dissimilarity_matrix[row, leader_idx]
if cluster_leader_dist < closest_neighbor_dist:
closest_neighbor_idx = leader_idx
closest_neighbor_dist = cluster_leader_dist
else:
dm_slice = dissimilarity_matrix[row, col_start:col_end]
if dm_slice.shape[0] > 0:
min_idx_slice = np.argmin(dm_slice)
min_dist = dm_slice[min_idx_slice]
if min_dist <= cutoff:
leader_idx = cluster_leader_idx_vector[col_start + min_idx_slice]
cluster_leader_dist = dissimilarity_matrix[row, leader_idx]
if cluster_leader_dist < closest_neighbor_dist:
closest_neighbor_idx = leader_idx
closest_neighbor_dist = cluster_leader_dist
# NOTE to self: check if true singletons truly have no cpd within cutoff.
if closest_neighbor_idx == row: # true singleton case -> assign to unique cluster
cluster_assigment_vector[row] = cluster_id_start + row
cluster_leader_idx_vector[row] = row
cluster_assigment_vector.flush()
cluster_leader_idx_vector.flush()
else: # false singleton case -> assign to cluster of closest cluster leader
cluster_assigment_vector[row] = cluster_assigment_vector[closest_neighbor_idx]
cluster_leader_idx_vector[row] = cluster_leader_idx_vector[closest_neighbor_idx]
cluster_assigment_vector.flush()
cluster_leader_idx_vector.flush()
del cluster_assigment_vector
del cluster_leader_idx_vector
def cluster_features(n_instances, n_features, dist_func, output_dir, tmp_dir,
cutoff=0.2, process_count=1, process_batch_size=2**17,
dissimilarity_memmap_filename=None):
total_clustering_time = 0
# step 1: generate
print('Generating dissimilarity_matrix...')
start_time = time.time()
if dissimilarity_memmap_filename is None:
dissimilarity_matrix = np.memmap(tmp_dir+'/dissimilarity_matrix_{}_{}.dat'.format(n_instances, n_instances),
dtype='float16', mode='w+', shape=(n_instances, n_instances))
del dissimilarity_matrix
# precompute indices of slices for dissimilarity_matrix
examples_per_slice = n_instances//process_count
dissimilarity_process_matrix = []
row_batch_size = process_batch_size // 2
col_batch_size = process_batch_size // 2
num_slices = 0
for process_id in range(process_count):
start_ind = process_id*examples_per_slice
end_ind = (process_id+1)*examples_per_slice
if process_id == (process_count-1):
end_ind = n_instances
if start_ind >= n_instances:
break
num_cols = end_ind - start_ind
for batch_col_i in range(num_cols//col_batch_size + 1):
col_start = start_ind + batch_col_i*col_batch_size
col_end = min(end_ind, start_ind + (batch_col_i+1)*col_batch_size)
for batch_row_i in range(col_end//row_batch_size + 1):
row_start = batch_row_i*row_batch_size
row_end = min(col_end, (batch_row_i+1)*row_batch_size)
dissimilarity_process_matrix.append([row_start, row_end, col_start, col_end])
num_slices += 1
dissimilarity_process_matrix = np.array(dissimilarity_process_matrix)
np.save(tmp_dir+'/dissimilarity_process_matrix.npy', dissimilarity_process_matrix)
del dissimilarity_process_matrix
# distribute slices among processes
process_pool = []
slices_per_process = num_slices//process_count
for process_id in range(process_count):
start_ind = process_id*slices_per_process
end_ind = (process_id+1)*slices_per_process
if process_id == (process_count-1):
end_ind = num_slices
if start_ind >= num_slices:
break
process_pool.append(Process(target=compute_dissimilarity_matrix_wrapper, args=(start_ind, end_ind,
n_instances, n_features,
tmp_dir, dist_func,
process_id, process_batch_size,
dissimilarity_memmap_filename)))
process_pool[process_id].start()
for process in process_pool:
process.join()
process.terminate()
end_time = time.time()
total_time = (end_time-start_time)/3600.0
total_clustering_time += total_time
print('Done generating dissimilarity_matrix. Took {} hours'.format(total_time))
# step 2: compute number of neighbors within cutoff for each instance
print('Computing nearest neighbors for each instance...')
start_time = time.time()
neighbor_matrix = np.memmap(tmp_dir+'/neighbor_matrix_{}_{}.dat'.format(n_instances, n_instances),
dtype='uint8', mode='w+', shape=(n_instances, n_instances))
nn_total_vector = np.memmap(tmp_dir+'/nn_total_vector_{}.dat'.format(cutoff),
dtype='int32', mode='w+', shape=(n_instances,))
del nn_total_vector
del neighbor_matrix
process_pool = []
examples_per_proc = n_instances//process_count
for process_id in range(process_count):
start_ind = process_id*examples_per_proc
end_ind = (process_id+1)*examples_per_proc
if process_id == (process_count-1):
end_ind = n_instances
if start_ind >= n_instances:
break
process_pool.append(Process(target=compute_nn_total_wrapper, args=(start_ind, end_ind,
n_instances, cutoff,
tmp_dir,
process_id, process_batch_size,
dissimilarity_memmap_filename)))
process_pool[process_id].start()
for process in process_pool:
process.join()
process.terminate()
end_time = time.time()
total_time = (end_time-start_time)/3600.0
total_clustering_time += total_time
print('Done computing nearest neighbors for each instance. Took {} hours'.format(total_time))
# step 3: start clustering non-singletons
print('Clustering non-singletons...')
start_time = time.time()
cluster_assigment_vector = np.memmap(output_dir+'/cluster_assigment_vector_{}.dat'.format(cutoff),
dtype='int32', mode='w+', shape=(n_instances,))
cluster_leader_idx_vector = np.memmap(output_dir+'/cluster_leader_idx_vector_{}.dat'.format(cutoff),
dtype='int32', mode='w+', shape=(n_instances,))
cluster_assigment_vector[:] = -1
cluster_leader_idx_vector[:] = -1
cluster_assigment_vector.flush()
cluster_leader_idx_vector.flush()
neighbor_matrix = np.memmap(tmp_dir+'/neighbor_matrix_{}_{}.dat'.format(n_instances, n_instances),
dtype='uint8', mode='r+', shape=(n_instances, n_instances))
nn_total_vector = np.memmap(tmp_dir+'/nn_total_vector_{}.dat'.format(cutoff),
dtype='int32', mode='r+', shape=(n_instances,))
cluster_id = 0
row_batch_size = process_batch_size
n_cols = n_instances
flush_every_n_iters = 5000
n_iters = 0
while nn_total_vector.max() > 1:
max_neighbor_ind = nn_total_vector.argmax()
print(max_neighbor_ind, nn_total_vector[max_neighbor_ind])
# gather neighbors of this instance with max number of neighbors
neighbor_indices = []
for batch_i in range(n_cols//row_batch_size + 1):
col_start = batch_i*row_batch_size
col_end = (batch_i+1)*row_batch_size
nm_slice = neighbor_matrix[max_neighbor_ind, col_start:col_end]
neighbor_indices.append(col_start + np.where(nm_slice > 0)[0])
neighbor_indices = np.hstack(neighbor_indices)
cluster_assigment_vector[neighbor_indices] = cluster_id
cluster_id += 1
cluster_leader_idx_vector[neighbor_indices] = max_neighbor_ind
# now remove these neighbors from other instance neighborhoods
for idx in neighbor_indices:
n_idx_neighbors = 0
for batch_i in range(n_cols//row_batch_size + 1):
col_start = batch_i*row_batch_size
col_end = (batch_i+1)*row_batch_size
nm_slice = neighbor_matrix[idx, col_start:col_end]
qualified_neighbors = np.where(nm_slice > 0)[0]
if qualified_neighbors.shape[0] > 0:
nn_total_vector[col_start + qualified_neighbors] -= 1
n_idx_neighbors += qualified_neighbors.shape[0]
# modify neighbor of idx with other indices so that it is no longer neighbor
neighbor_matrix[idx, col_start + qualified_neighbors] = 0
neighbor_matrix[col_start + qualified_neighbors, idx] = 0
nn_total_vector[idx] -= (n_idx_neighbors-1)
n_iters+=1
if n_iters > flush_every_n_iters:
n_iters=0
neighbor_matrix.flush()
nn_total_vector.flush()
cluster_leader_idx_vector.flush()
neighbor_matrix.flush()
nn_total_vector.flush()
cluster_leader_idx_vector.flush()
del cluster_assigment_vector
del cluster_leader_idx_vector
del neighbor_matrix
del nn_total_vector
end_time = time.time()
total_time = (end_time-start_time)/3600.0
total_clustering_time += total_time
print('Done clustering non-singletons. Took {} hours'.format(total_time))
# step 4: start clustering singletons
# false singletons are assigned to the same cluster of the closest instance that is within the cutoff
print('Clustering singletons...')
start_time = time.time()
process_pool = []
examples_per_proc = n_instances//process_count
for process_id in range(process_count):
start_ind = process_id*examples_per_proc
end_ind = (process_id+1)*examples_per_proc
if process_id == (process_count-1):
end_ind = n_instances
if start_ind >= n_instances:
break
process_pool.append(Process(target=cluster_singletons_wrapper, args=(start_ind, end_ind,
n_instances, cutoff,
output_dir, tmp_dir,
cluster_id,
process_id, process_batch_size,
dissimilarity_memmap_filename)))
process_pool[process_id].start()
for process in process_pool:
process.join()
process.terminate()
end_time = time.time()
total_time = (end_time-start_time)/3600.0
total_clustering_time += total_time
print('Done clustering singletons. Took {} hours'.format(total_time))
print('Done clustering. Took {} hours'.format(total_clustering_time))
np.random.seed(1103)
if __name__ == '__main__':
# read args
parser = argparse.ArgumentParser()
parser.add_argument('--csv_file_or_dir', action="store", dest="csv_file_or_dir", required=True)
parser.add_argument('--output_dir', action="store", dest="output_dir", required=True)
parser.add_argument('--feature_name', default='Morgan FP_2_1024', action="store", dest="feature_name", required=False)
parser.add_argument('--cutoff', type=float, default=0.2, action="store", dest="cutoff", required=False)
parser.add_argument('--dist_function', default='tanimoto_dissimilarity', action="store", dest="dist_function", required=False)
parser.add_argument('--process_count', type=int, default=1, action="store", dest="process_count", required=False)
parser.add_argument('--process_batch_size', type=int, default=2**16, action="store", dest="process_batch_size", required=False)
parser.add_argument('--dissimilarity_memmap_filename', default=None, action="store", dest="dissimilarity_memmap_filename", required=False)
parser.add_argument('--index_name', default=None, action="store", dest="index_name", required=False)
given_args = parser.parse_args()
csv_file_or_dir = given_args.csv_file_or_dir
output_dir = given_args.output_dir
feature_name = given_args.feature_name
cutoff = given_args.cutoff
dist_function = given_args.dist_function
process_count = given_args.process_count
process_batch_size = given_args.process_batch_size
dissimilarity_memmap_filename = given_args.dissimilarity_memmap_filename
index_name = given_args.index_name
# create tmp directory to store memmap arrays
tmp_dir = './tmp/'
pathlib.Path(tmp_dir).mkdir(parents=True, exist_ok=True)
pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True)
num_files = len(glob.glob(csv_file_or_dir.format('*')))
csv_files_list = [csv_file_or_dir.format(i) for i in range(num_files)]
print(csv_files_list)
if dissimilarity_memmap_filename is None:
n_instances, n_features = get_features(csv_files_list, feature_name, index_name, tmp_dir, process_batch_size)
else:
n_instances = get_n_instances(csv_files_list, process_batch_size)
n_features = -1
dist_func = feature_dist_func_dict()[dist_function]
# cluster
cluster_features(n_instances, n_features, dist_func, output_dir, tmp_dir,
cutoff=cutoff, process_count=process_count, process_batch_size=process_batch_size,
dissimilarity_memmap_filename=dissimilarity_memmap_filename)
# clean up tmp directory
#import shutil
#shutil.rmtree(tmp_dir)