# Path: Bunch.py
# class Bunch(object):
#     @staticmethod
#     def from_namespace(namespace):
#         b = Bunch()
#         for attr, value in namespace.__dict__.iteritems():
#             b.__dict__[attr] = value
#         return b
# 
#     def __init__(self, **kwds):
#         self.__dict__.update(kwds)
# 
#     def values(self, d, basename):
#         # return list of values basename.key = value
#         result = []
#         for k in sorted(d):
#             v = d[k]
#             if isinstance(v, Bunch):
#                 vs = self.values(v.__dict__, k)
#                 for v in vs:
#                     result.append(v)
#             elif isinstance(v, argparse.Namespace):
#                 for k2, v2 in v.__dict__.items():
#                     result.append('%s.%s = %s' % (str(k), str(k2), str(v2)))
#             elif basename == '':
#                 result.append('%s = %s' % (k, v))
#             else:
#                 result.append('%s.%s = %s' % (basename, k, v))
#         return result
# 
#     def __str__(self, base_name=''):
#         values = self.values(self.__dict__, '')
#         s = ''
#         for value in values:
#             s += '%s' % value
#             if len(values) > 1:
#                 s += '\n'
#         return s
# 
#     def __repr__(self):
#         s = 'Bunch('
#         first = True
#         for k, v in self.__dict__.iteritems():
#             if first:
#                 first = False
#             else:
#                 s += ', '
#             s += '%s=%s' % (k, v)
#         s += ')'
#         return s
# 
# Path: directory.py
# def directory(name):
#     '''Return path to specified directory in file system.
# 
#     Parameters
#     ----------
#     name : string
#       name of the directory, one of cells, input, log, working
# 
#     Returns
#     -------
#     string: path to the named directory, ending with a "/"
#     '''
# 
#     root = '../'  # root of this project
#     if name == 'cells' or name == 'cv-cell' or name == 'cv-cell-natural':
#         return root + 'data/working/cv-cell/'
#     elif name == 'cv-cell-rescaled':
#         return root + 'data/working/cv-cell-rescaled/'
#     elif name == 'log':
#         return root + 'data/working/log/'
#     elif name == 'working':
#         return root + 'data/working/'
#     elif name == 'src':
#         return root + 'src/'
#     else:
#         raise ValueError(name)
# 
# Path: Logger.py
# class Logger(object):
#     # from stack overflow: how do i duplicat sys stdout to a log file in python
# 
#     def __init__(self, logfile, logfile_path=None, logfile_mode='w', base_name=None, add_time_stamp=False):
#         if logfile_path is not None:
#             raise RuntimeError('logfile_path is deprecated; use positional arg')
#         if base_name is not None:
#             raise RuntimeError('base_name is deprecated; use positional arg')
#         if add_time_stamp:
#             parts = logfile.split('.')
#             timestamp =  datetime.datetime.now().isoformat('T')
#             path = '.'.join(parts[:-1]) + '-' + timestamp + '.' + parts[-1]
#         else:
#             path = logfile
#         self.terminal = sys.stdout
#         self.log = open(path, logfile_mode)
# 
#     def write(self, message):
#         self.terminal.write(message)
#         self.log.write(message)
# 
#     def flush(self):
#         self.terminal.flush()
#         pass


import collections
import cPickle as pickle
import datetime
import numpy as np
import os
import pandas as pd
import pdb
import sys
import warnings
import parse_command_line
from pprint import pprint
from sklearn import cross_validation
from sklearn import linear_model
from sklearn import ensemble
from Bunch import Bunch
from DataframeAppender import DataframeAppender
from directory import directory
from Logger import Logger
        usage('missing --model')
    if arg.td is None:
        usage('missing --td')

    # validate combinations of invocation options
    if arg.model == 'lr':
        if arg.hpx is None or arg.hpy is None:
            usage('model lr requires --hpx and --hpy')
    elif arg.model == 'rf':
        if arg.hpd is None or arg.hpw is None:
            usage('model rf requires --hpd and --hpw')
    else:
        usage('bad --model: %s' % str(arg.model))

    random_seed = 123
    now = datetime.datetime.now()
    predictors = make_predictors()
    print 'number of predictors', len(predictors)
    sale_date_range = make_DateRange(arg.sale_date, 15 if arg.month else 3)
    log_file_name = arg.base_name + '.' + now.isoformat('T') + '.log'
    # dir_out: WORKING/ege_[month|week]/<sale_date>/
    dir_out = (directory('working') +
               'ege_' +
               ('month' if arg.month else 'week') +
               '/' + argv[1] + '/'
               )

    debug = False
    test = arg.test


