1
2
3
feather.write_dataframe(zero, '../data/zero_train.feather')
feather.write_dataframe(one, '../data/one_train.feather')
feather.write_dataframe(two, '../data/two_train.feather')

Bokeh Plotting Exploration

#Bokeh-Plotting-Exploration
Loading output library...
Loading output library...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def make_plot_all(df, times = 1.75, name = ''):
    
    week_days = list(set(zero['wday_name']))
    color_mapper = CategoricalColorMapper(palette=Dark2_7, factors=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])
    hover = HoverTool(tooltips=[('Holiday', '@Holiday')])

    sub = df[['time', 'wday_name', 'Values', 'month']]
    
    months = list(set(sub['month']))

    mean = sub.groupby(['time', 'wday_name', 'month']).mean().reset_index(level=None)
    quantile = sub.groupby(['time', 'wday_name', 'month']).quantile(0.75).reset_index(level=None)
    std = sub.groupby(['time', 'wday_name', 'month']).std().reset_index(level=None)

    mean = mean.rename(columns={'Values': 'mean'})
    quantile = quantile.rename(columns={'Values': 'quantile'})
    std = std.rename(columns={'Values': 'std'})

    df = df.merge(mean, how='left', on = ['time', 'wday_name', 'month'])
    df = df.merge(quantile, how='left', on = ['time', 'wday_name', 'month'])
    df = df.merge(std, how='left', on = ['time', 'wday_name', 'month'])

    df['threshold'] = times * df['std'] + df['quantile']
    all_tabs=[]

    checkbox_group = CheckboxGroup(labels=color_mapper.factors, 
                                   active = [0, 1, 2, 3, 4, 5, 6])
    
    b = widgetbox([checkbox_group])
    
    
    for month in months[:2]:
        month_df = df[df['month'] == month]
        
        times = []
        thresholds = []

        for wday in [0, 1, 2, 3, 4, 5, 6]:
            if name == 'two':
                stop = 48
            else:
                stop = 96
            weekday = month_df[month_df['wday'] == wday].reset_index(level=0)[:stop]
            weekday = weekday.sort_values(['time'])
            
            times.append(list(weekday['time']))
            thresholds.append(list(weekday['threshold']))
    
        
        m = ColumnDataSource(month_df)
        p = figure(plot_width=800, plot_height=600, x_axis_label='Time', y_axis_label='Energy')
        
        t = ColumnDataSource({'time': times, 
                             'threshold': thresholds,
                             'color': list(color_mapper.palette)})
        
        p.circle('time', 'Values', color = dict({'field': 'wday_name', 'transform': color_mapper}),
        source=m, alpha=0.6, legend='wday_name')
        p.multi_line('time', 'threshold', color='color', line_width= 4, source=t)
        p.add_tools(hover)
        
        column = [p, b]
        layout = gridplot([column])
        
        tab = Panel(child=p, title=calendar.month_abbr[month])

        all_tabs.append(tab)

    tabs = Tabs(tabs=all_tabs)
    output_notebook()
    # output_file('month.html')
    show(tabs)
    return all_tabs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def modify_doc(doc):  
    # Map days of the week to a color
    color_mapper = CategoricalColorMapper(palette=Dark2_7, 
                                          factors=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])
    # Hover tool shows days which are holidays
    hover = HoverTool(tooltips=[('Holiday', '@Holiday')])
    
    def create_figure():
        times = threshold_select.value
        # Add one to the month to account for calendar month
        month_focus = month_select.active + 1
        current_day = weekday_name.value

        # Select the month and the day
        month_df = df[df['month'] == month_focus]
        day_df = month_df[month_df['wday_name'] == current_day]
        
        # Choose whether or not to exclude holidays from stats
        if holiday_check.active == [0]:
            df['threshold'] = times * df['std_no_holidays'] + df['quantile_no_holidays']
        
        else:
            df['threshold'] = times * df['std'] + df['quantile']
        
        if name == 'two': stop = 48
        else: stop = 96
 

        weekday = day_df[:stop]
        weekday = weekday.sort_values(['time'])
        times = (list(weekday['time']))
        thresholds = (list(weekday['threshold']))
        
        # Data for points
        m = ColumnDataSource(day_df)
        # Data for threshold
        t = ColumnDataSource({'time': times, 
                             'threshold': thresholds})
        
        # Create the plot, add circles for observations and lines for the threshold
        p = figure(plot_width=800, plot_height=600, x_axis_label='Time', y_axis_label='Energy')
        p.circle('time', 'Values', color = dict({'field': 'wday_name', 'transform': color_mapper}),
        source=m, alpha=0.6, legend='wday_name')
        p.line(times, thresholds, color = color_mapper.palette[color_mapper.factors.index(current_day)], line_width= 4)
        
        # Add the hover tool
        p.add_tools(hover)
        return p
        
    # Update the plot on change
    def update(attr, old, new):
        layout.children[1] = create_figure()

    # Choose to exclude holidays from the stats
    holiday_check = CheckboxGroup(labels=['Holidays Excluded'], active = [0])
    holiday_check.on_change('active', update)
    
    # Select the threshold for anomalies
    threshold_select = Slider(title='threshold', start=1.0, end=3.0, step=0.1, value=1.75)
    threshold_select.on_change('value', update)
    
    # Select the month with a Radio Button
    month_select = RadioButtonGroup(labels=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                                            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], 
                                   active=0)
    month_select.on_change('active', update)
    
    # Select the day of the week
    weekday_name = Select(title='weekday', options = color_mapper.factors,
                         value = color_mapper.factors[0])
    weekday_name.on_change('value', update)
    
    # Create the figure using the function
    p = create_figure()
    
    # Set up the control widgetbox
    controls = widgetbox([month_select, weekday_name, threshold_select, holiday_check], width=200)
    layout = row(controls, p)
    doc.add_root(layout)
    
handler = FunctionHandler(modify_doc)
app = Application(handler)

doc = app.create_document()
show(app, notebook_url = 'localhost:8890')
Loading output library...
Loading output library...
Loading output library...