Beispiel #1
0
                  new):  # callback function for selection of channels
    chans = []
    for l in new:
        chans.append(l.encode('UTF8'))
    df2 = updateDateRange()
    df2 = df2[df2['Light'].isin(chans)]
    updateData(df2)
    p1.y_range.factors = list(reversed(chans))


def allLights():  # callback function for all lights button
    area_select.value = list(an['Name'])


# configure actions for widgets
plotDates.on_click(dateChange)
selectAll.on_click(allLights)
area_select.on_change('value', areaChange)
channel_select.on_change('value', channelChange)
setTitle()

p = Paragraph(text="""""", width=200, height=10)

l = layout([[
    p1,
    column(widgetbox(p), selectAll, area_select, channel_select,
           row(datePickStart, datePickEnd, height=240), plotDates)
]])

curdoc().add_root(l)
x=[3,4,6,12,10,1]
y=[7,1,3,4,1,6]
label=['Red', 'Orange', 'Red', 'Orange','Red', 'Orange']

df=pd.DataFrame({'x':x,'y':y,'label':label}) #create a dataframe for future use

source = ColumnDataSource(data=dict(x=x, y=y,label=label))

plot_figure = figure(title='Multi-Select',plot_height=450, plot_width=600,
              tools="save,reset", toolbar_location="below")

plot_figure.scatter('x', 'y',color='label', source=source, size=10)

multi_select = MultiSelect(title="Filter Plot by color:", value=["Red", "Orange"],
                           options=[("Red", "Red"), ("Orange", "Orange")])

def multiselect_click(attr,old,new):
    active_mselect=multi_select.value ##Getting multi-select value

    selected_df=df[df['label'].isin(active_mselect)] #filter the dataframe with value in multi-select

    source.data=dict(x=selected_df.x, y=selected_df.y,label=selected_df.label)


multi_select.on_change('value',multiselect_click)

layout=row(multi_select, plot_figure)

curdoc().add_root(layout)
curdoc().title = "Multi-Select Bokeh Server"
plot_figure = figure(title='Multi-Select',
                     plot_height=450,
                     plot_width=600,
                     tools="save,reset",
                     toolbar_location="below")

plot_figure.scatter('x', 'y', color='label', source=source, size=10)

multi_select = MultiSelect(title="Filter Plot by color:",
                           value=["Red", "Orange"],
                           options=[("Red", "Red"), ("Orange", "Orange")])


def multiselect_click(attr, old, new):
    active_mselect = multi_select.value  ##Getting multi-select value

    selected_df = df[df['label'].isin(
        active_mselect)]  #filter the dataframe with value in multi-select

    source.data = dict(x=selected_df.x,
                       y=selected_df.y,
                       label=selected_df.label)


multi_select.on_change('value', multiselect_click)

layout = row(multi_select, plot_figure)

curdoc().add_root(layout)
curdoc().title = "Multi-Select Bokeh Server"
def tab_testing():
    data_list = glob.glob('./np/Regression/*.npy')
    data_list = sorted(data_list)
    select_data = Select(title="Data:", value="", options=data_list)

    model_list = os.listdir('./model/Regression/')
    model_list = sorted(model_list)
    select_model = Select(title="Trained Model:", value="", options=model_list)

    notifier = Paragraph(text=""" Notification """, width=200, height=100)

    def refresh_handler():
        data_list_new = glob.glob('./np/Regression/*.npy')
        data_list_new = sorted(data_list_new)
        select_data.options = data_list_new

        model_list_new = os.listdir('./model/Regression/')
        model_list_new = sorted(model_list_new)
        select_model.options = model_list_new

    button_refresh = Button(label="Refresh list")
    button_refresh.on_click(refresh_handler)

    button_test = Button(label="Test model")

    select_result = MultiSelect(title="Key(result):")

    src = ColumnDataSource()
    df = pd.DataFrame(columns=['key', 'y', 'y_hat'])

    table_src = ColumnDataSource(pd.DataFrame(columns=['Key', 'MSE', 'R^2']))
    table_columns = [
        TableColumn(field=col, title=col) for col in ['Key', 'MSE', 'R^2']
    ]
    table_acc = DataTable(source=table_src,
                          columns=table_columns,
                          width=350,
                          height=400,
                          fit_columns=True,
                          name="Accuracy per Key")

    def test_handler():
        df.drop(df.index, inplace=True)
        print("Start test")
        tf.reset_default_graph()
        K.clear_session()

        notifier.text = """ Start testing """

        if (select_data.value == ""):
            data = np.load(select_data.options[0])
        else:
            data = np.load(select_data.value)
        data = data.item()
        notifier.text = """ Import data """

        data_x = data.get('x')
        if (data_x.shape[-1] == 1 and not 'cnn' in select_model.value):
            data_x = np.squeeze(data_x, -1)
        data_y = data.get('y')
        data_key = data.get('key1')

        print(data_x.shape)
        print(data_y.shape)

        df['key'] = data_key
        df['y'] = data_y[:, 0]

        op_list = []
        for i in df['key'].unique():
            op_list.append(str(i))
        select_result.options = op_list

        print(data_x.shape)
        print(data_y.shape)
        print(data.get('key1'))

        if (select_model.value == ""):
            model_name = select_model.options[0]
        else:
            model_name = select_model.value
        model_save_dir = './model/Regression/' + model_name + '/'

        model_dl = glob.glob(model_save_dir + '*.h5')
        model_ml = glob.glob(model_save_dir + '*.sav')

        print(model_save_dir + '*.h5')
        print(model_dl)
        print(model_ml)

        if (len(model_dl) > len(model_ml)):
            model = keras.models.load_model(model_save_dir + model_name +
                                            '.h5')
            target_hat = model.predict(data_x)
            DL = True

        elif (len(model_dl) < len(model_ml)):
            model = pickle.load(
                open(model_save_dir + model_name + '.sav', 'rb'))
            data_x = data_x.reshape([data_x.shape[0], -1])
            target_hat = model.predict(data_x)
            target_hat = np.expand_dims(target_hat, -1)
            DL = False

        notifier.text = """ Model restored """
        print("Model restored.")

        xs = []
        ys = []
        keys = []
        color = ['blue', 'red']

        xs.append([i for i in range(data_y.shape[0])])
        xs.append([i for i in range(data_y.shape[0])])
        ys.append(data_y)
        keys.append(data_key)

        print(target_hat.shape)
        K.clear_session()

        ys.append(target_hat)
        keys.append(data_key)

        print(target_hat[:, 0])
        df['y_hat'] = target_hat[:, 0]

        src.data = ColumnDataSource(
            data=dict(xs=xs, ys=ys, color=color, keys=keys)).data

        figure_trend.multi_line('xs', 'ys', source=src, color='color')

        line_mse = []
        line_r_2 = []
        for unit in df['key'].unique():
            target = df[df['key'] == unit]

            y = target['y'].values
            y_hat = target['y_hat'].values

            unit_mse = np.sum((y - y_hat)**2) / target.shape[0]
            unit_r_2 = np.max([r2_score(y, y_hat), 0])

            line_mse.append(unit_mse)
            line_r_2.append(unit_r_2)

        acc = pd.DataFrame(columns=['Key', 'MSE', 'R^2'])
        acc['Key'] = df['key'].unique()

        mse_mean = np.mean(line_mse)
        r_2_mean = np.mean(line_r_2)

        line_mse = list(map(lambda x: format(x, '.2f'), line_mse))
        acc['MSE'] = line_mse
        line_r_2 = list(map(lambda x: format(x, '.2f'), line_r_2))
        acc['R^2'] = line_r_2

        acc_append = pd.DataFrame(columns=acc.columns)
        acc_append['Key'] = ['MSE average', 'R^2 average']
        acc_append['MSE'] = [mse_mean, r_2_mean]

        acc = pd.concat([acc, acc_append])

        table_src.data = ColumnDataSource(acc).data

        notifier.text = """ Drawing complete """
        history.text = history.text + "\n\t" + model_name + "'s R^2 score: " + format(
            np.mean(r_2_mean), '.2f')

    def update(attr, old, new):

        key_to_plot = select_result.value

        xs = []
        ys = []
        keys = []

        y = []
        y_hat = []
        key = []

        key_type = type(df['key'].values[0])
        for k in key_to_plot:

            y += list(df[df['key'] == key_type(k)]['y'].values)
            y_hat += list(df[df['key'] == key_type(k)]['y_hat'].values)
            key += [k for _ in range(df[df['key'] == key_type(k)].shape[0])]

        ys.append(y)
        ys.append(y_hat)

        xs.append([i for i in range(len(y))])
        xs.append([i for i in range(len(y))])

        keys.append(key)
        keys.append(key)

        color = ['blue', 'red']

        src.data = ColumnDataSource(
            data=dict(xs=xs, ys=ys, color=color, keys=keys)).data

    select_result.on_change("value", update)

    button_test.on_click(test_handler)

    button_export = Button(label="Export result")

    def handler_export():
        df.to_csv('./Export/result.csv', index=False)

    button_export.on_click(handler_export)

    figure_trend = figure(title="Prediction result", width=800, height=460)
    history = PreText(text="", width=300, height=460)

    layout = Column(
        Row(button_refresh),
        Row(select_data, select_model, button_test, select_result, notifier),
        Row(table_acc, figure_trend, history, button_export))

    tab = Panel(child=layout, title='Regression Test')

    return tab
Beispiel #5
0
def create_tab(data, name):
    def make_dataset(metric_fun, metric_sentiment, month_start, month_end,
                     editorial, category, social_network, product):
        """Constrói os datasets para cada tipo de gráfico utilizado no dashboard

        Parâmetros
        ----------
        data : DataFrame
            Pandas DataFrame expandido com dados do Sprinklr
        metric_fun : FUN
            Função para calcular métrica específica selecionada no widget
        metric_sentiment : str
            Sentimento relacionado à métrica escolhida (Positividade: positivo, Gradiente: negativo, Crise: negativo, Saúde do post: positivo)
        [restante] : str
            Valaores selecionados nas opções de filtros nos widgets 
        Retorna
        -------
        dict
            Dicionário com três chaves, correspondentes aos três gráficos apresentados. Cada chave é relacionada ao nome o gráfico 
            e os valores são datasets no formato column data source
        """
        month_start = pd.Timestamp(month_start)
        month_end = pd.Timestamp(month_end)

        # Filtragem dos dados com base nas seleções dos widgets
        filters = {
            'Editoria': editorial,
            'Categoria': category,
            'SocialNetwork': social_network,
            'Produto': product
        }
        filtered_data = filter_data(data, filters)

        # Gera datasets para cada gráfico
        ts_data = metric_fun(filtered_data)
        ts_data = ts_data[(ts_data.time >= month_start)
                          & (ts_data.time <= month_end)]

        donut_data = filtered_data[(filtered_data.Month >= month_start)
                                   & (filtered_data.Month <= month_end)]
        donut_data = percent(donut_data)
        donut_data['angle'] = donut_data['value'] / sum(
            donut_data['value']) * 2 * pi
        donut_data['color'] = Category20c[donut_data.shape[0]]

        avg_donut_data = percent_avg(filtered_data)
        avg_donut_data = avg_donut_data[avg_donut_data.Month == month_end][[
            'Sentimento', 'MAVG'
        ]]
        avg_donut_data.columns = ['label', 'value']
        avg_donut_data['angle'] = avg_donut_data['value'] / sum(
            avg_donut_data['value']) * 2 * pi
        avg_donut_data['color'] = Category20c[avg_donut_data.shape[0]]

        top_data = filter_sentiment(filtered_data, metric_sentiment)
        top_data = top_data[(top_data.Month >= month_start)
                            & (top_data.Month <= month_end)]
        top_data = brand_health_txengages(top_data)
        avg_top_data = round(top_data.score.mean(), 2)
        top_data = top_data.sort_values('score', ascending=False).iloc[:10]
        top_data = top_data.sort_values('score')
        top_data['recorte'] = [
            '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'
        ]

        # Converte dataframes em column data source
        datasets = {
            'ts': ColumnDataSource(ts_data),
            'donut': ColumnDataSource(donut_data),
            'avg_donut': ColumnDataSource(avg_donut_data),
            'top': ColumnDataSource(top_data),
            'avg_top': avg_top_data
        }

        return datasets

    def update(attr, old, new):
        """Constrói os datasets para cada tipo de gráfico utilizado no dashboard

        Parâmetros
        ----------
        old : ColumnDataSource
            Dataframe antigo relacionado aos filtros antigos
        new : ColumnDataSource
            Dataframe novo, com linhas filtradas de acordo com seleções mais recentes
        """
        month_start = month_select.value_as_date[0]
        month_end = month_select.value_as_date[1]
        editorial = editorial_select.value
        category = category_select.value
        product = product_select.value
        social_network = [
            social_network_select.labels[i]
            for i in social_network_select.active
        ]
        metric = metric_select.value
        metric_attr = get_metric_attr(metric)
        metric_fun = metric_attr['fun']
        metric_sentiment = metric_attr['sentiment']

        new_src = make_dataset(metric_fun=metric_fun,
                               metric_sentiment=metric_sentiment,
                               month_start=month_start,
                               month_end=month_end,
                               editorial=editorial,
                               category=category,
                               social_network=social_network,
                               product=product)
        src['ts'].data.update(new_src['ts'].data)
        src['top'].data.update(new_src['top'].data)
        src['avg_top'] = new_src['avg_top']
        src['donut'].data.update(new_src['donut'].data)
        src['avg_donut'].data.update(new_src['avg_donut'].data)

    networks = data.SocialNetwork.unique().tolist()
    editorials = get_multselect_options(data, 'Editoria')
    categories = get_multselect_options(data, 'Categoria')
    products = get_multselect_options(data, 'Produto')

    month_select = DateRangeSlider(start=date(2019, 1, 1),
                                   end=date(2019, 8, 1),
                                   value=(date(2019, 1, 1), date(2019, 8, 1)),
                                   step=1,
                                   format="%b %Y")
    metric_select = Select(value="gradiente",
                           options=[("velocity", "Parâmetro de Crise"),
                                    ("positivity", "Grau de Positividade"),
                                    ("gradiente", "Grau de Negatividade"),
                                    ("brand_health", "Saúde da Marca"),
                                    ("post_health", "Saúde do Post")])
    product_select = MultiSelect(value=['Todos'], options=products)
    category_select = MultiSelect(value=['Todos'], options=categories)
    editorial_select = MultiSelect(value=['Todos'], options=editorials)
    social_network_select = CheckboxGroup(labels=networks,
                                          active=list(range(len(networks))))

    metric_select.on_change('value', update)
    month_select.on_change('value', update)
    editorial_select.on_change('value', update)
    category_select.on_change('value', update)
    product_select.on_change('value', update)
    social_network_select.on_change('active', update)

    initial_metric_attr = get_metric_attr(metric_select.value)
    metric_sentiment = initial_metric_attr['sentiment']
    initial_networks = [
        social_network_select.labels[i] for i in social_network_select.active
    ]

    src = make_dataset(metric_fun=initial_metric_attr['fun'],
                       metric_sentiment=metric_sentiment,
                       month_start=month_select.value_as_date[0],
                       month_end=month_select.value_as_date[1],
                       editorial=editorial_select.value,
                       category=category_select.value,
                       social_network=initial_networks,
                       product=product_select.value)

    p_ts = make_plot_ts(src['ts'], 'Evolução', metric_sentiment)
    p_top = make_dotplot(src['top'])
    avg_top = src['avg_top']
    avg_top = create_div_title(f'Escore Médio: {avg_top}')
    p_donut = make_plot_donut(src['donut'], 'Percentual')
    p_avg_donut = make_plot_donut(src['avg_donut'], 'Norma Percentual')

    metric_title = create_div_title('MÉTRICA')
    month_title = create_div_title('PERÍODO')
    network_title = create_div_title('REDE SOCIAL')
    category_title = create_div_title('CATEGORIA')
    editorial_title = create_div_title('EDITORIA')
    product_title = create_div_title('PRODUTO')

    controls = WidgetBox(
        column(metric_title,
               metric_select,
               Div(height=5),
               month_title,
               month_select,
               Div(height=5),
               editorial_title,
               editorial_select,
               Div(height=5),
               category_title,
               category_select,
               Div(height=5),
               product_title,
               product_select,
               Div(height=5),
               network_title,
               social_network_select,
               width=250))

    plots = column(p_ts, Div(height=20), row(p_donut, p_avg_donut))
    layout = row(controls, Div(width=50), plots)
    layout = column(Div(text="", height=5), layout, Div(width=20), avg_top,
                    p_top)
    tab = Panel(child=layout, title=name)

    return tab
