Skip to content

Commit

Permalink
Fixed local source loading and updated BayesOptSource for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
theonlydvr committed Dec 12, 2024
1 parent 1feb65d commit 5bf39c1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
71 changes: 46 additions & 25 deletions pybehave/Sources/BayesOptSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
4 changes: 3 additions & 1 deletion pybehave/Workstation/SettingsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5bf39c1

Please sign in to comment.