Example #1
0
def _pack_column_data_source(kwargs, source):
    """
    TBD
    """
    if 'size' in kwargs and isinstance(kwargs['size'], list):
        source.add(kwargs['size'], 'size')
        del kwargs['size']
    if 'radius' in kwargs and isinstance(kwargs['radius'], list):
        source.add(kwargs['radius'], 'radius')
        del kwargs['radius']
    if 'c' in kwargs and isinstance(kwargs['c'], list):
        source.add(kwargs['c'], 'color')
        del kwargs['c']
    if 'line_color' in kwargs and isinstance(kwargs['line_color'], list):
        source.add(kwargs['line_color'], 'line_color')
        del kwargs['line_color']
    if 'color' in kwargs and isinstance(kwargs['color'], list):
        p = palette(max(kwargs['color']))
        source.add([p[i-1] for i in kwargs['color']], 'color')
        del kwargs['color']
    if 'marker' in kwargs and isinstance(kwargs['marker'], list):
        source.add(kwargs['marker'], 'marker')
        del kwargs['marker']

    # TODO i hate this
    for k,v in list(kwargs.items()):
        if isinstance(v, list):
            source.add(v, k)
            del kwargs[k]
Example #2
0
def _pack_column_data_source(kwargs, source):
    """
    TBD
    """
    if 'size' in kwargs and isinstance(kwargs['size'], list):
        source.add(kwargs['size'], 'size')
        del kwargs['size']
    if 'color' in kwargs and isinstance(kwargs['color'], list):
        p = palette(max(kwargs['color']))
        source.add([p[i - 1] for i in kwargs['color']], 'color')
        del kwargs['color']
    if 'marker' in kwargs and isinstance(kwargs['marker'], list):
        source.add(kwargs['marker'], 'marker')
        del kwargs['marker']
Example #3
0
    def visualize(self, bucketing=True):
        if bucketing:
            er_n = len(self.all_episodes_returns)
            er_buckets_number = min(int(np.ceil(0.1 * er_n)), 100)
            er_bucket_size = int(er_n / er_buckets_number)
            performance = (np.array(
                bucketify(self.all_episodes_returns, er_buckets_number,
                          np.mean)) + 1) / 2.0
        else:
            performance = self.all_episodes_returns
        mean_reward = np.mean(self.all_episodes_returns)

        ep_act_times_flatten = [
            item for sublist in self.episodes_actions_times for item in sublist
        ]
        mean_action_time = np.mean(ep_act_times_flatten)
        if bucketing:
            at_n = len(ep_act_times_flatten)
            at_buckets_number = min(int(np.ceil(0.1 * er_n)), 100)
            at_bucket_size = int(at_n / at_buckets_number)
            action_times = bucketify(ep_act_times_flatten, at_buckets_number,
                                     np.mean)
        else:
            action_times = ep_act_times_flatten

        # fig = plt.figure(constrained_layout=True)
        # fig.suptitle(f"{self.__class__.__name__}", fontsize=16, y=1.1)
        # spec = gridspec.GridSpec(ncols=1, nrows=2, figure=fig)
        #
        # ax0 = fig.add_subplot(spec[0, 0])
        # if bucketing:
        #     ax0.set_xlabel(f"Episodes buckets ({er_buckets_number} buckets of size {er_bucket_size})")
        #     ax0.set_ylabel("Percentage of winnings")
        # else:
        #     ax0.set_ylabel("Mean return")
        # ax0.set_title(f"Performance")
        # ax0.plot(performance)
        #
        # ax1 = fig.add_subplot(spec[1, 0])
        # if bucketing:
        #     ax1.set_xlabel(f"Actions buckets ({at_buckets_number} buckets of size {at_bucket_size})")
        # ax1.set_ylabel("Mean action time [s]")
        # ax1.set_title(f"Action times")
        # ax1.plot(action_times)
        #
        # plt.show()
        # output_notebook(hide_banner=True)

        performance_f = figure(
            title='Performance',
            x_axis_label=
            f"Episodes buckets ({er_buckets_number} buckets of size {er_bucket_size})",
            y_axis_label='Percentage of winnings',
            plot_width=900,
            plot_height=300,
            toolbar_location=None,
            y_range=(-0.1, 1.1))
        performance_f.yaxis.bounds = (0, 1)
        performance_f.yaxis.formatter = NumeralTickFormatter(format='0 %')

        length = len(performance)
        color_gradient_clipping = 8
        colors = list(palette(int(1.3 * length))[:length])
        color_mapper = LinearColorMapper(palette=colors,
                                         low=min(performance),
                                         high=max(performance))
        performance_f.scatter(range(len(performance)),
                              performance,
                              color={
                                  'field': 'y',
                                  'transform': color_mapper
                              })
        performance_f.line(range(len(performance)),
                           performance,
                           color='grey',
                           alpha=0.5)

        action_times_f = figure(
            title='Action times',
            x_axis_label=
            f"Actions buckets ({at_buckets_number} buckets of size {at_bucket_size})",
            y_axis_label='Mean action time [ms]',
            plot_width=900,
            plot_height=300,
            toolbar_location=None)

        action_times_f.line(range(len(action_times)),
                            np.array(action_times) * 10**3,
                            color='blue')

        show(
            column(performance_f, action_times_f,
                   self.epsilon_strategy.get_figure()))

        # print(f"Number of played episodes: {er_n}")
        print(f"Mean reward: {'{0:0.6f}'.format(mean_reward)}")
        # print(f"Number of taken actions: {at_n}")
        print(f"Mean action time: {'{0:0.6f}'.format(mean_action_time)}")
