1 from pyspark.sql import functions as f
2 from pyspark.sql import SparkSession
3 from pyspark.sql import Window
8 from itertools import islice
9 from pathlib import Path
10 from similarities_helper import *
11 from multiprocessing import Pool, cpu_count
13 def _week_similarities(tempdir, term_colname, week):
14 print(f"loading matrix: {week}")
15 mat = read_tfidf_matrix_weekly(tempdir.name, term_colname, week)
16 print('computing similarities')
17 sims = column_similarities(mat)
20 names = subreddit_names.loc[subreddit_names.week == week]
21 sims = pd.DataFrame(sims.todense())
23 sims = sims.rename({i: sr for i, sr in enumerate(names.subreddit.values)}, axis=1)
24 sims['_subreddit'] = names.subreddit.values
26 write_weekly_similarities(outfile, sims, week, names)
28 #tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_weekly.parquet')
29 def cosine_similarities_weekly(tfidf_path, outfile, term_colname, min_df = None, included_subreddits = None, topN = 500):
30 spark = SparkSession.builder.getOrCreate()
31 conf = spark.sparkContext.getConf()
33 tfidf = spark.read.parquet(tfidf_path)
35 if included_subreddits is None:
36 included_subreddits = select_topN_subreddits(topN)
38 included_subreddits = set(open(included_subreddits))
40 print(f"computing weekly similarities for {len(included_subreddits)} subreddits")
42 print("creating temporary parquet with matrix indicies")
43 tempdir = prep_tfidf_entries_weekly(tfidf, term_colname, min_df, max_df=None, included_subreddits=included_subreddits)
45 tfidf = spark.read.parquet(tempdir.name)
47 # the ids can change each week.
48 subreddit_names = tfidf.select(['subreddit','subreddit_id_new','week']).distinct().toPandas()
49 subreddit_names = subreddit_names.sort_values("subreddit_id_new")
50 subreddit_names['subreddit_id_new'] = subreddit_names['subreddit_id_new'] - 1
53 weeks = sorted(list(subreddit_names.week.drop_duplicates()))
54 # do this step in parallel if we have the memory for it.
55 # should be doable with pool.map
57 def week_similarities_helper(week):
58 _week_similarities(tempdir, term_colname, week)
60 with Pool(cpu_count()) as pool: # maybe it can be done with 40 cores on the huge machine?
61 list(pool.map(week_similarities_helper,weeks))
63 def author_cosine_similarities_weekly(outfile, min_df=2 , included_subreddits=None, topN=500):
64 return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
71 def term_cosine_similarities_weekly(outfile, min_df=None, included_subreddits=None, topN=500):
72 return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
79 if __name__ == "__main__":
80 fire.Fire({'authors':author_cosine_similarities_weekly,
81 'terms':term_cosine_similarities_weekly})