-
Notifications
You must be signed in to change notification settings - Fork 16
/
Concurrent_AP.py
1187 lines (868 loc) · 41 KB
/
Concurrent_AP.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
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
#!/usr/bin/env python
# Concurrent_AP/Concurrent_AP.py
# Author: Gregory Giecold for the GC Yuan Lab
# Affiliation: Harvard University
# Contact: g.giecold@gmail.com, ggiecold@jimmy.harvard.edu
"""Concurrent_AP is a scalable and concurrent programming implementation
of Affinity Propagation clustering.
Affinity Propagation is a clustering algorithm based on passing messages
between data-points.
Storing and updating matrices of 'affinities', 'responsibilities' and
'similarities' between samples can be memory-intensive.
We address this issue through the use of an HDF5 data structure,
allowing Affinity Propagation clustering of arbitrarily large data-sets,
where other Python implementations would return a MemoryError on most machines.
We also significantly speed up the computations by splitting them up
across subprocesses, thereby taking full advantage of the resources
of multi-core processors and bypassing the Global Interpreter Lock
of the standard Python interpreter, CPython.
Reference
---------
Brendan J. Frey and Delbert Dueck., "Clustering by Passing Messages Between Data Points".
In: Science, Vol. 315, no. 5814, pp. 972-976. 2007
"""
from abc import ABCMeta, abstractmethod
from contextlib import closing
from ctypes import c_double, c_int
import gc
import multiprocessing
import numpy as np
import optparse
import os
import psutil
from sklearn.metrics.pairwise import euclidean_distances
import sys
import tables
from tempfile import NamedTemporaryFile
import time
import warnings
np.seterr(invalid = 'ignore')
warnings.filterwarnings('ignore', category = DeprecationWarning)
__all__ = []
def memory():
"""Determine memory specifications of the machine.
Returns
-------
mem_info : dictonary
Holds the current values for the total, free and used memory of the system.
"""
mem_info = dict()
for k, v in psutil.virtual_memory().__dict__.items():
mem_info[k] = int(v)
return mem_info
def get_chunk_size(N, n):
"""Given a two-dimensional array with a dimension of size 'N',
determine the number of rows or columns that can fit into memory.
Parameters
----------
N : int
The size of one of the dimensions of a two-dimensional array.
n : int
The number of arrays of size 'N' times 'chunk_size' that can fit in memory.
Returns
-------
chunk_size : int
The size of the dimension orthogonal to the one of size 'N'.
"""
mem_free = memory()['free']
if mem_free > 60000000:
chunk_size = int(((mem_free - 10000000) * 1000) / (4 * n * N))
return chunk_size
elif mem_free > 40000000:
chunk_size = int(((mem_free - 7000000) * 1000) / (4 * n * N))
return chunk_size
elif mem_free > 14000000:
chunk_size = int(((mem_free - 2000000) * 1000) / (4 * n * N))
return chunk_size
elif mem_free > 8000000:
chunk_size = int(((mem_free - 1400000) * 1000) / (4 * n * N))
return chunk_size
elif mem_free > 2000000:
chunk_size = int(((mem_free - 900000) * 1000) / (4 * n * N))
return chunk_size
elif mem_free > 1000000:
chunk_size = int(((mem_free - 400000) * 1000) / (4 * n * N))
return chunk_size
else:
print("\nERROR: Concurrent_AP: get_chunk_size: this machine does not "
"have enough free memory.\n")
sys.exit(1)
def chunk_generator(N, n):
"""Returns a generator of slice objects.
Parameters
----------
N : int
The size of one of the dimensions of a two-dimensional array.
n : int
The number of arrays of shape ('N', 'get_chunk_size(N, n)') that fit into
memory.
Returns
-------
Slice objects of the type 'slice(start, stop)' are generated, representing
the set of indices specified by 'range(start, stop)'.
"""
chunk_size = get_chunk_size(N, n)
for start in range(0, N, chunk_size):
yield slice(start, min(start + chunk_size, N))
def parse_options():
"""Specify the command line options to parse.
Returns
-------
opts : optparse.Values instance
Contains the option values in its 'dict' member variable.
args[0] : string or file-handler
The name of the file storing the data-set submitted
for Affinity Propagation clustering.
"""
parser = optparse.OptionParser(
usage = "Usage: %prog [options] file_name\n\n"
"file_name denotes the path where the data to be "
"processed by affinity propagation clustering is stored"
)
parser.add_option('-m', '--multiprocessing', dest = 'count',
default = multiprocessing.cpu_count(), type = 'int',
help = ("The number of processes to use (1..20) "
"[default %default]"))
parser.add_option('-f', '--file', dest = 'hdf5_file', default = None,
type = 'str',
help = ("File name or file handle of the HDF5 "
"data structure holding the matrices involved in "
"affinity propagation clustering "
"[default %default]"))
parser.add_option('-s', '--similarities', dest = 'similarities',
default = False, action = 'store_true',
help = ("Specifies if a matrix of similarities "
"has already been computed; only makes sense "
"with -f or --file in effect [default %default]"))
parser.add_option('-i', '--iterations', dest = 'max_iter',
default = 200, type = 'int',
help = ("The maximum number of message passing "
"iterations undergone before affinity "
"propagation returns, having reached "
"convergence or not [default %default]"))
parser.add_option('-c', '--convergence', dest = 'convergence_iter',
default = 15, type = 'int',
help = ("Specifies the number of consecutive "
"iterations without change in the number "
"of clusters that signals convergence "
"[default %default]") )
parser.add_option('-p', '--preference', dest = 'preference',
default = None, type = 'float',
help = ("The preference parameter of affinity "
"propagation [default %default]"))
parser.add_option('-d', '--damping', dest = 'damping',
default = 0.5, type = 'float',
help = ("The damping parameter of affinity "
"propagation; must be within 0.5 and 1.0 "
"[default %default]"))
parser.add_option('-v', '--verbose', dest = 'verbose',
default = False, action = 'store_true',
help = ("Turns on the display of messaging regarding "
"the status of the various stages of affinity "
"propagation clustering currently ongoing "
"on the user-specified data-set "
"[default %default]"))
opts, args = parser.parse_args()
if len(args) == 0:
parser.error('A data file must be specified')
if opts.similarities and (opts.hdf5_file is None):
parser.error("Option -s is conditional on -f")
if not (1 <= opts.count <= 20):
parser.error("The number of processes must range "
"from 1 to 20, inclusive")
if opts.max_iter <= 0:
parser.error("The number of iterations must be "
"a non-negative integer")
if opts.convergence_iter >= opts.max_iter:
parser.error("The number of iterations signalling convergence "
"cannot exceed the maximum number of iterations possibly "
"required")
if not (0.5 <= opts.damping <= 1.0):
parser.error("The damping parameter is restricted to values "
"between 0.5 and 1.0")
return opts, args[0]
def check_HDF5_arrays(hdf5_file, N, convergence_iter):
"""Check that the HDF5 data structure of file handle 'hdf5_file'
has all the required nodes organizing the various two-dimensional
arrays required for Affinity Propagation clustering
('Responsibility' matrix, 'Availability', etc.).
Parameters
----------
hdf5_file : string or file handle
Name of the Hierarchical Data Format under consideration.
N : int
The number of samples in the data-set that will undergo Affinity Propagation
clustering.
convergence_iter : int
Number of iterations with no change in the number of estimated clusters
that stops the convergence.
"""
Worker.hdf5_lock.acquire()
with tables.open_file(hdf5_file, 'r+') as fileh:
if not hasattr(fileh.root, 'aff_prop_group'):
fileh.create_group(fileh.root, "aff_prop_group")
atom = tables.Float32Atom()
filters = None
#filters = tables.Filters(5, 'blosc')
for feature in ('availabilities', 'responsibilities',
'similarities', 'temporaries'):
if not hasattr(fileh.root.aff_prop_group, feature):
fileh.create_carray(fileh.root.aff_prop_group, feature,
atom, (N, N), "Matrix of {0} for affinity "
"propagation clustering".format(feature),
filters = filters)
if not hasattr(fileh.root.aff_prop_group, 'parallel_updates'):
fileh.create_carray(fileh.root.aff_prop_group,
'parallel_updates', atom, (N, convergence_iter),
"Matrix of parallel updates for affinity propagation "
"clustering", filters = filters)
Worker.hdf5_lock.release()
class Worker(multiprocessing.Process, metaclass=ABCMeta):
"""Abstract Base Class whose methods are meant to be overriden
by the various classes of processes designed to handle
the various stages of Affinity Propagation clustering.
"""
hdf5_lock = multiprocessing.Lock()
@abstractmethod
def __init__(self, hdf5_file, path, slice_queue):
multiprocessing.Process.__init__(self)
self.hdf5_file = hdf5_file
self.path = path
self.slice_queue = slice_queue
def run(self):
while True:
try:
slc = self.slice_queue.get()
self.process(slc)
finally:
self.slice_queue.task_done()
@abstractmethod
def process(self, slc):
raise NotImplementedError()
class Similarities_worker(Worker):
"""Class of worker processes handling the computation of
a similarities matrix of pairwise distances between samples.
"""
def __init__(self, hdf5_file, path, array, slice_queue):
super(self.__class__, self).__init__(hdf5_file, path, slice_queue)
self.array = array
def process(self, rows_slice):
tmp = self.array[rows_slice, ...]
result = - euclidean_distances(tmp, self.array, squared = True)
with Worker.hdf5_lock:
with tables.open_file(self.hdf5_file, 'r+') as fileh:
hdf5_array = fileh.get_node(self.path)
hdf5_array[rows_slice, ...] = result
del tmp
class Fluctuations_worker(Worker):
"""Class of worker processes adding small random fluctuations
to the array specified by the node accessed via 'path' in 'hdf5_file'.
"""
def __init__(self, hdf5_file, path, random_state, N, slice_queue):
super(self.__class__, self).__init__(hdf5_file, path, slice_queue)
self.random_state = random_state
self.N = N
def process(self, rows_slice):
with Worker.hdf5_lock:
with tables.open_file(self.hdf5_file, 'r+') as fileh:
hdf5_array = fileh.get_node(self.path)
X = hdf5_array[rows_slice, ...]
eensy = np.finfo(np.float32).eps
weensy = np.finfo(np.float32).tiny * 100
tmp = self.random_state.randn(rows_slice.stop - rows_slice.start, self.N)
X += (eensy * X + weensy) * tmp
with Worker.hdf5_lock:
with tables.open_file(self.hdf5_file, 'r+') as fileh:
hdf5_array = fileh.get_node(self.path)
hdf5_array[rows_slice, ...] = X
del X
class Responsibilities_worker(Worker):
"""Class of worker processes that are tasked with computing
and updating the responsibility matrix.
"""
def __init__(self, hdf5_file, path, N, damping, slice_queue):
super(self.__class__, self).__init__(hdf5_file, path, slice_queue)
self.N = N
self.damping = damping
def process(self, rows_slice):
Worker.hdf5_lock.acquire()
with tables.open_file(self.hdf5_file, 'r+') as fileh:
A = fileh.get_node(self.path + '/availabilities')
S = fileh.get_node(self.path + '/similarities')
T = fileh.get_node(self.path + '/temporaries')
s = S[rows_slice, ...]
a = A[rows_slice, ...]
Worker.hdf5_lock.release()
ind = np.arange(0, rows_slice.stop - rows_slice.start)
tmp = a + s
I = tmp.argmax(axis = 1)
Y = tmp[ind, I]
tmp[ind, I] = - np.inf
Y_2 = tmp.max(axis = 1)
# tmp = R_new
np.subtract(s, Y[:, None], tmp)
tmp[ind, I] = s[ind, I] - Y_2
with Worker.hdf5_lock:
with tables.open_file(self.hdf5_file, 'r+') as fileh:
R = fileh.get_node(self.path + '/responsibilities')
r = R[rows_slice, ...]
# damping
r = r * self.damping + tmp * (1 - self.damping)
# tmp = R_p
tmp = np.where(r >= 0, r, 0)
tmp[ind, rows_slice.start + ind] = r[ind, rows_slice.start + ind]
Worker.hdf5_lock.acquire()
with tables.open_file(self.hdf5_file, 'r+') as fileh:
R = fileh.get_node(self.path + '/responsibilities')
T = fileh.get_node(self.path + '/temporaries')
R[rows_slice, ...] = r
T[rows_slice, ...] = tmp
Worker.hdf5_lock.release()
del a, r, s, tmp
class Rows_worker(Worker):
"""The processes instantiated from this class compute the sums
of row entries in an array accessed at node 'path' from the
hierarchidal data format at 'hdf5_file'. Those sums are stored
in the shared multiprocessing.Array data structure 'g_rows_sum'.
"""
def __init__(self, hdf5_file, path, N, slice_queue, g_rows_sum):
super(self.__class__, self).__init__(hdf5_file, path, slice_queue)
self.N = N
self.g_rows_sum = g_rows_sum
def process(self, rows_slice):
get_sum(self.hdf5_file, self.path, self.g_rows_sum,
out_lock, rows_slice)
def get_sum(hdf5_file, path, array_out, out_lock, rows_slice):
"""Access an array at node 'path' of the 'hdf5_file', compute the sums
along a slice of rows specified by 'rows_slice' and add the resulting
vector to 'array_out'.
Parameters
----------
hdf5_file : string or file handle
The location of the HDF5 data structure containing the matrices of availabitilites,
responsibilities and similarities among others.
path : string
Specify the node where the matrix whose row-sums are to be computed is located
within the given hierarchical data format.
array_out : multiprocessing.Array object
This ctypes array is allocated from shared memory and used by various
processes to store the outcome of their computations.
out_lock : multiprocessing.Lock object
Synchronize access to the values stored in 'array_out'.
rows_slice : slice object
Specifies a range of rows indices.
"""
Worker.hdf5_lock.acquire()
with tables.open_file(hdf5_file, 'r+') as fileh:
hdf5_array = fileh.get_node(path)
tmp = hdf5_array[rows_slice, ...]
Worker.hdf5_lock.release()
szum = np.sum(tmp, axis = 0)
with out_lock:
array_out += szum
del tmp
class Availabilities_worker(Worker):
"""Class of processes working on the computation and update of the
availability matrix for Affinity Propagation Clustering.
"""
def __init__(self, hdf5_file, path, N, damping, slice_queue, rows_sum):
super(self.__class__, self).__init__(hdf5_file, path, slice_queue)
self.N = N
self.damping = damping
self.rows_sum = rows_sum
def process(self, rows_slice):
with Worker.hdf5_lock:
with tables.open_file(self.hdf5_file, 'r+') as fileh:
T = fileh.get_node(self.path + '/temporaries')
tmp = T[rows_slice, ...]
ind = np.arange(0, rows_slice.stop - rows_slice.start)
# tmp = - A_new
tmp -= self.rows_sum
diag_A = tmp[ind, rows_slice.start + ind].copy()
np.clip(tmp, 0, np.inf, tmp)
tmp[ind, rows_slice.start + ind] = diag_A
Worker.hdf5_lock.acquire()
with tables.open_file(self.hdf5_file, 'r+') as fileh:
A = fileh.get_node(self.path + '/availabilities')
a = A[rows_slice, ...]
Worker.hdf5_lock.release()
# yet more damping
a = a * self.damping - tmp * (1 - self.damping)
with Worker.hdf5_lock:
with tables.open_file(self.hdf5_file, 'r+') as fileh:
A = fileh.get_node(self.path + '/availabilities')
T = fileh.get_node(self.path + '/temporaries')
A[rows_slice, ...] = a
T[rows_slice, ...] = tmp
del a, tmp
def terminate_processes(pid_list):
"""Terminate a list of processes by sending to each of them a SIGTERM signal,
pre-emptively checking if its PID might have been reused.
Parameters
----------
pid_list : list
A list of process identifiers identifying active processes.
"""
for proc in psutil.process_iter():
if proc.pid in pid_list:
proc.terminate()
def compute_similarities(hdf5_file, data, N_processes):
"""Compute a matrix of pairwise L2 Euclidean distances among samples from 'data'.
This computation is to be done in parallel by 'N_processes' distinct processes.
Those processes (which are instances of the class 'Similarities_worker')
are prevented from simultaneously accessing the HDF5 data structure
at 'hdf5_file' through the use of a multiprocessing.Lock object.
"""
slice_queue = multiprocessing.JoinableQueue()
pid_list = []
for i in range(N_processes):
worker = Similarities_worker(hdf5_file, '/aff_prop_group/similarities',
data, slice_queue)
worker.daemon = True
worker.start()
pid_list.append(worker.pid)
for rows_slice in chunk_generator(data.shape[0], 2 * N_processes):
slice_queue.put(rows_slice)
slice_queue.join()
slice_queue.close()
terminate_processes(pid_list)
gc.collect()
def add_preference(hdf5_file, preference):
"""Assign the value 'preference' to the diagonal entries
of the matrix of similarities stored in the HDF5 data structure
at 'hdf5_file'.
"""
Worker.hdf5_lock.acquire()
with tables.open_file(hdf5_file, 'r+') as fileh:
S = fileh.root.aff_prop_group.similarities
diag_ind = np.diag_indices(S.nrows)
S[diag_ind] = preference
Worker.hdf5_lock.release()
def add_fluctuations(hdf5_file, N_columns, N_processes):
"""This procedure organizes the addition of small fluctuations on top of
a matrix of similarities at 'hdf5_file' across 'N_processes'
different processes. Each of those processes is an instance of the
class 'Fluctuations_Worker' defined elsewhere in this module.
"""
random_state = np.random.RandomState(0)
slice_queue = multiprocessing.JoinableQueue()
pid_list = []
for i in range(N_processes):
worker = Fluctuations_worker(hdf5_file,
'/aff_prop_group/similarities', random_state,
N_columns, slice_queue)
worker.daemon = True
worker.start()
pid_list.append(worker.pid)
for rows_slice in chunk_generator(N_columns, 4 * N_processes):
slice_queue.put(rows_slice)
slice_queue.join()
slice_queue.close()
terminate_processes(pid_list)
gc.collect()
def compute_responsibilities(hdf5_file, N_columns, damping, N_processes):
"""Organize the computation and update of the responsibility matrix
for Affinity Propagation clustering with 'damping' as the eponymous
damping parameter. Each of the processes concurrently involved in this task
is an instance of the class 'Responsibilities_worker' defined above.
"""
slice_queue = multiprocessing.JoinableQueue()
pid_list = []
for i in range(N_processes):
worker = Responsibilities_worker(hdf5_file, '/aff_prop_group',
N_columns, damping, slice_queue)
worker.daemon = True
worker.start()
pid_list.append(worker.pid)
for rows_slice in chunk_generator(N_columns, 8 * N_processes):
slice_queue.put(rows_slice)
slice_queue.join()
slice_queue.close()
terminate_processes(pid_list)
def rows_sum_init(hdf5_file, path, out_lock, *numpy_args):
"""Create global variables sharing the same object as the one pointed by
'hdf5_file', 'path' and 'out_lock'.
Also Create a NumPy array copy of a multiprocessing.Array ctypes array
specified by '*numpy_args'.
"""
global g_hdf5_file, g_path, g_out, g_out_lock
g_hdf5_file, g_path, g_out_lock = hdf5_file, path, out_lock
g_out = to_numpy_array(*numpy_args)
def multiprocessing_get_sum(columns_slice):
get_sum(g_hdf5_file, g_path, g_out, g_out_lock, columns_slice)
def to_numpy_array(multiprocessing_array, shape, dtype):
"""Convert a share multiprocessing array to a numpy array.
No data copying involved.
"""
return np.frombuffer(multiprocessing_array.get_obj(),
dtype = dtype).reshape(shape)
def compute_rows_sum(hdf5_file, path, N_columns, N_processes, method = 'Process'):
"""Parallel computation of the sums across the rows of two-dimensional array
accessible at the node specified by 'path' in the 'hdf5_file'
hierarchical data format.
"""
assert isinstance(method, str), "parameter 'method' must consist in a string of characters"
assert method in ('Ordinary', 'Pool'), "parameter 'method' must be set to either of 'Ordinary' or 'Pool'"
if method == 'Ordinary':
rows_sum = np.zeros(N_columns, dtype = float)
chunk_size = get_chunk_size(N_columns, 2)
with Worker.hdf5_lock:
with tables.open_file(hdf5_file, 'r+') as fileh:
hdf5_array = fileh.get_node(path)
N_rows = hdf5_array.nrows
assert N_columns == N_rows
for i in range(0, N_columns, chunk_size):
slc = slice(i, min(i+chunk_size, N_columns))
tmp = hdf5_array[:, slc]
rows_sum[slc] = tmp[:].sum(axis = 0)
else:
rows_sum_array = multiprocessing.Array(c_double, N_columns, lock = True)
chunk_size = get_chunk_size(N_columns, 2 * N_processes)
numpy_args = rows_sum_array, N_columns, np.float64
with closing(multiprocessing.Pool(N_processes,
initializer = rows_sum_init,
initargs = (hdf5_file, path, rows_sum_array.get_lock()) +
numpy_args)) as pool:
pool.map_async(multiprocessing_get_sum,
chunk_generator(N_columns, 2 * N_processes), chunk_size)
pool.close()
pool.join()
rows_sum = to_numpy_array(*numpy_args)
gc.collect()
return rows_sum
def compute_availabilities(hdf5_file, N_columns, damping, N_processes, rows_sum):
"""Coordinates the computation and update of the availability matrix
for Affinity Propagation clustering.
Parameters
----------
hdf5_file : string or file handle
Specify access to the hierarchical data format used throughout all the iterations
of message-passing between data-points involved in Affinity Propagation clustering.
N_columns : int
The number of samples in the data-set subjected to Affinity Propagation clustering.
damping : float
The damping parameter of Affinity Propagation clustering, typically set to 0.5.
N_processes : int
The number of subprocesses involved in the parallel computation and update of the
matrix of availabitilies.
rows_sum : array of shape (N_columns,)
A vector containing, for each column entry of the similarities matrix, the sum
of its rows entries.
"""
slice_queue = multiprocessing.JoinableQueue()
pid_list = []
for i in range(N_processes):
worker = Availabilities_worker(hdf5_file, '/aff_prop_group',
N_columns, damping, slice_queue, rows_sum)
worker.daemon = True
worker.start()
pid_list.append(worker.pid)
for rows_slice in chunk_generator(N_columns, 8 * N_processes):
slice_queue.put(rows_slice)
slice_queue.join()
slice_queue.close()
terminate_processes(pid_list)
gc.collect()
def check_convergence(hdf5_file, iteration, convergence_iter, max_iter):
"""If the estimated number of clusters has not changed for 'convergence_iter'
consecutive iterations in a total of 'max_iter' rounds of message-passing,
the procedure herewith returns 'True'.
Otherwise, returns 'False'.
Parameter 'iteration' identifies the run of message-passing
that has just completed.
"""
Worker.hdf5_lock.acquire()
with tables.open_file(hdf5_file, 'r+') as fileh:
A = fileh.root.aff_prop_group.availabilities
R = fileh.root.aff_prop_group.responsibilities
P = fileh.root.aff_prop_group.parallel_updates
N = A.nrows
diag_ind = np.diag_indices(N)
E = (A[diag_ind] + R[diag_ind]) > 0
P[:, iteration % convergence_iter] = E
e_mat = P[:]
K = E.sum(axis = 0)
Worker.hdf5_lock.release()
if iteration >= convergence_iter:
se = e_mat.sum(axis = 1)
unconverged = (np.sum((se == convergence_iter) + (se == 0)) != N)
if (not unconverged and (K > 0)) or (iteration == max_iter):
return True
return False
def cluster_labels_init(hdf5_file, I, c_array_lock, *numpy_args):
global g_hdf5_file, g_I, g_c_array_lock, g_c
g_hdf5_file, g_I, g_c_array_lock = hdf5_file, I, c_array_lock
g_c = to_numpy_array(*numpy_args)
def cluster_labels_init_B(hdf5_file, I, ii, iix, s_reduced_array_lock,
*numpy_args):
global g_hdf5_file, g_I, g_ii, g_iix, g_s_reduced_array_lock, g_s_reduced
g_hdf5_file, g_I, g_ii, g_iix = hdf5_file, I, ii, iix
g_s_reduced_array_lock = s_reduced_array_lock
g_s_reduced = to_numpy_array(*numpy_args)
def multiprocessing_cluster_labels_A(rows_slice):
cluster_labels_A(g_hdf5_file, g_c, g_c_array_lock, g_I, rows_slice)
def multiprocessing_cluster_labels_B(rows_slice):
cluster_labels_B(g_hdf5_file, g_s_reduced, g_s_reduced_array_lock,
g_I, g_ii, g_iix, rows_slice)
def multiprocessing_cluster_labels_C(rows_slice):
cluster_labels_C(g_hdf5_file, g_c, g_c_array_lock, g_I, rows_slice)
def cluster_labels_A(hdf5_file, c, lock, I, rows_slice):
"""One of the task to be performed by a pool of subprocesses, as the first
step in identifying the cluster labels and indices of the cluster centers
for Affinity Propagation clustering.
"""
with Worker.hdf5_lock:
with tables.open_file(hdf5_file, 'r+') as fileh:
S = fileh.root.aff_prop_group.similarities
s = S[rows_slice, ...]
s = np.argmax(s[:, I], axis = 1)
with lock:
c[rows_slice] = s[:]
del s
def cluster_labels_B(hdf5_file, s_reduced, lock, I, ii, iix, rows_slice):
"""Second task to be performed by a pool of subprocesses before
the cluster labels and cluster center indices can be identified.
"""
with Worker.hdf5_lock:
with tables.open_file(hdf5_file, 'r+') as fileh:
S = fileh.root.aff_prop_group.similarities
s = S[rows_slice, ...]
s = s[:, ii]
s = s[iix[rows_slice]]
with lock:
s_reduced += s[:].sum(axis = 0)
del s
def cluster_labels_C(hdf5_file, c, lock, I, rows_slice):
"""Third and final task to be executed by a pool of subprocesses, as part of the
goal of finding the cluster to which each data-point has been assigned by
Affinity Propagation clustering on a given data-set.
"""
with Worker.hdf5_lock:
with tables.open_file(hdf5_file, 'r+') as fileh:
S = fileh.root.aff_prop_group.similarities
s = S[rows_slice, ...]
s = s[:, I]
with lock:
c[rows_slice] = np.argmax(s[:], axis = 1)
del s
def get_cluster_labels(hdf5_file, N_processes):
"""
Returns
-------
cluster_centers_indices : array of shape (n_clusters,)
Indices of cluster centers
labels : array of shape (n_samples,)
Specify the label of the cluster to which each point has been assigned.
"""
with Worker.hdf5_lock:
with tables.open_file(hdf5_file, 'r+') as fileh:
A = fileh.root.aff_prop_group.availabilities
R = fileh.root.aff_prop_group.responsibilities
N = A.nrows
diag_ind = np.diag_indices(N)
a = A[diag_ind]
r = R[diag_ind]
I = np.where(np.add(a[:], r[:]) > 0)[0]
K = I.size
if K == 0:
labels = np.empty((N, 1))
labels.fill(np.nan)
cluster_centers_indices = None
else:
c_array = multiprocessing.Array(c_int, N, lock = True)
chunk_size = get_chunk_size(N, 3 * N_processes)
numpy_args = c_array, N, np.int32
with closing(multiprocessing.Pool(N_processes,
initializer = cluster_labels_init,
initargs = (hdf5_file, I, c_array.get_lock())
+ numpy_args)) as pool:
pool.map_async(multiprocessing_cluster_labels_A,
chunk_generator(N, 3 * N_processes), chunk_size)
pool.close()
pool.join()
gc.collect()
c = to_numpy_array(*numpy_args)
c[I] = np.arange(K)
# determine the exemplars of clusters, applying some
# cosmetics to our results before returning them
for k in range(K):
ii = np.where(c == k)[0]
iix = np.full(N, False, dtype = bool)
iix[ii] = True
s_reduced_array = multiprocessing.Array(c_double, ii.size,
lock = True)
chunk_size = get_chunk_size(N, 3 * N_processes)
numpy_args = s_reduced_array, ii.size, np.float64
with closing(multiprocessing.Pool(N_processes,
initializer = cluster_labels_init_B,
initargs = (hdf5_file, I, ii, iix,
s_reduced_array.get_lock())
+ numpy_args)) as pool:
pool.map_async(multiprocessing_cluster_labels_B,
chunk_generator(N, 3 * N_processes), chunk_size)
pool.close()
pool.join()
s_reduced = to_numpy_array(*numpy_args)
j = np.argmax(s_reduced)
I[k] = ii[j]
gc.collect()
c_array = multiprocessing.Array(c_int, N, lock = True)
chunk_size = get_chunk_size(N, 3 * N_processes)
numpy_args = c_array, N, np.int32
with closing(multiprocessing.Pool(N_processes,
initializer = cluster_labels_init,
initargs = (hdf5_file, I, c_array.get_lock())
+ numpy_args)) as pool:
pool.map_async(multiprocessing_cluster_labels_C,
chunk_generator(N, 3 * N_processes), chunk_size)
pool.close()
pool.join()
c = to_numpy_array(*numpy_args)
c[I] = np.arange(K)
labels = I[c]
gc.collect()
cluster_centers_indices = np.unique(labels)
labels = np.searchsorted(cluster_centers_indices, labels)
return cluster_centers_indices, labels
def output_clusters(labels, cluster_centers_indices):
"""Write in tab-separated files the vectors of cluster identities and
of indices of cluster centers.
"""
here = os.getcwd()
try:
output_directory = os.path.join(here, 'concurrent_AP_output')