コード例 #1
0
    def __init__(self,
                 model,
                 view,
                 ids=None,
                 domain=[],
                 context={},
                 options=None):
        TinyCalendar.__init__(self, model, ids, view, domain, context, options)

        y, m, d = time.localtime()[:3]
        if options:
            y, m, d = options.date1[:3]

        self.week = Week(Day(y, m, d))
        self.events = self.get_events(self.week.days)

        self.selected_day = _get_selection_day(Day(y, m, d), self.selected_day,
                                               'week')

        minical = MiniCalendar(self.week[0], True)
        groupbox = GroupBox(self.colors,
                            self.color_values,
                            self.week[0],
                            group_relation=self.fields[self.color_field],
                            title=(self.color_field or None)
                            and self.fields[self.color_field]['string'],
                            mode='week')

        self.sidebar = Sidebar(minical, groupbox, self.use_search)
コード例 #2
0
    def predict(self, predictor, columns, data_dict, time_steps_back, lines_reservations=None):
        """
        Prepares feature vector for prediction algorithms an generates prediction
        :param predictor: sklearn predictor or other with simmilar interface
        :param columns: List with names of columns that must be in feature vector
        :param data_dict: Dictionary with time data containing columns names and default vaules
        :param time_steps_back: Number of time steps for one input to prediction algorithm
        :param lines_reservations: List with line reservations for predicted day generated by get_lines_usage_for_day method
        :return: Vector with prediction for the day (288 items)
        """
        lines_reserved_id = -1
        org_ids = dict()
        for org_id, column in enumerate(columns):
            if column.startswith('reserved_'):
                org_ids[column] = org_id
            if column == 'lines_reserved':
                lines_reserved_id = org_id

        if lines_reservations is None:
            lines_reservations = ['']*64
            
        data = list()
        for i in range(self.prediction_steps):
            data.append([0]*len(columns))
            for j, column in enumerate(columns):
                if column in data_dict:
                    data[i][j] = data_dict[column]

            slot_id = (data_dict['minute_of_day']-360)//15
            if slot_id >= 0 and slot_id < 64:
                org_list = lines_reservations[slot_id].split(',')[:-1]
                for name in org_list:
                    feature_name = 'reserved_' + name
                    if feature_name in columns:
                        data[i][org_ids[feature_name]] += 1
                    elif 'reserved_other' in columns:
                        data[i][org_ids['reserved_other']] += 1
                        
                    if lines_reserved_id >= 0:
                        data[i][lines_reserved_id] += 1
                        
            data_dict['minute'] += 5
            data_dict['minute_of_day'] += 5
            if data_dict['minute'] == 60:
                data_dict['minute'] = 0
                data_dict['hour'] += 1

        df = pd.DataFrame(data, columns=columns) 
        day = Day('ts')
        day.data = df
        x, y = self.dh.get_feature_vectors_from_days([day], [], time_steps_back, 1, True)
        return self.dh.predict_day_from_features(x, predictor, time_steps_back)
コード例 #3
0
    def __init__(self,
                 model,
                 view,
                 ids=None,
                 domain=[],
                 context={},
                 options=None):
        TinyCalendar.__init__(self, model, ids, view, domain, context, options)

        y, m, d = time.localtime()[:3]
        if options:
            y, m, d = options.date1[:3]

        self.day = Day(y, m, d)

        self.events = self.get_events([self.day])

        minical = MiniCalendar(self.day)
        groupbox = GroupBox(self.colors,
                            self.color_values,
                            self.day,
                            group_relation=self.fields[self.color_field],
                            title=(self.color_field or None)
                            and self.fields[self.color_field]['string'],
                            mode='day')

        self.sidebar = Sidebar(minical, groupbox, self.use_search)
コード例 #4
0
    def __init__(self,
                 model,
                 view,
                 ids=None,
                 domain=[],
                 context={},
                 options=None):

        TinyCalendar.__init__(self, model, ids, view, domain, context, options)

        y, m = time.localtime()[:2]
        if options:
            y = options.year
            m = options.month

        self.month = Month(y, m)
        self.events = self.get_events(self.month.days)

        self.selected_day = _get_selection_day(Day(y, m, 1), self.selected_day,
                                               'month')

        minical = MiniCalendar(self.selected_day)
        groupbox = GroupBox(self.colors,
                            self.color_values,
                            self.selected_day,
                            group_relation=self.fields[self.color_field],
                            title=(self.color_field or None)
                            and self.fields[self.color_field]['string'],
                            mode='month')

        self.sidebar = Sidebar(minical, groupbox, self.use_search)
