-import fire
-import sys
-
-# silhouette is the only one that doesn't need the feature matrix. So it's probably the only one that's worth trying.
-
-@dataclass
-class clustering_result:
- outpath:Path
- damping:float
- max_iter:int
- convergence_iter:int
- preference_quantile:float
- silhouette_score:float
- alt_silhouette_score:float
- name:str
-
-def do_clustering(damping, convergence_iter, preference_quantile, name, mat, subreddits, max_iter, outdir:Path, random_state, verbose, alt_mat):
- if name is None:
- name = f"damping-{damping}_convergenceIter-{convergence_iter}_preferenceQuantile-{convergence_iter}"
- print(name)
- sys.stdout.flush()
- outpath = outdir / (str(name) + ".feather")
- print(outpath)
- clustering = _affinity_clustering(mat, subreddits, outpath, damping, max_iter, convergence_iter, preference_quantile, random_state, verbose)
- score = silhouette_score(clustering.affinity_matrix_, clustering.labels_, metric='precomputed')
- alt_score = silhouette_score(alt_mat, clustering.labels_, metric='precomputed')
-
- res = clustering_result(outpath=outpath,
- damping=damping,
- max_iter=max_iter,
- convergence_iter=convergence_iter,
- preference_quantile=preference_quantile,
- silhouette_score=score,
- alt_silhouette_score=score,
- name=str(name))
-
- return res
-
-# alt similiarities is for checking the silhouette coefficient of an alternative measure of similarity (e.g., topic similarities for user clustering).
-
-def select_affinity_clustering(similarities, outdir, damping=[0.9], max_iter=100000, convergence_iter=[30], preference_quantile=[0.5], random_state=1968, verbose=True, alt_similarities=None, J=None):