<jupyter_start><jupyter_text>Average DensityInvestigates the average density of adjacency matrices within the Project Rephetio graph. We found the averae density to be almost exactly 20%, meaning that 80% of positions within the matrix are zero.<jupyter_code>import pandas as pd
import numpy
import matplotlib.pyplot as plt
from scipy import sparse
import os
% matplotlib inline
dwpc_df = pd.read_csv('rephetio-DWPCs-hetmech.tsv', sep='\t')
dwpc_df.head()
dwpc_data = dwpc_df.loc[:, 'CbGaD':]
average_nnz = numpy.mean((dwpc_data != 0).sum(axis=0))
# Average density
average_nnz / dwpc_df.shape[0]
metapath_density_df = (pd.DataFrame((dwpc_data != 0).sum(axis=0) / dwpc_df.shape[0])
                    .reset_index()
                    .rename(columns={'index': 'metapath', 0: 'density'}))
metapath_density_df.sort_values(by='density', ascending=False).head()
metapath_density_df.hist(column='density')<jupyter_output><empty_output><jupyter_text>Sparse matrix file size by density<jupyter_code>for i in range(10, 100, 10):
    example_matrix = numpy.random.rand(10000, 10000)
    example_matrix[example_matrix > i / 100] = 0
    sparse_matrix = sparse.csc_matrix(example_matrix)
    sparse.save_npz(f'sparse_{i}.npz', sparse_matrix)
    numpy.save(f'dense_{i}.npy', example_matrix)
    del example_matrix, sparse_matrix
sizes = []
for i in range(10, 100, 10):
    dense_size = os.path.getsize(f'dense_{i}.npy') / 1000000
    sparse_size = os.path.getsize(f'sparse_{i}.npz') / 1000000
    sizes.append([i, dense_size, sparse_size])
    print(f"dense - {i} density: {dense_size :.4g} MB")
    print(f"sparse - {i} density: {sparse_size :.4g} MB\n")

sizes = numpy.array(sizes)
plt.plot(sizes[:, 0], sizes[:, 1])
plt.plot(sizes[:, 0], sizes[:, 2])
plt.legend(['Dense', 'Sparse'])
plt.xlabel('Density (percent)')
plt.ylabel('Sparse file size (MB)')
plt.title('Size of sparse NPZ files by density');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14545454545454545, 'fraction_numerical': 0.03064935064935065, 'mean_word_length': 5.094936708860759, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '89441', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 771, 'n_tokens_neox': 699, 'n_words': 155, 'path': 'explore/disk-storage/average-density.ipynb', 'repo_name': 'gwaygenomics/hetmech', 'size': '1925'}