Example #4
0
def operating_hours(data_sets=[]):

    ########## BUILD FIGURES ################

    if len(data_sets) < 1:
        data_sets = _get_dummies()

    series_count = len(data_sets)
    colours = palette(series_count)

    data_frames = []
    for ds in data_sets:
        df = ds.as_data_frame()
        day_filter = (df['timestamp'].dt.dayofweek
                      == 5) | (df['timestamp'].dt.dayofweek == 6)
        #df = df.drop(df[day_filter].index)
        hour_filter = (df['timestamp'].dt.hour < 15) | (df['timestamp'].dt.hour
                                                        > 21)
        #df = df.drop(df[hour_filter].index)

        #df = df.drop(df[day_filter | hour_filter].index)

        #df['temperature'][day_filter | hour_filter] = np.NaN
        #df['humidity'][day_filter | hour_filter] = np.NaN

        idx = df.ix[day_filter | hour_filter].index
        #df.temperature[idx] = np.NaN
        #df.humidity[idx] = np.NaN
        df.loc[idx, 'temperature'] = np.NaN
        df.loc[idx, 'humidity'] = np.NaN
        #df.at[dates[5], 'E'] = 7

        df['time'] = df['timestamp'].dt.time
        data_frames.append(df)

    all_data_temp = figure(responsive=True,
                           x_axis_label="Time of day",
                           x_axis_type="datetime",
                           y_axis_label="Temperature / C",
                           y_axis_type="linear",
                           **FIGURE_OPTIONS)
    for (clr, ds, df) in zip(colours, data_sets, data_frames):
        #my_plot = all_data_temp.scatter(df.time, df.temperature, color = clr, legend = ds.name, **SCATTER_OPTIONS)
        my_plot = all_data_temp.line(df.time,
                                     df.temperature,
                                     color=clr,
                                     legend=ds.name,
                                     **LINE_OPTIONS)

    all_data_humi = figure(x_range=all_data_temp.x_range,
                           responsive=True,
                           x_axis_label="Time of day",
                           x_axis_type="datetime",
                           y_axis_label="Relative humidity / \%",
                           y_axis_type="linear",
                           **FIGURE_OPTIONS)
    for (clr, ds, df) in zip(colours, data_sets, data_frames):
        my_plot = all_data_humi.scatter(df.time,
                                        df.humidity,
                                        color=clr,
                                        legend=ds.name,
                                        **SCATTER_OPTIONS)
        #my_plot = all_data_humi.line(df.time, df.humidity, color = clr, legend = ds.name, **LINE_OPTIONS)

    for p in [all_data_temp, all_data_humi]:
        p.xaxis.formatter = DatetimeTickFormatter(formats=dict(
            hours=["%k:%M"],
            days=["%d. %m. %y"],
            months=["%m %Y"],
            years=["%Y"],
        ))

    all_data = gridplot([all_data_temp, all_data_humi],
                        ncols=2,
                        plot_width=500,
                        plot_height=250,
                        sizing_mode='scale_width',
                        toolbar_options=dict(logo="grey"))
    #toolbar_options=dict(logo="grey", location='above'), merge_tools=False)

    ########## RENDER PLOTS ################

    resources = INLINE
    js_resources = resources.render_js()
    css_resources = resources.render_css()
    plot_script, plot_divs = components(
        {'Data fra kl. 15 - 22, uden loerdag': all_data})

    return js_resources, css_resources, plot_script, plot_divs