Beispiel #6
0
                                    options=b_names,
                                    size=12)

show_all_or_selected = Select(title="Show all data or selected:",
                              value="All",
                              options=["All", "selected"])

Reset_button = Button(label='Reset', button_type='warning')

source = get_dataset(data_all, test_ind, select_data_box.value,
                     multi_select.value)

p = make_plot(source)

select_data_box.on_change('value', update_plot)
multi_select.on_change('value', update_plot_chemistry)
multi_select_A_cation.on_change('value', update_plot_a_or_b)
multi_select_B_cation.on_change('value', update_plot_a_or_b)
Reset_button.on_click(reset_plot)

#select_data_box.on_change('value', update_plot)

#inputs = widgetbox(*controls)
selections = column(row(select_data_box, show_all_or_selected), multi_select,
                    row(multi_select_A_cation, multi_select_B_cation),
                    Reset_button)
curdoc().add_root(row(selections, p))
curdoc().title = "Weather"

#show()
Beispiel #7
0
    # next(color_list)

    # myString += '\n' + i
    # myText.text = myString


multi_locations = sorted(list(df18['location'].unique()),
                         key=str.upper,
                         reverse=True)
multi_select = MultiSelect(title="Country:",
                           value=["0"],
                           size=7,
                           options=multi_locations,
                           height=200)
# myText = Paragraph(text='Initial Text', width=1200)
multi_select.on_change('value', update)
multi_select_widgetbox = widgetbox(multi_select)

# plot 1: rank vs student staff ratio
f = figure()
f.xaxis.axis_label = "rank"
f.yaxis.axis_label = "student staff ratio"
f.x_range = DataRange1d(start=0, end=1103)
f.y_range = DataRange1d(start=0, end=90)
f.legend.location = 'bottom_right'

# plot 3: percentage international vs percentage man (omdat size handig met t bolletje zelf kan)
h = figure()
h.xaxis.axis_label = "percentage international"
h.yaxis.axis_label = "percentage man"
h.x_range = DataRange1d(start=0, end=50)
Beispiel #8
0
    except Exception as e:
        annotation_output.text = "An error occured"
        logger.error("Error: %s", e)


def mark_phrase_tag(text):
    return "<phrase>" + text + "</phrase>"


# set callbacks

expand_button.on_click(get_expand_results_callback)
expand_table_source.selected.on_change("indices", row_selected_callback)
expand_table_source.on_change("data", expand_data_changed_callback)
checkbox_group.on_click(checkbox_callback)
search_input_box.on_change("value", search_callback)
phrases_list.on_change("value", vocab_phrase_selected_callback)
clear_seed_button.on_click(clear_seed_callback)
with open(join(dirname(__file__), "download.js")) as f:
    code = f.read()
export_button.callback = CustomJS(args=dict(source=expand_table_source), code=code)
annotate_button.on_click(annotate_callback)
# table_area.on_change('children', table_area_change_callback)

# arrange components in page

doc = curdoc()
main_title = "Set Expansion Demo"
doc.title = main_title
doc.add_root(grid)
Beispiel #9
0
class GPE:

    def __init__(self,offline=False,test_n_plots=6,test_n_samples=1000,max_row_width=4):
        """
        Initialize Graph Projection Explorer
        """
        print("\n\n" + '#' * 75)
        print("Bokeh Graph Projection Explorer V.0.0.1")
        print('Class Format')
        print('#' * 75 + '\n\n')

        self.test_n_plots = test_n_plots
        self.test_n_samples = test_n_samples
        self.max_row_width = max_row_width
        self.testmode = False
        self.n_newplots = 0


        if offline:
            # Operate in non command line argument mode
            self.testmode = True
            self.verbose = True
        else:
            parser = argparse.ArgumentParser()
            parser.add_argument("--dir", help="directory housing data")
            parser.add_argument("--mode", help="Options: Test, Presentation, Default")
            parser.add_argument("--downsample", help="If provided, randomly samples the data the provided number of times")
            parser.add_argument("--verbose", help="If True, Prints messages to console where server is running")
            self.args = parser.parse_args()


            try:
                self.args.downsample = int(self.args.downsample)
            except:
                TypeError()



            if self.args.verbose:
                self.verbose = True
                print "Verbose Turned On"

            if isinstance(self.args.mode, str):
                if self.args.mode.lower() == 'test':
                    if self.verbose:
                        print('Test Mode Selected')
                        self.testmode = True
                    else:
                        raise ValueError("Mode argument: " + self.args.mode + "Not among valid modes")
            else:
                raise ValueError("Mode Argument must be of type str. Instead recieved: " + str(type(self.args.mode)))

        # Build Test Data if it exists
        if self.testmode:
            self.data_dir = self.gen_test_data()
        else:
            self.data_dir = self.args.dir


        # Initialize
        self.init_data()
        self.init_color()
        self.init_controls()
        if self.verbose:
            print("Initialization Timings")
            print("\tData Init:    "+str(self.init_data_time))
            print("\tColor Init:   " + str(self.init_color_time))
            print("\tControl Init: " + str(self.init_control_time))

    def read_data(self):
        """

        :return:
        """
        if self.verbose: print('\nReading Data\n')

        # Check Data
        assert os.path.isdir(self.data_dir)
        vecdir = os.path.join(self.data_dir,'vectors')
        graphdir = os.path.join(self.data_dir,'graphs')
        assert  os.path.isdir(vecdir)
        try:
            assert os.path.isdir(graphdir)
        except AssertionError:
            pass

        vec_files = [os.path.join(vecdir,file) for file in os.listdir(vecdir) if file.endswith(".csv")]
        vec_files.sort()

        assert len(vec_files) > 0

        self.plot_df = OrderedDict()
        self.data_df = OrderedDict()
        self.maps_dict = OrderedDict()
        self.true_cols = []
        self.initial_plot_2_data_mapper = {}

        n_plot = 1
        self.n_plots = 0
        for i,f in enumerate(vec_files):

            if self.verbose: print("Reading File: %s"%f)

            file_prefix = f.split('/')[-1].split('.')[0] + "_"

            df = pd.read_csv(f)
            # Sometimes Unnamed: 0 creeps in by mistake of the user
            if "Unnamed: 0" in df.columns:
                df.drop(["Unnamed: 0"],axis=1,inplace=True)

            # Confirm Data Dim
            if i == 0:
                self.n,p = df.shape
            else:
                n,p = df.shape
                #assert n == self.n

            if isinstance(self.args.downsample, int):
                if self.verbose: print("Downsampling: %d"%self.args.downsample)
                df = df.sample(n=int(self.args.downsample),replace=False,random_state=1,axis=0)
                print(df.shape)
                self.n, p = df.shape

            # Test if D1 and D2 columns are found
            has_d1 = 0
            has_d2 = 0
            for col in df.columns:
                if 'D1' in col:
                    has_d1 += 1
                elif 'D2' in col:
                    has_d2 += 1
            if has_d1 == 1 and has_d2 == 1:
                has_both = True
            else:
                has_both = False

            if has_d1 > 1:
                warnings.warn("Too many column headers contain D1, cannot disambiguate")
            if has_d2 > 1:
                warnings.warn("Too many column headers contain D2, cannot disambiguate")
            if has_d1 != has_d2:
                warnings.warn("The number of D1 and D2 variable do not match")

            # Now that data validation is done, actually add data to self.df
            for col in df.columns:

                if 'D1' in col and has_both:
                    self.data_df[file_prefix + col] = df[col].values.tolist()
                    self.plot_df['Plot_%d_x' % n_plot] = df[col].values.tolist()
                    self.plot_df[file_prefix + col] = df[col].values.tolist()
                    self.true_cols.append(file_prefix + col)
                    self.initial_plot_2_data_mapper['Plot_%d_x' % n_plot] = file_prefix + col


                elif 'D2' in col and has_both:
                    self.data_df[file_prefix + col] = df[col].values.tolist()
                    self.plot_df['Plot_%d_y' % n_plot] = df[col].values.tolist()
                    self.plot_df[file_prefix + col] = df[col].values.tolist()
                    self.true_cols.append(file_prefix + col)
                    self.initial_plot_2_data_mapper['Plot_%d_y' % n_plot] =  file_prefix + col

                else:
                    self.data_df[file_prefix + col] = df[col].values.tolist()
                    self.plot_df[file_prefix + col] = df[col].values.tolist()
                    self.true_cols.append(file_prefix + col)

            if has_both:
                self.maps_dict["Plot_%d" % n_plot] = ('Plot_%d_x' % n_plot,'Plot_%d_y' % n_plot)
                n_plot += 1

    def init_data(self):
        """
        Load and Validate Data
        :return:
        """

        t0 = time()

        if self.verbose: print("Initializing Data Resources")
        self.read_data()


        self.n_plots = len(self.maps_dict.keys())
        self.color = "__COLOR__"
        self.plot_df["__COLOR__"] = ["#80bfff"] * self.n
        self.plot_df["__selected__"] = np.ones_like(self.n, dtype=np.bool).tolist()

        self.data_df = pd.DataFrame(self.data_df)
        self.data_dict = self.data_df.to_dict(orient='list')
        assert isinstance(self.data_dict, dict)

        self.plot_df = pd.DataFrame(self.plot_df)
        self.plot_dict = self.plot_df.to_dict(orient='list')
        assert isinstance(self.data_dict, dict)

        # Used for indexing Selected Data
        self.inds_bool = np.ones_like(np.arange(self.n), dtype=np.bool)
        self.source = ColumnDataSource(data=self.plot_dict)
        self.table_source = ColumnDataSource(data=self.plot_df[self.true_cols].to_dict(orient='list'))

        self.init_data_time = time() - t0
        return self.init_data_time

    def gen_test_data(self):
        """
        Generate Test Data, Store in temp dir and return dir path
        :return: dir path
        """
        if self.verbose: print('Generating Test Data')

        # Initialize Temp Testing Dir Structure
        tmpdir = tempfile.mkdtemp()
        tmpdir_p = os.path.join(tmpdir,'vectors')
        tmpdir_g = os.path.join(tmpdir,'graphs')
        os.mkdir(tmpdir_p)
        os.mkdir(tmpdir_g)

        assert isinstance(self.test_n_plots, int)
        assert isinstance(self.test_n_samples, int)

        # Make Blob data
        X, y = make_blobs(n_samples=self.test_n_samples, n_features=self.test_n_plots * 2,
                          centers=6, cluster_std=0.75, random_state=1)


        # Store blob data in test dir
        for i in range(self.test_n_plots):


            cols = X[:, (i * 2):((i * 2) + 2)]
            #cols[np.random.choice(range(self.test_n_samples),1),:] = [np.null,np.null]

            df = pd.DataFrame(data=cols, columns=('D1','D2'))
            df.to_csv(os.path.join(tmpdir_p,'P%d.csv'%i))
        meta_df = pd.DataFrame({'Meta':['Class: ' + str(label) for label in y]})
        meta_df.to_csv(os.path.join(tmpdir_p, 'Meta.csv'))

        # Generate Graph Data
        # TODO

        return tmpdir

    def init_color(self):
        """

        :return:
        """
        t0 = time()
        if self.verbose: print("Initializing Color Resources")
        self.color_map_dict = {col: list(enumerate(get_color_map(self.data_dict[col]))) for col in self.data_dict.keys()}
        self.init_color_time = time() - t0
        return self.init_color_time

    def init_controls(self):
        """

        :return:
        """
        t0 = time()
        if self.verbose: print("Initializing Controls")


        # Initialize Controls
        self.color_selection = Select(title="Color By", options=self.data_dict.keys(), value=self.data_dict.keys()[0])
        self.selection_label = TextInput(value="MyGroup#1", title="Selection Label:")

        self.add_selection_label = Button(label="Add Selection Label")
        self.write_mod_file = Button(label="Download", button_type="primary")
        self.write_mod_file.callback = CustomJS(args=dict(source=self.source),
                                                code=open(os.path.join(os.path.dirname(__file__),
                                                                       "download.js")).read())

        self.tooltip_select = MultiSelect(title='Tooltip',value = [self.data_dict.keys()[0]],
                                          options=[(key,key.upper()) for key in self.data_dict.keys()])

        # Declare Tooltip Contents
        self.tooltip_list = [(col, "@" + col) for col in self.tooltip_select.value]

        self.init_control_time = time() - t0
        return self.init_control_time

    def add_selection(self):
        """
        Add new column to source containing copy of selection
        :return:
        """
        self.source.add(self.source.data['__selected__'],name=self.selection_label.value)





    def make_plot(self,title, x, y):
        """

        :param title:
        :param x:
        :param y:
        :return:
        """

        print(title,x,y)


        t0 = time()

        pt = PanTool()
        lst = LassoSelectTool()
        pst = PolySelectTool()
        bst = BoxSelectTool()
        wzt = WheelZoomTool()
        tt = TapTool()
        st = SaveTool()
        ut = UndoTool()
        rt = RedoTool()

        p = figure(
            tools=[pt,lst,pst,bst,wzt,tt,st,ut,rt],
            plot_width=400,
            plot_height=400,
            title=self.initial_plot_2_data_mapper[x]+" vs. "+self.initial_plot_2_data_mapper[y],
            webgl=accelerator)
        # configure so that no drag tools are active
        p.toolbar.active_drag = pt

        # configure so that Bokeh chooses what (if any) scroll tool is active
        p.toolbar.active_scroll = wzt

        # configure so that a specific PolySelect tap tool is active
        p.toolbar.active_tap = tt

        p.xaxis.axis_label = self.initial_plot_2_data_mapper[x]
        p.yaxis.axis_label = self.initial_plot_2_data_mapper[y]
        c = p.circle(x=x, y=y, size=5, color="__COLOR__", alpha=.75, source=self.source,
                     hover_color='white', hover_alpha=1, hover_line_color='grey')
        c.data_source.on_change('selected', self.update)


        # Edge generator
        '''
        self.graph_set = [{i: [[1,0.15],[2,0.5],[3,0.99]] for i in range(self.n)}]

        self.edge_colors = qual_2_color(['g'+str(i) for i,_ in enumerate(self.graph_set)])

        self.edge_sources = [ColumnDataSource({'x0': [],
                                               'y0': [],
                                               'x1': [],
                                               'y1': [],
                                               'alpha': []})
                             for i in self.graph_set]

        self.edge_segments = [p.segment(x0='x0',
                                        y0='y0',
                                        x1='x1',
                                        y1='y1',
                                        color=self.edge_colors[i],
                                        alpha='alpha',
                                        line_width=3,
                                        #line_dash=[1,1],
                                        source=self.edge_sources[i])
                              for i, _ in enumerate(self.graph_set)]

        for i, _ in enumerate(self.graph_set):
            code1 = """
                    var links = %s;
                    var data = {'x0': [], 'y0': [], 'x1': [], 'y1': [], 'alpha': []};
                    var cdata = circle.get('data');
                    var indices = cb_data.index['1d'].indices;
                    for (i=0; i < indices.length; i++) {
                    ind0 = indices[i]
                    for (j=0; j < links[ind0].length; j++) {
                    ind1 = links[ind0][j][0];
                    w = links[ind0][j][1];
                    """ % self.graph_set[i]
            code2 = "data['x0'].push(cdata['" + x + "'][ind0]);\n" + \
                    "data['y0'].push(cdata['" + y + "'][ind0]);\n" + \
                    "data['x1'].push(cdata['" + x + "'][ind1]);\n" + \
                    "data['y1'].push(cdata['" + y + "'][ind1]);\n" + \
                    "data['alpha'].push([w]);\n"
            code3 = "}}segment.set('data', data);"
            code = code1 + code2 + code3
            callback = CustomJS(args={'circle': c.data_source,
                                      'segment': self.edge_segments[i].data_source},
                                code=code)
            p.add_tools(HoverTool(tooltips=None, callback=callback, renderers=[c]))
        '''

        p.select(BoxSelectTool).select_every_mousemove = False
        p.select(LassoSelectTool).select_every_mousemove = False



        # Plot Controls
        xdim_select = Select(title="X Dim", options=self.data_dict.keys(), value=self.initial_plot_2_data_mapper[x],width=400)
        ydim_select = Select(title="Y Dim", options=self.data_dict.keys(), value=self.initial_plot_2_data_mapper[y],width=400)
        xdim_select.on_change('value', self.plot_update)
        ydim_select.on_change('value', self.plot_update)
        remove = Button(label="Remove", button_type="danger",width=400)
        remove.on_click(partial(self.remove_plot,title,x,y))

        self.plot_control_dict[title] = {'x':xdim_select,
                                         'y':ydim_select,
                                         'xprev':xdim_select.value,
                                         'yprev':ydim_select.value,
                                         'figure':p,
                                         'tooltip':HoverTool(tooltips=self.tooltip_list,point_policy='snap_to_data',show_arrow=False)}
        # Give the hover tool a tool tip
        self.plot_control_dict[title]['figure'].add_tools(self.plot_control_dict[title]['tooltip'])


        # Form Tab
        plot_options = WidgetBox(xdim_select,ydim_select,remove)
        tab1 = Panel(child=self.plot_control_dict[title]['figure'], title=title,width=400,height=400)
        tab2 = Panel(child=plot_options, title="options",width=400,height=400)
        tabs = Tabs(tabs=[tab1, tab2],width=400,height=400)


        self.tab_list.append(tabs)
        self.circle_list.append(c)

        print('Plot Time: ' + str(time() - t0))

        return tabs, c

    def change_color(self,attrname, old, new):
        """
        Change Color
        :return:
        """
        if self.verbose: print("---self.change_color---")
        self.source.patch({"__COLOR__": self.color_map_dict[self.color_selection.value]})
        if self.verbose: print('New Color: '+self.color_selection.value)

    def refresh_plots(self):
        """
        Refresh plots
        :return:
        """
        if self.verbose: print('---self.refresh_plots---')
        print(self.layout)
        print(self.layout.children)
        self.layout.children[0] = self.make_all_plots()
        print(self.layout)
        print(self.layout.children)

        #controls = self.make_all_controls()
        #dt = self.make_data_table()
        #self.layout.children[1] = row(controls, dt)

    def add_plot(self):
        """
        Add new plot to the
        :return:
        """
        if self.verbose: print('---self.add_plot---')
        self.n_newplots += 1


        d1 = self.true_cols[np.random.randint(len(self.true_cols))]
        d2 = self.true_cols[np.random.randint(len(self.true_cols))]

        self.source.add(self.source.data[d1],name='NewPlot%d_x'%self.n_newplots)
        self.source.add(self.source.data[d2], name='NewPlot%d_y'%self.n_newplots)
        self.maps_dict['NewPlot%d'%self.n_newplots] = ('NewPlot%d_x'%self.n_newplots,
                                                       'NewPlot%d_y'%self.n_newplots)

        self.initial_plot_2_data_mapper['NewPlot%d_x'%self.n_newplots] = d1
        self.initial_plot_2_data_mapper['NewPlot%d_y'%self.n_newplots] = d2

        if self.verbose: print('---self.add_plot---Done')
        self.refresh_plots()

    def remove_plot(self,plot_title,x,y):
        """
        Removes a plot
        :return:
        """
        if self.verbose: print('\n---self.remove_plot---')

        print('Getting Rid of')
        print(self.maps_dict[plot_title])
        self.maps_dict.pop(plot_title)
        print(x,y)
        #self.source.data.pop(x)
        #self.source.data.pop(y)

        print('After poping')
        print(self.maps_dict)
        print(self.source.data.keys())
        pprint(self.source.data)



        self.refresh_plots()


    def dist_plot(self):
        """

        :return:
        """
        dist_plots = []

        self.dist_dict = {}

        for col in self.true_cols[0:8]:

            x = self.data_dict[col]

            # create the horizontal histogram
            hhist, hedges = np.histogram(x, bins=20)
            hzeros = np.zeros(len(hedges) - 1)
            hmax = max(hhist) * 1.1

            LINE_ARGS = dict(color="#3A5785", line_color=None)

            ph = figure(toolbar_location=None, plot_width=200, plot_height=100,title=col,
                        y_range=(0, hmax), y_axis_location="left")
            ph.xgrid.grid_line_color = None
            ph.yaxis.major_label_orientation = np.pi / 4
            ph.background_fill_color = "#fafafa"

            ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hhist, color="white", line_color="#3A5785")
            hh1 = ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hzeros, alpha=0.5, **LINE_ARGS)


            # Add to dist dict
            self.dist_dict[col] = {'plot':ph,
                                   'hh1':hh1,
                                   'hedges':hedges}


            # Add to grid plot
            dist_plots.append(ph)

        gp = gridplot(dist_plots, plot_width=300, plot_height=200, ncols=4, height=400,toolbar_location=None,)


        return gp




    def make_all_plots(self):
        """

        :return:
        """
        self.tab_list = []
        self.plot_list = []
        self.circle_list = []
        self.plot_control_dict = {}

        self.n_plots = len(self.maps_dict.keys())

        # Make Each Plot
        for f in self.maps_dict.keys():
            xs = self.maps_dict[f][0]
            ys = self.maps_dict[f][1]
            self.make_plot(f, xs, ys)

        #if self.verbose: print("Grid of plots: "+str(self.tab_list))
        return gridplot(self.tab_list,ncols=self.max_row_width,plot_width=250, plot_height=250)

    def make_all_controls(self):
        # Controls

        self.addplot = Button(label="Add New Plot", button_type="success")
        self.addplot.on_click(self.add_plot)

        self.add_selection_label.on_click(self.add_selection)
        self.tooltip_select.on_change('value',self.tooltip_update)
        self.color_selection.on_change('value',self.change_color)


        controls = [self.color_selection,
                    self.tooltip_select,
                    self.selection_label,
                    self.add_selection_label,
                    self.addplot,
                    self.write_mod_file]

        return widgetbox(children=controls)

    def pseudo_update(self,n_selected):
        """

        :param n_selected:
        :return:
        """
        assert self.n >= n_selected
        pseudo_selected = np.random.choice(np.arange(self.n),size=n_selected)
        pseudo_new = {'1d':{'indices':pseudo_selected}}
        self.update(1,1,pseudo_new)

    def tooltip_update(self,attrname, old, new):
        """
        Updates tooltip content for each plot
        :param attrname:
        :param old:
        :param new:
        :return:
        """

        if self.verbose: print("\n------self.tooltip_update-----")
        self.tooltip_list = [(col.encode("utf-8"), "@" + col.encode("utf-8")) for col in self.tooltip_select.value]
        self.refresh_plots()
        #if self.verbose: print(contents)

        #for p in self.plot_control_dict:
        #    self.plot_control_dict[p]['tooltip'].tooltips = contents
        #    self.plot_control_dict[p]['tooltip'].plot = self.plot_control_dict[p]['figure']
        #    if self.verbose: print(self.plot_control_dict[p]['tooltip'].tooltips)

    def plot_update(self,attrname, old, new):
        """
        Updates plot contents, title and axis labels
        :param attrname:
        :param old:
        :param new:
        :return:
        """
        # modify column value of data source

        if self.verbose: print("\n------self.plot_update-----")

        for p in self.plot_control_dict:

            xy_mod = False
            # X
            if self.plot_control_dict[p]['x'].value != self.plot_control_dict[p]['xprev']:
                if self.verbose: print('X change on %s'%p)
                # patch
                self.source.patch({p+"_x": list(enumerate(self.data_dict[self.plot_control_dict[p]['x'].value]))})
                # update prev
                self.plot_control_dict[p]['xprev'] = self.plot_control_dict[p]['x'].value
                self.plot_control_dict[p]['figure'].xaxis.axis_label = self.plot_control_dict[p]['x'].value
                xy_mod = True


            # Y
            if self.plot_control_dict[p]['y'].value != self.plot_control_dict[p]['yprev']:
                if self.verbose: print('Y change on %s' % p)
                # patch
                self.source.patch({p+"_y": list(enumerate(self.data_dict[self.plot_control_dict[p]['y'].value]))})
                # update prev
                self.plot_control_dict[p]['yprev'] = self.plot_control_dict[p]['y'].value
                self.plot_control_dict[p]['figure'].yaxis.axis_label = self.plot_control_dict[p]['y'].value
                xy_mod = True


            # update title text
            if xy_mod:
                self.plot_control_dict[p]['figure'].title.text = self.plot_control_dict[p]['x'].value + " vs. " + self.plot_control_dict[p]['y'].value

    def update(self,attrname, old, new):
        """

        :param attrname:
        :param old:
        :param new:
        :return:
        """
        if self.verbose: print("\n------self.update-----")

        t0 = time()

        # Update Selected
        try:
            inds = np.array(new['1d']['indices'])

            if len(inds) == 0 or len(inds) == self.n:
                print("NOTHING SELECTED")
                pass
            else:
                print('Selected Set Size: ' + str(len(inds)))

                # Modify Dist Plots
                if self.verbose: print('\tUpdating DistPlots')
                for dist in self.dist_dict:
                    x = np.array(self.data_dict[dist])
                    xbins = self.dist_dict[dist]['hedges']
                    hhist1, _ = np.histogram(x[inds], bins=xbins)
                    self.dist_dict[dist]['hh1'].data_source.data["top"] = hhist1

                # Modify Data Table
                indbool = self.inds_bool
                indbool[inds] = False
                self.source.patch({"__selected__":list(enumerate(indbool))})







                full_table_dict = self.data_dict
                self.table_source.data = {col: np.array(full_table_dict[col])[inds] for col in full_table_dict.keys()}

        # Hack to stop string index type error that occurs when you change color
        except FloatingPointError:
            print("NOTHING SELECTED, error caught")
            pass

        self.update_time = time()-t0
        print(self.update_time)

    def make_data_table(self):

        # Add Table
        columns = [TableColumn(field=col, title=col) for col in self.data_df.keys()]
        dt = DataTable(source=self.table_source,
                       columns=columns,
                       width=1800,
                       height=400,
                       scroll_to_selection=False)

        return WidgetBox(dt)

    def go(self):
        """

        :return:
        """

        print("\n\n" + '#' * 75)
        print("Server Engaged")
        print('#' * 75 + '\n\n')


        plots = self.make_all_plots()
        controls = self.make_all_controls()
        dt = self.make_data_table()
        dp = self.dist_plot()

        dp = gridplot(dp, plot_width=300, plot_height=200, ncols=2, height=400)
        dp = Panel(child=dp, title="Distribution Plot", width=400, height=400)

        dt = Panel(child=dt, title="Data Table", width=400, height=400)

        dtdp = Tabs(tabs=[dp,dt],width=400,height=400)


        self.layout =  column(children=[plots,row(controls,dtdp)])
        curdoc().add_root(self.layout)
        curdoc().title = 'Graph Projection Explorer'
