#this file uses the batch Chi^2 score files created by Chi.py and averages out the scores across batches to obtain approximate gloabl Chi^2 scores

import os
import numpy as np
import pandas as pd

numBatches = 20 #specify number of batches

files = []
for r, d, f in os.walk("batches/"):#get files containing Chi2 scores from folder "batches"
    for file in f:
	    if 'chi' in file:
		    files.append(os.path.join(r, file))

sum = np.zeros(pd.read_csv(files[0]).fillna(0).to_numpy(dtype=float).shape, dtype=float)
for file in files: #sum scores over files while replacing NaNs with 0 
	sum = np.add(sum, pd.read_csv(file).fillna(0).to_numpy(dtype=float))

sum/=float(numBatches)#divide scores by number of batchs to obtain average scores

#save average scores
fp = open("globalChi.csv", "w") 
for i in sum:
	fp.write(str(i[0])+"\n")
fp.close()
print("DONE")