Example #5
0
def alldata(data_sets=[]):

    ########## BUILD FIGURES ################

    if len(data_sets) < 1:
        data_sets = _get_dummies()

    series_count = len(data_sets)
    colours = palette(series_count)

    all_data_temp = figure(responsive=True,
                           x_axis_label="Days",
                           x_axis_type="datetime",
                           y_axis_label="Temperature / C",
                           y_axis_type="linear",
                           **FIGURE_OPTIONS)
    for (clr, ds) in zip(colours, data_sets):
        my_plot = all_data_temp.line(ds.time_series,
                                     ds.temp_series,
                                     color=clr,
                                     legend=ds.name,
                                     **LINE_OPTIONS)

    all_data_humi = figure(x_range=all_data_temp.x_range,
                           responsive=True,
                           x_axis_label="Days",
                           x_axis_type="datetime",
                           y_axis_label="Relative humidity / \%",
                           y_axis_type="linear",
                           **FIGURE_OPTIONS)
    for (clr, ds) in zip(colours, data_sets):
        my_plot = all_data_humi.line(ds.time_series,
                                     ds.humi_series,
                                     color=clr,
                                     legend=ds.name,
                                     **LINE_OPTIONS)

    for p in [all_data_temp, all_data_humi]:
        p.xaxis.formatter = DatetimeTickFormatter(formats=dict(
            hours=["%k:%M"],
            days=["%d. %m. %y"],
            months=["%m %Y"],
            years=["%Y"],
        ))

    all_data = gridplot([all_data_temp, all_data_humi],
                        ncols=2,
                        plot_width=500,
                        plot_height=250,
                        sizing_mode='scale_width',
                        toolbar_options=dict(logo="grey"))
    #toolbar_options=dict(logo="grey", location='above'), merge_tools=False)

    ########## RENDER PLOTS ################

    resources = INLINE
    js_resources = resources.render_js()
    css_resources = resources.render_css()
    plot_script, plot_divs = components({'Oversigt over alt data': all_data})

    return js_resources, css_resources, plot_script, plot_divs
Example #6
0
def statistics(data_sets=[]):

    ########## BUILD FIGURES ################

    if len(data_sets) < 1:
        data_sets = _get_dummies()

    series_count = len(data_sets)
    colours = palette(series_count)

    data_frame = pd.DataFrame()
    data_frames = []
    for ds in data_sets:
        df = ds.as_data_frame()
        day_filter = (df['timestamp'].dt.dayofweek
                      == 5) | (df['timestamp'].dt.dayofweek == 6)
        #df = df.drop(df[day_filter].index)
        hour_filter = (df['timestamp'].dt.hour < 15) | (df['timestamp'].dt.hour
                                                        > 21)
        #df = df.drop(df[hour_filter].index)

        #df = df.drop(df[day_filter | hour_filter].index)

        #df['temperature'][day_filter | hour_filter] = np.NaN
        #df['humidity'][day_filter | hour_filter] = np.NaN

        idx = df.ix[day_filter | hour_filter].index
        #df.temperature[idx] = np.NaN
        #df.humidity[idx] = np.NaN
        df.loc[idx, 'temperature'] = np.NaN
        df.loc[idx, 'humidity'] = np.NaN
        #df.at[dates[5], 'E'] = 7
        df = df.drop(idx)

        df['time'] = df['timestamp'].dt.time
        df['box_label'] = [
            "{1}-{2} - {0}".format(ds.name, tt, tt + 1)
            for tt in df['timestamp'].dt.hour
        ]
        df['box_label_merged'] = [
            "kl. {1}-{2} - {0}".format(ds.name.split(',')[0], tt, tt + 1)
            for tt in df['timestamp'].dt.hour
        ]
        df.loc[:, 'colour'] = ds.name
        df.loc[:, 'colour_merged'] = ds.name.split(',')[0]
        data_frames.append(df)
        data_frame = pd.concat([data_frame, df], ignore_index=True)

    #data_frame = pd.DataFrame(columns=['timestamp', 'temperature', 'humidity', 'box_label'])

    all_data_temp = BoxPlot(data_frame,
                            values='temperature',
                            label='box_label',
                            color='colour',
                            responsive=True,
                            xlabel="Time and place",
                            ylabel="Temperature / C",
                            legend=False)
    all_data_humi = BoxPlot(data_frame,
                            values='humidity',
                            label='box_label',
                            color='colour',
                            responsive=True,
                            xlabel="Time and place",
                            ylabel="Relative humidity / \%",
                            legend=False)

    all_data = gridplot([all_data_temp, all_data_humi],
                        ncols=2,
                        plot_width=500,
                        plot_height=400,
                        sizing_mode='scale_width',
                        toolbar_options=dict(logo="grey"))
    #toolbar_options=dict(logo="grey", location='above'), merge_tools=False)

    merged_data_temp = BoxPlot(data_frame,
                               values='temperature',
                               label='box_label_merged',
                               color='colour_merged',
                               responsive=True,
                               xlabel="Time and place",
                               ylabel="Temperature / C",
                               legend=False)
    merged_data_humi = BoxPlot(data_frame,
                               values='humidity',
                               label='box_label_merged',
                               color='colour_merged',
                               responsive=True,
                               xlabel="Time and place",
                               ylabel="Relative humidity / \%",
                               legend=False)

    merged_data = gridplot([merged_data_temp, merged_data_humi],
                           ncols=2,
                           plot_width=500,
                           plot_height=400,
                           sizing_mode='scale_width',
                           toolbar_options=dict(logo="grey"))
    #toolbar_options=dict(logo="grey", location='above'), merge_tools=False)

    ########## RENDER PLOTS ################

    resources = INLINE
    js_resources = resources.render_js()
    css_resources = resources.render_css()
    plot_script, plot_divs = components({
        'Data fra kl. 15 - 22, uden loerdag':
        all_data,
        'Data fra kl. 15 - 22, uden loerdag, reference og uden udsugning':
        merged_data
    })

    return js_resources, css_resources, plot_script, plot_divs