From 393b1fbb8f213c294e3ad3120667a7e9b83f1dfd Mon Sep 17 00:00:00 2001 From: BillMakwae <91693504+BillMakwae@users.noreply.github.com> Date: Wed, 20 Oct 2021 13:15:28 -0700 Subject: [PATCH 1/3] Added Bokeh dependencies and implemented Bokeh --- simulation/main/MainSimulation.py | 56 ++++++++++++++----------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/simulation/main/MainSimulation.py b/simulation/main/MainSimulation.py index 18b19924..c4171909 100644 --- a/simulation/main/MainSimulation.py +++ b/simulation/main/MainSimulation.py @@ -17,6 +17,11 @@ from simulation.config import settings_directory from simulation.main.SimulationResult import SimulationResult +from bokeh.plotting import figure, show +from bokeh.layouts import gridplot +from bokeh.models import HoverTool + +from bokeh.palettes import Bokeh8 @@ -279,42 +284,31 @@ def __plot_graph(self, arrays_to_plot, array_labels, graph_title): """ compress_constant = int(self.timestamps.shape[0] / 5000) - sns.set_style("whitegrid") - - # Wow I used the walrus operator here! - if (num_arrays := len(arrays_to_plot)) == 1: - f, axes = plt.subplots() - t = np.arange(0, len(arrays_to_plot[0])) - - axes.plot(t, arrays_to_plot[0]) - - axes.set(xlabel='time (s)', ylabel=array_labels[0], - title=graph_title) - axes.grid() - plt.show() - return - elif (num_arrays / 2) % 2 == 0: - f, axes = plt.subplots(int(num_arrays / 2), 2, figsize=(12, 8)) - else: - f, axes = plt.subplots(int(num_arrays), 1, figsize=(12, 8)) - for index, array in enumerate(arrays_to_plot): arrays_to_plot[index] = array[::compress_constant] + + figures = list() + + hover_tool = HoverTool() + hover_tool.formatters = {"x": "datetime"} + hover_tool.tooltips = [ + ("time", "$x"), + ("data", "$y") + ] + + for (index, data_array) in enumerate(arrays_to_plot): + # create figures and put them in list + figures.append(figure(title=array_labels[index], x_axis_label="Time (hr)", + y_axis_label=array_labels[index], x_axis_type="datetime")) + + # add line renderers to each figure + figures[index].line(self.timestamps[::compress_constant], data_array, line_color=Bokeh8[index], line_width=2) - f.suptitle(f"{graph_title} ({self.race_type})", fontsize=16, weight="bold") + figures[index].add_tools(hover_tool) - with tqdm(total=len(arrays_to_plot), file=sys.stdout, desc="Plotting data") as pbar: - for index, axis in enumerate(axes.flatten()): - df = pd.DataFrame(dict(time=self.timestamps[::compress_constant] / 3600, value=arrays_to_plot[index])) - g = sns.lineplot(x="time", y="value", data=df, ax=axis) - g.set(xlabel="time (hrs)", ylabel=array_labels[index]) - pbar.update(1) - print() + grid = gridplot(figures, sizing_mode="scale_both", ncols=3, plot_height=200, plot_width=300) - sns.despine() - _ = plt.setp(axes) - _ = plt.tight_layout() - _ = plt.show() + show(grid) def __run_simulation_calculations(self, speed_kmh, verbose=False): """ From 2ac41e19e142ba3c6d0ae803c7ac433c6219c997 Mon Sep 17 00:00:00 2001 From: BillMakwae <91693504+BillMakwae@users.noreply.github.com> Date: Mon, 25 Oct 2021 09:22:23 -0700 Subject: [PATCH 2/3] Optimised result creates new tab --- simulation/main/MainSimulation.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/simulation/main/MainSimulation.py b/simulation/main/MainSimulation.py index c4171909..a8e60b8d 100644 --- a/simulation/main/MainSimulation.py +++ b/simulation/main/MainSimulation.py @@ -2,6 +2,7 @@ import json import sys import os +from bokeh.io.doc import curdoc from dotenv import load_dotenv import matplotlib.pyplot as plt @@ -17,7 +18,7 @@ from simulation.config import settings_directory from simulation.main.SimulationResult import SimulationResult -from bokeh.plotting import figure, show +from bokeh.plotting import figure, show, output_file from bokeh.layouts import gridplot from bokeh.models import HoverTool @@ -256,7 +257,7 @@ def optimize(self, *args, **kwargs): self.__plot_graph(arrays_to_plot.arrays, ["Optimized speed array", "Distance (km)", "SOC (%)", "Delta energy (J)", "Solar irradiance (W/m^2)", "Wind speeds (km/h)", "Elevation (m)", "Cloud cover (%)"], - "Simulation Result") + "Optimized Result") return optimizer.max @@ -308,6 +309,10 @@ def __plot_graph(self, arrays_to_plot, array_labels, graph_title): grid = gridplot(figures, sizing_mode="scale_both", ncols=3, plot_height=200, plot_width=300) + output_file(filename=graph_title + '.html', title=graph_title) + curdoc().add_root + curdoc().title = graph_title + show(grid) def __run_simulation_calculations(self, speed_kmh, verbose=False): From bb55c7fb2717e77657050d28919aa14e466d817f Mon Sep 17 00:00:00 2001 From: BillMakwae <91693504+BillMakwae@users.noreply.github.com> Date: Sun, 31 Oct 2021 17:02:28 -0700 Subject: [PATCH 3/3] Changed time scale and documentation --- simulation/main/MainSimulation.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/simulation/main/MainSimulation.py b/simulation/main/MainSimulation.py index a8e60b8d..2feed4ef 100644 --- a/simulation/main/MainSimulation.py +++ b/simulation/main/MainSimulation.py @@ -279,8 +279,7 @@ def __plot_graph(self, arrays_to_plot, array_labels, graph_title): graph_title: A string that serves as the plot's main title Result: - If number of plots is even, produces a 2 x (len(arrays_to_plot) / 2) plot - If number of plots is odd, produces a 1 x len(arrays_to_plot) plot + Produces a 3 x ceil(len(arrays_to_plot) / 3) plot """ compress_constant = int(self.timestamps.shape[0] / 5000) @@ -303,15 +302,13 @@ def __plot_graph(self, arrays_to_plot, array_labels, graph_title): y_axis_label=array_labels[index], x_axis_type="datetime")) # add line renderers to each figure - figures[index].line(self.timestamps[::compress_constant], data_array, line_color=Bokeh8[index], line_width=2) + figures[index].line(self.timestamps[::compress_constant] * 1000 , data_array, line_color=Bokeh8[index], line_width=2) figures[index].add_tools(hover_tool) grid = gridplot(figures, sizing_mode="scale_both", ncols=3, plot_height=200, plot_width=300) output_file(filename=graph_title + '.html', title=graph_title) - curdoc().add_root - curdoc().title = graph_title show(grid)