def check_text_properties(model, prefix="", font_size='16px', baseline='bottom', font_style='normal', align="left", scalar=False): text_font = getattr(model, prefix + "text_font") text_font_size = getattr(model, prefix + "text_font_size") text_font_style = getattr(model, prefix + "text_font_style") text_color = getattr(model, prefix + "text_color") text_alpha = getattr(model, prefix + "text_alpha") text_align = getattr(model, prefix + "text_align") text_baseline = getattr(model, prefix + "text_baseline") if scalar: assert text_font == "helvetica" assert text_font_size == font_size assert text_font_style == font_style assert text_color == "#444444" assert text_alpha == 1.0 assert text_align == align assert text_baseline == baseline else: assert text_font == value("helvetica") assert text_font_size == value(font_size) assert text_font_style == font_style assert text_color == "#444444" assert text_alpha == 1.0 assert text_align == align assert text_baseline == baseline
def test_HasProps_update() -> None: c = Child() c.update(**dict(lst2=[1,2], str2="baz", int1=25, ds1=value("foo"))) assert c.int1 == 25 assert c.ds1 == value("foo") assert c.lst1 == [] assert c.int2 == None assert c.str2 == "baz" assert c.ds2 == field("y") assert c.lst2 == [1,2]
def applianceUsagePattern(start, end, appliance): tmp = applianceTimeUsage(appliance, buildings_count, start, end) tmp = tmp.loc[start:end] tmp = (tmp['active'] / total['active']).values result = np.nan_to_num(tmp, copy=False).tolist() fruits = timeData years = [appliance] colors = ["#718dbf"] data = {'fruits': fruits, appliance: result} p = figure(x_range=fruits, plot_height=750, plot_width=1750, title=appliance + " 소비 패턴", toolbar_location=None, tools="hover", tooltips="$name @fruits: @$name") p1 = p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data, legend=[value(x) for x in years]) p.y_range.start = 0 p.x_range.range_padding = 0.5 p.xgrid.grid_line_color = None p.axis.minor_tick_line_color = None p.outline_line_color = None p.legend.location = "top_left" p.legend.orientation = "horizontal" output_file("html/" + appliance + " 소비 패턴.html") show(p)
def applianceLocationUsageVisualization(buildingNum, start, end): # data print(start, end) t2 = applianceLocationUsage(buildingNum, start, end) print(t2) # appliances = buildingAppliances[buildingNum] p1 = figure(x_range=t2['timestamp'], plot_height=700, plot_width=1500, title="지역별 평균 활동", toolbar_location=None, tools="hover", tooltips="$name @timestamp: @$name") legendKeyTmp = t2.keys() legendTmp = [] for k in legendKeyTmp: if k == 'timestamp': continue legendTmp.append(k) if len(legendTmp) > 20: colors = Category20c[20][:len(legendTmp)] else: colors = Category20c[len(legendTmp)] print(len(legendTmp)) print(len(colors)) p1.vbar_stack(legendTmp, x='timestamp', width=0.3, color=colors, source=t2, legend=[value(x) for x in legendTmp]) p1.y_range.start = 0 p1.y_range.end = 1.0 p1.x_range.range_padding = 0.5 p1.xgrid.grid_line_color = None p1.axis.minor_tick_line_color = None p1.outline_line_color = None p1.legend.location = "top_left" p1.legend.orientation = "vertical" p1.legend.click_policy = "hide" script_bok, div_bok = components(p1) return render_template('update_content.html', div_bok=div_bok, script_bok=script_bok)
def draw_2d_chart(idx, x, cluster_output_path, k): driver = webdriver.Chrome(os.path.join(BASE_DIR, "chromedriver"), options=opts) tsne = TSNE(random_state=42) points = tsne.fit_transform(x) t_df = pd.DataFrame(points, index=range(len(x)), columns=["x", "y"]) t_df["cluster_no"] = idx colors = [colormap[x] for x in t_df["cluster_no"]] t_df["color"] = colors plot_data = ColumnDataSource(data=t_df.to_dict(orient="list")) p = figure( # title='TSNE Twitter BIO Embeddings', plot_width=1200, plot_height=1200, active_scroll="wheel_zoom", output_backend="svg", ) p.add_tools(HoverTool(tooltips="@title")) p.circle( source=plot_data, x="x", y="y", line_alpha=0.9, fill_alpha=0.9, size=8, # size="radius", fill_color="color", line_color="color", ) p.title.text_font_size = value("16pt") p.xaxis.visible = True p.yaxis.visible = True p.background_fill_color = None p.border_fill_color = None p.grid.grid_line_color = None p.outline_line_color = None # tsne_plot.grid.grid_line_color = None # tsne_plot.outline_line_color = None p.toolbar.logo = None p.toolbar_location = None export_svg( p, filename=os.path.join(cluster_output_path, f"cluster2d-{k}.svg"), webdriver=driver, ) export_png( p, filename=os.path.join(cluster_output_path, f"cluster2d-{k}.png"), webdriver=driver, )
def sentiment_of_the_day(self, doc_id, db_name): # retrieve data from db data = self.retrieve_data(doc_id=doc_id, db_name=db_name) original_data = data['data'] time_list = list(original_data.keys()) sorted_original_data = {} for time, info in original_data.items(): sorted_item = {} if info: sorted_item['neg'] = info['neg'] sorted_item['neu'] = info['neu'] sorted_item['pos'] = info['pos'] sorted_original_data[time] = sorted_item dict_list = list(sorted_original_data.values()) bar_data_dict = {} bar_data_dict['time'] = time_list bar_data_dict['neg'] = [] bar_data_dict['neu'] = [] bar_data_dict['pos'] = [] for time, info in sorted_original_data.items(): for k, v in info.items(): bar_data_dict_item = {} bar_data_dict_item[k] = v if k == 'neg': bar_data_dict['neg'].append(v) if k == 'neu': bar_data_dict['neu'].append(v) if k == 'pos': bar_data_dict['pos'].append(v) sentiment_list = list(dict_list[0].keys()) colors = ["#7FB290", "#C4DE9F", "#F3B8AD"] p = figure(x_range=time_list, plot_height=450, plot_width=666, title="Sentiments of the Day", tools="hover, pan, save, reset", tooltips="$name @time: @$name") p.vbar_stack(sentiment_list, x='time', width=0.4, color=colors, source=bar_data_dict, legend=[value(x) for x in sentiment_list]) p.y_range.start = 0 p.x_range.range_padding = 0.1 p.yaxis.formatter = NumeralTickFormatter(format="00") p.xgrid.grid_line_color = None p.axis.minor_tick_line_color = None p.outline_line_color = None p.legend.location = "top_right" p.legend.orientation = "horizontal" script, div = components(p) return script, div
def appliancesTimeUsage(d, start, end, buildings_total): total = total_usage_per_hour(buildings_total, False).loc[start:end] timeData = total.index.strftime('%H').tolist() appliances = [ "fridge", "light", "microwave", "electric oven", "washer dryer", "dish washer", "electric space heater" ] data = {'timestamp': timeData} for a in appliances: tmp = applianceTimeUsage(a, buildings_total, start, end) tmp = tmp.loc[start:end] tmp = (tmp['active'] / total['active']).values data[a] = np.nan_to_num(tmp, copy=False).tolist() tmpTotal = [0] * len(timeData) for appliance in appliances: for l in range(len(timeData)): tmpTotal[l] += data[appliance][l] for appliance in appliances: for l in range(len(timeData)): data[appliance][l] = data[appliance][l] / tmpTotal[l] colors = Pastel2[len(appliances)] p = figure(x_range=data['timestamp'], plot_height=750, plot_width=1750, title="시간별 평균 활동(전국)", toolbar_location=None, tools="hover", tooltips="$name @timestamp: @$name") p.vbar_stack(appliances, x='timestamp', width=0.5, color=colors, source=data, legend=[value(x) for x in appliances]) p.y_range.start = 0 p.y_range.end = 1.0 p.x_range.range_padding = 0.5 p.xgrid.grid_line_color = None p.axis.minor_tick_line_color = None p.outline_line_color = None p.legend.location = "top_left" p.legend.orientation = "vertical" p.legend.click_policy = "hide"
def test_value_function() -> None: assert bcpd.value("foo") == dict(value="foo") assert bcpd.value("foo", "junk") == dict(value="foo", transform="junk") assert bcpd.value("foo", transform="junk") == dict(value="foo", transform="junk")
def write_cross_chart(df, cluster_output_path, k): height = 1600 width = 1600 driver = webdriver.Chrome(os.path.join(BASE_DIR, "chromedriver"), options=opts) x = df.c3.to_list() y = df.c1.to_list() clusters = df.cluster.to_list() plot = figure( # title='TSNE Twitter BIO Embeddings', plot_width=width, plot_height=height, active_scroll="wheel_zoom", # x_range=r, # y_range=r, output_backend="svg", ) plot.add_tools(HoverTool(tooltips="@title")) new_x = [] new_y = [] for coord in zip(x, y): x_coord, y_coord = coord x_rand = random.uniform(-(0.5**0.5), 0.5**0.5) y_rand_range = (0.5 - x_rand**2)**0.5 y_rand = random.uniform(-y_rand_range, y_rand_range) new_x.append(x_coord + x_rand) new_y.append(y_coord + y_rand) colors = [colormap[clusters[i]] for i in range(len(new_y))] source = ColumnDataSource(data={"x": new_x, "y": new_y, "color": colors}) plot.scatter( source=source, x="x", y="y", line_alpha=0.6, fill_alpha=0.6, size=10, color="color", ) # size # count_map = defaultdict(int) # for coord in zip(x, y): # count_map[coord] += 1 * 0.5 # source = ColumnDataSource( # data={ # "x": [k[0] for k in count_map.keys()], # "y": [k[1] for k in count_map.keys()], # "size": list(count_map.values()), # } # ) # plot.scatter( # source=source, # x="x", # y="y", # line_alpha=0.6, # fill_alpha=0.6, # size="size", # ) plot.yaxis.axis_label_text_font_size = "25pt" plot.yaxis.major_label_text_font_size = "25pt" plot.xaxis.axis_label_text_font_size = "25pt" plot.xaxis.major_label_text_font_size = "25pt" plot.title.text_font_size = value("32pt") plot.xaxis.visible = True # plot.xaxis.bounds = (0, 0) plot.yaxis.visible = True label_opts1 = dict( x_offset=0, y_offset=750, text_font_size="30px", ) msg1 = "C1" caption1 = Label(text=msg1, **label_opts1) label_opts2 = dict( x_offset=0, y_offset=-750, text_font_size="30px", ) msg2 = "-C1" caption2 = Label(text=msg2, **label_opts2) label_opts3 = dict( x_offset=750, y_offset=0, text_font_size="30px", ) msg3 = "C3" caption3 = Label(text=msg3, **label_opts3) label_opts4 = dict( x_offset=-750, y_offset=0, text_font_size="30px", ) msg4 = "-C3" caption4 = Label(text=msg4, **label_opts4) plot.add_layout(caption1, "center") plot.add_layout(caption2, "center") plot.add_layout(caption3, "center") plot.add_layout(caption4, "center") plot.background_fill_color = None plot.border_fill_color = None plot.grid.grid_line_color = None plot.outline_line_color = None plot.yaxis.fixed_location = 0 plot.xaxis.fixed_location = 0 plot.toolbar.logo = None plot.toolbar_location = None export_svg( plot, filename=os.path.join(cluster_output_path, f"cross-{k}.svg"), webdriver=driver, height=height, width=width, ) export_png( plot, filename=os.path.join(cluster_output_path, f"cross-{k}.png"), webdriver=driver, height=height, width=width, )
def show_server_utilization(self): import pandas as pd from bokeh.core.property.dataspec import value from bokeh.io import output_file, show from bokeh.layouts import gridplot from bokeh.plotting import figure from bokeh.palettes import Category20 # Plot of time_slots utilization: 4 barcharts where each bar can contain up to 12 colors. Colors are according to kind of tasks running on server # we have 12 kinds of tasks (number of columns in H) and 4 time_slots (number of rows in H) # if specific task (j) can run on the specific server (k) then we have H[k,j] > 0 # otherwise H[k,j] == 0 and we cannot run specific task on specific server # U is a (16,len(t)-1) matrix where we interesting only on first (12,len(t)-1) part # U[j,n] * H[k,j] indicate how many capacity of server k took task j at time period t[n]...t[n+1] # we need for each server k create barchart where width of bar is length of time period # and total height is sum(U[n,j] * H[k,j]) for all j this height splitted by different colors according to j (up to 12) #self.extract_final_solution() number_of_buffers = self.formulation.K number_of_servers = self.formulation.I time_horizon = 150 number_of_time_slots = len(self.t) - 1 output_file('server_utilization.html') tasks = [ 'task ' + str(i) for i in range(1, len(self.formulation.H[0]) + 1) ] new_legend_tasks = {} new_t = np.zeros(2 * number_of_time_slots) new_t[0] = self.t[1] / 2 new_t[1:-1] = np.repeat(self.t[1:-1], 2) new_t[-1] = self.t[-1] data = {'t': new_t} new_matrix = np.zeros((number_of_buffers, 2 * number_of_time_slots)) p = {} network_graph_tasks_indices = [] network_graph_server_indices = [] network_graph_tasks_server_hash = {} max_y_value = 1 for k in range(number_of_servers): # servers for j in range(number_of_buffers): # tasks for ti in range(0, number_of_time_slots): # time slices new_matrix[j, 2 * ti] = self._u[j, ti] * self.formulation.H[k, j] new_matrix[j, 2 * ti + 1] = self._u[j, ti] * self.formulation.H[k, j] if self.formulation.H[k, j] > 0: new_legend_tasks[j] = 'task ' + str(j + 1) network_graph_tasks_indices.append(j + 1) network_graph_server_indices.append(len(tasks) + k + 1) network_graph_tasks_server_hash[j + 1] = self.formulation.H[k, j] data['task ' + str(j + 1)] = new_matrix[j].tolist() df = pd.DataFrame(data) p[k] = figure(x_range=(0, time_horizon * 1.2), y_range=(0, max_y_value), plot_width=self.plot_width, plot_height=self.plot_height, title='Server ' + str(k) + ' Utilization') p[k].varea_stack(stackers=tasks, x='t', color=Category20[number_of_buffers], legend=[value(x) for x in tasks], source=df) # reverse the legend entries to match the stacked order for j in reversed(range(number_of_buffers)): if self.formulation.H[k, j] == 0: del p[k].legend[0].items[j] p[k].legend[0].items.reverse() grid = gridplot([[p[0], p[1]], [p[2], p[3]]]) show(grid) return None
def draw_vectors(): driver = webdriver.Chrome(os.path.join(BASE_DIR, "chromedriver"), options=options) df = pd.read_csv(os.path.join(OUTPUTS_DIR, "normalized_future_vectors.csv")) filter_list = { (5, 6), (5, 7), (6, 7), (8, 9), (8, 10), (9, 10), (11, 12), (11, 13), (12, 13), } comb = list(combinations(range(5, len(df.columns)), 2)) comb = [c for c in comb if c not in filter_list] for idx, coord in enumerate(comb, 1): x, y = coord X = df[df.columns[x]].to_list() Y = df[df.columns[y]].to_list() tsne_df = pd.DataFrame(zip(X, Y), index=range(len(X)), columns=["x_coord", "y_coord"]) tsne_df["title"] = df["title"].to_list() tsne_df["cluster_no"] = df["cluster"].to_list() colormap = {3: "#ffee33", 2: "#00a152", 1: "#2979ff", 0: "#d500f9"} # colormap = {3: "#bdbdbd", 2: "#bdbdbd", 1: "#bdbdbd", 0: "#d500f9"} # colormap = {3: "#bdbdbd", 2: "#bdbdbd", 1: "#2979ff", 0: "#bdbdbd"} # colormap = {3: "#bdbdbd", 2: "#00a152", 1: "#bdbdbd", 0: "#bdbdbd"} # colormap = {3: "#ffee33", 2: "#bdbdbd", 1: "#bdbdbd", 0: "#bdbdbd"} only_one_cluster = pd.DataFrame(tsne_df.loc[tsne_df.cluster_no == 3]) colors = [colormap[x] for x in only_one_cluster["cluster_no"]] only_one_cluster["color"] = colors plot_data = ColumnDataSource(data=only_one_cluster.to_dict( orient="list")) plot = figure( # title='TSNE Twitter BIO Embeddings', plot_width=1600, plot_height=1600, active_scroll="wheel_zoom", output_backend="svg", x_range=(-1.1, 1.1), y_range=(-1.1, 1.1), ) plot.add_tools(HoverTool(tooltips="@title")) plot.circle( source=plot_data, x="x_coord", y="y_coord", line_alpha=0.6, fill_alpha=0.6, size=20, fill_color="color", line_color="color", ) plot.yaxis.axis_label_text_font_size = "25pt" plot.yaxis.major_label_text_font_size = "25pt" plot.xaxis.axis_label_text_font_size = "25pt" plot.xaxis.major_label_text_font_size = "25pt" start_x, end_x = df.columns[x].split("|") start_y, end_y = df.columns[y].split("|") start_x = start_x.strip() end_x = end_x.strip() start_y = start_y.strip() end_y = end_y.strip() plot.title.text_font_size = value("32pt") plot.xaxis.visible = True # plot.xaxis.bounds = (0, 0) plot.yaxis.visible = True label_opts1 = dict( x_offset=0, y_offset=750, text_font_size="30px", ) msg1 = end_y caption1 = Label(text=msg1, **label_opts1) label_opts2 = dict( x_offset=0, y_offset=-750, text_font_size="30px", ) msg2 = start_y caption2 = Label(text=msg2, **label_opts2) label_opts3 = dict( x_offset=600, y_offset=0, text_font_size="30px", ) msg3 = end_x caption3 = Label(text=msg3, **label_opts3) label_opts4 = dict( x_offset=-750, y_offset=0, text_font_size="30px", ) msg4 = start_x caption4 = Label(text=msg4, **label_opts4) plot.add_layout(caption1, "center") plot.add_layout(caption2, "center") plot.add_layout(caption3, "center") plot.add_layout(caption4, "center") plot.background_fill_color = None plot.border_fill_color = None plot.grid.grid_line_color = None plot.outline_line_color = None plot.yaxis.fixed_location = 0 plot.xaxis.fixed_location = 0 plot.toolbar.logo = None plot.toolbar_location = None print(idx) export_svg( plot, filename=f"svgs/{idx}.svg", webdriver=driver, height=1600, width=1600, ) export_png( plot, filename=f"pngs/{idx}.png", webdriver=driver, height=1600, width=1600, )
def test_value_function(): assert bcpd.value("foo") == dict(value="foo") assert bcpd.value("foo", "junk") == dict(value="foo", transform="junk") assert bcpd.value("foo", transform="junk") == dict(value="foo", transform="junk")
def calculate_cluster_number(): driver = webdriver.Chrome( os.path.join(BASE_DIR, "chromedriver"), options=opts ) df = pd.read_csv(os.path.join(H_IN_DIRS, "happiness.csv")) x = [] for row in df.iterrows(): idx, r = row temp = [] for c in df.columns[1:-1]: temp.append(r[c]) x.append(temp) ok = 0 kmax = 10 maximum = 0 for k in range(2, kmax + 1): kmeans = KMeans(n_clusters=k) fit = kmeans.fit(x) labels = fit.labels_ score = silhouette_score(x, labels, metric="euclidean") idx = kmeans.fit_predict(x) tsne = TSNE(random_state=42) points = tsne.fit_transform(x) t_df = pd.DataFrame(points, index=range(len(x)), columns=["x", "y"]) t_df["cluster_no"] = idx colormap = { 0: "#f44336", 1: "#673ab7", 2: "#9c27b0", 3: "#e91e63", 4: "#3f51b5", 5: "#2196f3", 6: "#03a9f4", 7: "#00bcd4", 8: "#009688", 9: "#cddc39", } colors = [colormap[x] for x in t_df["cluster_no"]] t_df["color"] = colors plot_data = ColumnDataSource(data=t_df.to_dict(orient="list")) p = figure( # title='TSNE Twitter BIO Embeddings', plot_width=1200, plot_height=1200, active_scroll="wheel_zoom", output_backend="svg", ) p.add_tools(HoverTool(tooltips="@title")) p.circle( source=plot_data, x="x", y="y", line_alpha=0.9, fill_alpha=0.9, # size="radius", fill_color="color", line_color="color", ) p.title.text_font_size = value("16pt") p.xaxis.visible = True p.yaxis.visible = True p.background_fill_color = None p.border_fill_color = None p.grid.grid_line_color = None p.outline_line_color = None # tsne_plot.grid.grid_line_color = None # tsne_plot.outline_line_color = None p.toolbar.logo = None p.toolbar_location = None export_svg( p, filename=f"cluster-number{k}.svg", webdriver=driver, ) export_png( p, filename=f"cluster-number{k}.png", webdriver=driver, ) if score > maximum: maximum = score ok = k print(ok)