コード例 #5
0
def _get_selection_day(day, selected, mode):
    selected = selected or Day.today()

    if mode == 'day':
        return day

    if mode == 'week':
        return Week(day)[selected.weekday()]

    month = day.month2
    d = selected.day

    if d > month.range[-1]:
        d = month.range[-1]

    return Day(day.year, day.month, d)
コード例 #6
0
def _get_selection_day(day, selected, mode):
    selected = selected or Day.today()

    if mode == 'day':
        return day

    if mode == 'week':
        return Week(day)[selected.weekday()]

    month = day.month2
    d = selected.day

    if d > month.range[-1]:
        d = month.range[-1]

    return Day(day.year, day.month, d)
コード例 #7
0
    def prepare_days_data(self):
        """
        Loads pickle with all Days
        """
        if os.path.isfile(self.days_data_path):
            with open(self.days_data_path, 'rb') as input_file:
                self.days_train, self.days_test, self.days_valid = pickle.load(
                    input_file)
            self.columns = self.days_train[0].data.columns
        else:
            if os.path.isfile(self.csv_path):
                days_stats = [0, 0, 0, 0, 0, 0, 0]
                print('Preparing days.pickle')
                days_list = []
                last_date = 'start'
                day_start_id = 0
                day_stop_id = 0
                n_bad_days = 0

                data_frame = pd.read_csv(self.csv_path)
                for index, row in data_frame.iterrows():
                    if row['minute_of_day'] > 1320:
                        data_frame['pool'].iloc[index] = 0

                    new_date = data_frame['time'].iloc[index][:10]
                    if not last_date == new_date:
                        day_stop_id = index
                        if index > 0:
                            new_day = Day(last_date)
                            new_day.data = data_frame.iloc[
                                day_start_id:day_stop_id]
                            if day_stop_id - day_start_id == 288:
                                days_list.append(new_day)
                                days_stats[
                                    data_frame['day_of_week'].iloc[index]] += 1
                            else:
                                if abs(day_stop_id - day_start_id - 288) < 15:
                                    expected = 0
                                    n_bad_days += 1
                                    print(
                                        'Error in day %s, length of day is %d'
                                        % (last_date,
                                           day_stop_id - day_start_id))
                                    for value in list(
                                            data_frame['minute_of_day'].
                                            iloc[day_start_id:day_stop_id]):
                                        if not value == expected:
                                            print('Should be %d is %d' %
                                                  (expected, value))
                                            expected = value
                                        expected += 5
                                    print('\n\n')

                                # TODO: Most of them have less than 10 missing values.
                                # If the missinga values are out of openning hours - fill with zeros and use
                                # Many other missing values can be filled in
                                # Also change of time from summer to winter makes 1 hour gap or duplicate hour
                                # Move this function to data preprocessing

                        last_date = data_frame['time'].iloc[index][:10]
                        day_start_id = index

                Random(RANDOM_SEED).shuffle(days_list)
                train_portion = 0.4
                validation_portion = 0.2
                n_days = len(days_list)
                print('Generated %d days. (%d days removed)' %
                      (n_days, n_bad_days))
                print('Number of days from Monday to Sunday', days_stats)
                n_train_days = int(n_days * train_portion)
                n_validation_days = int(n_days * validation_portion)
                train_days = days_list[:n_train_days]
                validation_days = days_list[n_train_days:n_train_days +
                                            n_validation_days]
                test_days = days_list[n_train_days + n_validation_days:]

                with open(self.days_data_path, 'wb') as input_file:
                    pickle.dump([train_days, test_days, validation_days],
                                input_file)

            else:
                raise Exception(
                    'Missing days.pickle and dataset.csv.\nGenerate dataset.csv in preprocess_data.py first.'
                )