class CityBikeAnalysis:
    def __init__(self,
                 log_file,
                 city='Oslo',
                 coord_mapping_file='',
                 altitude_file=''):

        self.city = city
        self.station_info_file = 'station_info.csv'
        self.df_s_info = pd.read_csv(self.station_info_file, index_col=0)
        self.df_s_info.index = self.df_s_info.index.astype(str)

        self.tz_offset = pd.Timedelta('0h')

        self.log_file = log_file
        self.df_log = pd.read_csv(log_file)
        self.re_index()
        self.stations = [
            str(s_id) for s_id in np.sort(self.df_log['station_id'].unique())
        ]

        self.df_bikes_avail = pd.DataFrame()
        self.df_docs_avail = pd.DataFrame()
        self.pivot_df()

        self.df_bikes_source = pd.DataFrame()
        self.df_docks_source = pd.DataFrame()

        if os.path.isfile(altitude_file):
            self.df_altitude = pd.read_csv(altitude_file, index_col=0)

        self.id_coord_mapping_df = pd.DataFrame()

        if os.path.isfile(coord_mapping_file):
            s_mod = os.path.getmtime(coord_mapping_file)
            s_now = datetime.datetime.now().timestamp()
            if s_mod < (s_now - 3600):
                self.id_coord_mapping_df = self.make_station_coord_transformed_df(
                    self.get_station_info())
                with open(coord_mapping_file, 'wb') as handle:
                    pickle.dump(self.id_coord_mapping_df,
                                handle,
                                protocol=pickle.HIGHEST_PROTOCOL)
                    # TODO Do not overwrite?
            else:
                with open('mapping_df.pickle', 'rb') as handle:
                    self.id_coord_mapping_df = pickle.load(handle)

        if city == 'Oslo':
            self.mid_lat, self.mid_lon = pyproj.transform(
                pyproj.Proj(init='epsg:4326'), pyproj.Proj(init='epsg:3857'),
                10.735, 59.928)  # Center of map location
            self.coord_range_delta = 7500
        elif city == 'NewYorkCity':
            self.mid_lat, self.mid_lon = pyproj.transform(
                pyproj.Proj(init='epsg:4326'), pyproj.Proj(init='epsg:3857'),
                -73.973, 40.736)
            self.coord_range_delta = 12500

        # Colors
        self.empty_color = "#7e7e7e"
        self.avail_color = "#87e03e"
        self.full_color = "#ff2332"

        x_mul_sel_opt = [(s, s + ' | ' + name) for s, name in zip(
            self.stations,
            self.df_s_info.reindex(self.stations, fill_value='Unknown')
            ['name'])]
        self.x_mul_select = MultiSelect(
            title="Stations",
            value=[self.stations[0], self.stations[1]],
            options=x_mul_sel_opt,
            size=8)
        self.x_mul_select.on_change('value', self.x_mul_select_callback)

        s_date = datetime.date.fromtimestamp(
            self.df_bikes_avail.index[0].value / 10**9)
        e_date = datetime.date.fromtimestamp(
            self.df_bikes_avail.index[-1].value / 10**9)
        e_date += datetime.timedelta(days=1)
        self.dr_slider = DateRangeSlider(
            title='Date Range',
            start=s_date,
            end=e_date,
            step=24,
            value=(e_date - datetime.timedelta(days=5), e_date),
            callback_throttle=500)
        self.dr_slider.on_change('value', self.date_rage_slider_callback)

        self.range_slider = RangeSlider(title='Time of day',
                                        start=5,
                                        end=25,
                                        step=1,
                                        value=(5, 25))
        self.range_slider.on_change('value', self.hour_rage_slider_callback)

        self.histogram_policy = RadioButtonGroup(
            labels=["Aggregate", "Flatten"], active=0)
        self.histogram_policy.on_change('active',
                                        self.histogram_policy_callback)

        self.x_select = Select(title="X-axis",
                               value=self.stations[0],
                               options=self.stations)
        self.x_select.on_change('value', self.x_select_callback)
        self.y_select = Select(title="Y-axis",
                               value=self.stations[1],
                               options=self.stations)
        self.y_select.on_change('value', self.y_select_callback)

        self.create_sliced_df()
        self.station_history_fig, self.station_history_fig_source = self.create_station_history_fig(
        )
        self.station_avail_histogram, self.station_avail_histogram_source = self.create_station_avail_histogram(
        )
        self.extremes_annulus, self.extremes_annulus_source = self.create_extremes_figure(
        )
        self.station_map_fig, self.station_map_fig_source = self.create_station_map(
        )
        self.station_map_fig_source.selected.indices = [
            self.id_coord_mapping_df.index.get_loc(s)
            for s in self.x_mul_select.value
        ]

        self._layout = column(
            row(
                column(self.x_mul_select, self.dr_slider, self.range_slider,
                       self.histogram_policy, self.extremes_annulus),
                self.station_avail_histogram, self.station_map_fig),
            row(self.station_history_fig))
        # row(column(self.x_select, self.y_select), self.two_station_heatmap()))

    @property
    def layout(self):
        return self._layout

    def re_index(self):
        self.df_log.index = pd.to_datetime(10**9 *
                                           self.df_log['last_reported'])
        self.df_log.drop(['last_reported'], axis=1, inplace=True)
        self.df_log.index.tz_localize('UTC').tz_convert('Europe/Oslo')

    def pivot_df(self):
        def get_column_mapper(df):
            d = {}
            for col in df.columns.values:
                d[col] = str(col)
            return d

        self.df_bikes_avail = self.df_log.pivot_table(
            index='last_reported',
            columns='station_id',
            values='num_bikes_available')
        self.df_bikes_avail.rename(columns=get_column_mapper(
            self.df_bikes_avail),
                                   inplace=True)

        self.df_docs_avail = self.df_log.pivot_table(
            index='last_reported',
            columns='station_id',
            values='num_docks_available')
        self.df_docs_avail.rename(columns=get_column_mapper(
            self.df_docs_avail),
                                  inplace=True)

    def create_sliced_df(self):
        s_hour = self.range_slider.value[0]
        e_hour = self.range_slider.value[1]
        if e_hour == 25:
            hours_of_day = self.df_bikes_avail.index.hour.isin(
                np.append([0], range(s_hour, e_hour - 1)))
        else:
            hours_of_day = self.df_bikes_avail.index.hour.isin(
                range(s_hour, e_hour))

        df_bikes_sliced = self.df_bikes_avail.loc[
            hours_of_day][self.dr_slider.value_as_datetime[0]:self.dr_slider.
                          value_as_datetime[1]]
        df_docks_sliced = self.df_docs_avail.loc[
            hours_of_day][self.dr_slider.value_as_datetime[0]:self.dr_slider.
                          value_as_datetime[1]]

        self.df_bikes_source = df_bikes_sliced.loc[:, [
            val for val in self.x_mul_select.value
        ]].fillna(0)  # TODO  Filling nan with 0 might not be best solution
        self.df_docks_source = df_docks_sliced.loc[:, [
            val for val in self.x_mul_select.value
        ]].fillna(0)  #

    def create_station_history_fig(self):

        source_dict = self.create_station_history_fig_source_dict()
        source = ColumnDataSource(source_dict)

        fig = figure(plot_width=800, plot_height=600, x_axis_type='datetime')

        colors = Category20[20][::2]
        for val, color in zip(self.x_mul_select.value, colors):
            fig.step('last_reported',
                     val,
                     color=color,
                     legend=value(val),
                     source=source)

        return fig, source

    def create_station_history_fig_source_dict(self):
        df_sliced = self.df_bikes_avail[self.dr_slider.value_as_datetime[0]:
                                        self.dr_slider.value_as_datetime[1]]
        df_source = df_sliced.loc[:, [val for val in self.x_mul_select.value]]
        df_source.index = df_source.index + self.tz_offset
        df_source = df_source.reset_index()
        return df_source.to_dict(orient='list')

    def create_station_avail_histogram(self):

        d = self.create_station_avail_histogram_source_dict()
        source = ColumnDataSource(d)

        fig = figure(plot_width=800,
                     plot_height=600,
                     title='Histogram of availability',
                     background_fill_color="#fafafa")

        fig.quad(top='top',
                 bottom=0,
                 left='left',
                 right='right',
                 color="colors",
                 line_color="white",
                 alpha=0.9,
                 source=source)

        fig.y_range.start = 0
        # fig.legend.location = "center_right"
        # fig.legend.background_fill_color = "#fefefe"
        x_labl = 'x, Available bikes in station' if len(
            self.x_mul_select.value) == 1 else 'x, Available bikes in stations'
        fig.xaxis.axis_label = x_labl
        fig.yaxis.axis_label = 'P(x)'
        fig.grid.grid_line_color = "white"

        return fig, source

    def create_station_avail_histogram_source_dict(self):

        if self.histogram_policy.labels[
                self.histogram_policy.active] == 'Flatten':
            df_bikes_source = self.df_bikes_source
            df_docks_source = self.df_docks_source
        elif self.histogram_policy.labels[
                self.histogram_policy.active] == 'Aggregate':
            df_bikes_source = self.df_bikes_source.sum(axis=1)
            df_docks_source = self.df_docks_source.sum(axis=1)
        else:
            df_bikes_source = self.df_bikes_source
            df_docks_source = self.df_docks_source

        b = np.array((range(-1, int(df_bikes_source.max().max()) + 1))) + 0.5
        hist, edges = np.histogram(df_bikes_source, density=True, bins=b)

        colors = np.array([self.avail_color] * (len(hist)))

        if np.any(df_bikes_source.values == 0):
            colors[0] = self.empty_color

        if np.any(df_docks_source.values == 0):
            colors[-1] = self.full_color  # TODO Add for all

        d = dict(top=hist, left=edges[:-1], right=edges[1:], colors=colors)

        return d

    def create_extremes_figure(self):

        fig = figure(title='Availability summary',
                     width=300,
                     height=300,
                     x_range=(-100, 100),
                     y_range=(-100, 100),
                     tools='')
        fig.axis.visible = False
        fig.grid.visible = False
        fig.outline_line_alpha = 0.0

        source = ColumnDataSource(self.create_extremes_figure_source_dict())

        fig.annular_wedge(x=0,
                          y=0,
                          inner_radius=50,
                          outer_radius=75,
                          start_angle=0,
                          end_angle='end_angle_empty',
                          color=self.empty_color,
                          source=source)  # Totally empty
        fig.annular_wedge(x=0,
                          y=0,
                          inner_radius=50,
                          outer_radius=75,
                          start_angle='end_angle_empty',
                          end_angle='end_angle_mid',
                          color=self.avail_color,
                          source=source)  # Not empty, not full
        fig.annular_wedge(x=0,
                          y=0,
                          inner_radius=50,
                          outer_radius=75,
                          start_angle='end_angle_mid',
                          end_angle=2 * np.pi,
                          color=self.full_color,
                          source=source)  # Totally full

        fig.circle(x=0, y=0, radius=50, color="#fafafa")

        fig.text(x=0,
                 y=14,
                 text='empty_percent',
                 text_color=self.empty_color,
                 text_baseline='bottom',
                 text_align='center',
                 text_font_size='16pt',
                 text_font_style='bold',
                 source=source)
        fig.text(x=0,
                 y=0,
                 text='avail_percent',
                 text_color=self.avail_color,
                 text_baseline='middle',
                 text_align='center',
                 text_font_size='21pt',
                 text_font_style='bold',
                 source=source)
        fig.text(x=0,
                 y=-14,
                 text='full_percent',
                 text_color=self.full_color,
                 text_baseline='top',
                 text_align='center',
                 text_font_size='16pt',
                 text_font_style='bold',
                 source=source)

        return fig, source

    def create_extremes_figure_source_dict(self):

        df_bikes_source = self.df_bikes_source.sum(axis=1)
        df_docks_source = self.df_docks_source.sum(axis=1)

        d = dict(end_angle_empty=[
            2 * np.pi * np.count_nonzero(df_bikes_source.values == 0) /
            len(df_bikes_source.values)
        ],
                 end_angle_mid=[
                     2 * np.pi *
                     (1 - (np.count_nonzero(df_docks_source.values == 0) /
                           len(df_docks_source.values)))
                 ])

        d['empty_percent'] = [
            f"{100 * (np.abs(d['end_angle_empty'][0]) / (2 * np.pi)):2.1f}% "
        ]  # TODO deal with nan
        d['avail_percent'] = [
            f"{100*(np.abs(d['end_angle_mid'][0] - d['end_angle_empty'][0])/(2 * np.pi)):.1f}%"
        ]  # TODO deal with nan
        d['full_percent'] = [
            f"{100 * (np.abs(2 * np.pi - d['end_angle_mid'][0]) / (2 * np.pi)):.1f}%"
        ]  # TODO deal with nan

        return d

    def create_station_map(self):

        fig = figure(
            plot_width=780,
            plot_height=600,
            title='Stations map and selector',
            tools=['pan', 'box_zoom', 'wheel_zoom', 'lasso_select', 'reset'],
            x_range=(self.mid_lat - self.coord_range_delta,
                     self.mid_lat + self.coord_range_delta),
            y_range=(self.mid_lon - self.coord_range_delta,
                     self.mid_lon + self.coord_range_delta),
            x_axis_type="mercator",
            y_axis_type="mercator")
        fig.add_tile(CARTODBPOSITRON)

        lst = fig.select(dict(type=LassoSelectTool))[0]
        lst.select_every_mousemove = False

        # # Bikes available
        # fig.annular_wedge(x=status_df['x_proj'], y=status_df['y_proj'], color=self.avail_color,
        #                 inner_radius=np.zeros(len(status_df)), outer_radius=25 * np.sqrt(status_df['num_docs']),
        #                 start_angle=(np.pi / 2 + np.zeros(len(status_df))),
        #                 end_angle=(np.pi / 2 - status_df['docks_start_ang']), direction='clock')
        # # Docks available
        # fig.annular_wedge(x=status_df['x_proj'], y=status_df['y_proj'], color="#ea6d3f",
        #                 inner_radius=np.zeros(len(status_df)), outer_radius=25 * np.sqrt(status_df['num_docs']),
        #                 start_angle=(np.pi / 2 - status_df['docks_start_ang']),
        #                 end_angle=(10 ** (-3) + np.pi / 2 * np.ones(len(status_df))), direction='clock')
        #
        # fig.text(x=status_df['x_proj'], y=status_df['y_proj'], text=status_df.index)

        source = ColumnDataSource(self.id_coord_mapping_df)

        c = fig.circle(x='x_proj',
                       y='y_proj',
                       size=10,
                       color="navy",
                       source=source)
        fig.text(x='x_proj', y='y_proj', text='station_id', source=source)

        c.data_source.selected.on_change('indices', self.lasso_select_callback)

        return fig, source

    def two_station_heatmap(self):
        fig = figure(width=700, match_aspect=True, tools='')
        fig.xgrid.grid_line_color = None
        fig.ygrid.grid_line_color = None
        s_id1, s_id2 = self.x_select.value, self.y_select.value
        df_counts = self.df_bikes_avail.groupby(
            [s_id1, s_id2]).size().reset_index(name='counts')
        df_counts.rename(columns={s_id1: s_id1, s_id2: s_id2}, inplace=True)
        source = ColumnDataSource(df_counts)

        pallette = [
            '#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef',
            '#deebf7', '#f7fbff'
        ][::-1]
        mapper = LinearColorMapper(palette=pallette,
                                   low=0,
                                   high=df_counts.counts.max())
        color_bar = ColorBar(color_mapper=mapper)

        fig.rect(x=str(s_id1),
                 y=str(s_id2),
                 width=1.0,
                 height=1.0,
                 line_alpha=0.0,
                 fill_color=transform('counts', mapper),
                 source=source)

        fig.add_layout(color_bar, 'right')

        return fig

    def lasso_select_callback(self, attr, old, new):
        # print(self.id_coord_mapping_df.iloc[new].index.values)
        if new:
            self.x_mul_select.value = list(
                self.id_coord_mapping_df.iloc[new].index.values)

    def get_station_info(self):

        if self.city == 'Oslo':
            json_url = "https://gbfs.urbansharing.com/oslobysykkel.no/station_information.json"
        elif self.city == 'NewYorkCity':
            json_url = "https://gbfs.citibikenyc.com/gbfs/es/station_information.json"

        with urllib.request.urlopen(json_url) as url:
            station_info = json.loads(url.read().decode())
            return station_info

    # @staticmethod
    def make_station_coord_transformed_df(self, station_info_in):
        def apply_transfomation(lon_in, lat_in):
            return pyproj.transform(pyproj.Proj(init='epsg:4326'),
                                    pyproj.Proj(init='epsg:3857'), lon_in,
                                    lat_in)

        lon = [s['lon'] for s in station_info_in['data']['stations']]
        lat = [s['lat'] for s in station_info_in['data']['stations']]
        x_proj, y_proj = zip(*map(apply_transfomation, lon, lat))
        index = [s['station_id'] for s in station_info_in['data']['stations']]
        df = pd.DataFrame(data={
            'x_proj': x_proj,
            'y_proj': y_proj
        },
                          index=index)
        df.index.name = 'station_id'
        return df

    def update(self):
        t_start = time()
        self.station_history_fig, self.station_history_fig_source = self.create_station_history_fig(
        )
        self.station_avail_histogram, self.station_avail_histogram_source = self.create_station_avail_histogram(
        )
        print(f"Used: {(time() - t_start) * 1000} ms to regenerate figures")
        t_start = time()
        self._layout.children[0].children[1] = self.station_avail_histogram
        self._layout.children[1].children[0] = self.station_history_fig
        print(f"Used: {(time() - t_start)*1000} ms to update layout")
        # self._layout.children[1].children[1] = self.two_station_heatmap()

    def y_select_callback(self, attr, old, new):
        self.update()

    def x_select_callback(self, attr, old, new):
        self.update()

    def x_mul_select_callback(self, attr, old, new):
        self.create_sliced_df()
        self.station_history_fig, self.station_history_fig_source = self.create_station_history_fig(
        )
        self.station_avail_histogram, self.station_avail_histogram_source = self.create_station_avail_histogram(
        )
        self.extremes_annulus_source.data = self.create_extremes_figure_source_dict(
        )
        self.station_map_fig_source.selected.indices = [
            self.id_coord_mapping_df.index.get_loc(s)
            for s in self.x_mul_select.value
        ]
        self._layout.children[0].children[1] = self.station_avail_histogram
        self._layout.children[1].children[0] = self.station_history_fig

    def date_rage_slider_callback(self, attr, old, new):
        t_start = time()
        self.create_sliced_df()
        self.station_history_fig_source.data = self.create_station_history_fig_source_dict(
        )
        self.station_avail_histogram_source.data = self.create_station_avail_histogram_source_dict(
        )
        self.extremes_annulus_source.data = self.create_extremes_figure_source_dict(
        )
        print(f"Used: {(time() - t_start) * 1000} ms to calculate sources")

    def hour_rage_slider_callback(self, attr, old, new):
        t_start = time()
        # self.station_history_fig_source.data = self.create_station_history_fig_source_dict()
        self.create_sliced_df()
        self.extremes_annulus_source.data = self.create_extremes_figure_source_dict(
        )
        self.station_avail_histogram_source.data = self.create_station_avail_histogram_source_dict(
        )
        print(f"Used: {(time() - t_start) * 1000} ms to calculate sources")

    def histogram_policy_callback(self, attr, old, new):
        self.station_avail_histogram_source.data = self.create_station_avail_histogram_source_dict(
        )
