'''
Find linear & ranked correlation
at every sample
'''

import numpy
import scipy.stats

startTime = 1335139200 
increment = 300
nextTime = startTime

fdIn = open('multipleAttributes.txt', 'r')
fdOut1 = open('correlation_all_linear.txt', 'w')
fdOut2 = open('correlation_all_ranked.txt', 'w')

data = []
atts = []
line = fdIn.readline()
tmp = line[:-1].split('\t')
atts = tmp[2:]
tmpStr = ''
numAtt = len(tmp) - 2
#nid     time    CSp     NCores  CFree   MSize   MFree   DSize   DFree   OS      1mLd    5mLd    15mLd   Rx      Tx
for i in range(numAtt):
    data.append([])
    if i != numAtt - 1: tmpStr += tmp[i + 2] + '\t' 
    else: tmpStr += tmp[i + 2]
tmpStr = ''
for i in range(numAtt):
    for j in range(i + 1):
        tmpStr += atts[i] + '_' + atts[j] + '\t'
fdOut1.write(tmpStr)
fdOut2.write(tmpStr)

for line in fdIn:
    tmp = line[:-1].split('\t')
    time = int(float(tmp[1])) 
    if time > nextTime:
        #Linear correlation
        p = numpy.corrcoef(data)
        tmpStr = ''
        for i in range(len(p)):
            for j in range(i + 1):
                tmpStr += str(round(p[i][j], 4)) + '\t'
        fdOut1.write(tmpStr + '\n')

        #ranked correlation
        tmpStr = ''
        for i in range(numAtt):
            for j in range(i + 1):
                rho, pval = scipy.stats.spearmanr(data[i], data[j])
                tmpStr += str(round(rho, 4)) + '\t'
        fdOut2.write(tmpStr + '\n')
        #Reset
        nextTime += increment
        data = []
        for i in range(numAtt): data.append([])

    if time % 300 != 0: continue #Take only 5 min samples
    for i in range(len(tmp)):
        if i <= 1: continue #Skip nid & time
        #print i, tmp[i], len(data), i - 2
        data[i - 2].append(float(tmp[i]))

fdIn.close()


#Linear correlation
p = numpy.corrcoef(data)
tmpStr = ''
for i in range(len(p)):
    for j in range(i + 1):
        tmpStr += str(round(p[i][j], 4)) + '\t'
fdOut1.write(tmpStr + '\n')

#ranked correlation
tmpStr = ''
for i in range(numAtt):
    for j in range(i + 1):
        rho, pval = scipy.stats.spearmanr(data[i], data[j])
        tmpStr += str(round(rho, 4)) + '\t'
fdOut2.write(tmpStr + '\n')

fdOut1.close()
fdOut2.close()