コード例 #8
0
ファイル: base.py プロジェクト: xrg/openerp-client-web
    def __init__(self, model, ids, view, domain=[], context={}, options=None):

        super(ICalendar, self).__init__()

        self.info_fields = []
        self.fields = []

        self.events = {}

        self.colors = {}
        self.color_values = []

        self.calendar_fields = {}
        self.concurrency_info = None

        self.ids = ids
        self.model = model
        self.domain = domain or []
        self.context = context or {}
        self.options = options

        self.date_format = format.get_datetime_format('date')
        self.use_search = (options or None) and options.use_search

        try:
            dt = parse_datetime(options.selected_day)
            self.selected_day = Day(dt.year, dt.month, dt.day)
        except:
            pass

        proxy = rpc.RPCProxy(model)

        view_id = view.get('view_id', False)

        dom = xml.dom.minidom.parseString(view['arch'].encode('utf-8'))
        root = dom.childNodes[0]
        attrs = tools.node_attributes(root)

        self.string = attrs.get('string', '')
        self.date_start = attrs.get('date_start')
        self.date_delay = attrs.get('date_delay')
        self.date_stop = attrs.get('date_stop')
        self.color_field = attrs.get('color')
        self.day_length = int(attrs.get('day_length', 8))

        if options and options.mode:
            self.mode = options.mode
        else:
            self.mode = attrs.get('mode') or self.mode or 'month'

        self.info_fields = self.parse(root, view['fields'])

        fields = view['fields']
        fields = fields.keys() + [
            self.date_start, self.date_stop, self.date_delay, self.color_field
        ]

        fields = list(set([x for x in fields if x]))

        self.fields = proxy.fields_get(fields)

        if self.color_field and options and options.colors:
            self.colors = options.colors

        if self.color_field and options and options.color_values:
            self.color_values = options.color_values

        self.calendar_fields['date_start'] = dict(
            name=self.date_start, kind=self.fields[self.date_start]['type'])

        if self.date_delay:
            self.calendar_fields['date_delay'] = dict(
                name=self.date_delay,
                kind=self.fields[self.date_delay]['type'])

        if self.date_stop:
            self.calendar_fields['date_stop'] = dict(
                name=self.date_stop, kind=self.fields[self.date_stop]['type'])

        self.calendar_fields['day_length'] = self.day_length
