<jupyter_start><jupyter_text>Import the usual suspects<jupyter_code># Import pandas
import pandas as pd

# Import matplotlib
import matplotlib.pyplot as plt

# Import Network X
import networkx as nx<jupyter_output><empty_output><jupyter_text>Run script to construct Disease/Drug graphs<jupyter_code># Run
%run ./41_0_L_Create_DD_Graph.py<jupyter_output><empty_output><jupyter_text>Run statistics on graphs<jupyter_code># Read Disease
disease_g = nx.read_gpickle(DISEASE_GRAPH)

# Read Drug
drug_g = nx.read_gpickle(DRUGS_GRAPH)
# Disease eigenvector centrality
disease_eig = nx.eigenvector_centrality(disease_g, max_iter=500, weight='weight')

# Drug eigenvector centrality
drug_eig = nx.eigenvector_centrality(drug_g, max_iter=500, weight='weight')
# Disease PageRank 
disease_pgn = nx.pagerank(disease_g, alpha=0.9, weight='weight')

# Drug PageRank 
drug_pgn = nx.pagerank(drug_g, alpha=0.9, weight='weight')
# Disease Degree 
disease_deg = nx.degree_centrality(disease_g)

# Drug Degree 
drug_deg = nx.degree_centrality(drug_g)<jupyter_output><empty_output><jupyter_text>Get Top Diseases/Drugs for time-series analysis<jupyter_code># Get n-top nodes according to a specific metric
def get_top(dictionary, top):
    items = list(dictionary.items())
    items.sort(reverse=True, key=lambda x: x[1])
    return list(map(lambda x:x[0], items[:top]))
# Choose n-top
n_top = 20

# Creatre multi-index
outside = [*['Diseases']*3,*['Drugs']*3]
inside = ['EigenCentrality','PageRank','Degree']*2
hier_index = list(zip(outside,inside))
hier_index = pd.MultiIndex.from_tuples(hier_index)

# Create DataFrame with n-top diseases/drugs according to all the metrics
all_time_top = pd.DataFrame(data=[get_top(disease_eig, n_top),
                                  get_top(disease_pgn, n_top),
                                  get_top(disease_deg, n_top),
                                  get_top(drug_eig, n_top), 
                                  get_top(drug_pgn, n_top),
                                  get_top(drug_deg, n_top)], index=hier_index).transpose()

# Save as pickle file for Claire's time series analysis
all_time_top.to_pickle('All_Time_Top.pkl')

#Echo
all_time_top.head()
df1 = all_time_top.Diseases.rename(columns={'PageRank':'mesh_id'})['mesh_id']
pd.merge(df1, mesh_df, on='mesh_id')
geo_df.groupby('mesh_id').count().head()
geo_df[geo_df['mesh_id']=='C483997']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11803823773898586, 'fraction_numerical': 0.011637572734829594, 'mean_word_length': 4.703791469194313, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '533956', 'license': 'Unlicense', 'n_tokens_mistral': 943, 'n_tokens_neox': 880, 'n_words': 159, 'path': 'notebooks/42_0_L_Graphs.ipynb', 'repo_name': 'luiservela/AstraZeneca', 'size': '2406'}