#Post-analysis step. Used to visualize results from regression. Some graphs generated by this file are included in CISO_REGS

import csv 
import os
import matplotlib.pyplot as plt
import pandas as p

path = "/home/human/Desktop/Good/Better/regs"

files = {}
for folder in os.listdir(path):    #import files into dictionary
    for filename in os.listdir("{0}/{1}".format(path,folder)):
        if filename.endswith(".csv"):
            this = p.DataFrame.from_csv("{0}/{1}/{2}".format(path,folder,filename))
            files[folder] = this
            print(filename)

beta_spreads = {}
avg_spreads = {}
beta_change = {}

for filename in files.keys():
        beta_spreads[filename] = []
        avg_spreads[filename] = []
        beta_change[filename] = []
        betas = [ [eval(item)[0] for item in row] for i, row in files[filename].iterrows() ] #gets betas for every day; ugly but works
        for day in betas:
            beta_spreads[filename].append(max(day)-min(day))
        avg_spreads[filename] = sum(beta_spreads[filename])/len(beta_spreads[filename])
        for day in betas:
            beta_change[filename].append([day[i]-day[i-1] for i in range(1,len(day))])


for filename in beta_spreads.keys():
    writer = csv.writer(open("{0}_SPREADS.csv".format(filename), "w"), delimiter=",")
    writer.writerow(["DAY", "MAXB-MINB"])
    for row in enumerate(beta_spreads[filename]):
        writer.writerow(list(row))

for filename in beta_spreads.keys():
    plt.hist(beta_spreads[filename], bins=20)
    plt.title("{0} SPREADS".format(filename))
    plt.xlabel("Max B - Min B")
    plt.ylabel("Frequency")
    plt.show()