Beispiel #11
0
    """This is the callback function that will update the figure curdoc
    model."""
    mainLayout.children[1].children[1] = create_figure()


def generate_selection_callback(metadata):
    """Generates and returns a callback function that updates a div element."""
    def generated_callback(event):
        """The new callback function to be assigned."""
        print(event)
        mainLayout.children[2] = Paragraph(text=str(metadata))

    return generated_callback


DATAFRAME_SEL.on_change('value', update_dataframe_selector)

MAKE_PLOT_BUTTON = Button(label="Build Plot")
MAKE_PLOT_BUTTON.on_click(build_fig_callback)

p = Paragraph(text="""The dataframe selector represents the selection
    of one *.csv file.""")

controls = widgetbox(DATAFRAME_SEL, BOND_SEL, MAKE_PLOT_BUTTON, p)

mainLayout = layout(children=[[title_div], [controls,
                                            create_figure()],
                              Paragraph()],
                    sizing_mode='fixed')

curdoc().add_root(mainLayout)
Beispiel #12
0
class StateDisplay:
    def __init__(self, dataset=STATES):

        self.dataset = dataset

        self.state_selection = MultiSelect(
            title="States:",
            options=self.dataset,
            value=["New York", "Texas"],
            sizing_mode="stretch_both",
        )
        self.per_capita = RadioGroup(
            labels=["Total", "Per Capita"],
            active=0,
            sizing_mode="stretch_width",
        )
        self.data_getter = RadioGroup(
            labels=[
                "Cases",
                "Deaths",
                "Positivity",
                "Testing",
                "Constant Positivity",
                "Constant Testing",
            ],
            active=0,
            sizing_mode="stretch_width",
        )
        self.plot_type = RadioGroup(
            labels=["Linear", "Logarithmic"],
            active=0,
            sizing_mode="stretch_width",
        )

        self.constant_date = DatePicker(
            title="Constant Date",
            value=(datetime.today() - timedelta(days=1)).date(),
            sizing_mode="stretch_width",
        )

        self.show_total = CheckboxGroup(
            labels=["Show total"],
            sizing_mode="stretch_width",
        )

        self.total_only = CheckboxGroup(
            labels=["Total only"],
            sizing_mode="stretch_width",
        )

        self.src = None
        self.p = None
        self.logp = None

        self.tooltips = [("State", "@state")]

    def make_dataset(self, state_list):

        by_state = {
            "avg_date": [],
            "avg_data": [],
            "state": [],
            "color": [],
            "line-width": [],
        }

        color_cycle = cycle(Category20_20)
        palette = [next(color_cycle) for _ in self.dataset]

        show_total = self.show_total.active == [0]
        total_only = self.total_only.active == [0]

        totals = None
        totals_denom = None

        for state_name in state_list:

            per_capita = self.per_capita.active == 1
            data_getter = self.data_getter.labels[
                self.data_getter.active].lower()
            constant_date = self.constant_date.value

            (
                dates,
                avg_dates,
                data,
                avg_data,
                test_data,
                label,
                tot_positive,
                tot_testing,
            ) = get_data(state_name, per_capita, data_getter, constant_date)

            if tot_positive is None and tot_testing is None:
                subtotal = pd.Series(avg_data.values[7:])
                subtotal.index = pd.DatetimeIndex(avg_dates.values[7:])
                subtotal_denom = None
            else:
                subtotal = pd.Series(tot_positive.values[7:])
                subtotal.index = pd.DatetimeIndex(avg_dates.values[7:])
                subtotal_denom = pd.Series(tot_testing.values[7:])
                subtotal_denom.index = pd.DatetimeIndex(avg_dates.values[7:])

            idx = pd.date_range(subtotal.index.min(), subtotal.index.max())
            subtotal = subtotal.reindex(idx)
            subtotal.interpolate(method="time", inplace=True)
            if subtotal_denom is not None:
                subtotal_denom = subtotal_denom.reindex(idx)
                subtotal_denom.interpolate(method="time", inplace=True)

            if totals is None:
                totals = subtotal
                if subtotal_denom is not None:
                    totals_denom = subtotal_denom
            else:
                idx = pd.date_range(
                    min(subtotal.index.min(), totals.index.min()),
                    max(subtotal.index.max(), totals.index.max()),
                )
                totals = totals.reindex(idx, fill_value=0)
                subtotal = subtotal.reindex(idx, fill_value=0)
                totals += subtotal
                if subtotal_denom is not None:
                    totals_denom = totals_denom.reindex(idx, fill_value=0)
                    subtotal_denom = subtotal_denom.reindex(idx, fill_value=0)
                    totals_denom += subtotal_denom

            if len(state_list) == 1 or not show_total or not total_only:
                by_state["avg_date"].append(avg_dates.values)
                by_state["avg_data"].append(avg_data.values)

                by_state["state"].append(state_name)
                by_state["color"].append(
                    palette[self.dataset.index(state_name)])
                by_state["line-width"].append(1)

        if totals_denom is not None:
            totals /= totals_denom

        if show_total:
            by_state["avg_date"].append(totals.index.values)
            by_state["avg_data"].append(totals.values)
            by_state["state"].append("Total")
            by_state["color"].append("black")
            by_state["line-width"].append(2)

        return label, ColumnDataSource(by_state)

    def make_plot(self):

        self.p = figure(
            x_axis_label="Date",
            x_axis_type="datetime",
            y_axis_label="Total Cases",
            width=900,
        )

        self.p.multi_line(
            source=self.src,
            xs="avg_date",
            ys="avg_data",
            legend_field="state",
            color="color",
            line_width="line-width",
        )

        self.p.add_tools(HoverTool(tooltips=self.tooltips))

        self.p.legend.location = "top_left"

        self.logp = figure(
            x_axis_label="Date",
            x_axis_type="datetime",
            y_axis_label="Total Cases",
            y_axis_type="log",
            width=900,
        )

        self.logp.multi_line(
            source=self.src,
            xs="avg_date",
            ys="avg_data",
            legend_field="state",
            color="color",
            line_width="line-width",
        )

        self.logp.add_tools(HoverTool(tooltips=self.tooltips))

        self.logp.legend.location = "top_left"

    def update_data(self, label, src):

        if self.src is None:
            self.src = src
            self.make_plot()
        else:
            self.src.data.update(src.data)

        if self.plot_type.active == 0:
            self.p.visible = True
            self.logp.visible = False
        else:
            self.p.visible = False
            self.logp.visible = True

        self.p.yaxis.axis_label = label
        self.logp.yaxis.axis_label = label

        if len(self.data_getter.labels) == 1:
            self.data_getter.visible = False

        data_getter = self.data_getter.labels[self.data_getter.active].lower()
        self.constant_date.visible = data_getter in (
            "constant positivity",
            "constant testing",
        )

    def update(self, attr, old, new):

        states_to_plot = sorted(self.state_selection.value)

        label, new_src = self.make_dataset(states_to_plot)

        self.update_data(label, new_src)

        self.show_total.visible = len(states_to_plot) != 1
        self.total_only.visible = self.show_total.active == [0]

    def run(self):

        self.state_selection.on_change("value", self.update)

        self.per_capita.on_change("active", self.update)
        self.data_getter.on_change("active", self.update)
        self.plot_type.on_change("active", self.update)
        self.constant_date.on_change("value", self.update)
        self.show_total.on_change("active", self.update)
        self.total_only.on_change("active", self.update)

        controls = column(
            [
                self.state_selection,
                self.per_capita,
                self.data_getter,
                self.plot_type,
                self.constant_date,
                self.show_total,
                self.total_only,
            ],
            sizing_mode="fixed",
            width=300,
            height=600,
        )

        self.update(None, None, None)

        plots = column(self.p, self.logp)

        return row(controls, plots, sizing_mode="stretch_both")
