import argparse
import lmdb
import matplotlib.pyplot as plt
import numpy as np

"""
Makes histograms of s-offsets for each dt. To use this script, first run:
//experimental/bmattinson/route_frame:s_bucket_hist to generate an LMDB which
will be used by this script.
"""

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--lmdb')

    return parser.parse_args()

def process_lmdb(lmdb, hists_out):
    print 'Making hists'
    count = 0
    with lmdb.begin() as txn:
        cursor = txn.cursor()
        for key, value in cursor:
            count += 1
            if not key in hists_out:
                hists_out[key] = []
            v = float(value)
            # if v > 100:
            #     continue
            hists_out[key].append(v)
    print 'Created hists'


def main():
    args = parse_args()

    print 'Loading LDMB'
    l = lmdb.Environment(args.lmdb, readonly=True, subdir=False,
            map_size=999999999)
    print 'Loaded LMDB'

    print 'Making hists'
    hists = dict()
    count = 0
    with l.begin() as txn:
        cursor = txn.cursor()
        for key, value in cursor:
            count += 1
            if not key in hists:
                hists[key] = []
            v = float(value)
            # if v > 100:
            #     continue
            hists[key].append(v)
    print 'Created hists'

    print len(hists)
    counter = 0
    for dt in hists:
        plt.figure(counter)
        counter += 1
        print 'Setting vals'
        vals = hists[dt]
        print len(vals)
        n_bins = 20

        plt.hist(vals, bins=np.arange(0, 100, 0.5), cumulative=True,
                normed=True)

        plt.title("dt:" + str(dt))
    plt.show()

if __name__ == '__main__':
    main()
