# Path: columns_contain.py
# def columns_contain(s, df):
#     'print columns names in df that contain string s'
#     'for interactive debugging'
#     result = [column_name for column_name in df.columns if s.lower() in column_name.lower()]
#     print result
#     return result
# 
# Path: Month.py
# class Month(object):
#     def __init__(self, value1, value2=None):
#         '''Constructors
#            Month('200703')
#            Month(200703)
#            Month(2007, 3)
#            Month(datetime.date)
#         '''
# 
#         if value2 is None:
#             if isinstance(value1, str):
#                 self.year = int(value1[:4])
#                 self.month = int(value1[4:])
#             elif isinstance(value1, int):
#                 self.month = value1 % 100
#                 self.year = (value1 - self.month) / 100
#             elif isinstance(value1, Month):
#                 self.month = value1.month
#                 self.year = value1.year
#             elif isinstance(value1, datetime.date):
#                 self.month = value1.month
#                 self.year = value1.year
#             else:
#                 print 'construction error: value1 is of type %s' % type(value1), value1
#         else:
#             self.year = int(value1)
#             self.month = int(value2)
# 
#         # enforce invariant (other methods depend on this)
#         assert self.year > 0, self
#         assert 1 <= self.month <= 12, self
# 
#     def __repr__(self):
#         return 'Month(year=%d, month=%d)' % (self.year, self.month)
# 
#     def increment(self, by=1):
#         'return new Month one month after self'
#         assert by >= 0, by
#         month = self.month + by
#         if month > 12:
#             delta_years = month // 12
#             month = month - 12 * delta_years
#             year = self.year + delta_years
#             return Month(year, month)
#         else:
#             return Month(self.year, month)
# 
#     def decrement(self, by=1):
#         'return new Month one month before self'
#         assert by >= 0, by
#         month = self.month - by
#         year = self.year
#         while month <= 0:
#             month += 12
#             year -= 1
#         return Month(year, month)
# 
#     def as_str(self):
#         return '%04d%02d' % (self.year, self.month)
# 
#     def as_int(self):
#         return self.year * 100 + self.month
# 
#     def equal(self, other):
#         return self.year == other.year and self.month == other.month
# 
#     def __eq__(self, other):
#         result = self.year == other.year and self.month == other.month
#         return result
# 
#     def __hash__(self):
#         return hash((self.year, self.month))  # hash of tuple of properties


import matplotlib.pyplot as plt
import pdb
from columns_contain import columns_contain
from Month import Month

from __future__ import division


cc = columns_contain


def make_chart_a(reduction, median_prices, control):
    'graph range of errors by month by method'
    print 'make_chart_a'

    def make_subplot(validation_month, reduction, relevant_median_prices):
        'mutate the default axes'
        # draw one line for each model family
        for model in ('en', 'gb', 'rf'):
            y = [v.mae
                 for k, v in reduction[validation_month].iteritems()
                 if k.model == model
                 ]
            plt.plot(y, label=model)  # the reduction is sorted by increasing mae
            plt.yticks(size='xx-small')
            if Month(validation_month) not in relevant_median_prices:
                print validation_month
                print relevant_median_prices
                print 'should not happen'
                pdb.set_trace()
            plt.title('yr mnth %s med price %6.0f' % (
                validation_month,

# Path: columns_contain.py
# def columns_contain(s, df):
#     'print columns names in df that contain string s'
#     'for interactive debugging'
#     result = [column_name for column_name in df.columns if s.lower() in column_name.lower()]
#     print result
#     return result
# 
# Path: Month.py
# class Month(object):
#     def __init__(self, value1, value2=None):
#         '''Constructors
#            Month('200703')
#            Month(200703)
#            Month(2007, 3)
#            Month(datetime.date)
#         '''
# 
#         if value2 is None:
#             if isinstance(value1, str):
#                 self.year = int(value1[:4])
#                 self.month = int(value1[4:])
#             elif isinstance(value1, int):
#                 self.month = value1 % 100
#                 self.year = (value1 - self.month) / 100
#             elif isinstance(value1, Month):
#                 self.month = value1.month
#                 self.year = value1.year
#             elif isinstance(value1, datetime.date):
#                 self.month = value1.month
#                 self.year = value1.year
#             else:
#                 print 'construction error: value1 is of type %s' % type(value1), value1
#         else:
#             self.year = int(value1)
#             self.month = int(value2)
# 
#         # enforce invariant (other methods depend on this)
#         assert self.year > 0, self
#         assert 1 <= self.month <= 12, self
# 
#     def __repr__(self):
#         return 'Month(year=%d, month=%d)' % (self.year, self.month)
# 
#     def increment(self, by=1):
#         'return new Month one month after self'
#         assert by >= 0, by
#         month = self.month + by
#         if month > 12:
#             delta_years = month // 12
#             month = month - 12 * delta_years
#             year = self.year + delta_years
#             return Month(year, month)
#         else:
#             return Month(self.year, month)
# 
#     def decrement(self, by=1):
#         'return new Month one month before self'
#         assert by >= 0, by
#         month = self.month - by
#         year = self.year
#         while month <= 0:
#             month += 12
#             year -= 1
#         return Month(year, month)
# 
#     def as_str(self):
#         return '%04d%02d' % (self.year, self.month)
# 
#     def as_int(self):
#         return self.year * 100 + self.month
# 
#     def equal(self, other):
#         return self.year == other.year and self.month == other.month
# 
#     def __eq__(self, other):
#         result = self.year == other.year and self.month == other.month
#         return result
# 
#     def __hash__(self):
#         return hash((self.year, self.month))  # hash of tuple of properties


# Path: chart06_make_chart_a.py
import matplotlib.pyplot as plt
import pdb
from columns_contain import columns_contain
from Month import Month


from __future__ import division


cc = columns_contain


def make_chart_a(reduction, median_prices, control):
    'graph range of errors by month by method'
    print 'make_chart_a'

    def make_subplot(validation_month, reduction, relevant_median_prices):
        'mutate the default axes'
        # draw one line for each model family
        for model in ('en', 'gb', 'rf'):
            y = [v.mae
                 for k, v in reduction[validation_month].iteritems()
                 if k.model == model
                 ]
            plt.plot(y, label=model)  # the reduction is sorted by increasing mae
            plt.yticks(size='xx-small')
            if Month(validation_month) not in relevant_median_prices:
                print validation_month
                print relevant_median_prices
                print 'should not happen'
                pdb.set_trace()
            plt.title('yr mnth %s med price %6.0f' % (
                validation_month,

                relevant_median_prices[Month(validation_month)]),