# Path: Bunch.py
# class Bunch(object):
#     @staticmethod
#     def from_namespace(namespace):
#         b = Bunch()
#         for attr, value in namespace.__dict__.iteritems():
#             b.__dict__[attr] = value
#         return b
# 
#     def __init__(self, **kwds):
#         self.__dict__.update(kwds)
# 
#     def values(self, d, basename):
#         # return list of values basename.key = value
#         result = []
#         for k in sorted(d):
#             v = d[k]
#             if isinstance(v, Bunch):
#                 vs = self.values(v.__dict__, k)
#                 for v in vs:
#                     result.append(v)
#             elif isinstance(v, argparse.Namespace):
#                 for k2, v2 in v.__dict__.items():
#                     result.append('%s.%s = %s' % (str(k), str(k2), str(v2)))
#             elif basename == '':
#                 result.append('%s = %s' % (k, v))
#             else:
#                 result.append('%s.%s = %s' % (basename, k, v))
#         return result
# 
#     def __str__(self, base_name=''):
#         values = self.values(self.__dict__, '')
#         s = ''
#         for value in values:
#             s += '%s' % value
#             if len(values) > 1:
#                 s += '\n'
#         return s
# 
#     def __repr__(self):
#         s = 'Bunch('
#         first = True
#         for k, v in self.__dict__.iteritems():
#             if first:
#                 first = False
#             else:
#                 s += ', '
#             s += '%s=%s' % (k, v)
#         s += ')'
#         return s
# 
# Path: directory.py
# def directory(name):
#     '''Return path to specified directory in file system.
# 
#     Parameters
#     ----------
#     name : string
#       name of the directory, one of cells, input, log, working
# 
#     Returns
#     -------
#     string: path to the named directory, ending with a "/"
#     '''
# 
#     root = '../'  # root of this project
#     if name == 'cells' or name == 'cv-cell' or name == 'cv-cell-natural':
#         return root + 'data/working/cv-cell/'
#     elif name == 'cv-cell-rescaled':
#         return root + 'data/working/cv-cell-rescaled/'
#     elif name == 'log':
#         return root + 'data/working/log/'
#     elif name == 'working':
#         return root + 'data/working/'
#     elif name == 'src':
#         return root + 'src/'
#     else:
#         raise ValueError(name)
# 
# Path: Logger.py
# class Logger(object):
#     # from stack overflow: how do i duplicat sys stdout to a log file in python
# 
#     def __init__(self, logfile, logfile_path=None, logfile_mode='w', base_name=None, add_time_stamp=False):
#         if logfile_path is not None:
#             raise RuntimeError('logfile_path is deprecated; use positional arg')
#         if base_name is not None:
#             raise RuntimeError('base_name is deprecated; use positional arg')
#         if add_time_stamp:
#             parts = logfile.split('.')
#             timestamp =  datetime.datetime.now().isoformat('T')
#             path = '.'.join(parts[:-1]) + '-' + timestamp + '.' + parts[-1]
#         else:
#             path = logfile
#         self.terminal = sys.stdout
#         self.log = open(path, logfile_mode)
# 
#     def write(self, message):
#         self.terminal.write(message)
#         self.log.write(message)
# 
#     def flush(self):
#         self.terminal.flush()
#         pass


# Path: ege_week.py
import collections
import cPickle as pickle
import datetime
import numpy as np
import os
import pandas as pd
import pdb
import sys
import warnings
import parse_command_line
from pprint import pprint
from sklearn import cross_validation
from sklearn import linear_model
from sklearn import ensemble
from Bunch import Bunch
from DataframeAppender import DataframeAppender
from directory import directory
from Logger import Logger

        usage('missing --model')
    if arg.td is None:
        usage('missing --td')

    # validate combinations of invocation options
    if arg.model == 'lr':
        if arg.hpx is None or arg.hpy is None:
            usage('model lr requires --hpx and --hpy')
    elif arg.model == 'rf':
        if arg.hpd is None or arg.hpw is None:
            usage('model rf requires --hpd and --hpw')
    else:
        usage('bad --model: %s' % str(arg.model))

    random_seed = 123
    now = datetime.datetime.now()
    predictors = make_predictors()
    print 'number of predictors', len(predictors)
    sale_date_range = make_DateRange(arg.sale_date, 15 if arg.month else 3)
    log_file_name = arg.base_name + '.' + now.isoformat('T') + '.log'
    # dir_out: WORKING/ege_[month|week]/<sale_date>/
    dir_out = (directory('working') +
               'ege_' +
               ('month' if arg.month else 'week') +
               '/' + argv[1] + '/'
               )

    debug = False
    test = arg.test


    b = Bunch(