import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde as kde


def readChains(nFreqBins):

    ''' 
    read in the data and return the lowest nFreqBins
    chains and frequencies
    '''
    frequencies = np.genfromtxt('../chains/dr2new_fs/freqs.txt')
    samples = np.genfromtxt('../chains/dr2new_fs/chains_h.txt')

    chainsToUse = samples[:,0:nFreqBins]
    frequenciesToUse = frequencies[0:nFreqBins]
    
    return frequenciesToUse, chainsToUse



def makeKDEs(samples,nFreqBins):
    '''
    make the KDEs to use in the likelihood
    '''
   
    KDE = {i: kde(samples[:,i]) for i in range(nFreqBins)}

    return KDE


f,data = readChains(5)
kdes = makeKDEs(data,len(f))

hcsToPlot = np.linspace(-17,-13,20)


for i in range(len(f)):

    chain = data[:,i]
    kdeToPlot = kdes[i].logpdf(hcsToPlot)
    print(kdeToPlot)
    
    plt.hist(chain,density=True,histtype='step',bins=50)
    plt.plot(hcsToPlot,kdeToPlot)
    plt.show()
