]> code.communitydata.science - cdsc_reddit.git/commitdiff
Changes for cosine similarities on klone.
authorNate E TeBlunthuis <nathante@n3008.hyak.local>
Tue, 6 Apr 2021 06:21:06 +0000 (23:21 -0700)
committerNate E TeBlunthuis <nathante@n3008.hyak.local>
Tue, 6 Apr 2021 06:21:06 +0000 (23:21 -0700)
clustering/clustering.py
similarities/cosine_similarities.py
similarities/similarities_helper.py
similarities/tfidf.py
similarities/weekly_cosine_similarities.py

index e6523045267fd93c1424b63ff46af81e5f02b289..4cde71787eb5f208a0e51afb68ef57f1f99c1106 100755 (executable)
@@ -14,8 +14,9 @@ def affinity_clustering(similarities, output, damping=0.9, max_iter=100000, conv
 
     df = pd.read_feather(similarities)
     n = df.shape[0]
-    mat = np.array(df.drop('subreddit',1))
+    mat = np.array(df.drop('_subreddit',1))
     mat[range(n),range(n)] = 1
+    assert(all(np.diag(mat)==1))
 
     preference = np.quantile(mat,preference_quantile)
 
index 95fa1fbf6ca70111838a05b4272fb39e1b7464bf..38b1d7c7c1643ac4c64cd85153cbda4bc9c3eec1 100644 (file)
@@ -9,7 +9,8 @@ def cosine_similarities(infile, term_colname, outfile, min_df=None, max_df=None,
 
 
 def term_cosine_similarities(outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, exclude_phrases=False, from_date=None, to_date=None):
-    return cosine_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms.parquet',
+
+    return cosine_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms_100k.parquet',
                                'term',
                                outfile,
                                min_df,
@@ -22,7 +23,7 @@ def term_cosine_similarities(outfile, min_df=None, max_df=None, included_subredd
                                )
 
 def author_cosine_similarities(outfile, min_df=2, max_df=None, included_subreddits=None, topN=10000, from_date=None, to_date=None):
-    return cosine_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
+    return cosine_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',
                                'author',
                                outfile,
                                min_df,
@@ -35,7 +36,7 @@ def author_cosine_similarities(outfile, min_df=2, max_df=None, included_subreddi
                                )
 
 def author_tf_similarities(outfile, min_df=2, max_df=None, included_subreddits=None, topN=10000, from_date=None, to_date=None):
-    return cosine_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
+    return cosine_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',
                                'author',
                                outfile,
                                min_df,
index 9e33c9d105c4c4190ce47c84cf56be3b753326b0..57a36ca924ea5d0d991e976347cac0362b97ac20 100644 (file)
@@ -89,7 +89,8 @@ def similarities(infile, simfunc, term_colname, outfile, min_df=None, max_df=Non
     print("loading matrix")
     #    mat = read_tfidf_matrix("term_tfidf_entries7ejhvnvl.parquet", term_colname)
     mat = read_tfidf_matrix(tempdir.name, term_colname, tfidf_colname)
-    print('computing similarities')
+    print(f'computing similarities on mat. mat.shape:{mat.shape}')
+    print(f"size of mat is:{mat.data.nbytes}")
     sims = simfunc(mat)
     del mat
 
@@ -387,7 +388,7 @@ def build_tfidf_dataset(df, include_subs, term_colname, tf_family=tf_weight.Norm
 
     return df
 
-def select_topN_subreddits(topN, path="/gscratch/comdata/output/reddit_similarity/subreddits_by_num_comments_nonswf.csv"):
+def select_topN_subreddits(topN, path="/gscratch/comdata/output/reddit_similarity/subreddits_by_num_comments_nonsfw.csv"):
     rankdf = pd.read_csv(path)
     included_subreddits = set(rankdf.loc[rankdf.comments_rank <= topN,'subreddit'].values)
     return included_subreddits
index f0b5d6471898045ce5559f7d41868331c671c784..30033a829dc1e563b1267f817d92378414f0c9d1 100644 (file)
@@ -24,8 +24,8 @@ def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_
 def tfidf(inpath, outpath, topN, term_colname, exclude, included_subreddits):
     return _tfidf_wrapper(build_tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
 
-def tfidf_weekly(inpath, outpath, topN, term_colname, exclude):
-    return _tfidf_wrapper(build_weekly_tfidf_dataset, inpath, outpath, topN, term_colname, included_subreddits)
+def tfidf_weekly(inpath, outpath, topN, term_colname, exclude, included_subreddits):
+    return _tfidf_wrapper(build_weekly_tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
 
 def tfidf_authors(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
                   topN=25000):
index 4d496f0ea6d60647fe4b2811f788a3f82536642d..f9c96664b1a8aae485d39e1bfb112430800b4c8e 100644 (file)
@@ -8,7 +8,22 @@ import fire
 from itertools import islice
 from pathlib import Path
 from similarities_helper import *
+from multiprocessing import pool
 
+def _week_similarities(tempdir, term_colname, week):
+        print(f"loading matrix: {week}")
+        mat = read_tfidf_matrix_weekly(tempdir.name, term_colname, week)
+        print('computing similarities')
+        sims = column_similarities(mat)
+        del mat
+
+        names = subreddit_names.loc[subreddit_names.week == week]
+        sims = pd.DataFrame(sims.todense())
+
+        sims = sims.rename({i: sr for i, sr in enumerate(names.subreddit.values)}, axis=1)
+        sims['_subreddit'] = names.subreddit.values
+
+        write_weekly_similarities(outfile, sims, week, names)
 
 #tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_weekly.parquet')
 def cosine_similarities_weekly(tfidf_path, outfile, term_colname, min_df = None, included_subreddits = None, topN = 500):
@@ -36,24 +51,17 @@ def cosine_similarities_weekly(tfidf_path, outfile, term_colname, min_df = None,
     spark.stop()
 
     weeks = sorted(list(subreddit_names.week.drop_duplicates()))
-    for week in weeks:
-        print(f"loading matrix: {week}")
-        mat = read_tfidf_matrix_weekly(tempdir.name, term_colname, week)
-        print('computing similarities')
-        sims = column_similarities(mat)
-        del mat
+    # do this step in parallel if we have the memory for it.
+    # should be doable with pool.map
 
-        names = subreddit_names.loc[subreddit_names.week == week]
-        sims = pd.DataFrame(sims.todense())
-
-        sims = sims.rename({i: sr for i, sr in enumerate(names.subreddit.values)}, axis=1)
-        sims['subreddit'] = names.subreddit.values
-
-        write_weekly_similarities(outfile, sims, week, names)
+    def week_similarities_helper(week):
+        _week_similarities(tempdir, term_colname, week)
 
+    with Pool(40) as pool: # maybe it can be done with 40 cores on the huge machine?
+        list(pool.map(weeks,week_similarities_helper))
 
-def author_cosine_similarities_weekly(outfile, min_df=None , included_subreddits=None, topN=500):
-    return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
+def author_cosine_similarities_weekly(outfile, min_df=2 , included_subreddits=None, topN=500):
+    return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors_100k.parquet',
                                       outfile,
                                       'author',
                                       min_df,
@@ -61,7 +69,7 @@ def author_cosine_similarities_weekly(outfile, min_df=None , included_subreddits
                                       topN)
 
 def term_cosine_similarities_weekly(outfile, min_df=None, included_subreddits=None, topN=500):
-    return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
+    return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms_100k.parquet',
                                       outfile,
                                       'term',
                                       min_df,
@@ -69,5 +77,5 @@ def term_cosine_similarities_weekly(outfile, min_df=None, included_subreddits=No
                                       topN)
 
 if __name__ == "__main__":
-    fire.Fire({'author':author_cosine_similarities_weekly,
-               'term':term_cosine_similarities_weekly})
+    fire.Fire({'authors':author_cosine_similarities_weekly,
+               'terms':term_cosine_similarities_weekly})

Community Data Science Collective || Want to submit a patch?