Skip to content

Commit

Permalink
Merge pull request #1077 from hschilling/P168758070-show-xy-values
Browse files Browse the repository at this point in the history
P168758070 Show x/y values for scatter plot training points
  • Loading branch information
swryan committed Oct 9, 2019
2 parents 416060c + 0903979 commit 0dcb180
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
1 change: 1 addition & 0 deletions openmdao/docs/conf.py
Expand Up @@ -127,6 +127,7 @@
'test_suite.scripts',
'vectors',
'utils',
'visualization',
]

if os.path.isfile("make_sourcedocs"):
Expand Down
14 changes: 14 additions & 0 deletions openmdao/docs/features/model_visualization/meta_model_basics.rst
Expand Up @@ -51,6 +51,20 @@ run :code:`openmdao view_mm unstructured_meta_model_example.py` in the command l
in depth overview of what :ref:`Unstructured <feature_MetaModelUnStructuredComp>` and :ref:`Structured <feature_MetaModelStructuredComp>`
metamodels are.

Multiple Meta Models in Script
-------------------------------------
If your model has multiple metamodels, you can specify which of them you want to visualize. For example, in this code
there are two metamodels.

.. embed-code::
../visualization/meta_model_viewer/tests/multiple_metamodels.py

To visualize only the first one, you would use the command:

.. code::
openmdao view_mm -m cos_mm multiple_metamodels.py
Command Line Interface
----------------------

Expand Down
Expand Up @@ -10,7 +10,7 @@
from bokeh.io import curdoc
from bokeh.layouts import row, column
from bokeh.plotting import figure
from bokeh.models import Slider, ColumnDataSource
from bokeh.models import Slider, ColumnDataSource, HoverTool
from bokeh.models import ColorBar, BasicTicker, LinearColorMapper, Range1d
from bokeh.models.widgets import TextInput, Select
from bokeh.server.server import Server
Expand Down Expand Up @@ -498,11 +498,24 @@ def _contour_data(self):
if len(data):
# Add training data points overlay to contour plot
data = np.array(data)
self.contour_training_data_source.data = dict(x=data[:, 0], y=data[:, 1])
self.contour_plot.circle(
if self.is_structured_meta_model:
self.contour_training_data_source.data = dict(x=data[:, 0], y=data[:, 1],
z=self.meta_model.training_outputs[
self.output_select.value].flatten())
else:
self.contour_training_data_source.data = dict(x=data[:, 0], y=data[:, 1],
z=self.meta_model._training_output[
self.output_select.value])

training_data_renderer = self.contour_plot.circle(
x='x', y='y', source=self.contour_training_data_source,
size=5, color='white', alpha=0.50)

self.contour_plot.add_tools(HoverTool(renderers=[training_data_renderer], tooltips=[
(self.x_input_select.value + " (train)", '@x'),
(self.y_input_select.value + " (train)", '@y'),
(self.output_select.value + " (train)", '@z'), ]))

return self.contour_plot

def _right_plot(self):
Expand Down Expand Up @@ -560,6 +573,14 @@ def _right_plot(self):
self.right_alphas = 1.0 - data[:, 2] / self.dist_range

# Training data scatter plot
scatter_renderer = right_plot_fig.scatter(x=data[:, 3], y=data[:, 1], line_color=None,
fill_color='#000000',
fill_alpha=self.right_alphas.tolist())

right_plot_fig.add_tools(HoverTool(renderers=[scatter_renderer], tooltips=[
(self.output_select.value + " (train)", '@x'),
(y_idx + " (train)", '@y'),
]))
right_plot_fig.scatter(x=data[:, 3], y=data[:, 1], line_color=None, fill_color='#000000',
fill_alpha=self.right_alphas.tolist())

Expand Down Expand Up @@ -627,8 +648,14 @@ def _bottom_plot(self):
self.bottom_alphas = 1.0 - data[:, 2] / self.dist_range

# Training data scatter plot
bottom_plot_fig.scatter(x=data[:, 0], y=data[:, 3], line_color=None, fill_color='#000000',
fill_alpha=self.bottom_alphas.tolist())
scatter_renderer = bottom_plot_fig.scatter(x=data[:, 0], y=data[:, 3], line_color=None,
fill_color='#000000',
fill_alpha=self.bottom_alphas.tolist())

bottom_plot_fig.add_tools(HoverTool(renderers=[scatter_renderer], tooltips=[
(x_idx + " (train)", '@x'),
(self.output_select.value + " (train)", '@y'),
]))

# Set the right_plot data source to new values
self.bottom_plot_scatter_source.data = dict(
Expand Down
@@ -0,0 +1,48 @@
import numpy as np
import openmdao.api as om


class CosMetaModel(om.MetaModelUnStructuredComp):
def setup(self):
# Training Data
x_train = np.linspace(0, 10, 20)
y_train = np.linspace(0, 20, 20)

# Inputs
self.add_input('x', 0., training_data=x_train)
self.add_input('y', 0., training_data=y_train)

# Outputs
self.add_output('cos_x', 0., training_data=np.cos(x_train + y_train))

# Surrogate Model
self.options['default_surrogate'] = om.ResponseSurface()


class SinMetaModel(om.MetaModelUnStructuredComp):
def setup(self):
# Training Data
x_train = np.linspace(0, 10, 20)
y_train = np.linspace(0, 20, 20)

# Inputs
self.add_input('x', 0., training_data=x_train)
self.add_input('y', 0., training_data=y_train)

# Outputs
self.add_output('sin_x', 0., training_data=np.sin(x_train + y_train))

# Surrogate Model
self.options['default_surrogate'] = om.ResponseSurface()


# define model with two metamodel components
model = om.Group()
cos_mm = model.add_subsystem('cos_mm', CosMetaModel())
sin_mm = model.add_subsystem('sin_mm', SinMetaModel())

# setup a problem using our dual metamodel model
prob = om.Problem(model)
prob.setup()
prob.final_setup()

0 comments on commit 0dcb180

Please sign in to comment.