for coord in X[labels == i]: location_map3.circle(x=coord[1], y=coord[0], size=10, color=cluster_pallet[i], fill_alpha=0.8) label_to_stations[i].append(coord_to_station[(coord[0], coord[1])]) # find the station from its coordinates and append it to the dictionary location_map3.plot_width = 700 location_map3.plot_height = 500 location_map3.sizing_mode = 'scale_width' # Risky rivers warning_text2 = Div( text="""<p><b>{}</b> rivers with stations at risk, the top 5 is tabulated below.</p>""".format( len(rivers_with_station(risky_stations))), width=600 ) warning_text2.sizing_mode = 'scale_width' risky_rivers = rivers_by_station_number(risky_stations, 5) risky_rivers_source = ColumnDataSource( data=dict(name=[i[0] for i in risky_rivers], num=[i[1] for i in risky_rivers]) ) risky_river_table_columns = [ TableColumn(field="name", title="River Name"), TableColumn(field="num", title="Number of Risky Stations"), ] risky_river_table = DataTable(source=risky_rivers_source, columns=risky_river_table_columns, width=500, height=140) risky_river_table.sizing_mode = 'scale_width' # Risky towns risky_towns = [] key_station_in_cluster = []
def generate_bokeh_umap(self, media_type): output_notebook() topics = [] labels = [] for key, value in self.top_words_map.items(): topics.append(value) labels.append(key) if len(labels) >= 20000: reducer = umap.UMAP(n_neighbors=100, metric='hellinger') if len(labels) >= 5000: reducer = umap.UMAP(n_neighbors=50, metric='hellinger') else: reducer = umap.UMAP(metric='hellinger') X = self.vectorized_out.copy() X_embedded = reducer.fit_transform(X) # tsne = TSNE(verbose=1, perplexity=100, random_state=42) # X = self.vectorized_out # X_embedded = tsne.fit_transform(X.toarray()) df_tmp = pd.DataFrame(self.doc_topic_dists) df_tmp['topic'] = df_tmp.idxmax(axis=1) y_labels = df_tmp['topic'].values y_labels_new = [] for i in y_labels: y_labels_new.append(labels[i]) df = self.original_df.copy() # data sources if media_type == 'videos': source = ColumnDataSource( data=dict(x=X_embedded[:, 0], y=X_embedded[:, 1], x_backup=X_embedded[:, 0], y_backup=X_embedded[:, 1], desc=y_labels, ids=df['id'], titles=df['title'], published_times=df['first_airing'], text=df['text'], publication_end_times=df['publication_end_time'], media_availables=df['media_available'], duration_minutes=df['duration_minutes'], finnpanel_genres=df['finnpanel_genre'], labels=["Topic " + str(x) for x in y_labels_new], links=df['link'])) # hover over information hover = HoverTool( tooltips=[ ("Id", "@ids{safe}"), ("Title", "@titles{safe}"), ("Published", "@published_times{safe}"), # ("Text", "@texts{safe}"), ("Publication ends", "@publication_end_times{safe}"), ("Currently available", "@media_availables{safe}"), ("Duration (minutes)", "@duration_minutes{safe}"), ("Finnpanel genres", "@finnpanel_genres{safe}"), ("Link", "@links") ], point_policy="follow_mouse") elif media_type == 'articles': source = ColumnDataSource( data=dict(x=X_embedded[:, 0], y=X_embedded[:, 1], x_backup=X_embedded[:, 0], y_backup=X_embedded[:, 1], desc=y_labels, ids=df.index, titles=df['title'], published_times=df['published_time'].dt.strftime( '%Y-%m-%d %H:%M'), text=df['text'], labels=["Topic " + str(x) for x in y_labels_new], links=df['link'])) # hover over information hover = HoverTool( tooltips=[ ("Id", "@ids{safe}"), ("Title", "@titles{safe}"), ("Published", "@published_times{safe}"), # ("Text", "@texts{safe}"), ("Link", "@links") ], point_policy="follow_mouse") # map colors mapper = linear_cmap(field_name='desc', palette=Category20[20], low=min(y_labels), high=max(y_labels)) # prepare the figure plot = figure(plot_width=1200, plot_height=850, tools=[ hover, 'pan', 'wheel_zoom', 'box_zoom', 'reset', 'save', 'tap' ], title="Clustering of the content with UMAP and NMF", toolbar_location="above") # plot settings plot.scatter('x', 'y', size=5, source=source, fill_color=mapper, line_alpha=0.3, line_color="black", legend='labels') plot.legend.background_fill_alpha = 0.6 # Keywords text_banner = Paragraph( text='Keywords: Slide to specific cluster to see the keywords.', height=45) input_callback_1 = input_callback(plot, source, text_banner, topics, self.nr_of_topics) # currently selected article div_curr = Div( text="""Click on a plot to see the link to the article.""", height=150) if media_type == 'videos': callback_selected = CustomJS(args=dict(source=source, current_selection=div_curr), code=selected_code_videos()) elif media_type == 'articles': callback_selected = CustomJS(args=dict(source=source, current_selection=div_curr), code=selected_code_articles()) taptool = plot.select(type=TapTool) taptool.callback = callback_selected # WIDGETS slider = Slider( start=0, end=self.nr_of_topics, value=self.nr_of_topics, step=1, title="Topic #") #, js_event_callbacks=input_callback_1) slider.js_on_change("value", input_callback_1) keyword = TextInput( title="Search:") #, js_event_callbacks=input_callback_1) keyword.js_on_change("value", input_callback_1) # pass call back arguments input_callback_1.args["text"] = keyword input_callback_1.args["slider"] = slider # STYLE slider.sizing_mode = "stretch_width" slider.margin = 15 keyword.sizing_mode = "scale_both" keyword.margin = 15 div_curr.style = { 'color': '#BF0A30', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '1.1em' } div_curr.sizing_mode = "scale_both" div_curr.margin = 20 text_banner.style = { 'color': '#0269A4', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '1.1em' } text_banner.sizing_mode = "scale_both" text_banner.margin = 20 plot.sizing_mode = "scale_both" plot.margin = 5 r = row(div_curr, text_banner) r.sizing_mode = "stretch_width" # LAYOUT OF THE PAGE l = layout([ [slider, keyword], [text_banner], [div_curr], [plot], ]) l.sizing_mode = "scale_both" # show output_file('t-sne_interactive_streamlit.html') show(l) return (l)
keyword = TextInput(title="Search:", callback=input_callback_1) # pass call back arguments input_callback_1.args["text"] = keyword input_callback_1.args["slider"] = slider # STYLE slider.sizing_mode = "stretch_width" slider.margin=15 keyword.sizing_mode = "scale_both" keyword.margin=15 div_curr.style={'color': '#BF0A30', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '1.1em'} div_curr.sizing_mode = "scale_both" div_curr.margin = 20 text_banner.style={'color': '#0269A4', 'font-family': 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '1.1em'} text_banner.sizing_mode = "scale_both" text_banner.margin = 20 plot.sizing_mode = "scale_both" plot.margin = 5 r = row(div_curr,text_banner) r.sizing_mode = "stretch_width" # LAYOUT OF THE PAGE
def make_time_by_product(): title = "US Exports to China of " + level_select.value foobar = make_trade_time(timedf, level_select.value) start_range = dt.datetime(2017, 7, 1) #end_range = dt.datetime(2021,8,1) numlines = len(foobar.columns) multi_line_source = ColumnDataSource({ 'xs': [foobar.index.values] * numlines, 'ys': [foobar[name].values for name in foobar.columns], 'label': [name for name in foobar.columns], 'line_color': ["crimson"] }) p = figure(plot_height=height, plot_width=width, x_axis_type="datetime", toolbar_location='below', tools="box_zoom, reset, pan, xwheel_zoom", title=title, x_range=(start_range, end_range)) p.multi_line(xs="xs", ys="ys", line_width=3.5, line_alpha=0.5, line_color="line_color", hover_line_alpha=0.75, hover_line_width=5, hover_line_color="crimson", source=multi_line_source) TIMETOOLTIPS = """ <div style="background-color:#F5F5F5; opacity: 0.95; border: 5px 5px 5px 5px;"> <div style = "text-align:left;"> <span style="font-size: 13px; font-weight: bold"> @label </span> </div> <div style = "text-align:left;">""" TIMETOOLTIPS = TIMETOOLTIPS + """ <span style="font-size: 13px; font-weight: bold"> $data_x{%b %Y}: $data_y{$0.0a}</span> </div> </div> """ p.add_tools( HoverTool(tooltips=TIMETOOLTIPS, line_policy='nearest', formatters={'$data_x': 'datetime'})) p.title.text_font_size = '13pt' p.background_fill_color = background p.background_fill_alpha = 0.75 p.border_fill_color = background tradewar_box = BoxAnnotation(left=dt.datetime(2018, 7, 1), right=dt.datetime(2019, 10, 11), fill_color='red', fill_alpha=0.1) p.add_layout(tradewar_box) tradewar_box = BoxAnnotation(left=dt.datetime(2020, 1, 1), right=dt.datetime(2021, 12, 31), fill_color='blue', fill_alpha=0.1) p.add_layout(tradewar_box) #p.yaxis.axis_label = p.yaxis.axis_label_text_font_style = 'bold' p.yaxis.axis_label_text_font_size = "13px" p.yaxis.minor_tick_line_color = None p.yaxis.formatter = NumeralTickFormatter(format="($0. a)") div0 = Div( text= """Red marks the period of Section 301 tariffs and retaliation. Blue is period of agreement. """, width=555, background=background) p.outline_line_color = None p.sizing_mode = "scale_both" div0.sizing_mode = "scale_both" p.toolbar.active_drag = None #p.WheelZoomTool.maintain_focus = False #print(p.y_range.end) p = column(p, div0, sizing_mode="scale_both", max_height=height, max_width=width, min_height=int(0.25 * height), min_width=int(0.25 * width)) return p
def make_cum_purchase(): title = "Cummulative US Exports to China of " + level_select.value foobar = make_trade_time(timedf, level_select.value) cuml = cum_trade(foobar) x = cuml.index y2017 = cuml["cuml_trade_2017"] y2020 = cuml["cuml_trade_2020"] p = figure(x_axis_type="datetime", plot_height=height, plot_width=width, toolbar_location='below', tools="box_zoom, reset, pan", title=title, x_range=(dt.datetime(2020, 1, 1), dt.datetime(2020, 12, 15))) p.line(x=x, y=y2017, line_width=3.5, line_alpha=0.5, line_color="crimson", line_dash="dashed", legend_label="2017") p.line(x=x, y=y2020, line_width=3.5, line_alpha=0.75, line_color="darkblue", legend_label="2020") singlesource2020 = ColumnDataSource({ 'xs': x.values, 'ys': y2020.values, "dates": np.array(x), }) c2020 = p.circle(x="xs", y="ys", size=35, source=singlesource2020, color="crimson", alpha=0.0) singlesource2017 = ColumnDataSource({ 'xs': x.values, 'ys': y2017.values, "dates": np.array(pd.date_range(start="2017-01-01", end="2017-12-01", freq="MS")), }) c2017 = p.circle(x="xs", y="ys", size=35, source=singlesource2017, color="darkblue", alpha=0.0) TIMETOOLTIPS = """ <div style="background-color:#F5F5F5; opacity: 0.95; border: 15px 15px 15px 15px;"> <div style = "text-align:left;">""" TIMETOOLTIPS = TIMETOOLTIPS + """ <span style="font-size: 13px; font-weight: bold"> @dates{%b %Y}: $data_y{$0.0a}</span> </div> </div> """ p.add_tools( HoverTool(tooltips=TIMETOOLTIPS, line_policy='nearest', formatters={'@dates': 'datetime'}, renderers=[c2017, c2020])) p.legend.title = 'Cumulative Purchases' p.legend.location = "top_left" p.legend.title_text_font_style = "bold" p.xaxis.formatter = DatetimeTickFormatter(months=['%B']) p.title.text_font_size = '13pt' p.background_fill_color = background p.background_fill_alpha = 0.75 p.border_fill_color = background tradewar_box = BoxAnnotation(left=dt.datetime(2018, 7, 1), right=dt.datetime(2019, 10, 11), fill_color='red', fill_alpha=0.1) p.add_layout(tradewar_box) tradewar_box = BoxAnnotation(left=dt.datetime(2020, 1, 1), right=dt.datetime(2021, 12, 31), fill_color='blue', fill_alpha=0.1) p.add_layout(tradewar_box) #p.yaxis.axis_label = p.yaxis.axis_label_text_font_style = 'bold' p.yaxis.axis_label_text_font_size = "13px" p.yaxis.minor_tick_line_color = None p.yaxis.formatter = NumeralTickFormatter(format="($0. a)") div0 = Div( text= """Each line displays the cumulative purchases through the calendar month of the selected product category for the years 2017, 2020, and 2021. """, width=555, background=background) p.outline_line_color = None p.sizing_mode = "scale_both" div0.sizing_mode = "scale_both" p.toolbar.active_drag = None #p.WheelZoomTool.maintain_focus = False #print(p.y_range.end) p = column(p, div0, sizing_mode="scale_both", max_height=height, max_width=width, min_height=int(0.25 * height), min_width=int(0.25 * width)) return p
def make_bar_chart(): source, goal_met = make_source(df, level_select.value) if chart_select.value == 'Overview 2021': title = " 2021 Overview of Purchase Commitments for " + level_select.value else: title = " 2020 Overview of Purchase Commitments for " + level_select.value p = figure(plot_height=height, plot_width=width, title=title, toolbar_location='below', tools="reset") p.vbar(x="position", top=level_select.value, width=0.6, color="colors", alpha=0.75, hatch_pattern=" ", hatch_alpha=0.10, source=source, legend_field="name") ########################################################################## TIMETOOLTIPS = """ <div style="background-color:#F5F5F5; opacity: 0.95; border: 0px 0px 0px 0px"> <div style = "text-align:left;"> <span style="font-size: 13px; font-weight: bold">@name:</span> </div> <div style = "text-align:left;"> <span style="font-size: 13px; font-weight: bold">$@hover_label Billion</span> </div> </div> """ p.add_tools(HoverTool(tooltips=TIMETOOLTIPS)) ########################################################################## #p.ygrid.grid_line_color = None p.xgrid.grid_line_color = None p.title.text_font_size = '13pt' p.xaxis.major_tick_line_color = None # turn off x-axis major ticks p.xaxis.minor_tick_line_color = None # turn off x-axis minor ticks p.xaxis.major_label_text_font_size = '0pt' # turn off x-axis tick labels p.yaxis.formatter = NumeralTickFormatter(format="($0. a)") p.yaxis.minor_tick_line_color = None p.y_range.start = 0 p.y_range.end = 200000000000 mytext = Label(x=605, y=370, text='''China's progress towards''', text_font_size="1.2em", text_font_style="bold", x_units='screen', y_units='screen') p.add_layout(mytext) mytext = Label(x=605, y=350, text='meeting commitments:', text_font_size="1.2em", text_font_style="bold", x_units='screen', y_units='screen') p.add_layout(mytext) mytext = Label(x=605, y=300, text=goal_met + '%', text_font_size="3em", text_font_style="bold", x_units='screen', y_units='screen') p.add_layout(mytext) p.border_fill_color = background p.legend.orientation = "horizontal" p.legend.background_fill_color = background p.legend.location = "top_left" p.legend.background_fill_alpha = 0.10 p.legend.label_text_font_size = "1em" p.background_fill_color = background p.background_fill_alpha = 0.75 p.toolbar.autohide = True p.outline_line_color = None p.sizing_mode = "scale_both" p.max_height = height p.max_width = width p.min_height = int(0.25 * height) p.min_width = int(0.25 * width) div0 = Div( text="""Offical Text from the <b>ECONOMIC AND TRADE AGREEMENT<b>""", width=555, background=background, style={ "justify-content": "space-between", "display": "flex" }) div0.sizing_mode = "scale_both" div1 = Div( text= """<b>Chapter 6 (page 6-1), Article 6.2: Trade Opportunities.<b>""", width=555, background=background, style={ "justify-content": "space-between", "display": "flex" }) div1.sizing_mode = "scale_both" if level_select.value == 'All Phase One Products': div2 = Div( text= """During the two-year period from January 1, 2020 through December 31, 2021, China shall ensure that purchases and imports into China from the United States of the manufactured goods, agricultural goods, energy products, ... exceed the corresponding 2017 baseline amount by no less than $200 billion ($64 billion in calandar year 2020);""", width=555, background=background, style={ "justify-content": "space-between", "display": "flex" }) #div2.sizing_mode= "scale_both" if level_select.value == 'Manufactures': div2 = Div( text= """(a) For the category of manufactured goods identified in Annex 6.1, no less than $32.9 billion above the corresponding 2017 baseline amount is purchased and imported into China from the United States in calendar year 2020, and no less than $44.8 billion above the corresponding 2017 baseline amount is purchased and imported into China from the United States in calendar year 2021;""", width=555, background=background, style={ "justify-content": "space-between", "display": "flex" }) #div2.sizing_mode= "scale_both" if level_select.value == 'Energy': div2 = Div( text= """(c) For the category of energy products identified in Annex 6.1, no less than $18.5 billion above the corresponding 2017 baseline amount is purchased and imported into China from the United States in calendar year 2020, and no less than $33.9 billion above the corresponding 2017 baseline amount is purchased and imported into China from the United States in calendar year 2021;""", width=555, background=background, style={ "justify-content": "space-between", "display": "flex" }) #div2.sizing_mode= "scale_both" if level_select.value == 'Agriculture': div2 = Div( text= """(b) For the category of agricultural goods identified in Annex 6.1, no less than $12.5 billion above the corresponding 2017 baseline amount is purchased and imported into China from the United States in calendar year 2020, and no less than $19.5 billion above the corresponding 2017 baseline amount is purchased and imported into China from the United States in calendar year 2021;""", width=555, background=background, style={ "justify-content": "space-between", "display": "flex" }) #div2.sizing_mode= "scale_both" p = column(p, div0, div1, div2, sizing_mode="scale_both", max_height=height, max_width=width, min_height=int(0.25 * height), min_width=int(0.25 * width)) return p
title = Paragraph(text= 'M25 DBFO: Average Hourly Traffic Profiles', height=30) title.style={'color': 'black', 'font-weight': 'bold', 'font-family': \ 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '2em'} title.sizing_mode = "scale_width" title.margin = 20 presentation = Div(text="""If you require any further information, please contact <a href="mailto:[email protected]">[email protected]</a>.<br> <br> Click on legend entries to hide the corresponding lines.<br> <b>*AADT :</b> Annual Average Daily Traffic """) presentation.style={'color': 'black', 'font-family': \ 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '1.1em'} presentation.sizing_mode = "scale_width" presentation.margin = 20 text_banner2 = Paragraph(text= 'M25 : Clockwise (LEFT) versus Anti-Clockwise (RIGHT)', height=30) text_banner2.style={'color': '#0269A4', 'font-family': \ 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '2em'} text_banner2.sizing_mode = "scale_width" text_banner2.margin = 20 text_banner3 = Paragraph(text= 'Link Roads : Northbound/Westbound (LEFT) versus Southbound/Eastbound (RIGHT)', height=30) text_banner3.style={'color': '#0269A4', 'font-family': \ 'Helvetica Neue, Helvetica, Arial, sans-serif;', 'font-size': '2em'} text_banner3.sizing_mode = "scale_width" text_banner3.margin = 20