2020-12-09 01:32:20 +00:00
|
|
|
from pyspark.sql import functions as f
|
|
|
|
from pyspark.sql import SparkSession
|
|
|
|
from pyspark.sql import Window
|
|
|
|
import numpy as np
|
|
|
|
import pyarrow
|
2021-05-03 06:39:55 +00:00
|
|
|
import pyarrow.dataset as ds
|
2020-12-09 01:32:20 +00:00
|
|
|
import pandas as pd
|
|
|
|
import fire
|
2021-05-03 06:39:55 +00:00
|
|
|
from itertools import islice, chain
|
2020-12-09 01:32:20 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from similarities_helper import *
|
2021-04-22 17:37:04 +00:00
|
|
|
from multiprocessing import Pool, cpu_count
|
2021-05-03 06:39:55 +00:00
|
|
|
from functools import partial
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-04-06 06:21:06 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
def _week_similarities(week, simfunc, tfidf_path, term_colname, min_df, max_df, included_subreddits, topN, outdir:Path):
|
|
|
|
term = term_colname
|
|
|
|
term_id = term + '_id'
|
|
|
|
term_id_new = term + '_id_new'
|
|
|
|
print(f"loading matrix: {week}")
|
|
|
|
entries, subreddit_names = reindex_tfidf(infile = tfidf_path,
|
|
|
|
term_colname=term_colname,
|
|
|
|
min_df=min_df,
|
|
|
|
max_df=max_df,
|
|
|
|
included_subreddits=included_subreddits,
|
|
|
|
topN=topN,
|
|
|
|
week=week)
|
|
|
|
mat = csr_matrix((entries[tfidf_colname],(entries[term_id_new], entries.subreddit_id_new)))
|
|
|
|
print('computing similarities')
|
|
|
|
sims = column_similarities(mat)
|
|
|
|
del mat
|
|
|
|
sims = pd.DataFrame(sims.todense())
|
|
|
|
sims = sims.rename({i: sr for i, sr in enumerate(subreddit_names.subreddit.values)}, axis=1)
|
|
|
|
sims['_subreddit'] = names.subreddit.values
|
|
|
|
outfile = str(Path(outdir) / str(week))
|
|
|
|
write_weekly_similarities(outfile, sims, week, names)
|
2021-04-06 06:21:06 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
def pull_weeks(batch):
|
|
|
|
return set(batch.to_pandas()['week'])
|
2020-12-09 01:32:20 +00:00
|
|
|
|
|
|
|
#tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_weekly.parquet')
|
2021-05-03 06:39:55 +00:00
|
|
|
def cosine_similarities_weekly(tfidf_path, outfile, term_colname, min_df = None, max_df=None, included_subreddits = None, topN = 500):
|
2020-12-09 01:32:20 +00:00
|
|
|
print(outfile)
|
2021-05-03 06:39:55 +00:00
|
|
|
tfidf_ds = ds.dataset(tfidf_path)
|
|
|
|
tfidf_ds = tfidf_ds.to_table(columns=["week"])
|
|
|
|
batches = tfidf_ds.to_batches()
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
with Pool(cpu_count()) as pool:
|
|
|
|
weeks = set(chain( * pool.imap_unordered(pull_weeks,batches)))
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
weeks = sorted(weeks)
|
2021-04-06 06:21:06 +00:00
|
|
|
# do this step in parallel if we have the memory for it.
|
|
|
|
# should be doable with pool.map
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
print(f"computing weekly similarities")
|
|
|
|
week_similarities_helper = partial(_week_similarities,simfunc=column_similarities, tfidf_path=tfidf_path, term_colname=term_colname, outdir=outfile, min_df=min_df,max_df=max_df,included_subreddits=included_subreddits,topN=topN)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-04-22 17:37:04 +00:00
|
|
|
with Pool(cpu_count()) as pool: # maybe it can be done with 40 cores on the huge machine?
|
|
|
|
list(pool.map(week_similarities_helper,weeks))
|
2020-12-09 01:32:20 +00:00
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
def author_cosine_similarities_weekly(outfile, min_df=2, max_df=None, included_subreddits=None, topN=500):
|
2021-04-22 17:37:04 +00:00
|
|
|
return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
|
2020-12-09 01:32:20 +00:00
|
|
|
outfile,
|
|
|
|
'author',
|
|
|
|
min_df,
|
2021-05-03 06:39:55 +00:00
|
|
|
max_df,
|
2020-12-09 01:32:20 +00:00
|
|
|
included_subreddits,
|
|
|
|
topN)
|
|
|
|
|
2021-05-03 06:39:55 +00:00
|
|
|
def term_cosine_similarities_weekly(outfile, min_df=None, max_df=None, included_subreddits=None, topN=500):
|
|
|
|
return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
|
|
|
|
outfile,
|
|
|
|
'term',
|
|
|
|
min_df,
|
|
|
|
max_df,
|
|
|
|
included_subreddits,
|
|
|
|
topN)
|
2020-12-09 01:32:20 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-04-06 06:21:06 +00:00
|
|
|
fire.Fire({'authors':author_cosine_similarities_weekly,
|
|
|
|
'terms':term_cosine_similarities_weekly})
|