From 5bf39c17686ca054b8c9934c8239f1c833160013 Mon Sep 17 00:00:00 2001 From: theonlydvr Date: Thu, 12 Dec 2024 09:33:28 -0600 Subject: [PATCH] Fixed local source loading and updated BayesOptSource for compatibility --- pybehave/Sources/BayesOptSource.py | 71 +++++++++++++++++--------- pybehave/Workstation/SettingsDialog.py | 4 +- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/pybehave/Sources/BayesOptSource.py b/pybehave/Sources/BayesOptSource.py index 38e4b8e..ec2bb15 100644 --- a/pybehave/Sources/BayesOptSource.py +++ b/pybehave/Sources/BayesOptSource.py @@ -106,7 +106,7 @@ def initialize(self): self.plots[datum["close"]].clf() plt.close(self.plots[datum["close"]]) plt.pause(0.005) - del self.plots[datum["close"]] + del self.plots[datum["close"]] del self.bayes_objs[datum["close"]] del self.metadata[datum["close"]] if datum["close"] in self.output_paths: @@ -179,28 +179,49 @@ def constants_updated(self, event: PybEvents.ConstantsUpdateEvent) -> None: self.metadata[cid].update(event.constants) def posterior_plot(self, component_id, num_steps=120): # Show plot in separate thread? - grid_x, grid_y = np.meshgrid(np.linspace(0, 1, num_steps), np.linspace(0, 1, num_steps), indexing='ij') - values = np.concatenate([grid_x.reshape(-1, 1), grid_y.reshape(-1, 1)], axis=1) - bounds = self.bayes_objs[component_id].bounds - grid_x = grid_x * (bounds[1][0] - bounds[0][0]) + bounds[0][0] - grid_y = grid_y * (bounds[1][1] - bounds[0][1]) + bounds[0][1] - fig = self.plots[component_id] - fig.clf() - for i in range(len(self.bayes_objs[component_id].metadata["outputs"])): - post = self.bayes_objs[component_id].predict(values, output_index=self.bayes_objs[component_id].metadata[ - "output_labels"].index(self.bayes_objs[component_id].metadata["outputs"][i])) - ax = fig.add_subplot(self.bayes_objs[component_id].metadata["plot_dims"][0], self.bayes_objs[component_id].metadata["plot_dims"][1], i + 1) - CS = ax.contourf(grid_x, grid_y, post.reshape((num_steps, num_steps)), 20) - y_data = np.asarray(self.bayes_objs[component_id].train_y[self.bayes_objs[component_id].metadata["outputs"][i]]) - x_data = [] - if self.bayes_objs[component_id].x_data is not None: - for key in self.bayes_objs[component_id].input_labels: - x_data.append(self.bayes_objs[component_id].train_x[key]) - x_data = np.atleast_2d(np.asarray(x_data)) - ax.scatter(x_data[0, :], x_data[1, :], c=y_data, edgecolors='k', norm=CS.norm) - ax.set_xlabel(self.bayes_objs[component_id].input_labels[0]) - ax.set_ylabel(self.bayes_objs[component_id].input_labels[1]) - # fig.subplots_adjust(right=0.9) - cbar = fig.colorbar(CS, ax=ax) - cbar.ax.set_title(self.bayes_objs[component_id].metadata["outputs"][i]) + if len(self.bayes_objs[component_id].input_labels) == 1: + grid_x = np.linspace(0, 1, num_steps) + bounds = self.bayes_objs[component_id].bounds + fig = self.plots[component_id] + fig.clf() + for i in range(len(self.bayes_objs[component_id].metadata["outputs"])): + post, ub, lb = self.bayes_objs[component_id].predict(grid_x, output_index=self.bayes_objs[component_id].metadata[ + "output_labels"].index(self.bayes_objs[component_id].metadata["outputs"][i]), std=True) + ax = fig.add_subplot(self.bayes_objs[component_id].metadata["plot_dims"][0], + self.bayes_objs[component_id].metadata["plot_dims"][1], i + 1) + ax.fill_between(grid_x * (bounds[1] - bounds[0]) + bounds[0], lb.squeeze(), ub.squeeze(), alpha=0.3) + ax.plot(grid_x * (bounds[1] - bounds[0]) + bounds[0], post.squeeze()) + y_data = np.asarray( + self.bayes_objs[component_id].train_y[self.bayes_objs[component_id].metadata["outputs"][i]]) + if self.bayes_objs[component_id].x_data is not None: + for key in self.bayes_objs[component_id].input_labels: + x_data = self.bayes_objs[component_id].train_x[key] + ax.scatter(x_data, y_data) + ax.set_xlabel(self.bayes_objs[component_id].input_labels[0]) + ax.set_ylabel(self.bayes_objs[component_id].output_labels[i]) + else: + grid_x, grid_y = np.meshgrid(np.linspace(0, 1, num_steps), np.linspace(0, 1, num_steps), indexing='ij') + values = np.concatenate([grid_x.reshape(-1, 1), grid_y.reshape(-1, 1)], axis=1) + bounds = self.bayes_objs[component_id].bounds + grid_x = grid_x * (bounds[1][0] - bounds[0][0]) + bounds[0][0] + grid_y = grid_y * (bounds[1][1] - bounds[0][1]) + bounds[0][1] + fig = self.plots[component_id] + fig.clf() + for i in range(len(self.bayes_objs[component_id].metadata["outputs"])): + post = self.bayes_objs[component_id].predict(values, output_index=self.bayes_objs[component_id].metadata[ + "output_labels"].index(self.bayes_objs[component_id].metadata["outputs"][i])) + ax = fig.add_subplot(self.bayes_objs[component_id].metadata["plot_dims"][0], self.bayes_objs[component_id].metadata["plot_dims"][1], i + 1) + CS = ax.contourf(grid_x, grid_y, post.reshape((num_steps, num_steps)), 20) + y_data = np.asarray(self.bayes_objs[component_id].train_y[self.bayes_objs[component_id].metadata["outputs"][i]]) + x_data = [] + if self.bayes_objs[component_id].x_data is not None: + for key in self.bayes_objs[component_id].input_labels: + x_data.append(self.bayes_objs[component_id].train_x[key]) + x_data = np.atleast_2d(np.asarray(x_data)) + ax.scatter(x_data[0, :], x_data[1, :], c=y_data, edgecolors='k', norm=CS.norm) + ax.set_xlabel(self.bayes_objs[component_id].input_labels[0]) + ax.set_ylabel(self.bayes_objs[component_id].input_labels[1]) + # fig.subplots_adjust(right=0.9) + cbar = fig.colorbar(CS, ax=ax) + cbar.ax.set_title(self.bayes_objs[component_id].metadata["outputs"][i]) plt.pause(0.005) diff --git a/pybehave/Workstation/SettingsDialog.py b/pybehave/Workstation/SettingsDialog.py index 81df983..490adc4 100644 --- a/pybehave/Workstation/SettingsDialog.py +++ b/pybehave/Workstation/SettingsDialog.py @@ -189,10 +189,12 @@ def __init__(self, sd: SettingsDialog): for f in pkgutil.iter_modules(pybehave.Sources.__path__): if not f.name == "Source" and not f.name == 'ThreadSource': self.sources.append(f.name) - for f in pkgutil.iter_modules(['Local.Sources']): + desktop = os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') + for f in pkgutil.iter_modules([f'{desktop}/py-behav/Local/Sources']): if f.name.endswith('Source'): self.local_sources.append(f.name) self.source.addItems(self.sources) + self.source.addItems(self.local_sources) source_box_layout.addWidget(self.source) self.layout.addWidget(source_box) self.layout.addWidget(self.control_buttons)