from glob import glob
import gzip


def parse_file_generator(filename):
    with gzip.open(filename) as f:
        for line in f:
            if line.startswith(b"# sweeps:"):
                speed = float(line[9:])
            elif line.startswith(b"#"):
                positions = list(map(float, line[2:].split()))
            else:
                sizes = list(map(float, line.split()))

                # for every dataset (consisting of 3 lines) return one result
                yield positions, sizes, speed


# if started as a program, calculate <S>(eps) for the fully connected case and show it
if __name__ == "__main__":
    # if matplotlib is installed, visualize the results
    try:
        import matplotlib.pyplot as plt
    except:
        vis = False
    else:
        vis = True
        x = []
        y = []
 
    N = 262144
    
    for file in sorted(glob("full/n{}_e*.cluster.dat.gz".format(N))):
        # the confidence value has the decimal point removed, so we need to reinsert it
        # since values of eps >= 1 are not sensible, they will always start with "0."
        eps_string = file.split("_e")[1].split(".")[0]
        confidence = float("0.{}".format(eps_string[1:]))

        avg_S = 0
        samples = 0
        for dataset in parse_file_generator(file):
            sizes = dataset[1]
            avg_S += max(sizes) / N
            samples += 1
        avg_S /= samples

        print(confidence, avg_S)
        if vis:
            x.append(confidence)
            y.append(avg_S)

    if vis:
        plt.xlabel("confidence")
        plt.ylabel("<S>")
        plt.plot(x, y)
        plt.show()

