Updates to similarities code for smap project.
This commit is contained in:
parent
87ffaa6858
commit
6e43294a41
@ -6,7 +6,7 @@ from functools import partial
|
|||||||
|
|
||||||
def cosine_similarities(infile, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, from_date=None, to_date=None, tfidf_colname='tf_idf'):
|
def cosine_similarities(infile, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, from_date=None, to_date=None, tfidf_colname='tf_idf'):
|
||||||
|
|
||||||
return similarities(infile=infile, simfunc=column_similarities, term_colname=term_colname, outfile=outfile, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN, from_date=from_date, to_date=to_date, tfidf_colname=tfidf_colname)
|
return similarities(inpath=infile, simfunc=column_similarities, term_colname=term_colname, outfile=outfile, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN, from_date=from_date, to_date=to_date, tfidf_colname=tfidf_colname)
|
||||||
|
|
||||||
# change so that these take in an input as an optional argument (for speed, but also for idf).
|
# change so that these take in an input as an optional argument (for speed, but also for idf).
|
||||||
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):
|
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):
|
||||||
|
@ -1,20 +1,41 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import fire
|
import fire
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from similarities_helper import similarities, lsi_column_similarities
|
from similarities_helper import *
|
||||||
|
#from similarities_helper import similarities, lsi_column_similarities
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
def lsi_similarities(infile, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, from_date=None, to_date=None, tfidf_colname='tf_idf',n_components=100,n_iter=5,random_state=1968,algorithm='arpack'):
|
inpath = "/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/tfidf/comment_terms_compex.parquet/"
|
||||||
|
term_colname='term'
|
||||||
|
outfile='/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/similarity/comment_terms_compex_LSI'
|
||||||
|
n_components=[10,50,100]
|
||||||
|
included_subreddits="/gscratch/comdata/users/nathante/competitive_exclusion_reddit/data/included_subreddits.txt"
|
||||||
|
n_iter=5
|
||||||
|
random_state=1968
|
||||||
|
algorithm='arpack'
|
||||||
|
topN = None
|
||||||
|
from_date=None
|
||||||
|
to_date=None
|
||||||
|
min_df=None
|
||||||
|
max_df=None
|
||||||
|
def lsi_similarities(inpath, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None, tfidf_colname='tf_idf',n_components=100,n_iter=5,random_state=1968,algorithm='arpack',lsi_model=None):
|
||||||
print(n_components,flush=True)
|
print(n_components,flush=True)
|
||||||
|
|
||||||
simfunc = partial(lsi_column_similarities,n_components=n_components,n_iter=n_iter,random_state=random_state,algorithm=algorithm)
|
|
||||||
|
if lsi_model is None:
|
||||||
|
if type(n_components) == list:
|
||||||
|
lsi_model = Path(outfile) / f'{max(n_components)}_{term_colname}_LSIMOD.pkl'
|
||||||
|
else:
|
||||||
|
lsi_model = Path(outfile) / f'{n_components}_{term_colname}_LSIMOD.pkl'
|
||||||
|
|
||||||
return similarities(infile=infile, simfunc=simfunc, term_colname=term_colname, outfile=outfile, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN, from_date=from_date, to_date=to_date, tfidf_colname=tfidf_colname)
|
simfunc = partial(lsi_column_similarities,n_components=n_components,n_iter=n_iter,random_state=random_state,algorithm=algorithm,lsi_model_save=lsi_model)
|
||||||
|
|
||||||
|
return similarities(inpath=inpath, simfunc=simfunc, term_colname=term_colname, outfile=outfile, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN, from_date=from_date, to_date=to_date, tfidf_colname=tfidf_colname)
|
||||||
|
|
||||||
# change so that these take in an input as an optional argument (for speed, but also for idf).
|
# change so that these take in an input as an optional argument (for speed, but also for idf).
|
||||||
def term_lsi_similarities(outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, from_date=None, to_date=None, n_components=300,n_iter=5,random_state=1968,algorithm='arpack'):
|
def term_lsi_similarities(inpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms_100k.parquet',outfile=None, min_df=None, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None, algorithm='arpack', n_components=300,n_iter=5,random_state=1968):
|
||||||
|
|
||||||
return lsi_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms_100k.parquet',
|
res = lsi_similarities(inpath,
|
||||||
'term',
|
'term',
|
||||||
outfile,
|
outfile,
|
||||||
min_df,
|
min_df,
|
||||||
@ -23,11 +44,13 @@ def term_lsi_similarities(outfile, min_df=None, max_df=None, included_subreddits
|
|||||||
topN,
|
topN,
|
||||||
from_date,
|
from_date,
|
||||||
to_date,
|
to_date,
|
||||||
n_components=n_components
|
n_components=n_components,
|
||||||
|
algorithm = algorithm
|
||||||
)
|
)
|
||||||
|
return res
|
||||||
|
|
||||||
def author_lsi_similarities(outfile, min_df=2, max_df=None, included_subreddits=None, topN=10000, from_date=None, to_date=None,n_components=300,n_iter=5,random_state=1968,algorithm='arpack'):
|
def author_lsi_similarities(inpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',outfile=None, min_df=2, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None,algorithm='arpack',n_components=300,n_iter=5,random_state=1968):
|
||||||
return lsi_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',
|
return lsi_similarities(inpath,
|
||||||
'author',
|
'author',
|
||||||
outfile,
|
outfile,
|
||||||
min_df,
|
min_df,
|
||||||
@ -39,8 +62,8 @@ def author_lsi_similarities(outfile, min_df=2, max_df=None, included_subreddits=
|
|||||||
n_components=n_components
|
n_components=n_components
|
||||||
)
|
)
|
||||||
|
|
||||||
def author_tf_similarities(outfile, min_df=2, max_df=None, included_subreddits=None, topN=10000, from_date=None, to_date=None,n_components=300,n_iter=5,random_state=1968,algorithm='arpack'):
|
def author_tf_similarities(inpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',outfile=None, min_df=2, max_df=None, included_subreddits=None, topN=None, from_date=None, to_date=None,n_components=300,n_iter=5,random_state=1968):
|
||||||
return lsi_similarities('/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors_100k.parquet',
|
return lsi_similarities(inpath,
|
||||||
'author',
|
'author',
|
||||||
outfile,
|
outfile,
|
||||||
min_df,
|
min_df,
|
||||||
@ -50,7 +73,8 @@ def author_tf_similarities(outfile, min_df=2, max_df=None, included_subreddits=N
|
|||||||
from_date=from_date,
|
from_date=from_date,
|
||||||
to_date=to_date,
|
to_date=to_date,
|
||||||
tfidf_colname='relative_tf',
|
tfidf_colname='relative_tf',
|
||||||
n_components=n_components
|
n_components=n_components,
|
||||||
|
algorithm=algorithm
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,24 +15,53 @@ import numpy as np
|
|||||||
import pathlib
|
import pathlib
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import pickle
|
||||||
|
|
||||||
class tf_weight(Enum):
|
class tf_weight(Enum):
|
||||||
MaxTF = 1
|
MaxTF = 1
|
||||||
Norm05 = 2
|
Norm05 = 2
|
||||||
|
|
||||||
infile = "/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet"
|
# infile = "/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet"
|
||||||
cache_file = "/gscratch/comdata/users/nathante/cdsc_reddit/similarities/term_tfidf_entries_bak.parquet"
|
# cache_file = "/gscratch/comdata/users/nathante/cdsc_reddit/similarities/term_tfidf_entries_bak.parquet"
|
||||||
|
|
||||||
# subreddits missing after this step don't have any terms that have a high enough idf
|
# subreddits missing after this step don't have any terms that have a high enough idf
|
||||||
# try rewriting without merges
|
# try rewriting without merges
|
||||||
def reindex_tfidf(infile, term_colname, min_df=None, max_df=None, included_subreddits=None, topN=500, week=None, from_date=None, to_date=None, rescale_idf=True, tf_family=tf_weight.MaxTF):
|
|
||||||
print("loading tfidf", flush=True)
|
# does reindex_tfidf, but without reindexing.
|
||||||
tfidf_ds = ds.dataset(infile)
|
def reindex_tfidf(*args, **kwargs):
|
||||||
|
df, tfidf_ds, ds_filter = _pull_or_reindex_tfidf(*args, **kwargs, reindex=True)
|
||||||
|
|
||||||
|
print("assigning names")
|
||||||
|
subreddit_names = tfidf_ds.to_table(filter=ds_filter,columns=['subreddit','subreddit_id'])
|
||||||
|
batches = subreddit_names.to_batches()
|
||||||
|
|
||||||
|
with Pool(cpu_count()) as pool:
|
||||||
|
chunks = pool.imap_unordered(pull_names,batches)
|
||||||
|
subreddit_names = pd.concat(chunks,copy=False).drop_duplicates()
|
||||||
|
subreddit_names = subreddit_names.set_index("subreddit_id")
|
||||||
|
|
||||||
|
new_ids = df.loc[:,['subreddit_id','subreddit_id_new']].drop_duplicates()
|
||||||
|
new_ids = new_ids.set_index('subreddit_id')
|
||||||
|
subreddit_names = subreddit_names.join(new_ids,on='subreddit_id').reset_index()
|
||||||
|
subreddit_names = subreddit_names.drop("subreddit_id",1)
|
||||||
|
subreddit_names = subreddit_names.sort_values("subreddit_id_new")
|
||||||
|
return(df, subreddit_names)
|
||||||
|
|
||||||
|
def pull_tfidf(*args, **kwargs):
|
||||||
|
df, _, _ = _pull_or_reindex_tfidf(*args, **kwargs, reindex=False)
|
||||||
|
return df
|
||||||
|
|
||||||
|
def _pull_or_reindex_tfidf(infile, term_colname, min_df=None, max_df=None, included_subreddits=None, topN=500, week=None, from_date=None, to_date=None, rescale_idf=True, tf_family=tf_weight.MaxTF, reindex=True):
|
||||||
|
print(f"loading tfidf {infile}", flush=True)
|
||||||
|
if week is not None:
|
||||||
|
tfidf_ds = ds.dataset(infile, partitioning='hive')
|
||||||
|
else:
|
||||||
|
tfidf_ds = ds.dataset(infile)
|
||||||
|
|
||||||
if included_subreddits is None:
|
if included_subreddits is None:
|
||||||
included_subreddits = select_topN_subreddits(topN)
|
included_subreddits = select_topN_subreddits(topN)
|
||||||
else:
|
else:
|
||||||
included_subreddits = set(open(included_subreddits))
|
included_subreddits = set(map(str.strip,open(included_subreddits)))
|
||||||
|
|
||||||
ds_filter = ds.field("subreddit").isin(included_subreddits)
|
ds_filter = ds.field("subreddit").isin(included_subreddits)
|
||||||
|
|
||||||
@ -68,15 +97,20 @@ def reindex_tfidf(infile, term_colname, min_df=None, max_df=None, included_subre
|
|||||||
'relative_tf':ds.field('relative_tf').cast('float32'),
|
'relative_tf':ds.field('relative_tf').cast('float32'),
|
||||||
'tf_idf':ds.field('tf_idf').cast('float32')}
|
'tf_idf':ds.field('tf_idf').cast('float32')}
|
||||||
|
|
||||||
tfidf_ds = ds.dataset(infile)
|
|
||||||
|
|
||||||
df = tfidf_ds.to_table(filter=ds_filter,columns=projection)
|
df = tfidf_ds.to_table(filter=ds_filter,columns=projection)
|
||||||
|
|
||||||
df = df.to_pandas(split_blocks=True,self_destruct=True)
|
df = df.to_pandas(split_blocks=True,self_destruct=True)
|
||||||
print("assigning indexes",flush=True)
|
print("assigning indexes",flush=True)
|
||||||
df['subreddit_id_new'] = df.groupby("subreddit_id").ngroup()
|
if reindex:
|
||||||
grouped = df.groupby(term_id)
|
df['subreddit_id_new'] = df.groupby("subreddit_id").ngroup()
|
||||||
df[term_id_new] = grouped.ngroup()
|
else:
|
||||||
|
df['subreddit_id_new'] = df['subreddit_id']
|
||||||
|
|
||||||
|
if reindex:
|
||||||
|
grouped = df.groupby(term_id)
|
||||||
|
df[term_id_new] = grouped.ngroup()
|
||||||
|
else:
|
||||||
|
df[term_id_new] = df[term_id]
|
||||||
|
|
||||||
if rescale_idf:
|
if rescale_idf:
|
||||||
print("computing idf", flush=True)
|
print("computing idf", flush=True)
|
||||||
@ -88,26 +122,13 @@ def reindex_tfidf(infile, term_colname, min_df=None, max_df=None, included_subre
|
|||||||
else: # tf_fam = tf_weight.Norm05
|
else: # tf_fam = tf_weight.Norm05
|
||||||
df["tf_idf"] = (0.5 + 0.5 * df.relative_tf) * df.idf
|
df["tf_idf"] = (0.5 + 0.5 * df.relative_tf) * df.idf
|
||||||
|
|
||||||
print("assigning names")
|
return (df, tfidf_ds, ds_filter)
|
||||||
subreddit_names = tfidf_ds.to_table(filter=ds_filter,columns=['subreddit','subreddit_id'])
|
|
||||||
batches = subreddit_names.to_batches()
|
|
||||||
|
|
||||||
with Pool(cpu_count()) as pool:
|
|
||||||
chunks = pool.imap_unordered(pull_names,batches)
|
|
||||||
subreddit_names = pd.concat(chunks,copy=False).drop_duplicates()
|
|
||||||
|
|
||||||
subreddit_names = subreddit_names.set_index("subreddit_id")
|
|
||||||
new_ids = df.loc[:,['subreddit_id','subreddit_id_new']].drop_duplicates()
|
|
||||||
new_ids = new_ids.set_index('subreddit_id')
|
|
||||||
subreddit_names = subreddit_names.join(new_ids,on='subreddit_id').reset_index()
|
|
||||||
subreddit_names = subreddit_names.drop("subreddit_id",1)
|
|
||||||
subreddit_names = subreddit_names.sort_values("subreddit_id_new")
|
|
||||||
return(df, subreddit_names)
|
|
||||||
|
|
||||||
def pull_names(batch):
|
def pull_names(batch):
|
||||||
return(batch.to_pandas().drop_duplicates())
|
return(batch.to_pandas().drop_duplicates())
|
||||||
|
|
||||||
def similarities(infile, simfunc, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, from_date=None, to_date=None, tfidf_colname='tf_idf'):
|
def similarities(inpath, simfunc, term_colname, outfile, min_df=None, max_df=None, included_subreddits=None, topN=500, from_date=None, to_date=None, tfidf_colname='tf_idf'):
|
||||||
'''
|
'''
|
||||||
tfidf_colname: set to 'relative_tf' to use normalized term frequency instead of tf-idf, which can be useful for author-based similarities.
|
tfidf_colname: set to 'relative_tf' to use normalized term frequency instead of tf-idf, which can be useful for author-based similarities.
|
||||||
'''
|
'''
|
||||||
@ -127,7 +148,7 @@ def similarities(infile, simfunc, term_colname, outfile, min_df=None, max_df=Non
|
|||||||
output_feather = Path(str(p).replace("".join(p.suffixes), ".feather"))
|
output_feather = Path(str(p).replace("".join(p.suffixes), ".feather"))
|
||||||
output_csv = Path(str(p).replace("".join(p.suffixes), ".csv"))
|
output_csv = Path(str(p).replace("".join(p.suffixes), ".csv"))
|
||||||
output_parquet = Path(str(p).replace("".join(p.suffixes), ".parquet"))
|
output_parquet = Path(str(p).replace("".join(p.suffixes), ".parquet"))
|
||||||
outfile.parent.mkdir(exist_ok=True, parents=True)
|
p.parent.mkdir(exist_ok=True, parents=True)
|
||||||
|
|
||||||
sims.to_feather(outfile)
|
sims.to_feather(outfile)
|
||||||
|
|
||||||
@ -135,7 +156,7 @@ def similarities(infile, simfunc, term_colname, outfile, min_df=None, max_df=Non
|
|||||||
term_id = term + '_id'
|
term_id = term + '_id'
|
||||||
term_id_new = term + '_id_new'
|
term_id_new = term + '_id_new'
|
||||||
|
|
||||||
entries, subreddit_names = reindex_tfidf(infile, term_colname=term_colname, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN,from_date=from_date,to_date=to_date)
|
entries, subreddit_names = reindex_tfidf(inpath, term_colname=term_colname, min_df=min_df, max_df=max_df, included_subreddits=included_subreddits, topN=topN,from_date=from_date,to_date=to_date)
|
||||||
mat = csr_matrix((entries[tfidf_colname],(entries[term_id_new], entries.subreddit_id_new)))
|
mat = csr_matrix((entries[tfidf_colname],(entries[term_id_new], entries.subreddit_id_new)))
|
||||||
|
|
||||||
print("loading matrix")
|
print("loading matrix")
|
||||||
@ -144,6 +165,7 @@ def similarities(infile, simfunc, term_colname, outfile, min_df=None, max_df=Non
|
|||||||
|
|
||||||
print(f'computing similarities on mat. mat.shape:{mat.shape}')
|
print(f'computing similarities on mat. mat.shape:{mat.shape}')
|
||||||
print(f"size of mat is:{mat.data.nbytes}",flush=True)
|
print(f"size of mat is:{mat.data.nbytes}",flush=True)
|
||||||
|
# transform this to debug term tfidf
|
||||||
sims = simfunc(mat)
|
sims = simfunc(mat)
|
||||||
del mat
|
del mat
|
||||||
|
|
||||||
@ -151,7 +173,7 @@ def similarities(infile, simfunc, term_colname, outfile, min_df=None, max_df=Non
|
|||||||
for simmat, name in sims:
|
for simmat, name in sims:
|
||||||
proc_sims(simmat, Path(outfile)/(str(name) + ".feather"))
|
proc_sims(simmat, Path(outfile)/(str(name) + ".feather"))
|
||||||
else:
|
else:
|
||||||
proc_sims(simmat, outfile)
|
proc_sims(sims, outfile)
|
||||||
|
|
||||||
def write_weekly_similarities(path, sims, week, names):
|
def write_weekly_similarities(path, sims, week, names):
|
||||||
sims['week'] = week
|
sims['week'] = week
|
||||||
@ -204,7 +226,7 @@ def test_lsi_sims():
|
|||||||
# if n_components is a list we'll return a list of similarities with different latent dimensionalities
|
# if n_components is a list we'll return a list of similarities with different latent dimensionalities
|
||||||
# if algorithm is 'randomized' instead of 'arpack' then n_iter gives the number of iterations.
|
# if algorithm is 'randomized' instead of 'arpack' then n_iter gives the number of iterations.
|
||||||
# this function takes the svd and then the column similarities of it
|
# this function takes the svd and then the column similarities of it
|
||||||
def lsi_column_similarities(tfidfmat,n_components=300,n_iter=10,random_state=1968,algorithm='randomized'):
|
def lsi_column_similarities(tfidfmat,n_components=300,n_iter=10,random_state=1968,algorithm='randomized',lsi_model_save=None,lsi_model_load=None):
|
||||||
# first compute the lsi of the matrix
|
# first compute the lsi of the matrix
|
||||||
# then take the column similarities
|
# then take the column similarities
|
||||||
print("running LSI",flush=True)
|
print("running LSI",flush=True)
|
||||||
@ -215,21 +237,32 @@ def lsi_column_similarities(tfidfmat,n_components=300,n_iter=10,random_state=196
|
|||||||
n_components = sorted(n_components,reverse=True)
|
n_components = sorted(n_components,reverse=True)
|
||||||
|
|
||||||
svd_components = n_components[0]
|
svd_components = n_components[0]
|
||||||
svd = TruncatedSVD(n_components=svd_components,random_state=random_state,algorithm=algorithm,n_iter=n_iter)
|
|
||||||
mod = svd.fit(tfidfmat.T)
|
if lsi_model_load is not None:
|
||||||
|
mod = pickle.load(open(lsi_model_load ,'rb'))
|
||||||
|
|
||||||
|
else:
|
||||||
|
svd = TruncatedSVD(n_components=svd_components,random_state=random_state,algorithm=algorithm,n_iter=n_iter)
|
||||||
|
mod = svd.fit(tfidfmat.T)
|
||||||
|
|
||||||
lsimat = mod.transform(tfidfmat.T)
|
lsimat = mod.transform(tfidfmat.T)
|
||||||
|
if lsi_model_save is not None:
|
||||||
|
pickle.dump(mod, open(lsi_model_save,'wb'))
|
||||||
|
|
||||||
|
sims_list = []
|
||||||
for n_dims in n_components:
|
for n_dims in n_components:
|
||||||
sims = column_similarities(lsimat[:,np.arange(n_dims)])
|
sims = column_similarities(lsimat[:,np.arange(n_dims)])
|
||||||
if len(n_components) > 1:
|
if len(n_components) > 1:
|
||||||
yield (sims, n_dims)
|
yield (sims, n_dims)
|
||||||
else:
|
else:
|
||||||
return sims
|
return sims
|
||||||
|
|
||||||
|
|
||||||
def column_similarities(mat):
|
def column_similarities(mat):
|
||||||
return 1 - pairwise_distances(mat,metric='cosine')
|
return 1 - pairwise_distances(mat,metric='cosine')
|
||||||
|
|
||||||
|
# need to rewrite this so that subreddit ids and term ids are fixed over the whole thing.
|
||||||
|
# this affords taking the LSI similarities.
|
||||||
|
# fill all 0s if we don't have it.
|
||||||
def build_weekly_tfidf_dataset(df, include_subs, term_colname, tf_family=tf_weight.Norm05):
|
def build_weekly_tfidf_dataset(df, include_subs, term_colname, tf_family=tf_weight.Norm05):
|
||||||
term = term_colname
|
term = term_colname
|
||||||
term_id = term + '_id'
|
term_id = term + '_id'
|
||||||
@ -254,20 +287,21 @@ def build_weekly_tfidf_dataset(df, include_subs, term_colname, tf_family=tf_weig
|
|||||||
idf = idf.withColumn('idf',f.log(idf.subreddits_in_week) / (1+f.col('count'))+1)
|
idf = idf.withColumn('idf',f.log(idf.subreddits_in_week) / (1+f.col('count'))+1)
|
||||||
|
|
||||||
# collect the dictionary to make a pydict of terms to indexes
|
# collect the dictionary to make a pydict of terms to indexes
|
||||||
terms = idf.select([term,'week']).distinct() # terms are distinct
|
terms = idf.select([term]).distinct() # terms are distinct
|
||||||
|
|
||||||
terms = terms.withColumn(term_id,f.row_number().over(Window.partitionBy('week').orderBy(term))) # term ids are distinct
|
terms = terms.withColumn(term_id,f.row_number().over(Window.orderBy(term))) # term ids are distinct
|
||||||
|
|
||||||
# make subreddit ids
|
# make subreddit ids
|
||||||
subreddits = df.select(['subreddit','week']).distinct()
|
subreddits = df.select(['subreddit']).distinct()
|
||||||
subreddits = subreddits.withColumn('subreddit_id',f.row_number().over(Window.partitionBy("week").orderBy("subreddit")))
|
subreddits = subreddits.withColumn('subreddit_id',f.row_number().over(Window.orderBy("subreddit")))
|
||||||
|
|
||||||
df = df.join(subreddits,on=['subreddit','week'])
|
# df = df.cache()
|
||||||
|
df = df.join(subreddits,on=['subreddit'])
|
||||||
|
|
||||||
# map terms to indexes in the tfs and the idfs
|
# map terms to indexes in the tfs and the idfs
|
||||||
df = df.join(terms,on=[term,'week']) # subreddit-term-id is unique
|
df = df.join(terms,on=[term]) # subreddit-term-id is unique
|
||||||
|
|
||||||
idf = idf.join(terms,on=[term,'week'])
|
idf = idf.join(terms,on=[term])
|
||||||
|
|
||||||
# join on subreddit/term to create tf/dfs indexed by term
|
# join on subreddit/term to create tf/dfs indexed by term
|
||||||
df = df.join(idf, on=[term_id, term,'week'])
|
df = df.join(idf, on=[term_id, term,'week'])
|
||||||
@ -327,7 +361,7 @@ def _calc_tfidf(df, term_colname, tf_family):
|
|||||||
return df
|
return df
|
||||||
|
|
||||||
|
|
||||||
def build_tfidf_dataset(df, include_subs, term_colname, tf_family=tf_weight.Norm05):
|
def tfidf_dataset(df, include_subs, term_colname, tf_family=tf_weight.Norm05):
|
||||||
term = term_colname
|
term = term_colname
|
||||||
term_id = term + '_id'
|
term_id = term + '_id'
|
||||||
# aggregate counts by week. now subreddit-term is distinct
|
# aggregate counts by week. now subreddit-term is distinct
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import fire
|
import fire
|
||||||
from pyspark.sql import SparkSession
|
from pyspark.sql import SparkSession
|
||||||
from pyspark.sql import functions as f
|
from pyspark.sql import functions as f
|
||||||
from similarities_helper import build_tfidf_dataset, build_weekly_tfidf_dataset, select_topN_subreddits
|
from similarities_helper import tfidf_dataset, build_weekly_tfidf_dataset, select_topN_subreddits
|
||||||
|
|
||||||
def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_subreddits):
|
def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_subreddits):
|
||||||
spark = SparkSession.builder.getOrCreate()
|
spark = SparkSession.builder.getOrCreate()
|
||||||
@ -11,7 +11,7 @@ def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_
|
|||||||
df = df.filter(~ f.col(term_colname).isin(exclude))
|
df = df.filter(~ f.col(term_colname).isin(exclude))
|
||||||
|
|
||||||
if included_subreddits is not None:
|
if included_subreddits is not None:
|
||||||
include_subs = list(open(included_subreddits))
|
include_subs = set(map(str.strip,open(included_subreddits)))
|
||||||
else:
|
else:
|
||||||
include_subs = select_topN_subreddits(topN)
|
include_subs = select_topN_subreddits(topN)
|
||||||
|
|
||||||
@ -21,42 +21,45 @@ def _tfidf_wrapper(func, inpath, outpath, topN, term_colname, exclude, included_
|
|||||||
spark.stop()
|
spark.stop()
|
||||||
|
|
||||||
def tfidf(inpath, outpath, topN, term_colname, exclude, included_subreddits):
|
def tfidf(inpath, outpath, topN, term_colname, exclude, included_subreddits):
|
||||||
return _tfidf_wrapper(build_tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
|
return _tfidf_wrapper(tfidf_dataset, inpath, outpath, topN, term_colname, exclude, included_subreddits)
|
||||||
|
|
||||||
def tfidf_weekly(inpath, outpath, topN, term_colname, exclude, 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)
|
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',
|
def tfidf_authors(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_authors.parquet',
|
||||||
topN=25000):
|
topN=None,
|
||||||
|
included_subreddits=None):
|
||||||
|
|
||||||
return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
|
return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
|
||||||
outpath,
|
outpath,
|
||||||
topN,
|
topN,
|
||||||
'author',
|
'author',
|
||||||
['[deleted]','AutoModerator'],
|
['[deleted]','AutoModerator'],
|
||||||
included_subreddits=None
|
included_subreddits=included_subreddits
|
||||||
)
|
)
|
||||||
|
|
||||||
def tfidf_terms(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms.parquet',
|
def tfidf_terms(outpath='/gscratch/comdata/output/reddit_similarity/tfidf/comment_terms.parquet',
|
||||||
topN=25000):
|
topN=None,
|
||||||
|
included_subreddits=None):
|
||||||
|
|
||||||
return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
|
return tfidf("/gscratch/comdata/output/reddit_ngrams/comment_terms.parquet",
|
||||||
outpath,
|
outpath,
|
||||||
topN,
|
topN,
|
||||||
'term',
|
'term',
|
||||||
[],
|
[],
|
||||||
included_subreddits=None
|
included_subreddits=included_subreddits
|
||||||
)
|
)
|
||||||
|
|
||||||
def tfidf_authors_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
|
def tfidf_authors_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
|
||||||
topN=25000):
|
topN=None,
|
||||||
|
include_subreddits=None):
|
||||||
|
|
||||||
return tfidf_weekly("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
|
return tfidf_weekly("/gscratch/comdata/output/reddit_ngrams/comment_authors.parquet",
|
||||||
outpath,
|
outpath,
|
||||||
topN,
|
topN,
|
||||||
'author',
|
'author',
|
||||||
['[deleted]','AutoModerator'],
|
['[deleted]','AutoModerator'],
|
||||||
included_subreddits=None
|
included_subreddits=included_subreddits
|
||||||
)
|
)
|
||||||
|
|
||||||
def tfidf_terms_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
|
def tfidf_terms_weekly(outpath='/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_terms.parquet',
|
||||||
|
@ -8,32 +8,47 @@ import pandas as pd
|
|||||||
import fire
|
import fire
|
||||||
from itertools import islice, chain
|
from itertools import islice, chain
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from similarities_helper import *
|
from similarities_helper import pull_tfidf, column_similarities, write_weekly_similarities
|
||||||
|
from scipy.sparse import csr_matrix
|
||||||
from multiprocessing import Pool, cpu_count
|
from multiprocessing import Pool, cpu_count
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
|
# infile = "/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors_test.parquet"
|
||||||
|
# tfidf_path = infile
|
||||||
|
# min_df=None
|
||||||
|
# max_df = None
|
||||||
|
# topN=100
|
||||||
|
# term_colname='author'
|
||||||
|
# outfile = '/gscratch/comdata/output/reddit_similarity/weekly/comment_authors_test.parquet'
|
||||||
|
# included_subreddits=None
|
||||||
|
|
||||||
def _week_similarities(week, simfunc, tfidf_path, term_colname, min_df, max_df, included_subreddits, topN, outdir:Path):
|
def _week_similarities(week, simfunc, tfidf_path, term_colname, min_df, max_df, included_subreddits, topN, outdir:Path, subreddit_names, nterms):
|
||||||
term = term_colname
|
term = term_colname
|
||||||
term_id = term + '_id'
|
term_id = term + '_id'
|
||||||
term_id_new = term + '_id_new'
|
term_id_new = term + '_id_new'
|
||||||
print(f"loading matrix: {week}")
|
print(f"loading matrix: {week}")
|
||||||
entries, subreddit_names = reindex_tfidf(infile = tfidf_path,
|
|
||||||
term_colname=term_colname,
|
entries = pull_tfidf(infile = tfidf_path,
|
||||||
min_df=min_df,
|
term_colname=term_colname,
|
||||||
max_df=max_df,
|
min_df=min_df,
|
||||||
included_subreddits=included_subreddits,
|
max_df=max_df,
|
||||||
topN=topN,
|
included_subreddits=included_subreddits,
|
||||||
week=week)
|
topN=topN,
|
||||||
mat = csr_matrix((entries[tfidf_colname],(entries[term_id_new], entries.subreddit_id_new)))
|
week=week.isoformat(),
|
||||||
|
rescale_idf=False)
|
||||||
|
|
||||||
|
tfidf_colname='tf_idf'
|
||||||
|
# if the max subreddit id we found is less than the number of subreddit names then we have to fill in 0s
|
||||||
|
mat = csr_matrix((entries[tfidf_colname],(entries[term_id_new]-1, entries.subreddit_id_new-1)),shape=(nterms,subreddit_names.shape[0]))
|
||||||
|
|
||||||
print('computing similarities')
|
print('computing similarities')
|
||||||
sims = column_similarities(mat)
|
sims = simfunc(mat.T)
|
||||||
del mat
|
del mat
|
||||||
sims = pd.DataFrame(sims.todense())
|
sims = pd.DataFrame(sims)
|
||||||
sims = sims.rename({i: sr for i, sr in enumerate(subreddit_names.subreddit.values)}, axis=1)
|
sims = sims.rename({i: sr for i, sr in enumerate(subreddit_names.subreddit.values)}, axis=1)
|
||||||
sims['_subreddit'] = names.subreddit.values
|
sims['_subreddit'] = subreddit_names.subreddit.values
|
||||||
outfile = str(Path(outdir) / str(week))
|
outfile = str(Path(outdir) / str(week))
|
||||||
write_weekly_similarities(outfile, sims, week, names)
|
write_weekly_similarities(outfile, sims, week, subreddit_names)
|
||||||
|
|
||||||
def pull_weeks(batch):
|
def pull_weeks(batch):
|
||||||
return set(batch.to_pandas()['week'])
|
return set(batch.to_pandas()['week'])
|
||||||
@ -41,25 +56,29 @@ def pull_weeks(batch):
|
|||||||
#tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_weekly.parquet')
|
#tfidf = spark.read.parquet('/gscratch/comdata/users/nathante/subreddit_tfidf_weekly.parquet')
|
||||||
def cosine_similarities_weekly(tfidf_path, outfile, term_colname, min_df = None, max_df=None, included_subreddits = None, topN = 500):
|
def cosine_similarities_weekly(tfidf_path, outfile, term_colname, min_df = None, max_df=None, included_subreddits = None, topN = 500):
|
||||||
print(outfile)
|
print(outfile)
|
||||||
tfidf_ds = ds.dataset(tfidf_path)
|
|
||||||
tfidf_ds = tfidf_ds.to_table(columns=["week"])
|
|
||||||
batches = tfidf_ds.to_batches()
|
|
||||||
|
|
||||||
with Pool(cpu_count()) as pool:
|
|
||||||
weeks = set(chain( * pool.imap_unordered(pull_weeks,batches)))
|
|
||||||
|
|
||||||
weeks = sorted(weeks)
|
|
||||||
# do this step in parallel if we have the memory for it.
|
# do this step in parallel if we have the memory for it.
|
||||||
# should be doable with pool.map
|
# should be doable with pool.map
|
||||||
|
|
||||||
print(f"computing weekly similarities")
|
spark = SparkSession.builder.getOrCreate()
|
||||||
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)
|
df = spark.read.parquet(tfidf_path)
|
||||||
|
subreddit_names = df.select(['subreddit','subreddit_id']).distinct().toPandas()
|
||||||
|
subreddit_names = subreddit_names.sort_values("subreddit_id")
|
||||||
|
nterms = df.select(f.max(f.col(term_colname + "_id")).alias('max')).collect()[0].max
|
||||||
|
weeks = df.select(f.col("week")).distinct().toPandas().week.values
|
||||||
|
spark.stop()
|
||||||
|
|
||||||
|
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, subreddit_names=subreddit_names,nterms=nterms)
|
||||||
|
|
||||||
|
pool = Pool(cpu_count())
|
||||||
|
|
||||||
|
list(pool.imap(week_similarities_helper,weeks))
|
||||||
|
pool.close()
|
||||||
|
# with Pool(cpu_count()) as pool: # maybe it can be done with 40 cores on the huge machine?
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
def author_cosine_similarities_weekly(outfile, min_df=2, max_df=None, included_subreddits=None, topN=500):
|
def author_cosine_similarities_weekly(outfile, min_df=2, max_df=None, included_subreddits=None, topN=500):
|
||||||
return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors.parquet',
|
return cosine_similarities_weekly('/gscratch/comdata/output/reddit_similarity/tfidf_weekly/comment_authors_test.parquet',
|
||||||
outfile,
|
outfile,
|
||||||
'author',
|
'author',
|
||||||
min_df,
|
min_df,
|
||||||
|
Loading…
Reference in New Issue
Block a user