Beispiel #13
0
import pandas as pd
import numpy as np
#Pandas version 0.22.0
#Bokeh version 0.12.10
#Numpy version 1.12.1
from bokeh.io import output_file, show,curdoc
from bokeh.models import Quad
from bokeh.layouts import row, layout,widgetbox
from bokeh.models.widgets import Select,MultiSelect
from bokeh.plotting import ColumnDataSource,Figure,reset_output,gridplot
d= {'A': [1,1,1,2,2,3,4,4,4,4,4], 'B': [1,2,2,2,3,3,4,5,6,6,6], 'C' : [2,2,2,2,2,3,4,5,6,6,6]}
df = pd.DataFrame(data=d)
names = ["A","B", "C"]
#Since bokeh.charts are deprecated so using the new method using numpy histogram
hist,edge = np.histogram(df['A'],bins=4)
#This is the method you need to pass the histogram objects to source data here it takes edge values for each bin start and end and hist gives count.
source = ColumnDataSource(data={'hist': hist, 'edges_rt': edge[1:], 'edges_lt':edge[:-1]})
plot = Figure(plot_height = 300,plot_width = 400)
#The quad is used to display the histogram using bokeh.
plot.quad(top='hist', bottom=0, left='edges_lt', right='edges_rt',fill_color="#036564",
 line_color="#033649",source = source)
#When you change the selection it will this function and changes the source data so that values are updated.
def callback_menu(attr, old, new):
    hist,edge = np.histogram(df[menu.value],bins=4)
    source.data={'hist': hist,'edges_rt': edge[1:], 'edges_lt': edge[:-1]}
#These are interacting tools in the final graph
menu = MultiSelect(options=names,value= ['A','B'], title='Sensor Data')
menu.on_change('value', callback_menu)
layout = gridplot([[widgetbox(menu),plot]])
curdoc().add_root(layout)
def update_plots(attr, old, new):
    b.children = [plot_state_by_year(), make_scatterplot()]


def update_plot_1(attr, old, new):
    b.children[0] = plot_state_by_year()


states = [
    'AL', 'AR', 'AZ', 'CO', 'CT', 'DC', 'DE', 'GA', 'HI', 'IA', 'ID', 'IL',
    'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT',
    'NC', 'NE', 'NH', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC',
    'SD', 'TN', 'TX', 'UT', 'VA', 'VT'
]

multi_select = MultiSelect(title="States:", value=['VT'], options=states)

checkbox_group = CheckboxGroup(
    labels=["show US average", "show regression line"], active=[0, 1])

multi_select.on_change('value', update_plots)

checkbox_group.on_change('active', update_plots)

controls = widgetbox(multi_select, checkbox_group)

b = VBox(plot_state_by_year(), make_scatterplot())
l = row(controls, b)

curdoc().add_root(l)
Beispiel #15
0
    plot.add_layout(legend, 'right')
    plot.legend.click_policy = 'hide'
    return plot


def update_plot():
    page_layout.children[3].children[1] = create_plot()


animals = data['animal'].unique().tolist()
animals.sort()
animal_select = Select(title='Animal', options=animals, value=animals[0])
animal_select.on_change('value', lambda attr, old, new: animal_changed())

date_select = MultiSelect(title='Dates', size=10)
date_select.on_change('value', lambda attr, old, new: update_plot())

groupby_options = {
    'masker level': 'masker_level',
    'sensation level': 'sens_level',
    'target center frequency': 'target_fc',
}

groupby_select = CheckboxButtonGroup(labels=list(groupby_options.keys()), active=[0])
groupby_select.on_change('active', lambda attr, old, new: update_plot())


animal_trials = ColumnDataSource()
animal_trials_plot = figure(title='number of trials', tools="", width=1000,
                             height=150)
animal_trials_plot.xaxis.axis_label = 'Session'
Beispiel #16
0
background.on_change("active", update_background)

map_controls = column(Div(text="<b>Kaartopties</b><br><br>Kaartlagen"),
                      map_layers, Div(text="Achtergrond"), background)

# %% define main filter selection and handlers
filters = list()
for name, subfilter in zip(data.filters.names, data.filters.filters):
    select_filter = MultiSelect(
        title=f"{name}:",
        value=subfilter.value,
        options=subfilter.options,
    )
    select_filter.size = min(len(subfilter.options), 7)
    select_filter.on_change("value", update_on_filter_select)
    filters += [select_filter]

# %% define location selection and handlers
select_locations = MultiSelect(title="Locaties:",
                               value=[],
                               options=data.locations.options)

select_locations.on_change("value", update_on_locations_select)
# select_locations.js_link("value",
#                          data.locations.source.selected,
#                          "indices")

