<jupyter_start><jupyter_text>Generate Project Rephetio DWPCs using hetmech's matrix implementation<jupyter_code>import collections
import itertools
import pathlib

import hetio.readwrite
import numpy
import pandas
import requests
import scipy.sparse
import tqdm

import hetmech.degree_weight<jupyter_output><empty_output><jupyter_text>Load Hetionet v1.0<jupyter_code>%%time
commit = '<KEY>'
url = f'https://github.com/dhimmel/hetionet/raw/{commit}/hetnet/json/hetionet-v1.0.json.bz2'
graph = hetio.readwrite.read_graph(url)
metagraph = graph.metagraph<jupyter_output>CPU times: user 1min 26s, sys: 2.3 s, total: 1min 28s
Wall time: 1min 30s<jupyter_text>Load Project Rephetio metapaths<jupyter_code>commit = 'ef5f7a6b76b6a01499d65b95e3d7ca93ac5aba57'
url = f'https://github.com/dhimmel/learn/raw/{commit}/all-features/data/metapaths.json'
metapath_info = requests.get(url).json()
len(metapath_info)<jupyter_output><empty_output><jupyter_text>Categorize metapaths<jupyter_code>for info in metapath_info:
    metapath = metagraph.metapath_from_abbrev(info['abbreviation'])
    info['metapath'] = metapath
    info['category'] = hetmech.degree_weight.categorize(metapath)

metapath_df = pandas.DataFrame(metapath_info)
metapath_df.head(2)
len(metapath_info)
metapath_df.category.value_counts()
metapath_df.category.value_counts(normalize=True).map('{:.1%}'.format)<jupyter_output><empty_output><jupyter_text>Compute DWPCs<jupyter_code>exclude_categories = {'long_repeat', 'other'}
for info in tqdm.tqdm_notebook(metapath_info):
    if info['category'] in exclude_categories:
        continue
    metapath = info['metapath']
    compounds, diseases, dwpc_matrix, seconds = hetmech.degree_weight.dwpc(
        graph, metapath, damping=0.4, dense_threshold=1)
    info['dwpc_matrix'] = dwpc_matrix
    info['dwpc_hetmech_runtime'] = seconds
runtime_df = pandas.DataFrame(metapath_info)[['abbreviation', 'category', 'length', 'dwpc_hetmech_runtime']]
path = pathlib.Path('data/rephetio-DWPCs-hetmech-runtime.tsv')
runtime_df.to_csv(path, sep='\t', float_format='%.5g', index=False)
runtime_df.head(2)<jupyter_output><empty_output><jupyter_text>Save DWPC matrices as outputs in Compound-Disease pairs<jupyter_code>pairs = numpy.array(list(itertools.product(compounds, diseases)))
columns = collections.OrderedDict({
    'compound': pairs[:, 0],
    'disease': pairs[:, 1],
})

for info in metapath_info:
    if 'dwpc_matrix' not in info:
        continue
    matrix = info['dwpc_matrix']
    if scipy.sparse.issparse(matrix):
        matrix = matrix.toarray()
    assert isinstance(matrix, numpy.ndarray)
    columns[info['abbreviation']] = matrix.flatten(order='C').tolist()

dwpc_df = pandas.DataFrame.from_dict(columns)
dwpc_df.head(2)
path = pathlib.Path('data/rephetio-DWPCs-hetmech.tsv.xz')
dwpc_df.to_csv(path, sep='\t', float_format='%.5g', index=False, compression='xz')<jupyter_output><empty_output>