[35, 138, 10, 60], [85, 175, -20, 27], [-130, -25, -50, 25], ] t0 = time.time() i = 0 map, mapTitle = draw_map(region_groups[i], extents[i], geojson, country_shapes, df, labels[i]) cr = (dfid_topics[dfid_topics["DFID region"] == labels[i]].sort_values( 'ldev').reset_index()) bar = draw_bar(cr) header = dbc.Row( [ dbc.Col(html.Div(), ), dbc.Col(html.Div(html.H1("Climate and Health", className="mt-4")), ), dbc.Col(html.Div(), ) ], id="header", ) bar_buttons = [] for j, meta_topic in enumerate(meta_topics): bar_buttons.append( html.Button(meta_topic, id=f"barfilter-{j}",
def region_interaction(i, selectedData, clickData, relThreshold, bf_0, bf_1, bf_2, bf_3, bf_4, relayoutData, clearTopics, storeData): rel_ids = doc_df.loc[doc_df['prediction'] >= 1 - relThreshold, "id"] ctx = dash.callback_context if "region-select" in ctx.triggered[0]['prop_id']: relayoutData = None selectedData = None topic_selection = "No topics selected" place_ids = [] rel_df = None # did we just, or have we cleared topics without since clicking on them? if "clear-topics" in ctx.triggered[0]['prop_id']: storeData["topics"] = [] storeData["cleared"] = True # have we just clicked on a topic bar if "bar.clickData" in ctx.triggered[0]['prop_id']: storeData["cleared"] = False if clickData is not None: cd = clickData["points"][0]["label"] if cd in storeData["topics"]: storeData["topics"] = [ x for x in storeData["topics"] if x != cd ] else: storeData["topics"] += [cd] # Filter topics to just include those in the metacategories selected sub_topics = dfid_topics[dfid_topics["DFID region"] == labels[i]] topicids = set([]) for j, bf in enumerate([bf_0, bf_1, bf_2, bf_3, bf_4]): if bf is None: continue elif bf % 2 == 1: topicids = topicids | set(topic_df.loc[ topic_df['Aggregated meta-topic'] == meta_topics[j], "id"]) if len(topicids) > 0: sub_topics = sub_topics[sub_topics.topic_id.isin(topicids)] cr = (sub_topics.sort_values('ldev').reset_index()) if selectedData is not None: place_ids = [x["id"] for x in selectedData["points"] if "id" in x] if len(place_ids) == 1: place = df.loc[df['place_doc_id'] == place_ids[0]] place_ids = df.loc[(df['lat'] == place.lat.values[0]) & (df['lon'] == place.lon.values[0]), "place_doc_id"] docids = df.loc[df['place_doc_id'].isin(place_ids), "doc_id"] rel_df = table_df[(table_df['id'].isin(docids)) & (table_df['id'].isin(rel_ids))] else: docids = df.loc[df['DFID region'] == labels[i], "doc_id"] rel_df = table_df[(table_df["id"].isin(docids)) & (table_df['id'].isin(rel_ids))] sel_df = df sel_df["subset"] = 0 sel_df.loc[sel_df["id"].isin(rel_df["id"]), "subset"] = 1 gdt = (sel_df[["id", "subset"]].merge(dts, left_on="id", right_on="doc_id").groupby([ 'topic_id', 'subset' ])['score'].sum().reset_index()) # How many documents are there in each topic tns = (rel_df[["id"]].merge(dts, left_on="id", right_on="doc_id").query( 'score>0.01').groupby('topic_id')['score'].count().to_frame( name="count").sort_values('count').reset_index()) gdt['share'] = gdt['score'] / gdt.groupby('subset')['score'].transform( 'sum') gdt = gdt.merge(dt_sum) gdt['deviation'] = gdt['share'] / gdt['total_share'] gdt['ldev'] = np.log(gdt['deviation']) gdt = gdt[gdt['subset'] == 1] if len(topicids) > 0: gdt = gdt[gdt.topic_id.isin(topicids)] cr = (gdt.merge(topic_df, left_on="topic_id", right_on="id").merge(tns, how="left").sort_values('ldev')) if len(storeData["topics"]) > 0: topics = topic_df[topic_df["short_title"].isin( storeData["topics"])]["id"].values topic_selection = ", ".join(storeData["topics"]) for top in topics: rel_df = rel_df[(rel_df[top] > 0.01)] rel_df["topics"] = rel_df[topics].sum() rel_df = rel_df.sort_values("topics", ascending=False) if len(place_ids) > 0: place_ids = set(place_ids) & set( df.loc[df["doc_id"].isin(rel_df["id"]), "place_doc_id"]) else: place_ids = set(df.loc[df["doc_id"].isin(rel_df["id"]), "place_doc_id"]) cr = cr.reset_index(drop=True) sbar = cr.loc[cr["short_title"].isin(storeData["topics"])].index bar = draw_bar(cr, sbar) if "barfilter" not in ctx.triggered[0]['prop_id']: if relayoutData is not None: if "autosize" not in relayoutData: if "xaxis.range[0]" in relayoutData: bar.update_xaxes(range=[ relayoutData["xaxis.range[0]"], relayoutData["xaxis.range[1]"] ]) if "yaxis.range[0]" in relayoutData: bar.update_yaxes(range=[ relayoutData["yaxis.range[0]"], relayoutData["yaxis.range[1]"] ]) map, mapTitle = draw_map(region_groups[i], extents[i], geojson, country_shapes, df[df['id'].isin(rel_ids)], labels[i], place_ids) if rel_df is not None: rel_df = rel_df.to_dict('records') return bar, map, rel_df, topic_selection, storeData
'''How to install matplotlib ?''' from graphs import draw_multiline, draw_bar from CsvReader import CsvReader # draw_multiline() # csv_reader = CsvReader("movies.csv") # csv_reader.read_csv() #csv_reader.draw_films_by_years() # csv_reader.draw_films_by_profits() draw_bar()