<jupyter_start><jupyter_text>Sparsity of Weights (Transformations)<jupyter_code>import numpy as np
# This notebook is to measure the sparsity of weights from ABA transformations
# It uses the array_sparsity_function defined here
# This function counts the voxels over 'mean + stdev_ratio * stdev'
def array_sparsity_function(input_arr, stdev_ratio = 1, sparsity_method = 'normal'):
    '''
    Measures the sparsity of the array by thresholding it to 'mean + stdev_ratio * stdev',
    and counting the number of voxels.
    
    Args:
        input_arr: np array
            The input array.
        
        sparsity_method: {'normal', 'inverse'}, default = 'normal'
            'normal': number of voxels above threshold / total number of voxels
            'inverse': total number of voxels / number of voxels above threshold
        
        stdev_ratio: float, default = 1
            The ratio in the threshold formula; threshold = mean + stdev_ratio * stdev
            
    Returns:
        sparsity_ratio: float
            ratio of sparsity.
    '''
    
    # Thresholding the feature
    mean = np.mean(input_arr)
    stdev = np.std(input_arr)
    threshold = mean + stdev_ratio * stdev
    thresh_feature = input_arr > threshold
    
    # Count of voxels abpve threshold
    count_thresh = np.sum(thresh_feature)
    if count_thresh == 0:
        print('O voxels above threshold!')
    
    # Calculating sparsity and adding to the dataframe
    if sparsity_method == 'normal':
        sparsity_ratio = count_thresh / input_arr.shape[0]
    elif sparsity_method == 'inverse':
        sparsity_ratio = input_arr.shape[0] / count_thresh
    
    return sparsity_ratio
# Loading the features derived from the represetation learning methods

# Sparsity metric
sparsity_method = 'inverse'

# stdev ratio
stdev_ratio = 0

# Loading the weights array
features_arr = np.load('files/SFT_100weights.npy')
features_arr = features_arr.T

sparsity_list = []

for i in range(features_arr.shape[1]):
    # Selecting the feature
    # Getting the absolute value
    feature = np.abs(features_arr[:,i])
    
    # Calculating sparsity and adding to the dataframe
    sp_ratio = array_sparsity_function(feature, stdev_ratio = stdev_ratio, sparsity_method = sparsity_method)
    sparsity_list.append(sp_ratio)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07240788346186804, 'fraction_numerical': 0.004284490145672665, 'mean_word_length': 3.1474245115452932, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '336628', 'license': 'MIT', 'n_tokens_mistral': 732, 'n_tokens_neox': 694, 'n_words': 213, 'path': 'Notebooks/02_analyses/Fig2_Weight_Sparsity.ipynb', 'repo_name': 'BioProteanLabs/SFt_pipeline', 'size': '2334'}