コード例 #9
0
    def __init__(self, model, ids, view, domain=[], context={}, options=None):

        self.level = None
        self.groups = []
        self.days = []
        self.headers = []

        super(GanttCalendar, self).__init__(model, ids, view, domain, context, options)

        y, m, d = time.localtime()[:3]
        if options:
            y, m, d = options.date1[:3]

        day = Day(y, m, d)

        if self.mode == 'day':
            self.days = [day]
            self.title = ustr(day)
            self.selected_day = day

            self.headers = [(48, ustr(day))]
            self.subheaders = [time.strftime('%H', (y, m, d, i, 0, 0, 1, 1, 0)) for i in range(24)]

        elif self.mode == '3days':
            dp = day - 1
            dn = day + 1
            self.days = [dp, day, dn]
            self.title = u"%s, %s, %s" % (ustr(dp), ustr(day), ustr(dn))
            self.selected_day = day

            self.headers = [(24, ustr(dp)), (24, ustr(day)), (24, ustr(dn))]
            self.subheaders = [time.strftime('%H', (y, 1, 1, i, 0, 0, 1, 1, 0)) for i in range(0, 24, 6)]

        elif self.mode == 'week':
            self.days = [d for d in Week(day)]
            self.title = _("%s, Week %s") % (y, day.strftime("%W"))
            self.selected_day = _get_selection_day(day, self.selected_day, 'week')
            self.headers = [(12, u"%s %s" % (d.month2.name, d.day)) for d in self.days]
            self.subheaders = []
            for x in self.days:
                for i in [0, 12]:
                    self.subheaders.append(time.strftime('%H', (y, 1, 1, i, 0, 0, 1, 1, 0)))

        elif self.mode == '3weeks':
            w = Week(day)
            wp = w - 1
            wn = w + 1
            self.days = wp.days + w.days + wn.days
            self.title = _(u"%s - %s") % (ustr(self.days[0]), ustr(self.days[-1]))
            self.selected_day = _get_selection_day(day, self.selected_day, 'week')
            self.headers = [(7, _("Week %s") % w1[0].strftime('%W')) for w1 in [wp, w, wn]]
            self.subheaders = [format_date_custom(x, "E d") for x in itertools.chain(wp, w, wn)]

        elif self.mode == '3months':
            q = 1 + (m - 1) / 3

            mn = Month(y, q * 3)
            mt = mn.prev()
            mp = mt.prev()

            days = [d for d in mp if d.year == mp.year and d.month == mp.month] \
                 + [d for d in mt if d.year == mt.year and d.month == mt.month] \
                 + [d for d in mn if d.year == mn.year and d.month == mn.month]

            self.days = days
            self.title = _("%s, Qtr %s") % (y, q)
            self.selected_day = _get_selection_day(day, self.selected_day, '3months')

            headers = list(itertools.chain(mp.weeks, mt.weeks, mn.weeks))

            self.headers = [(mp.range[-1], ustr(mp)), (mt.range[-1], ustr(mt)), (mn.range[-1], ustr(mn))]
            self.subheaders = []
            for x in headers:
                x = _("Week %s") % x[0].strftime('%W')
                if x not in self.subheaders:
                    self.subheaders.append(x)

        elif self.mode == 'year':
            yr = Year(y)

            self.days = yr.days
            self.title = u"Year %s" % (y)
            self.selected_day = _get_selection_day(day, self.selected_day, 'year')
            self.headers = [(m.range[-1], m.name) for m in yr.months]
            self.subheaders = [_("W %s") % x[0].strftime('%W') for x in yr.weeks]

        elif self.mode == '3years':
            yr = Year(y)
            yp = yr - 1
            yn = yr + 1

            self.days = yp.days + yr.days + yn.days
            self.title = _("Year %s to Year %s") % (y - 1, y + 1)
            self.selected_day = _get_selection_day(day, self.selected_day, 'year')

            self.headers = [(4, y - 1), (4, y), (4, y + 1)]
            self.subheaders = ['Q1', 'Q2', 'Q3', 'Q4'] * 3

        elif self.mode == '5years':
            yr = Year(y)
            yp1 = yr - 1
            yp2 = yr - 2
            yn1 = yr + 1
            yn2 = yr + 1

            self.days = yp2.days + yp1.days + yr.days + yn1.days + yn2.days
            self.title = _("Year %s to Year %s") % (y - 2, y + 2)
            self.selected_day = _get_selection_day(day, self.selected_day, 'year')

            self.headers = [(2, y - 2), (2, y - 1), (2, y), (2, y + 1), (2, y + 2)]
            self.subheaders = ['H1', 'H2'] * 5

        else:
            month = Month(y, m)
            self.days = [d for d in month]
            self.title = ustr(month)
            self.selected_day = _get_selection_day(day, self.selected_day, 'month')
            self.headers = [(7, _("Week %s") % w[0].strftime('%W')) for w in month.weeks]
            self.subheaders = [d.day for d in month]

        if self.level:
            field = self.level['link']
            fields = rpc.RPCProxy(self.model).fields_get([field], rpc.session.context)
            self.fields.update(fields)

        self.events = self.get_events(self.days)
        self.groups = self.get_groups(self.events)

        minical = MiniCalendar(day)
        groupbox = GroupBox(self.colors, self.color_values, day,
                group_relation=self.fields[self.color_field],
                title=(self.color_field or None) and self.fields[self.color_field]['string'], mode=self.mode)

        self.sidebar = Sidebar(minical, groupbox, self.use_search)
