## implement scale free network estimation
## do this according to the paper WCGA

import numpy as np
from scipy.stats import spearmanr
from collections import Counter

def pick_threshold(matrix):

    for j in np.linspace(0,1,50):
        tmp_array = matrix.copy()
        tmp_array[tmp_array > j] = 1
        tmp_array[tmp_array < j] = 0
        rho, pval = spearmanr(tmp_array)
        np.fill_diagonal(tmp_array, 0) ## self loops
        rw_sum = np.sum(tmp_array,axis=0)
        unique_counts = [int(x) for x in np.unique(rw_sum)]
        counts = Counter(unique_counts)
        key_counts = np.log(list(counts.keys()))
        counts = np.log(list(counts.values()))

        print(key_counts,counts)
    ## compute this based on the power law distribution
    ## for x in spearman range
    ## fit a line to log10(p(k)),log10(k)
    
    
    pass

def default_correlation_to_network(matrix):
    
    pass

if __name__ == "__main__":

    from numpy import genfromtxt
    a = genfromtxt('/home/blazs/random_data/data.csv', delimiter=',',skip_header=1)
    a = np.nan_to_num(a)
    print("Read the data..")
    idx = np.random.randint(10)
    print(idx)
#    pick_threshold = pick_threshold(a)
#    print(pick_threshold)