# %% define parameter selection and handlers
select_parameters = MultiSelect(title="Parameters:",
                                value=[],
def update(attr, old, new):
    layout.children[1] = plot_scatter()


##Create Sliders
columns = cl_clean
years = list(df['Year'].unique().astype(str))
regions = list(df['Region'].unique())


x_column = Select(title='X-Axis', value=columns[0], options=columns)
x_column.on_change('value', update)

y_column = Select(title='Y-Axis', value=columns[3], options=columns)
y_column.on_change('value', update)

size_column = Select(title='Size', value=columns[4], options=columns)
size_column.on_change('value', update)

year_column = Select(title='Year', value=years[-3], options=years)
year_column.on_change('value', update)

region_column = MultiSelect(title='Regions', value=regions[:], options=regions)
region_column.on_change('value',update)

controls = widgetbox([x_column, y_column, size_column, year_column, region_column], width=550)
layout = row(controls, plot_scatter())

curdoc().add_root(layout)
curdoc().title = "FH Scatterplot" 
Beispiel #18
0
df_cases = {}

pNc0_select_results = Button(label='Add GeneMapper Results',
                             button_type='success')
pNc0_select_results.on_click(pNc0_on_results_click)
pNc0_results_text = TextAreaInput(value='<results file>',
                                  disabled=True,
                                  rows=5)

# select_host_case = Select(title='Select Host', options=list(source_cases.keys()))
p0c0_select_host_case = Select(title='Select Host', options=[])
p0c0_select_host_case.on_change('value', p0c0_on_host_click)

p0c0_select_donor_cases = MultiSelect(
    title='Select Donor(s) <ctrl+click to multiselect>', options=[], size=20)
p0c0_select_donor_cases.on_change('value', p0c0_on_donor_change)

p0c0_export_template = Button(label='Export Template', button_type='warning')
p0c0_export_template.on_click(p0c0_on_export_template_click)

p0c0_enter_host_name = TextInput(title='Enter Host Name',
                                 value='<type host name here>')
p1c0_enter_host_name = TextInput(title='Enter Host Name',
                                 value='<type host name here>')

p1c0_select_template = Button(label='Select Template', button_type='success')
p1c0_select_template.on_click(p1c0_on_select_template_click)
p1c0_template_text = TextAreaInput(value='<template file>', disabled=True)

p1c0_select_samples = MultiSelect(
    title='Select Sample(s) <ctrl+click to multiselect>', options=[], size=20)
Beispiel #19
0
select_term = Select(title="Selected Term:",
                     value=all_terms[-1],
                     options=all_terms)
select_term.on_change("value", update_term)

program_list = sorted(
    list(pt.loc[((pt["year_term"] == select_term.value)
                 & (pt["stage"] == stage_list[stage_rg.active])),
                "curriculum", ].dropna().unique()))
program = Select(title="Selected Academic Program:",
                 value=program_list[0],
                 options=program_list)
program.on_change("value", update_prog)

terms_opt = all_terms.copy()
terms_opt.remove(select_term.value)
terms = MultiSelect(
    title="Other Displayed Terms: (ctrl-click to select/de-select)",
    options=terms_opt,
    size=5,
    value=[terms_opt[-1]],
)
terms.on_change("value", update)

# layout
controls = widgetbox([stage_rg, select_term, program, terms])
layout = row(controls, create_figure(summ_t))

curdoc().add_root(layout)
curdoc().title = "Admissions Weekly Report"
def tab_visualize(csv):

    csv_original = csv.copy()
    csv_modified = csv.copy()

    target_csv = {'csv': csv_original}

    source_length = target_csv['csv'].shape[0]

    table_source = ColumnDataSource(target_csv['csv'])
    table_columns = [
        TableColumn(field=col, title=col)
        for col in list(target_csv['csv'].columns)
    ]

    table_original = DataTable(source=table_source,
                               columns=table_columns,
                               width=1550,
                               height=250,
                               fit_columns=False,
                               name="Original")

    g = target_csv['csv'].columns.to_series().groupby(
        target_csv['csv'].dtypes).groups
    g_list = list(g.keys())

    float_list = []
    rest_list = []
    for l in g_list:
        if ('float' or 'int' in str(l)):
            float_list += list(g[l])
        else:
            rest_list += list(g[l])
    print(float_list)
    print(rest_list)

    def make_dataset(col_list, range_start, range_end):

        xs = []
        ys = []
        labels = []
        colors = []

        target_length = target_csv['csv'].shape[0]
        end = range_end if (range_end < target_length) else target_length

        target_data = target_csv['csv'][col_list +
                                        rest_list].iloc[range_start:end]
        target_x = list(np.arange(target_data.shape[0]) + range_start)

        for col, i in zip(col_list, range(len(col_list))):
            y = list(target_data[col].values)

            xs.append(target_x)
            ys.append(y)
            labels.append(col)
            colors.append(line_colors[i])

        new_src = ColumnDataSource(data={
            'x': xs,
            'y': ys,
            'label': labels,
            'colors': colors
        })

        return new_src

    def make_plot(src):
        p = figure(plot_width=1300,
                   plot_height=400,
                   title='Raw data',
                   x_axis_label='Index',
                   y_axis_label='Sensor value')

        p.multi_line('x',
                     'y',
                     legend='label',
                     color='colors',
                     line_width=1,
                     source=src)

        tools = HoverTool()
        TOOLTIPS = [("", ""), ("", "")]

        return p

    def update(attr, old, new):

        column_to_plot = select_column.value

        new_src = make_dataset(column_to_plot, range_select.value[0],
                               range_select.value[1])
        src.data.update(new_src.data)

    line_colors = Category20_16
    line_colors.sort()

    select_column = MultiSelect(title="Column visualization",
                                value=float_list,
                                options=float_list)
    select_column.on_change("value", update)

    range_select = RangeSlider(start=0,
                               end=source_length,
                               value=(0, int(source_length / 100)),
                               step=1,
                               title='Sensor range')
    range_select.on_change('value', update)

    src = make_dataset([float_list[0]], range_select.value[0],
                       range_select.value[1])

    p = make_plot(src)

    select_normalize = Select(title="Select normalize transformation",
                              options=["-", "Min-Max", "Z normalize", "Raw"])
    button_get_normal = Button(label="Transform")

    def normal_handler():
        print("Normalize")
        # cols_to_normalize = float_list
        cols_to_normalize = select_transform.value

        if (select_normalize.value == "Min-Max"):
            scaler = preprocessing.MinMaxScaler()
            csv_modified[cols_to_normalize] = scaler.fit_transform(
                X=csv_original[cols_to_normalize].values)

            target_csv['csv'] = csv_modified
            table_source.data = ColumnDataSource(target_csv['csv']).data

            csv[cols_to_normalize] = target_csv['csv'][cols_to_normalize]
            print("Min Max Mormalize")

        elif (select_normalize.value == "Z normalize"):
            scaler = preprocessing.StandardScaler()
            csv_modified[cols_to_normalize] = scaler.fit_transform(
                X=csv_original[cols_to_normalize].values)

            target_csv['csv'] = csv_modified
            table_source.data = ColumnDataSource(target_csv['csv']).data

            csv[cols_to_normalize] = target_csv['csv'][cols_to_normalize]
            print("Z normalize")

        elif (select_normalize.value == "Raw"):

            csv_modified[cols_to_normalize] = csv_original[cols_to_normalize]

            target_csv['csv'] = csv_modified
            table_source.data = ColumnDataSource(target_csv['csv']).data

            csv[cols_to_normalize] = target_csv['csv'][cols_to_normalize]
            print("Raw")

    button_get_normal.on_click(normal_handler)

    select_transform = MultiSelect(title="Select columns for transformation",
                                   value=list(target_csv['csv'].columns),
                                   options=list(target_csv['csv'].columns))

    controls = WidgetBox(select_normalize, select_transform, button_get_normal,
                         select_column, range_select)

    layout = Column(Row(table_original), Row(controls, p))

    tab = Panel(child=layout, title='Visualization')

    return tab
Beispiel #21
0
def minimial_page_4_server_test(doc):
    doc.title = "**testing demo page**"

    user_dict = load_JSON_file_to_Dict(rel_User_fileName)

    p = Paragraph(text="""
                    this is an initialized text widget
                    """,
                  width=200,
                  height=100)
    text_box = widgetbox(p)

    if __name__ == '__main__':

        def button_reaction():
            print("stopping test-server (self kill. restart)")
            p.text = "stopping server"
            server1.stop()
            p.text = "stopping server .. failed"

        toggle = Button(label='kill test-server', button_type='success')
    else:

        def button_reaction():
            print("pressed button")
            p.text = "json content is :\n " + str(user_dict)

        toggle = Button(
            label='smile:) with test', button_type='success'
        )  # Options: ‘default’, ‘primary’, ‘success’, ‘warning’, ‘danger’, ‘link’
    toggle.on_click(button_reaction)

    img_paths = []
    img_paths.append(join(rel_DATA_DIR, 'logoScrnSht.png'))
    img_paths.append(join(rel_DATA_DIR, 'tree.png'))
    x_range = (-20, 10)
    y_range = (20, 30)
    factor = 1.2
    figImg = figure(x_range=x_range,
                    y_range=y_range,
                    width=500,
                    height=400,
                    active_drag='pan',
                    active_scroll='wheel_zoom')
    figImg.toolbar.logo = None
    figImg.image_url(url=[img_paths[0]],
                     x=x_range[0] / factor,
                     y=(y_range[0] + y_range[1]) / 2,
                     w=(x_range[1] - x_range[0]) / factor,
                     h=(y_range[1] - y_range[0]) / factor,
                     anchor="bottom_left")
    factor = 2
    figImg.image_url(url=[img_paths[1]],
                     x=x_range[0] / factor,
                     y=(y_range[0] + y_range[1]) / 2,
                     w=(x_range[1] - x_range[0]) / factor,
                     h=(y_range[1] - y_range[0]) /
                     factor)  #, anchor="bottom_left") default it left-up

    multi_select = MultiSelect(title="Option:",
                               value=["foo", "quux"],
                               size=7,
                               options=[("foo", "Foo"), ("bar", "BAR"),
                                        ("baz", "bAz"), ("quux", "quux")])

    def mSlct_update(attrname, old, new):
        myString = ''
        for i in multi_select.value:
            myString += '\n' + i
        myString += '\n' + str(range_slider.value[0])
        myString += '\n' + str(range_slider.value[1])
        myString += '\n' + text_input_as_filter.value
        print("\n\n selected:\n", myString)
        # myText.text = myString

    multi_select.on_change('value', mSlct_update)

    range_slider = RangeSlider(start=0,
                               end=10,
                               value=(1, 9),
                               step=.1,
                               title="Stuff")
    range_slider.on_change('value', mSlct_update)

    text_input_as_filter = TextInput(
        value="default", title="Label:")  # no workable callback option

    doc_add_root(doc, toggle)
    doc_add_root(doc, figImg)
    doc_add_root(doc, text_box)
    doc_add_root(doc, multi_select)
    doc_add_root(doc, range_slider)
    doc_add_root(doc, text_input_as_filter)
tooltip_fix_callback = CustomJS(code="""
    var tooltips = document.getElementsByClassName("bk-tooltip");
    for (var i = 0, len = tooltips.length; i < len; i ++) {
        tooltips[i].style.top = ""; // unset what bokeh.js sets
        tooltips[i].style.right = "";
        tooltips[i].style.bottom = "";
        tooltips[i].style.left = "";
        tooltips[i].style.bottom = "0px";
        tooltips[i].style.right = "-150px";
    }
    """)

# bind widget callbacks
color_class_select.on_change('value', update_color_class)
toggle_class_select.on_change('value', update_toggle_class)
class_toggle_multi_select.on_change('value', update_class_selection)
file_select_dropdown.on_click(file_select_handler)
hover_tip_tool.callback = tooltip_fix_callback

update_toggle_class('value', '', features_list[0])

control_panel = column([
    file_select_dropdown, color_class_select, toggle_class_select,
    class_toggle_multi_select
],
                       sizing_mode="stretch_height",
                       width=200)
p.add_tools(wheel_zoom_tool)
p.add_tools(hover_tip_tool)
p.toolbar.active_scroll = wheel_zoom_tool
curdoc().add_root(row(control_panel, p, sizing_mode='stretch_both'))
    wordsource_up.data = newsource.data


def update2(attr, old, new):
    entity = multi_select2.value
    newdf_down = make_df(entity_bag_2021, average_sentiment_array, entity[0],
                         -1, spacing)
    newsource = ColumnDataSource(newdf_down)
    wordsource_down.data = newsource.data


menu = [(ent_key[key], key) for key in ent_key.keys()]
multi_select1 = MultiSelect(title="Entity type",
                            value=["PERSON", "person"],
                            options=menu)
multi_select1.on_change('value', update1)

multi_select2 = MultiSelect(title="Entity type",
                            value=["ORG", "Organization"],
                            options=menu)
multi_select2.on_change('value', update2)

# ipdb.set_trace()

pre = PreText(text="""Select article by clicking on circles""",
              width=500,
              height=100)

p.tools.append(TapTool(plot=p))

Beispiel #24
0
from help_text import text_dict

header_text = Div(text="""<pre>          <font size="6"><a href="http://192.168.86.29:5006/plojo">Plojo</a> Help Document</font><pre>""",width=400)

topic_list = [
chr(9608)*2+' Top Row Buttons ','+---- Upload/Browse/Fitting Button','+---- Information Box','+---- Edit Button','+---- Plot Button','+---- Load/Save Button',
chr(9608)*2+' Upload Layout ', '+---- Upload Data Options',
chr(9608)*2+' Browse Layout ','+---- Project/Experiment... Tabs','+---- plojo Keyword Search','+---- Edit/Save Experiment Information',
chr(9608)*2+' Fitting Layout ','+---- Curve Fitting with plojo',
chr(9608)*2+' Simuojo ', '+---- How to use simuojo?',
chr(9608)*2+' Fitting Methods ','+---- How does fitting work?','+---- Supported Fitting Models','+---- Confidence Intervals'
]

topic_key = ['toprow','ubfbutton','infobox','editbutton','plotbutton','loadsavebutton',
'uploadlayout','uploadoptions','browselayout','tabs','search','editinfo','fittinglayout','curvefit',
'simuojo','howtosimuojo','fitmethod','howfit','supportedfitmodel','confidence']

topic_menu = [i for i in zip(topic_key,topic_list)]

topic_select = MultiSelect(title='Topic',value=['none'],options=topic_menu,size=30)
text_show=Div(text='Select a topic to view.',width=800,height=800)

def topic_select_cb(attr,old,new):
    new=new[0]
    text_show.text = text_dict[new]

topic_select.on_change('value',topic_select_cb)
div_1 = Div(text='',width=50)
display_layout = layout([header_text],[div_1,topic_select,text_show])
curdoc().add_root(display_layout)
Beispiel #25
0
dtcds=ColumnDataSource(data=setupdict)
#    dtcds.on_change('data',on_change_data_source)
dtcolumns=[TableColumn(field='accs',title='GBAccession')]
dtbl=DataTable(source=dtcds,columns=dtcolumns,width=200,height=80,editable=True,selectable=True)
dtbl.source.selected.on_change('indices',tablecallback)
#layout=column(row(dtbl,gms,acw),row(p1,p2))

jscallback=CustomJS(args={'cds':ptree.leaf_cds,'dtcds':dtcds},code=stuffcode)
p1.add_tools(TapTool(callback=jscallback))#,names=['leaf_node']))

#cooldude=CustomJS(args={'cds':leaf_source},code=coolcode)

pms=MultiSelect(title='Select phylum',\
                options=[x[0] for x in collections.Counter(ptree.leaf_cds.data['phylum']).most_common()],\
                width=200,height=70)
pms.on_change('value',lambda attr,old,new:pmsfunc())

cms=MultiSelect(title='Select class',
                options=[x[0] for x in collections.Counter(ptree.leaf_cds.data['class']).most_common()],\
                width=200,height=70)
cms.on_change('value',lambda attr,old,new:cmsfunc())

gms=MultiSelect(title='Select genus',\
                options=[x[0] for x in collections.Counter(ptree.leaf_cds.data['genus']).most_common()],\
                width=200,height=70)
gms.on_change('value',lambda attr,old,new:gmsfunc())

kms=MultiSelect(title='Kingdom',\
                options=[x[0] for x in collections.Counter(ptree.leaf_cds.data['superkingdom']).most_common()],\
                width=200,height=70)
kms.on_change('value',lambda attr,old,new:kmsfunc())
Beispiel #26
0
NIGHTS_SLIDER.on_change(c.VALUE, update_data)

# Range Slider Widget
PRICE = FILTER_PROPERTIES[c.PRICE]
PRICE_SLIDER = RangeSlider(start=PRICE[0],
                           end=PRICE[1],
                           value=(PRICE[0], PRICE[1]),
                           step=50,
                           title='Nightly Price')
PRICE_SLIDER.on_change(c.VALUE, update_data)

# Multi Select Widgets
AMENITIES_SELECT = MultiSelect(title='Amenities:',
                               value=[],
                               options=FILTER_PROPERTIES[c.AMENITIES])
AMENITIES_SELECT.on_change(c.VALUE, update_data)

PROPERTY_TYPE_SELECT = MultiSelect(title='Property Type:',
                                   value=[],
                                   options=FILTER_PROPERTIES[c.PT])
PROPERTY_TYPE_SELECT.on_change(c.VALUE, update_data)

NEIGHBOURHOOD_SELECT = MultiSelect(title='Neighbourhood:',
                                   value=[],
                                   options=FILTER_PROPERTIES[c.NC])
NEIGHBOURHOOD_SELECT.on_change(c.VALUE, update_data)

# Checkbox Group (Multi Select) Widgets
NG_LIST = FILTER_PROPERTIES[c.NGC]
NEIGHBOURHOOD_GROUP = CheckboxButtonGroup(labels=NG_LIST,
                                          active=list(range(0, len(NG_LIST))))
PS3C2Voltage.line(x='x', y='voltages8', source=source)
PS3C3Current.line(x='x', y='currents9', source=source)
PS3C3Voltage.line(x='x', y='voltages9', source=source)

file_control.on_change('value', file_handler)
apply_button.on_click(apply_settings)
current_control.on_change('value', current_handler)
voltage_control.on_change('value', voltage_handler)
power_control.on_change('value', power_handler)
ocp_control.on_change('value', ocp_handler)
file_control.on_change('value', file_handler)
directory_control.on_change('value', file_handler)
comment_box.on_change('value', comment_handler)
channel_dropdown.on_change('value', channel_handler)
start_button.on_click(start_button_handler)
connect_button.on_click(connect_button_handler)
supply_dropdown.on_change('value', multiselect_handler)
t0 = time.time()
curdoc().add_root(
    row(
        column(supply_textbox, supply_dropdown, connect_button,
               current_control, voltage_control, power_control, ocp_control,
               channel_dropdown, apply_button, directory_control, file_control,
               start_button, comment_box),
        column(PS1C1Current, PS1C1Voltage, PS1C2Current, PS1C2Voltage,
               PS1C3Current, PS1C3Voltage),
        column(PS2C1Current, PS2C1Voltage, PS2C2Current, PS2C2Voltage,
               PS2C3Current, PS2C3Voltage),
        column(PS3C1Current, PS3C1Voltage, PS3C2Current, PS3C2Voltage,
               PS3C3Current, PS3C3Voltage)))
Beispiel #28
0
        update_map(filtered_data)
        slider_active_toggle.label = "slider not active"
        slider_active_toggle.button_type = "default"


# assign callbacks
play_button = Toggle(label="PLAY",
                     button_type="primary",
                     width=100,
                     callback=callback_play)
time_slider.on_change('value', callback_time_slider)
slider_active_toggle.on_click(callback_toggle_slider_activity)
pattern_select.on_change('active', callback_pattern_selection)
aggregate_select.on_change('active', callback_aggregation_selection)
groupby_select.on_change('active', callback_groupby_selection)
type_filter.on_change('value', callback_type_filter)
geo_source.on_change('selected', callback_map_selection)
select_all_types_button.on_click(callback_select_all_types)
## end callbacks

# headers
map_head = Div(text="<h2>Spatial distribution of incidents</h2>",
               css_classes=["plot-head"],
               width=LEFT_COLUMN_WIDTH)
ts_head = Div(text="<h2>Time distribution of incidents</h2>",
              css_classes=["plot-head"],
              width=RIGHT_COLUMN_WIDTH)
status_available_style = {"font-size": "8pt", "color": "green"}
status_unavailable_style = {"font-size": "8pt", "color": "red"}
status = Div(text="""<i>Status: at your service</i>""",
             style={
Beispiel #29
0
def graphs_change():
    """Callback on change of selected graph type"""
    d = curdoc()
    _remove_fig(d)
    _remove_selection(d)
    graph_val = d.get_model_by_name(GRAPH_SELECTION).value
    model_id, message_name, model_type = run_handlers.get_modelid_messagename_type(
        d)
    props = run_handlers.get_model_properties(model_id, message_name,
                                              model_type)

    if graph_val in ["line", "scatter", "step"]:
        # never want to plot this special string field
        field_options = [
            "{0} : {1}".format(k, props[k]) for k in props
            if not any(apv in k for apv in [APV_MODEL])
        ]
        xselect = Select(title="X Axis",
                         value=DEFAULT_UNSELECTED,
                         options=field_options + [DEFAULT_UNSELECTED],
                         name=X_AXIS_SELECTION)
        yselect = Select(title="Y Axis",
                         value=DEFAULT_UNSELECTED,
                         options=field_options + [DEFAULT_UNSELECTED],
                         name=Y_AXIS_SELECTION)
        xselect.on_change('value', lambda attr, old, new: make_2axis_graph())
        yselect.on_change('value', lambda attr, old, new: make_2axis_graph())
        d.add_root(
            column(Div(text=""),
                   row(widgetbox([xselect]), widgetbox([yselect])),
                   name=FIELD_SELECTION))

    if graph_val in ["image"]:
        # alter the field options for known non-image fields
        field_options = [
            "{0} : {1}".format(k, props[k]) for k in props
            if not any(apv in k for apv in [APV_RECVD, APV_SEQNO, APV_MODEL])
        ]
        imageselect = Select(title="Image Field",
                             value=DEFAULT_UNSELECTED,
                             options=[DEFAULT_UNSELECTED] + field_options,
                             name=IMAGE_SELECTION)
        mimeselect = Select(title="MIME Type",
                            value=DEFAULT_UNSELECTED,
                            options=[DEFAULT_UNSELECTED] +
                            SUPPORTED_MIME_TYPES,
                            name=MIME_SELECTION)
        imageselect.on_change('value',
                              lambda attr, old, new: image_selection_change())
        mimeselect.on_change('value',
                             lambda attr, old, new: image_selection_change())
        d.add_root(
            column(Div(text=""),
                   widgetbox([imageselect, mimeselect]),
                   name=IMAGE_MIME_SELECTION))

    if graph_val in ["table"]:
        # TODO: limit selectable columns to whose of the same size (table height)
        # use just the field name; don't show properties in the multi-select box
        col_options = [
            k for k in props
            if not any(apv in k for apv in [APV_RECVD, APV_SEQNO, APV_MODEL])
        ]
        columnmultiselect = MultiSelect(title="Columns:",
                                        value=[],
                                        options=col_options,
                                        name=COLUMN_MULTISELECT)
        columnmultiselect.on_change(
            'value', lambda attr, old, new: column_selection_change())
        d.add_root(
            column(Div(text=""),
                   widgetbox([columnmultiselect]),
                   name=COLUMN_SELECTION))

    if graph_val in ["raw"]:
        p = figure(plot_width=500,
                   plot_height=500,
                   background_fill_color="white",
                   y_range=(-40, 0),
                   title="",
                   name=FIGURE_MODEL)
        p.xaxis.visible = False
        p.yaxis.visible = False
        sind = run_handlers.get_source_index(d.session_context.id, model_id,
                                             message_name)
        _install_callback_and_cds(sind, model_id, message_name, stream_limit=1)
        p.text(x='apv_sequence_number',
               y=0,
               text='apv_model_as_string',
               source=d.get_model_by_name(sind),
               text_font_size="10pt",
               text_line_height=0.7,
               text_baseline="top",
               text_align="left")
        p.x_range.follow = "end"  # don't jam all the data into the graph; "window" it
        p.x_range.follow_interval = 1  # don't jam all the data into the graph; "window" it
        p.x_range.range_padding = 0
        d.add_root(p)
Beispiel #30
0
update_files_button = Button(label="Update Files", button_type="default", width=50)
update_files_button.on_click(reload_all_files)

auto_update_toggle_button = Toggle(label="Auto Update", button_type="default", width=50, active=True)
auto_update_toggle_button.on_click(toggle_auto_update)

unload_file_button = Button(label="Unload", button_type="danger", width=50)
unload_file_button.on_click(unload_file)

# files selection box
files_selector = Select(title="Files:", options=[""])
files_selector.on_change('value', change_data_selector)

# data selection box
data_selector = MultiSelect(title="Data:", options=[], size=12)
data_selector.on_change('value', select_data)

# x axis selection box
x_axis_selector_title = Div(text="""X Axis:""", height=10)
x_axis_selector = RadioButtonGroup(labels=x_axis_options, active=0)
x_axis_selector.on_click(change_x_axis)

# toggle second axis button
toggle_second_axis_button = Button(label="Toggle Second Axis", button_type="success")
toggle_second_axis_button.on_click(toggle_second_axis)

# averaging slider
# This data source is just used to communicate / trigger the real callback
averaging_slider_dummy_source = ColumnDataSource(data=dict(value=[]))
averaging_slider_dummy_source.on_change('data', update_averaging)
averaging_slider = Slider(title="Averaging window", start=1, end=101, step=10, callback_policy='mouseup')
Beispiel #31
0
def map_viz_tab():
    def plot_sensor_path(sensors, df_final):
        for i in sensors:
            #plotter.line(x=path_data.get_group(i).x, y=path_data.get_group(i).y, name=str(i), color="black")
            plotter.line(x=df_final['x'].loc[df_final['sid'] == i],
                         y=df_final['y'].loc[df_final['sid'] == i],
                         color="orange")

    def updateOverlays(attr, old, new):
        overlays = checkbox_btn_group.active
        if 0 in overlays:
            img_hsp = "https://upload.wikimedia.org/wikipedia/commons/d/d0/Flag_for_hospital_ship_of_the_Regia_Marina.svg"
            hospital_data = dict(url=[img_hsp] * 8,
                                 x=df_hospitalLocation.x,
                                 y=df_hospitalLocation.y)
            source_hospital.data = hospital_data

        else:
            h_x, h_y, h_url = [], [], []
            hospital_data = dict(url=h_url, x=h_x, y=h_y)
            source_hospital.data = hospital_data

        if 1 in overlays:
            x_patch, y_patch, x_vor_ls, y_vor_ls = get_voronoi(
                df_hospitalLocation.x, df_hospitalLocation.y)
            patch_data = dict(xs=x_patch, ys=y_patch)
            lines_data = dict(xs=x_vor_ls, ys=y_vor_ls)
            source_vor.data = patch_data
            source_vor_ls.data = lines_data
        else:
            x_patch, y_patch, x_vor_ls, y_vor_ls = [], [], [], []
            patch_data = dict(xs=x_patch, ys=y_patch)
            lines_data = dict(xs=x_vor_ls, ys=y_vor_ls)
            source_vor.data = patch_data
            source_vor_ls.data = lines_data

        if 2 in overlays:
            color_mapper = LinearColorMapper(palette=palette, low=0, high=50)
            mp.fill_color = {'field': 'Value', 'transform': color_mapper}
            mp.fill_alpha = 1.0
            tick_labels = {
                '0': '0',
                '10': '10',
                '20': '20',
                '30': '30',
                '40': '40',
                '50': '>50'
            }
            color_bar = ColorBar(color_mapper=color_mapper,
                                 label_standoff=5,
                                 width=500,
                                 height=20,
                                 border_line_color=None,
                                 location=(0, 0),
                                 orientation='horizontal',
                                 major_label_overrides=tick_labels)
            plotter.add_layout(color_bar, 'below')
        else:
            mp.fill_color = 'lightslategrey'
            mp.fill_alpha = 0.5

    def updateMotionMap(attr, old, new):
        check = checkbox_btn_group1.active
        xmin = -13326251
        if 0 in check:
            img_state = [
                "https://i.imgur.com/IBpHIs1.png",
                "https://i.imgur.com/oawpbdU.png",
                "https://i.imgur.com/kigeppa.png",
                "https://i.imgur.com/mHAUX9K.png",
                "https://i.imgur.com/pxbaYlp.png",
                "https://i.imgur.com/QNhcnRn.png",
                "https://i.imgur.com/GB2fRMS.png",
                "https://i.imgur.com/qXfEJNL.png",
                "https://i.imgur.com/QZOTgG9.png",
                "https://i.imgur.com/sE1U89y.png",
                "https://i.imgur.com/vAxwSQE.png",
                "https://i.imgur.com/yO1iOCw.png",
                "https://i.imgur.com/jFpJ2UQ.png",
                "https://i.imgur.com/lVkwzYH.png",
                "https://i.imgur.com/Q519cud.png",
                "https://i.imgur.com/FtnTUAC.png",
                "https://i.imgur.com/j2D9ud6.png",
                "https://i.imgur.com/Tb4tWI5.png",
                "https://i.imgur.com/yhl5nAX.png"
            ]
            states_x = [
                xmin - 28000, xmin - 22500, xmin - 16000, xmin - 12000,
                xmin - 22000, xmin - 22500, xmin - 1000, xmin - 6000,
                xmin - 14000, xmin - 8000, xmin - 4700, xmin - 4700,
                xmin - 9000, xmin - 16000, xmin - 19500, xmin - 19500,
                xmin - 14000, xmin - 13000, xmin - 16500
            ]
            states_y = [
                19000, 19700, 20000, 17000, 12000, 16000, 11000, 3800, 6000,
                5900, 7000, 10500, 12500, 17000, 17000, 13500, 9200, 13500,
                13000
            ]
            w = [
                4000, 5000, 4000, 4000, 4000, 4000, 3000, 4800, 5000, 2500,
                4000, 4000, 4000, 3000, 3000, 3000, 4300, 3500, 3500
            ]
            h = [
                3600, 3000, 3000, 3000, 3000, 3000, 6000, 4800, 3000, 5000,
                4000, 3000, 4000, 2000, 2000, 3000, 2700, 3500, 3500
            ]
            states_data = dict(url=img_state, x=states_x, y=states_y, w=w, h=h)
            source_states.data = states_data
        else:
            h_x, h_y, h_url, w, h = [], [], [], [], []
            states_data = dict(url=h_url, x=h_x, y=h_y, w=w, h=h)
            source_states.data = states_data

        if 1 in check:
            source_path1.data = data_path1
        else:
            source_path1.data = data_path

    def update_time_frame(attr, old, new):
        """ update the time frame and load new dataframes
        """
        timeline.value = time_start
        time_current = time_start
        if time_frame.active == 0:
            timeline.step = 5
            df_mobile_location_cur = df_mobileLocation_raw
            df_static_reading_cur = df_staticSensors_raw
            df_mobile_reading_cur = df_mobileSensors_raw

        if time_frame.active == 1:
            timeline.step = 60
            df_mobile_location_cur = df_mobileLocation_min
            df_static_reading_cur = df_staticSensors_min
            df_mobile_reading_cur = df_mobileSensors_min

        if time_frame.active == 2:
            timeline.step = 3600
            df_mobile_location_cur = df_mobileLocation_hr
            df_static_reading_cur = df_staticSensors_hr
            df_mobile_reading_cur = df_mobileSensors_hr

        static_sensors_update(attr, old, new)
        mobile_sensors_update(attr, old, new)

    def getDFfromDB(table_name):
        url = join(basename(split(dirname(__file__))[0]), 'data',
                   'StHimarkDB.db')
        conn = sq.connect(url)
        df = pd.read_sql_query("select * from " + table_name + ";", conn)
        return df

    def static_sensors_update(attr, old, new):
        time_current = datetime.fromtimestamp(timeline.value + baseTime)
        sid, x, y, val, user = [], [], [], [], []
        data_new = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'value': val,
            'user': user,
        })
        source_static.data = data_new
        #clean(source_static_locations)
        selected = static_sensors_ms.value
        selectedStaticSensors.clear()
        for name in selected:
            selectedStaticSensors.append(name.split("_", 1)[1])
        for sensor in selectedStaticSensors:
            sid.append(sensor)
            x.append(df_staticLocation['x'].loc[df_staticLocation['sid'] ==
                                                int(sensor)].values.item())
            y.append(df_staticLocation['y'].loc[df_staticLocation['sid'] ==
                                                int(sensor)].values.item())
            val.append(df_static_reading_cur[sensor].loc[
                df_static_reading_cur['Timestamp'] ==
                time_current].values.item())
            user.append('Sensor ' + sensor)

        data_new = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'value': val,
            'user': user,
        })
        source_static.data = data_new

    def mobile_sensors_update(attr, old, new):
        time_current = datetime.fromtimestamp(timeline.value + baseTime)
        greencar = 'https://i.imgur.com/YyWzmgH.png'
        selected = mobile_sensors_ms.value
        overlays = checkbox_btn_group.active
        path_flag = False
        if 3 in overlays:
            path_flag = True
        selectedMobileSensors.clear()
        sid,x,y,clr,val,user, x_path, y_path,img=[],[],[],[],[],[],[],[],[]

        data_new_mobile = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'colo': clr,
            'value': val,
            'user': user,
            'url': img,
        })
        source_mobile.data = data_new_mobile

        df_path = pd.DataFrame(columns=['sid', 'x', 'y'])
        data_path = collections.OrderedDict({
            'x_path': x_path,
            'y_path': y_path,
        })
        source_path.data = data_path
        safety_thresh = safe_threshold.value
        for name in selected:
            selectedMobileSensors.append(name.split("_", 1)[1])

        img = [greencar] * len(selectedMobileSensors)
        for sensor in selectedMobileSensors:
            col_x = '(\'x\', \'{}\')'.format(sensor)
            col_y = '(\'y\', \'{}\')'.format(sensor)
            sid.append(sensor)
            x.append(df_mobile_location_cur[col_x].loc[
                df_mobile_location_cur['Timestamp'] ==
                time_current].values.item())
            y.append(df_mobile_location_cur[col_y].loc[
                df_mobile_location_cur['Timestamp'] ==
                time_current].values.item())
            val1 = df_mobile_reading_cur[sensor].loc[
                df_mobile_reading_cur['Timestamp'] ==
                time_current].values.item()
            if val1 == None:
                val1 = 0
            user.append(df_mobileUsers['user'].loc[df_mobileUsers['sensor'] ==
                                                   int(sensor)].values.item())
            if int(float(val1)) > safety_thresh:
                clr.append('darkred')
                #clr.append(redcar)
            else:
                clr.append('lawngreen')
                #clr.append(greencar)
            val.append(val1)
            #clr.append(RGB( 255 - int(sensor), (int(sensor)*2)+50, int(sensor)*4))
            if path_flag == True:
                df_temp = pd.DataFrame()
                df_temp['x'] = df_mobile_location_cur[col_x]
                df_temp['y'] = df_mobile_location_cur[col_y]
                df_temp['sid'] = sensor
                df_path = df_path.append(df_temp)

        data_new_mobile = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'colo': clr,
            'value': val,
            'user': user,
            'url': img,
        })
        source_mobile.data = data_new_mobile

        if path_flag == True:
            data_path = collections.OrderedDict({
                'x_path': [
                    df_path['x'].loc[df_path['sid'] == i]
                    for i in selectedMobileSensors
                ],
                'y_path': [
                    df_path['y'].loc[df_path['sid'] == i]
                    for i in selectedMobileSensors
                ],
            })
            source_path.data = data_path
            #df_path.to_csv('total_path.csv', index=None, header=True)
        #path_data = df_path.groupby('sid')
        #plot_sensor_path(selectedMobileSensors, df_path)

    def update_safeT(attr, old, new):
        global safety_thresh
        safety_thresh = safe_threshold.value

    def update_time(attr, old, new):
        greencar = 'https://i.imgur.com/YyWzmgH.png'
        safety_thresh = safe_threshold.value
        time_current = datetime.fromtimestamp(timeline.value + baseTime)
        text = template.format(curTime=time_current)
        label.text = str(time_current)
        some_div.text = text
        sid, x, y, clr, val, user, img = [], [], [], [], [], [], []

        if 2 in checkbox_btn_group.active:
            radiationFlag = True
        else:
            radiationFlag = False
        if radiationFlag == True:
            time1 = str(time_current)
            new_data3 = json_data(time1)
            geosource.geojson = new_data3

        data_new_mobile = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'colo': clr,
            'value': val,
            'user': user,
            'url': img,
        })
        source_mobile.data = data_new_mobile

        data_new_static = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'value': val,
            'user': user,
        })
        source_static.data = data_new_static
        img = [greencar] * len(selectedMobileSensors)
        for sensor in selectedMobileSensors:
            col_x = '(\'x\', \'{}\')'.format(sensor)
            col_y = '(\'y\', \'{}\')'.format(sensor)
            sid.append(sensor)
            x.append(df_mobile_location_cur[col_x].loc[
                df_mobile_location_cur['Timestamp'] ==
                time_current].values.item())
            y.append(df_mobile_location_cur[col_y].loc[
                df_mobile_location_cur['Timestamp'] ==
                time_current].values.item())
            val1 = df_mobile_reading_cur[sensor].loc[
                df_mobile_reading_cur['Timestamp'] ==
                time_current].values.item()
            if val1 == None:
                val1 = 0
            if int(float(val1)) > safety_thresh:
                clr.append('red')
                #clr.append(redcar)
            else:
                clr.append('green')
                #clr.append(greencar)
            #clr.append(RGB( 255 - int(sensor), (int(sensor)*2)+50, int(sensor)*4))
            val.append(val1)
            user.append(df_mobileUsers['user'].loc[df_mobileUsers['sensor'] ==
                                                   int(sensor)].values.item())

        data_new_mobile = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'colo': clr,
            'value': val,
            'user': user,
            'url': img,
        })
        source_mobile.data = data_new_mobile
        x, y, val, user, sid = [], [], [], [], []
        for sensor in selectedStaticSensors:
            sid.append(sensor)
            x.append(df_staticLocation['x'].loc[df_staticLocation['sid'] ==
                                                int(sensor)].values.item())
            y.append(df_staticLocation['y'].loc[df_staticLocation['sid'] ==
                                                int(sensor)].values.item())
            val.append(df_static_reading_cur[sensor].loc[
                df_static_reading_cur['Timestamp'] ==
                time_current].values.item())
            user.append('Sensor ' + sensor)

        data_new_static = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'value': val,
            'user': user,
        })
        source_static.data = data_new_static

    def clearStaticSensors():
        sid, x, y, clr, val, user = [], [], [], [], [], []
        static_sensors_ms.value = []
        data_new_static = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'value': val,
            'user': user,
        })
        source_static.data = data_new_static

    def clearMobileSensors():
        mobile_sensors_ms.value = []
        sid,x,y,clr,val,user, x_path, y_path,img=[],[],[],[],[],[],[],[],[]
        data_path = collections.OrderedDict({
            'x_path': x_path,
            'y_path': y_path,
        })
        source_path.data = data_path
        data_new_mobile = collections.OrderedDict({
            'sid': sid,
            'x_loc': x,
            'y_loc': y,
            'colo': clr,
            'value': val,
            'user': user,
            'url': img,
        })
        source_mobile.data = data_new_mobile

    def animate_update():
        time_current = timeline.value + timeline.step
        if time_current > time_end:
            time_current = time_start
        timeline.value = time_current
        update_time(None, None, None)

    def animate():
        global callback_id
        if btn_animate.label == '► Play':
            btn_animate.label = '❚❚ Pause'
            callback_id = curdoc().add_periodic_callback(animate_update, 200)
        else:
            btn_animate.label = '► Play'
            curdoc().remove_periodic_callback(callback_id)

    def json_data(timestamp):
        #time = df_hour_data['Timestamp'].loc[df_hour_data['Timestamp'] ==timestamp]
        time1 = pd.to_datetime(timestamp)
        time1 = time1.replace(minute=0, second=0)
        timestamp = str(time1.to_pydatetime())
        df_temp = df_hour_data_group.get_group(timestamp)
        merged = stHimarkShape.merge(df_temp,
                                     left_on='Nbrhood',
                                     right_on='Neighbourhood',
                                     how='left')
        merged_json = json.loads(merged.to_json())
        return json.dumps(merged_json)

    #Declare all the variables and datasources here
    plotter, stHimarkShape, gsource = displayMap()
    selectedStaticSensors, selectedMobileSensors = [], []
    time_start = 0
    time_end = 1
    time_current = 0
    safety_thresh = 40
    #Get Dataframe for static sensors
    sid,x,y,clr,val,user,x_path, y_path, img=[],[],[],[],[],[],[],[],[]
    data_static = collections.OrderedDict({
        'sid': sid,
        'x_loc': x,
        'y_loc': y,
        'value': val,
    })
    source_static = ColumnDataSource(data=data_static)

    data_mobile = collections.OrderedDict({
        'sid': sid,
        'x_loc': x,
        'y_loc': y,
        'colo': clr,
        'value': val,
        'user': user,
        'url': img,
    })
    source_mobile = ColumnDataSource(data=data_mobile)

    data_path = collections.OrderedDict({
        'x_path': [x_path],
        'y_path': [y_path],
    })
    source_path = ColumnDataSource(data=data_path)

    url = join(basename(split(dirname(__file__))[0]), 'data', 'total_path.csv')
    df_tp = pd.read_csv(url)
    data_path1 = collections.OrderedDict({
        'x_path': [df_tp['x'].loc[df_tp['sid'] == i] for i in range(1, 51)],
        'y_path': [df_tp['y'].loc[df_tp['sid'] == i] for i in range(1, 51)],
    })
    source_path1 = ColumnDataSource(data=data_path)

    df_staticSensors_hr = getDFfromDB("StaticSensorReading_hours")
    df_mobileSensors_hr = getDFfromDB("MobileSensorReading_hours")

    df_staticSensors_min = getDFfromDB("StaticSensorReading_minutes")
    df_mobileSensors_min = getDFfromDB("MobileSensorReading_minutes")

    df_staticSensors_raw = getDFfromDB("StaticSensorReading_raw")
    df_mobileSensors_raw = getDFfromDB("MobileSensorReading_raw")

    df_staticLocation = getDFfromDB("StaticSensorLocation")
    df_hospitalLocation = getDFfromDB("HospitalLocation")
    df_mobileLocation_hr = getDFfromDB("MobileSensorLocation_hours")
    df_mobileLocation_min = getDFfromDB("MobileSensorLocation_minutes")
    df_mobileLocation_raw = getDFfromDB("MobileSensorLocation_raw")
    #read mobile sensor users data
    url = join(basename(split(dirname(__file__))[0]), 'data', 'mobileuser.csv')
    df_mobileUsers = pd.read_csv(url)

    df_mobileLocation_hr['Timestamp'] = pd.to_datetime(
        df_mobileLocation_hr['Timestamp'])
    df_mobileLocation_min['Timestamp'] = pd.to_datetime(
        df_mobileLocation_min['Timestamp'])
    df_mobileLocation_raw['Timestamp'] = pd.to_datetime(
        df_mobileLocation_raw['Timestamp'])
    df_mobileSensors_hr['Timestamp'] = pd.to_datetime(
        df_mobileSensors_hr['Timestamp'])
    df_mobileSensors_min['Timestamp'] = pd.to_datetime(
        df_mobileSensors_min['Timestamp'])
    df_mobileSensors_raw['Timestamp'] = pd.to_datetime(
        df_mobileSensors_raw['Timestamp'])
    df_staticSensors_hr['Timestamp'] = pd.to_datetime(
        df_staticSensors_hr['Timestamp'])
    df_staticSensors_min['Timestamp'] = pd.to_datetime(
        df_staticSensors_min['Timestamp'])
    df_staticSensors_raw['Timestamp'] = pd.to_datetime(
        df_staticSensors_raw['Timestamp'])

    df_static_reading_cur = df_staticSensors_raw
    df_mobile_reading_cur = df_mobileSensors_raw
    df_mobile_location_cur = df_mobileLocation_raw

    #Radiation Map
    url = join(basename(split(dirname(__file__))[0]), 'data',
               'df_hour_combined.csv')
    df_hour_data = pd.read_csv(url)
    df_hour_data_group = df_hour_data.groupby('Timestamp')
    time = df_hour_data['Timestamp'][0]
    df_first = df_hour_data_group.get_group(time)
    merged = stHimarkShape.merge(df_first,
                                 left_on='Nbrhood',
                                 right_on='Neighbourhood')

    static_list = [1, 4, 6, 9, 11, 12, 13, 14, 15]
    mobile_list = list(range(1, 51))
    static_sensor_list = {}
    mobile_sensor_list = {}
    for sensor in static_list:
        static_sensor_list.update({int(sensor): "Sensor_" + str(sensor)})
    for sensor in mobile_list:
        mobile_sensor_list.update({int(sensor): "Sensor_" + str(sensor)})
    static_sensor_list = collections.OrderedDict(
        sorted(static_sensor_list.items()))
    mobile_sensor_list = collections.OrderedDict(
        sorted(mobile_sensor_list.items()))

    df_staticSensors_hr['Timestamp'] = pd.to_datetime(
        df_staticSensors_hr['Timestamp'])
    time_start = df_staticSensors_hr['Timestamp'][0]
    time_current = time_start
    time_end = df_staticSensors_hr['Timestamp'][len(df_staticSensors_hr.index)
                                                - 1]
    baseTime = datetime.timestamp(time_start)
    time_start = datetime.timestamp(time_start) - baseTime
    time_end = datetime.timestamp(time_end) - baseTime

    #Widget: Multiselect --- Create two multiselect widgets for static and mobile sensors
    static_sensors_ms = MultiSelect(title="Static Sensors:",
                                    options=list(static_sensor_list.values()),
                                    height=250)
    static_sensors_ms.on_change('value', static_sensors_update)

    mobile_sensors_ms = MultiSelect(title="Mobile Sensors:",
                                    options=list(mobile_sensor_list.values()),
                                    height=250)
    mobile_sensors_ms.on_change('value', mobile_sensors_update)
    controls = WidgetBox(row([static_sensors_ms, mobile_sensors_ms]))
    btn_staticClear = Button(label='Clear Static Selection')
    btn_mobileClear = Button(label='Clear Mobile Selection')
    btn_staticClear.on_click(clearStaticSensors)
    btn_mobileClear.on_click(clearMobileSensors)
    btn_group = WidgetBox(
        row([
            WidgetBox(btn_staticClear, width=310),
            WidgetBox(btn_mobileClear, width=310)
        ]))
    overlay_div = Div(text='<b>Map Overlays</b>',
                      style={
                          'font-size': '120%',
                          'color': 'black'
                      })

    checkbox_btn_group = CheckboxButtonGroup(labels=[
        'Show Hospitals', 'Hospital Vornoi Map', 'Radiation Map', 'Trace Paths'
    ],
                                             active=[])
    checkbox_btn_group.on_change('active', updateOverlays)
    checkbox_btn_group1 = CheckboxButtonGroup(
        labels=['State Labels', 'Trace Motion Map'], active=[])
    checkbox_btn_group1.on_change('active', updateMotionMap)

    timeframe_div = Div(text='<b>Time Frame</b>',
                        style={
                            'font-size': '120%',
                            'color': 'black'
                        })
    time_frame = RadioButtonGroup(labels=["Raw", "By Minutes", "By Hours"],
                                  active=0,
                                  name="Sort By:")
    time_frame.on_change('active', update_time_frame)
    template = ("""
              <b>Timestamp: </b> <span class='number'>{curTime}</span>
              """)
    text = template.format(curTime=time_current)
    some_div = Div(text=text, style={'font-size': '100%', 'color': 'black'})
    timeline = Slider(title="",
                      value=0,
                      start=time_start,
                      end=time_end,
                      step=5)
    timeline.show_value = False
    timeline.on_change('value', update_time)
    btn_animate = Button(label='► Play', width=180)
    btn_animate.on_click(animate)

    x_patch, y_patch, x_vor_ls, y_vor_ls = [], [], [], []
    patch_data = dict(xs=x_patch, ys=y_patch)
    lines_data = dict(xs=x_vor_ls, ys=y_vor_ls)
    source_vor = ColumnDataSource()
    source_vor_ls = ColumnDataSource()
    source_vor.data = patch_data
    source_vor_ls.data = lines_data
    h_x, h_y, h_url = [], [], []
    hospital_data = dict(url=h_url, x=h_x, y=h_y)
    source_hospital = ColumnDataSource()
    source_hospital.data = hospital_data

    w_s, h_s = [], []
    states_data = dict(url=h_url, x=h_x, y=h_y, w=w_s, h=h_s)
    source_states = ColumnDataSource()
    source_states.data = states_data

    palette = brewer['RdYlGn'][5]
    color_mapper = LinearColorMapper(palette=palette, low=0, high=50)
    merged_json = json.loads(merged.to_json())
    json_data1 = json.dumps(merged_json)
    geosource = GeoJSONDataSource(geojson=json_data1)

    tick_labels = {
        '0': '0',
        '10': '10',
        '20': '20',
        '30': '30',
        '40': '40',
        '50': '>50'
    }
    color_bar = ColorBar(color_mapper=color_mapper,
                         label_standoff=5,
                         width=500,
                         height=20,
                         border_line_color=None,
                         location=(0, 0),
                         orientation='horizontal',
                         major_label_overrides=tick_labels)
    plotter.add_layout(color_bar, 'below')

    mp = Patches(xs='xs',
                 ys='ys',
                 fill_color='lightslategrey',
                 line_color="black",
                 line_width=0.05,
                 fill_alpha=0.5)
    plotter.add_glyph(geosource, mp)

    plotter.patches('xs',
                    'ys',
                    source=source_vor,
                    alpha=0.3,
                    line_width=1,
                    fill_color='lightslategrey',
                    line_color='black')
    plotter.multi_line('xs',
                       'ys',
                       source=source_vor_ls,
                       alpha=1,
                       line_width=1,
                       line_color='black')

    safe_threshold = Slider(title="Safety Threshold(In cpm)",
                            value=safety_thresh,
                            start=0,
                            end=100,
                            step=1)
    safe_threshold.on_change('value', update_safeT)
    layout = column(
        row([
            plotter,
            column([
                controls, btn_group, overlay_div, checkbox_btn_group,
                checkbox_btn_group1, timeframe_div, time_frame,
                row([some_div, btn_animate]), timeline, safe_threshold
            ])
        ]))

    glyph = MultiLine(xs="x_path",
                      ys="y_path",
                      line_color="saddlebrown",
                      line_width=0.8,
                      line_alpha=0.5)
    plotter.add_glyph(source_path1, glyph)
    glyph = MultiLine(xs="x_path",
                      ys="y_path",
                      line_color="darkred",
                      line_width=2,
                      line_alpha=0.9)
    plotter.add_glyph(source_path, glyph)

    image1 = ImageURL(url='url', x="x", y="y", w=600, h=600, anchor="center")
    plotter.add_glyph(source_hospital, image1)

    image_states = ImageURL(url='url',
                            x="x",
                            y="y",
                            w="w",
                            h="h",
                            anchor="center")
    plotter.add_glyph(source_states, image_states)

    label = Label(x=-13358000,
                  y=2000,
                  text=str(time_current),
                  text_font_size='20pt',
                  text_color='yellowgreen')
    plotter.add_layout(label)
    plotter.hex(name="static_hover",
                x='x_loc',
                y='y_loc',
                color="yellow",
                size=12,
                alpha=1,
                source=source_static)

    carimage = ImageURL(url='url',
                        x='x_loc',
                        y='y_loc',
                        w=600,
                        h=1100,
                        anchor='center')
    plotter.add_glyph(source_mobile, carimage)

    plotter.circle(name="dynamic_hover",
                   x='x_loc',
                   y='y_loc',
                   fill_color='colo',
                   line_color='colo',
                   size=6,
                   alpha=1,
                   source=source_mobile)
    hover = HoverTool(names=["static_hover", "dynamic_hover"],
                      tooltips=[("Sensor", "@sid"), ("Radiation", "@value"),
                                ("User", "@user")],
                      show_arrow=False)
    plotter.tools = [hover]
    #plotter.diamond(x='x', y ='y', color='green',size=15, source = source_hospital)
    img_path = "https://upload.wikimedia.org/wikipedia/commons/b/b8/Nuclear_plant.svg"
    plotter.image_url(url=[img_path],
                      x=-13334385.723761385,
                      y=18109.34344275895,
                      w=1000,
                      h=1000,
                      anchor='center')
    tab = Panel(child=layout, title='Visual Analysis')

    return tab