]> code.communitydata.science - cdsc_reddit.git/blob - author_cosine_similarity.py
7ae708b72289bb8cb36eb332cbc11b54e22f6dfa
[cdsc_reddit.git] / author_cosine_similarity.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 cosine_similarities, prep_tfidf_entries, read_tfidf_matrix, column_similarities
11
12 spark = SparkSession.builder.getOrCreate()
13 conf = spark.sparkContext.getConf()
14
15 # outfile = '/gscratch/comdata/users/nathante/test_similarities_500.feather'; min_df = None; included_subreddits=None; similarity_threshold=0;
16 def author_cosine_similarities(outfile, min_df = None, included_subreddits=None, similarity_threshold=0, topN=500):
17     '''
18     Compute similarities between subreddits based on tfi-idf vectors of author comments
19     
20     included_subreddits : string
21         Text file containing a list of subreddits to include (one per line) if included_subreddits is None then do the top 500 subreddits
22
23     similarity_threshold : double (default = 0)
24         set > 0 for large numbers of subreddits to get an approximate solution using the DIMSUM algorithm
25 https://stanford.edu/~rezab/papers/dimsum.pdf. If similarity_threshold=0 we get an exact solution using an O(N^2) algorithm.
26
27     min_df : int (default = 0.1 * (number of included_subreddits)
28          exclude terms that appear in fewer than this number of documents.
29
30     outfile: string
31          where to output csv and feather outputs
32 '''
33
34     spark = SparkSession.builder.getOrCreate()
35     conf = spark.sparkContext.getConf()
36     print(outfile)
37
38     tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_authors.parquet')
39
40     if included_subreddits is None:
41         rankdf = pd.read_csv("/gscratch/comdata/users/nathante/cdsc-reddit/subreddits_by_num_comments.csv")
42         included_subreddits = set(rankdf.loc[rankdf.comments_rank <= topN,'subreddit'].values)
43
44     else:
45         included_subreddits = set(open(included_subreddits))
46
47     print("creating temporary parquet with matrix indicies")
48     tempdir = prep_tfidf_entries(tfidf, 'author', min_df, included_subreddits)
49     tfidf = spark.read.parquet(tempdir.name)
50     subreddit_names = tfidf.select(['subreddit','subreddit_id_new']).distinct().toPandas()
51     subreddit_names = subreddit_names.sort_values("subreddit_id_new")
52     subreddit_names['subreddit_id_new'] = subreddit_names['subreddit_id_new'] - 1
53     spark.stop()
54
55     print("loading matrix")
56     mat = read_tfidf_matrix(tempdir.name,'author')
57     print('computing similarities')
58     sims = column_similarities(mat)
59     del mat
60     
61     sims = pd.DataFrame(sims.todense())
62     sims = sims.rename({i:sr for i, sr in enumerate(subreddit_names.subreddit.values)},axis=1)
63     sims['subreddit'] = subreddit_names.subreddit.values
64
65     p = Path(outfile)
66
67     output_feather =  Path(str(p).replace("".join(p.suffixes), ".feather"))
68     output_csv =  Path(str(p).replace("".join(p.suffixes), ".csv"))
69     output_parquet =  Path(str(p).replace("".join(p.suffixes), ".parquet"))
70
71     sims.to_feather(outfile)
72     tempdir.cleanup()
73
74     # print(outfile)
75
76     # tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_authors.parquet')
77
78     # if included_subreddits is None:
79     #     included_subreddits = list(islice(open("/gscratch/comdata/users/nathante/cdsc-reddit/top_25000_subs_by_comments.txt"),topN))
80     #     included_subreddits = {s.strip('\n') for s in included_subreddits}
81
82     # else:
83     #     included_subreddits = set(open(included_subreddits))
84
85     # sim_dist, tfidf = cosine_similarities(tfidf, 'author', min_df, included_subreddits, similarity_threshold)
86
87     # p = Path(outfile)
88
89     # output_feather =  Path(str(p).replace("".join(p.suffixes), ".feather"))
90     # output_csv =  Path(str(p).replace("".join(p.suffixes), ".csv"))
91     # output_parquet =  Path(str(p).replace("".join(p.suffixes), ".parquet"))
92     # sim_dist = sim_dist.entries.toDF()
93
94     # sim_dist = sim_dist.repartition(1)
95     # sim_dist.write.parquet(str(output_parquet),mode='overwrite',compression='snappy')
96     
97
98
99     # #instead of toLocalMatrix() why not read as entries and put strait into numpy
100     # sim_entries = pd.read_parquet(output_parquet)
101
102     # df = tfidf.select('subreddit','subreddit_id_new').distinct().toPandas()
103
104     # spark.stop()
105     # df['subreddit_id_new'] = df['subreddit_id_new'] - 1
106     # df = df.sort_values('subreddit_id_new').reset_index(drop=True)
107     # df = df.set_index('subreddit_id_new')
108
109     # similarities = sim_entries.join(df, on='i')
110     # similarities = similarities.rename(columns={'subreddit':"subreddit_i"})
111     # similarities = similarities.join(df, on='j')
112     # similarities = similarities.rename(columns={'subreddit':"subreddit_j"})
113
114     # similarities.to_feather(output_feather)
115     # similarities.to_csv(output_csv)
116     # return similarities
117     
118 if __name__ == '__main__':
119     fire.Fire(author_cosine_similarities)

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