#! /usr/bin/env python

import matplotlib
import matplotlib.pyplot as plt
import math

labels = []
height_all = []
height_class0 = []
height_class1 = []
log_height_class0 = []
log_height_class1 = []
with open("ClassBCDE.input", "r") as f:
    for line in f:
        items = line.strip().split()

        label = items[0]
        labels.append(label)

        height = items[1].split(":")[1]
        height_all.append(height)

        height = int(height) + 1 # plus 1 to avoid log(0)
        if label == "+1":
            height_class0.append(height)
            log_height_class0.append(math.log(height, 2))
        elif label == "-1":
            height_class1.append(height)
            log_height_class1.append(math.log(height, 2))
        else:
            print("error label" + label)


title0 = "Class0: coding with small CUs (8x8 or 4x4)"
title1 = "Class1: coding with big CUs (> 8x8)"

fig_id = 0
# --------------------------------------------------------------
# plot height histgram for different classes
# --------------------------------------------------------------
# fig = plt.figure(fig_id)
# fig_id += 1

# ax11 = fig.add_subplot(211)
# n, bins, patches = plt.hist(height_class0, normed=1)
# ax11.plot(bins)
# ax11.set_title(' '.join(["height histgram of", title0]))
# ax11.set_xlabel("height")
# ax11.axis([200, 1200, 0, max(n) * 1.1])

# ax12 = fig.add_subplot(212)
# n, bins, patches = plt.hist(height_class1, normed=1)
# ax12.plot(bins)
# ax12.set_title(' '.join(["height histgram of", title1]))
# ax12.set_xlabel("height")
# ax12.axis([200, 1200, 0, max(n) * 1.1])

# --------------------------------------------------------------
# plot log2(height) histgram for different classes
# --------------------------------------------------------------
fig = plt.figure(fig_id)
fig_id += 1

ax11 = fig.add_subplot(211)
n, bins, patches = plt.hist(log_height_class0, normed=1)
ax11.plot(bins)
ax11.set_title(' '.join(["log2(height) histgram of", title0]))
ax11.set_xlabel("log2(height)")
ax11.axis([7, 11, 0, max(n) * 1.1])

ax12 = fig.add_subplot(212)
n, bins, patches = plt.hist(log_height_class1, normed=1)
ax12.plot(bins)
ax12.set_title(' '.join(["log2(height) histgram of", title1]))
ax12.set_xlabel("log2(height)")
ax12.axis([7, 11, 0, max(n) * 1.1])

plt.show()
