2020-12-13 04:42:47 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-04-20 18:33:54 +00:00
|
|
|
# TODO: replace prints with logging.
|
|
|
|
import sys
|
2020-11-17 23:59:20 +00:00
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
2021-05-03 18:28:48 +00:00
|
|
|
from sklearn.cluster import AffinityPropagation
|
2020-11-17 23:59:20 +00:00
|
|
|
import fire
|
2021-04-20 18:33:54 +00:00
|
|
|
from pathlib import Path
|
2021-05-03 06:39:55 +00:00
|
|
|
from multiprocessing import cpu_count
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from clustering_base import sim_to_dist, process_clustering_result, clustering_result, read_similarity_mat
|
2021-04-20 18:33:54 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
def affinity_clustering(similarities, output, *args, **kwargs):
|
2021-04-20 18:33:54 +00:00
|
|
|
subreddits, mat = read_similarity_mat(similarities)
|
2021-05-03 06:39:55 +00:00
|
|
|
clustering = _affinity_clustering(mat, *args, **kwargs)
|
|
|
|
cluster_data = process_clustering_result(clustering, subreddits)
|
|
|
|
cluster_data['algorithm'] = 'affinity'
|
|
|
|
return(cluster_data)
|
2020-11-17 23:59:20 +00:00
|
|
|
|
2021-04-20 18:33:54 +00:00
|
|
|
def _affinity_clustering(mat, subreddits, output, damping=0.9, max_iter=100000, convergence_iter=30, preference_quantile=0.5, random_state=1968, verbose=True):
|
2020-11-17 23:59:20 +00:00
|
|
|
'''
|
2021-05-03 06:39:55 +00:00
|
|
|
similarities: matrix of similarity scores
|
2020-11-17 23:59:20 +00:00
|
|
|
preference_quantile: parameter controlling how many clusters to make. higher values = more clusters. 0.85 is a good value with 3000 subreddits.
|
2020-12-13 04:42:47 +00:00
|
|
|
damping: parameter controlling how iterations are merged. Higher values make convergence faster and more dependable. 0.85 is a good value for the 10000 subreddits by author.
|
2020-11-17 23:59:20 +00:00
|
|
|
'''
|
2021-04-21 23:56:25 +00:00
|
|
|
print(f"damping:{damping}; convergenceIter:{convergence_iter}; preferenceQuantile:{preference_quantile}")
|
2020-11-17 23:59:20 +00:00
|
|
|
|
|
|
|
preference = np.quantile(mat,preference_quantile)
|
|
|
|
|
2020-12-13 04:42:47 +00:00
|
|
|
print(f"preference is {preference}")
|
2020-12-09 01:32:20 +00:00
|
|
|
print("data loaded")
|
2021-04-20 18:33:54 +00:00
|
|
|
sys.stdout.flush()
|
2020-11-17 23:59:20 +00:00
|
|
|
clustering = AffinityPropagation(damping=damping,
|
|
|
|
max_iter=max_iter,
|
|
|
|
convergence_iter=convergence_iter,
|
|
|
|
copy=False,
|
|
|
|
preference=preference,
|
|
|
|
affinity='precomputed',
|
2020-12-13 04:42:47 +00:00
|
|
|
verbose=verbose,
|
2020-11-17 23:59:20 +00:00
|
|
|
random_state=random_state).fit(mat)
|
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
cluster_data = process_clustering_result(clustering, subreddits)
|
|
|
|
output = Path(output)
|
|
|
|
output.parent.mkdir(parents=True,exist_ok=True)
|
|
|
|
cluster_data.to_feather(output)
|
|
|
|
print(f"saved {output}")
|
|
|
|
return clustering
|
2020-11-17 23:59:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
fire.Fire(affinity_clustering)
|