]> code.communitydata.science - cdsc_reddit.git/blob - similarities/weekly_cosine_similarities.py
54856b030d10aa123e609da067ec6dcc9f74df62
[cdsc_reddit.git] / similarities / weekly_cosine_similarities.py
1 from pyspark.sql import functions as f
2 from pyspark.sql import SparkSession
3 from pyspark.sql import Window
4 import numpy as np
5 import pyarrow
6 import pandas as pd
7 import fire
8 from itertools import islice
9 from pathlib import Path
10 from similarities_helper import *
11
12
13 #tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_weekly.parquet')
14 def cosine_similarities_weekly(tfidf_path, outfile, term_colname, min_df = None, included_subreddits = None, topN = 500):
15     spark = SparkSession.builder.getOrCreate()
16     conf = spark.sparkContext.getConf()
17     print(outfile)
18     tfidf = spark.read.parquet(tfidf_path)
19     
20     if included_subreddits is None:
21         included_subreddits = select_topN_subreddits(topN)
22     else:
23         included_subreddits = set(open(included_subreddits))
24
25     print(f"computing weekly similarities for {len(included_subreddits)} subreddits")
26
27     print("creating temporary parquet with matrix indicies")
28     tempdir = prep_tfidf_entries_weekly(tfidf, term_colname, min_df, included_subreddits)
29
30     tfidf = spark.read.parquet(tempdir.name)
31
32     # the ids can change each week.
33     subreddit_names = tfidf.select(['subreddit','subreddit_id_new','week']).distinct().toPandas()
34     subreddit_names = subreddit_names.sort_values("subreddit_id_new")
35     subreddit_names['subreddit_id_new'] = subreddit_names['subreddit_id_new'] - 1
36     spark.stop()
37
38 d    weeks = sorted(list(subreddit_names.week.drop_duplicates()))
39     for week in weeks:
40         print(f"loading matrix: {week}")
41         mat = read_tfidf_matrix_weekly(tempdir.name, term_colname, week)
42         print('computing similarities')
43         sims = column_similarities(mat)
44         del mat
45
46         names = subreddit_names.loc[subreddit_names.week == week]
47         sims = pd.DataFrame(sims.todense())
48
49         sims = sims.rename({i: sr for i, sr in enumerate(names.subreddit.values)}, axis=1)
50         sims['subreddit'] = names.subreddit.values
51
52         write_weekly_similarities(outfile, sims, week, names)
53
54
55 def author_cosine_similarities_weekly(outfile, min_df=None , included_subreddits=None, topN=500):
56     return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
57                                       outfile,
58                                       'author',
59                                       min_df,
60                                       included_subreddits,
61                                       topN)
62
63 def term_cosine_similarities_weekly(outfile, min_df=None, included_subreddits=None, topN=500):
64     return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
65                                       outfile,
66                                       'term',
67                                       min_df,
68                                       included_subreddits,
69                                       topN)
70
71 if __name__ == "__main__":
72     fire.Fire({'author':author_cosine_similarities_weekly,
73                'term':term_cosine_similarities_weekly})

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