import glob
import os

from dqc_wrapper.utils.utils_stats import valid_range

with open('/tmp/valid_ranges_conservative.csv', 'w') as fo:
    for f in sorted(glob.glob('/shared/QAR_dqc/results/*hist.csv')):

        # Exhaustive min/max find
        dr_gap = [0.5, 0.25, 0.1, 0.05, 0.025, 0.01, 0.005]
        r_zero = [0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001]
        valid_min = 999999999999
        valid_max = -999999999999

        for d in dr_gap:
            for r in r_zero:
                vr = valid_range(f, domain='total', dr_gap=d, r_zero=r, k_gap=1)
                try:
                    mn, mx = vr.get()
                    valid_max = max(valid_max, mx)
                    valid_min = min(valid_min, mn)
                except:
                    pass

        # Check
        s = f.replace('-hist.csv', '-stats.csv')
        errMin = False
        errMax = False
        with open(s, 'r') as fp:
            head = fp.readline()
            for l in fp.readlines():
                File,Min,Max,Med,q1,q3,IQR = l.split(',')
                if float(Min) < valid_min:
                    errMin = True
                if float(Max) > valid_max:
                    errMax = True

        fo.write('{},{},{},{},{}\n'.format(os.path.basename(f).replace('-hist.csv', ''), valid_min, valid_max, errMin, errMax))