import csv
import matplotlib.pyplot as plt
import time

PLOT_PER_WINDOW = False
WINDOW_LENGTH = 60000
BINS = 1000
delay_store = {}
perwindow_delay_store = {}
plotting_delay_store = {}

filename = "output-large.csv"
# filename = "output.csv"
# filename = "output-medium.csv"
# filename = "output-small.csv"
# filename = "output-tiny.csv"

with open(filename, "rU") as dataFile:
    csvreader = csv.reader(dataFile)
    for row in csvreader:
        if len(row) > 2 and str(row[0]).isdigit():
            delay_store[long(row[1])] = long(row[2])

window_begin = min(delay_store.keys())
window_end = max(delay_store.keys())

if PLOT_PER_WINDOW:
    window_end = window_begin + WINDOW_LENGTH
    # find the time delays that are within the window of choice
    for (tapp, delay) in delay_store.iteritems():
        if window_begin <= tapp <= window_end:
            perwindow_delay_store[tapp] = delay
    plotting_delay_store = perwindow_delay_store
else:
    plotting_delay_store = delay_store

# the histogram of the data
n, bins, patches = plt.hist(plotting_delay_store.values(), BINS, histtype='stepfilled',
                            normed=True, cumulative=False, facecolor='blue', alpha=0.9)
# plt.axhline(y=0.95, color='red', label='0.95')

max_delay = max(plotting_delay_store.values())
min_delay = min(plotting_delay_store.values())
count = len(plotting_delay_store.values())
# format epoch time to date time to be shown in the plot figure
window_begin_in_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(window_begin / 1000))
window_end_in_datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(window_end / 1000))

title = "Window begin: %s\n" % window_begin_in_datetime
title += "Window end: %s\n" % window_end_in_datetime
# title += "Window length: %dms\n" % WINDOW_LENGTH
title += "Window length: ~%dmins\n" % ((window_end - window_begin)/60000)
title += "Maximum delay: %dms\n" % max_delay
title += "Minimum delay: %dms\n" % min_delay
title += "Count: %d" % count

# start plotting
plt.xlabel('Delay (ms)')
plt.ylabel('Probability')
plt.grid(True)
plt.legend()
plt.suptitle(title)
plt.subplots_adjust(top=0.8)
plt.show()

[((58, 0, 58, 24), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(58, 11, 58, 23): '"""Delay (ms)"""'}, {}), "('Delay (ms)')", True, 'import matplotlib.pyplot as plt\n'), ((59, 0, 59, 25), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(59, 11, 59, 24): '"""Probability"""'}, {}), "('Probability')", True, 'import matplotlib.pyplot as plt\n'), ((60, 0, 60, 14), 'matplotlib.pyplot.grid', 'plt.grid', ({(60, 9, 60, 13): '(True)'}, {}), '(True)', True, 'import matplotlib.pyplot as plt\n'), ((61, 0, 61, 12), 'matplotlib.pyplot.legend', 'plt.legend', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((62, 0, 62, 19), 'matplotlib.pyplot.suptitle', 'plt.suptitle', ({(62, 13, 62, 18): 'title'}, {}), '(title)', True, 'import matplotlib.pyplot as plt\n'), ((63, 0, 63, 28), 'matplotlib.pyplot.subplots_adjust', 'plt.subplots_adjust', (), '', True, 'import matplotlib.pyplot as plt\n'), ((64, 0, 64, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((19, 16, 19, 36), 'csv.reader', 'csv.reader', ({(19, 27, 19, 35): 'dataFile'}, {}), '(dataFile)', False, 'import csv\n'), ((46, 62, 46, 97), 'time.localtime', 'time.localtime', ({(46, 77, 46, 96): 'window_begin / 1000'}, {}), '(window_begin / 1000)', False, 'import time\n'), ((47, 60, 47, 93), 'time.localtime', 'time.localtime', ({(47, 75, 47, 92): 'window_end / 1000'}, {}), '(window_end / 1000)', False, 'import time\n')]