コード例 #10
0
    def __init__(self, model, ids, view, domain=[], context={}, options=None):

        self.level = None
        self.groups = []
        self.days = []
        self.headers = []

        super(GanttCalendar, self).__init__(model, ids, view, domain, context,
                                            options)

        y, m, d = time.localtime()[:3]
        if options:
            y, m, d = options.date1[:3]

        day = Day(y, m, d)

        if self.mode == 'day':
            self.days = [day]
            self.title = ustr(day)
            self.selected_day = day

            self.headers = [(48, ustr(day))]
            self.subheaders = [
                time.strftime('%H', (y, m, d, i, 0, 0, 1, 1, 0))
                for i in range(24)
            ]

        elif self.mode == '3days':
            dp = day - 1
            dn = day + 1
            self.days = [dp, day, dn]
            self.title = u"%s, %s, %s" % (ustr(dp), ustr(day), ustr(dn))
            self.selected_day = day

            self.headers = [(24, ustr(dp)), (24, ustr(day)), (24, ustr(dn))]
            self.subheaders = [
                time.strftime('%H', (y, 1, 1, i, 0, 0, 1, 1, 0))
                for i in range(0, 24, 6)
            ]

        elif self.mode == 'week':
            self.days = [d for d in Week(day)]
            self.title = _("%s, Week %s") % (y, day.strftime("%W"))
            self.selected_day = _get_selection_day(day, self.selected_day,
                                                   'week')
            self.headers = [(12, u"%s %s" % (d.month2.name, d.day))
                            for d in self.days]
            self.subheaders = []
            for x in self.days:
                for i in [0, 12]:
                    self.subheaders.append(
                        time.strftime('%H', (y, 1, 1, i, 0, 0, 1, 1, 0)))

        elif self.mode == '3weeks':
            w = Week(day)
            wp = w - 1
            wn = w + 1
            self.days = wp.days + w.days + wn.days
            self.title = _(u"%s - %s") % (ustr(
                self.days[0]), ustr(self.days[-1]))
            self.selected_day = _get_selection_day(day, self.selected_day,
                                                   'week')
            self.headers = [(7, _("Week %s") % w1[0].strftime('%W'))
                            for w1 in [wp, w, wn]]
            self.subheaders = [
                format_date_custom(x, "E d")
                for x in itertools.chain(wp, w, wn)
            ]

        elif self.mode == '3months':
            q = 1 + (m - 1) / 3

            mn = Month(y, q * 3)
            mt = mn.prev()
            mp = mt.prev()

            days = [d for d in mp if d.year == mp.year and d.month == mp.month] \
                 + [d for d in mt if d.year == mt.year and d.month == mt.month] \
                 + [d for d in mn if d.year == mn.year and d.month == mn.month]

            self.days = days
            self.title = _("%s, Qtr %s") % (y, q)
            self.selected_day = _get_selection_day(day, self.selected_day,
                                                   '3months')

            headers = list(itertools.chain(mp.weeks, mt.weeks, mn.weeks))

            self.headers = [(mp.range[-1], ustr(mp)), (mt.range[-1], ustr(mt)),
                            (mn.range[-1], ustr(mn))]
            self.subheaders = []
            for x in headers:
                x = _("Week %s") % x[0].strftime('%W')
                if x not in self.subheaders:
                    self.subheaders.append(x)

        elif self.mode == 'year':
            yr = Year(y)

            self.days = yr.days
            self.title = u"Year %s" % (y)
            self.selected_day = _get_selection_day(day, self.selected_day,
                                                   'year')
            self.headers = [(m.range[-1], m.name) for m in yr.months]
            self.subheaders = [
                _("W %s") % x[0].strftime('%W') for x in yr.weeks
            ]

        elif self.mode == '3years':
            yr = Year(y)
            yp = yr - 1
            yn = yr + 1

            self.days = yp.days + yr.days + yn.days
            self.title = _("Year %s to Year %s") % (y - 1, y + 1)
            self.selected_day = _get_selection_day(day, self.selected_day,
                                                   'year')

            self.headers = [(4, y - 1), (4, y), (4, y + 1)]
            self.subheaders = ['Q1', 'Q2', 'Q3', 'Q4'] * 3

        elif self.mode == '5years':
            yr = Year(y)
            yp1 = yr - 1
            yp2 = yr - 2
            yn1 = yr + 1
            yn2 = yr + 1

            self.days = yp2.days + yp1.days + yr.days + yn1.days + yn2.days
            self.title = _("Year %s to Year %s") % (y - 2, y + 2)
            self.selected_day = _get_selection_day(day, self.selected_day,
                                                   'year')

            self.headers = [(2, y - 2), (2, y - 1), (2, y), (2, y + 1),
                            (2, y + 2)]
            self.subheaders = ['H1', 'H2'] * 5

        else:
            month = Month(y, m)
            self.days = [d for d in month]
            self.title = ustr(month)
            self.selected_day = _get_selection_day(day, self.selected_day,
                                                   'month')
            self.headers = [(7, _("Week %s") % w[0].strftime('%W'))
                            for w in month.weeks]
            self.subheaders = [d.day for d in month]

        if self.level:
            field = self.level['link']
            fields = rpc.RPCProxy(self.model).fields_get([field],
                                                         rpc.session.context)
            self.fields.update(fields)

        self.events = self.get_events(self.days)
        self.groups = self.get_groups(self.events)

        minical = MiniCalendar(day)
        groupbox = GroupBox(self.colors,
                            self.color_values,
                            day,
                            group_relation=self.fields[self.color_field],
                            title=(self.color_field or None)
                            and self.fields[self.color_field]['string'],
                            mode=self.mode)

        self.sidebar = Sidebar(minical, groupbox, self.use_search)