I'm just get stared at OpenMDAO and multidisciplinary optimization, I'm going to use OpenMDAO to build a CO(Collaborative Optimization) framwork on the classical Sellar problem.
However, when I ran this code I always get the following error:
TypeError: _init_sys_data() missing 1 required positional argument: 'probdata'
which occurs when running the problem.setup()
I've no idear what does this error mean since there's no difference of this line of code to the OpenMDAO standard tut.
Could somebody give me some advices?
Here's the code, some hints from this question
Sellar problem formed as CO:
SystemOpt
min x1**2 + z2 + y1 + eye(-y2)
w.r.t z1, z2, x1, y1, y2
s.t (z1 - z1_d1)**2 + (x1 - x1_d1)**2 + (z2 - z2_d1)**2 + 0.2 * (y2 - y2_d1) <= epsilon
(y2 - y2_d2)**2 + (y1 - y1_d2)**2 + (z1 - z1_d2)**2 + (z2 - z2_d2)**2 <= epsilon
-10 <= z1 <= 10
0 <= z2 <= 10
0 <= x1 <= 10
SobOpt1
min (z1 - z1_d1)**2 + (x1 - x1_d1)**2 + (z2 - z2_d1)**2 + 0.2 * (y2 - y2_d1)
w.r.t z1_d1, x1_d1, z2_d1, y2_d1
s.t z1_d1**2 + x1_d1 + z2_d1 - 0.2 * y2_d1 >= 3.16
0 <= x1_d1 <=10
-10 <= z1_d1 <= 10
0 <= z2_d1 <= 10
SubOpt2
min (y2 - y2_d2)**2 + (y1 - y1_d2)**2 + (z1 - z1_d2)**2 + (z2 - z2_d2)**2
w.r.t y2_d2, y1_d2, z1_d2, z2_d2
s.t y1_d2 ** 0.5 + z1_d2 + z2_d2 <= 24
-10 <= z1_d2 <= 10
0 <= z2_d2 <= 10
==================================================
from __future__ import print_function
import numpy as np
from openmdao.api import ExecComp, IndepVarComp, Group
from openmdao.api import Component, ScipyOptimizer
class Discipline1(Component):
"""Component containing Discipline 1."""
def __init__(self):
super(Discipline1, self).__init__()
self.add_param('z1_d1', val=0.0)
self.add_param('x1_d1', val=0.)
self.add_param('z2_d1', val=1.0)
self.add_param('y2_d1', val=1.0)
self.add_param('z1', val=1.0)
self.add_param('x1', val=1.0)
self.add_param('z2', val=1.0)
self.add_param('y2', val=1.0)
self.add_param('obj1', val=1.0)
def solve_nonlinear(self, params, unknowns, resids):
"""
Evaluates the equation
y1 = z1**2 + z2 + x1 - 0.2*y2
"""
z1_d1 = params['z']
z2_d1 = params['z2_d1']
x1_d1 = params['x1_d1']
y2_d1 = params['y2_d1']
z1 = params['z1']
x1 = params['x2']
z2 = params['z2']
y2 = params['y2']
unknowns['obj1'] = (z1 - z1_d1) ** 2 + (x1 - x1_d1) ** 2 + (z2 - z2_d1) ** 2 + 0.2 * (y2 - y2_d1)
class SubOpt1(Component):
def __init__(self):
"""
Sobopt of discipline1
"""
super(SubOpt1, self).__init__()
self.add_param('S1px', IndepVarComp('x1_d1', 1.0), promotes=['x1_d1'])
self.add_param('S1pz', IndepVarComp('z1_d1', 5.0), promotes=['z1_d1'])
self.add_param('S1pz', IndepVarComp('z2_d1', 5.0), promotes=['z2_d1'])
self.add_param('S1py2', IndepVarComp('y2_d1', 2.0), promotes=['y2_d1'])
# Add Problem
from openmdao.api import Problem
self.problem = prob = Problem()
group = prob.root = Group()
# add Component:
group.add('Discipline', Discipline1,
promotes=['x1', 'z1', 'x2', 'y2', 'x1_d1', 'z1_d1', 'x2_d2', 'y2_d2', 'obj1'])
# Add Cons
group.add('con', ExecComp('con = z1_d1**2 + x1_d1 + z2_d1 - 0.2 * y2_d1'),
promotes=['con', 'z1_d1', 'x1_d1', 'z2_d1', 'y2_d1'])
# Add Solver
prob.driver = ScipyOptimizer()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['tol'] = 1.0e-8
# Add desvar
prob.driver.add_desvar('x1_d1', lower=0.0, upper=10.0)
prob.driver.add_desvar('z1_d1', lower=-10.0, upper=10.0)
prob.driver.add_desvar('z2_d1', lower=0.0, upper=10.0)
prob.driver.add_desvar('y2_d1')
# Add obj and cons
prob.driver.add_objective('obj1')
prob.driver.add_constraint('con', lower=3.16)
prob.setup()
def solve_nonlinear(self, params=None, unknowns=None, resids=None, metadata=None):
self.problem.run()
unknowns['obj1'] = self.problem['obj1']
class Discipline2(Component):
"""Component containing Discipline 1."""
def __init__(self):
super(Discipline2, self).__init__()
self.add_param('z1_d2', val=0.0)
self.add_param('z2_d2', val=0.)
self.add_param('y1_d2', val=1.0)
self.add_param('y2_d2', val=1.0)
self.add_param('z1', val=1.0)
self.add_param('y1', val=1.0)
self.add_param('z2', val=1.0)
self.add_param('y2', val=1.0)
# add objs
self.add_param('obj2', val=1.0)
def solve_nonlinear(self, params, unknowns, resids):
"""
Evaluates the equation
y1 = z1**2 + z2 + x1 - 0.2*y2
"""
z1_d2 = params['z1_d2']
z2_d2 = params['z2_d2']
y1_d2 = params['y1_d2']
y2_d2 = params['y2_d2']
z1 = params['z1']
y1 = params['y2']
z2 = params['z2']
y2 = params['y2']
unknowns['obj2'] = (y2 - y2_d2) ** 2 + (y1 - y1_d2) ** 2 + (z1 - z1_d2) ** 2 + (z2 - z2_d2) ** 2
class SubOpt2(Component):
def __init__(self):
"""
Subopt of discipline2
"""
super(SubOpt2, self).__init__()
# Add Desvar
self.add_param('S2pz', IndepVarComp('z1_d2', 5.0), promotes=['z1_d2'])
self.add_param('S2pz', IndepVarComp('z2_d2', 5.0), promotes=['z2_d2'])
self.add_param('S2py1', IndepVarComp('y1_d2', 2.0), promotes=['y1_d2'])
self.add_param('S2py2', IndepVarComp('y2_d2', 5.0), promotes=['y2_d2'])
# Add problem
from openmdao.api import Problem
self.problem = prob = Problem()
group = prob.root = Group()
# Add Component:
group.add('Discipline2', Discipline2,
promotes=['obj2', 'y2', 'y2_d2', 'y1', 'y1_d2', 'z1', 'z1_d2', 'z2', 'z2_d2'])
# Add cons:
group.add('con2', ExecComp('con = y1_d2 ** 0.5 + z1_d2 + z2_d2'),
promotes=['con', 'y1_d2', 'z1_d2', 'z2_d2'])
# Add solver:
prob.driver = ScipyOptimizer()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['tol'] = 1.0e-8
# Add desvar
prob.driver.add_desvar('y1_d2')
prob.driver.add_desvar('y2_d2')
prob.driver.add_desvar('z1_d2', lower=-10, upper=10)
prob.driver.add_desvar('z2_d2', lower=0.0, upper=10.0)
# Add ovj and cons
prob.driver.add_objective('obj2')
prob.driver.add_constraint('con2', upper=24)
prob.setup()
def solve_nonlinear(self, params=None, unknowns=None, resids=None, metadata=None):
self.problem.run()
class System_Opt(Group):
"""
Group containing the Sellar MDA. This version uses the disciplines
with derivatives."""
def __init__(self):
super(System_Opt, self).__init__()
self.add('px', IndepVarComp('x1', 1.0), promotes=['x1'])
self.add('pz1', IndepVarComp('z1', 5.0), promotes=['z1'])
self.add('pz2', IndepVarComp('z2', 5.0), promotes=['z2'])
self.add('py1', IndepVarComp('y1', 2.0), promotes=['y1'])
self.add('py2', IndepVarComp('y2', 2.0), promotes=['y2'])
self.add('obj_cmp', ExecComp('obj = x1**2 + z2 + y1 + eye(-y2)',
z2=0.0, x1=0.0, y1=0.0, y2=0.0),
promotes=['obj', 'z2', 'x1', 'y1', 'y2'])
self.add('SubOpt1', SubOpt1, promotes=['z1_d1', 'x1_d1', 'z2_d1', 'y2_d1', 'x1', 'z1', 'x2', 'y2'])
self.add('SubOpt2', SubOpt2, promotes=['y2_d2', 'y1_d2', 'z1_d2', 'z2_d2', 'y2', 'y1', 'z1', 'z2'])
self.add('con_cmp1',
ExecComp('con1 = (z1 - z1_d1)**2 + (x1 - x1_d1)**2 + (z2 - z2_d1)**2 + 0.2 * (y2 - y2_d1)'),
promotes=['con1', 'z1', 'z1_d1', 'x1', 'x1_d1', 'z2', 'z2_d1', 'y2', 'y2_d1'])
self.add('con_cmp2',
ExecComp('con2 = (y2 - y2_d2)**2 + (y1 - y1_d2)**2 + (z1 - z1_d2)**2 + (z2 - z2_d2)**2'),
promotes=['con2', 'y2', 'y2_d2', 'y1', 'y1_d2', 'z1', 'z1_d2', 'z2', 'z2_d2'])
if __name__ == '__main__':
epsilon = 1e-5
from openmdao.api import Problem, ScipyOptimizer
# Add problem
top = Problem()
top.root = System_Opt()
# Add solver
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['tol'] = 1.0e-8
# Add desvar
top.driver.add_desvar('x1', lower=0.0, upper=10.0)
top.driver.add_desvar('z1', lower=-10.0, upper=10.0)
top.driver.add_desvar('z2', lower=0.0, upper=10.0)
top.driver.add_desvar('y2')
top.driver.add_desvar('y1')
# Add obj and cons
top.driver.add_objective('obj')
top.driver.add_constraint('con1', upper=epsilon)
top.driver.add_constraint('con2', upper=epsilon)
top.setup()
# add init params of desvar
top['x1'] = 1.0
top['z1'] = 1.0
top['z2'] = 1.0
top['y2'] = 1.0
top['y1'] = 1.0
top.run()
The problem you are having is related to the following lines:
self.add('SubOpt1', SubOpt1, promotes=['z1_d1', 'x1_d1', 'z2_d1', 'y2_d1', 'x1', 'z1', 'x2', 'y2'])
self.add('SubOpt2', SubOpt2, promotes=['y2_d2', 'y1_d2', 'z1_d2', 'z2_d2', 'y2', 'y1', 'z1', 'z2'])
Note that here you passed the class into the add method, instead of an instance. You also did the same thing in SubOpt1 and SubOpt2
group.add('Discipline', Discipline1,
promotes=['x1', 'z1', 'x2', 'y2', 'x1_d1', 'z1_d1', 'x2_d2', 'y2_d2', 'obj1'])
# Add Cons
Instead, you need to pass instances into them as follows:
self.add('SubOpt1', SubOpt1(), promotes=['z1_d1', 'x1_d1', 'z2_d1', 'y2_d1', 'x1', 'z1', 'x2', 'y2'])
self.add('SubOpt2', SubOpt2(), promotes=['y2_d2', 'y1_d2', 'z1_d2', 'z2_d2', 'y2', 'y1', 'z1', 'z2'])
There are a few other minor typos in your script related to variable names, but the class vs instance issue is your main problem
Related
I want to plot the learning curve (loss & accuracy plot) for my gan? I try different solution but still can't plot the learning curve for my gan. I tried to use fit and history function but I get error and can't plot loss & accuracy curve. If anyone can help?
I run my code in google colab. Since I only train the discriminator I tried to use fit & history function in the definition of discriminator & gan but still get error
from tensorflow.keras.layers import Dense, Conv2DTranspose, LeakyReLU, Reshape, BatchNormalization, Activation, Conv2D
from tensorflow.keras.models import Model, Sequential
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.layers import Conv2D, Flatten, Dropout
from tensorflow.keras import optimizers
import tensorflow as tf
def image_generator():
generator = Sequential()
generator.add(Dense(256*4*4, input_shape = (100,)))
#generator.add(BatchNormalization())
generator.add(LeakyReLU())
generator.add(Reshape((4,4,256)))
generator.add(Conv2DTranspose(128,kernel_size=3, strides=2, padding = "same"))
#generator.add(BatchNormalization())
generator.add(LeakyReLU(alpha=0.2))
generator.add(Conv2DTranspose(128,kernel_size=3, strides=2, padding = "same"))
#generator.add(BatchNormalization())
generator.add(LeakyReLU(alpha=0.2))
generator.add(Conv2DTranspose(128,kernel_size=3, strides=2, padding = "same"))
#generator.add(BatchNormalization())
generator.add(LeakyReLU(alpha=0.2))
generator.add(Conv2D(3,kernel_size=3, padding = "same", activation='tanh'))
return(generator)
model_generator = image_generator()
model_generator.summary()
# Define input data
def generate_data_entry(n_samples):
X = np.random.randn(100 * n_samples)
X = X.reshape(n_samples, 100)
return X
def create_data_fake(model_generator, n_samples):
input = generate_data_entry(n_samples)
X = model_generator.predict(input)
y = np.zeros((n_samples, 1))
return X,y
number_samples = 4
X,_ = create_data_fake(model_generator, number_samples)
# Visualize results
for i in range(number_samples):
plt.subplot(2, 2, 1 + i)
plt.axis('off')
plt.imshow(X[i])
def images_discriminator():
discriminator = Sequential()
discriminator.add(Conv2D(64, kernel_size=3, padding = "same", input_shape = (32,32,3)))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Conv2D(128, kernel_size=3,strides=(2,2), padding = "same"))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Conv2D(128, kernel_size=3,strides=(2,2), padding = "same"))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Conv2D(256, kernel_size=3, strides=(2,2), padding = "same"))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Flatten())
discriminator.add(Dropout(0.4))
discriminator.add(Dense(1, activation='sigmoid'))
opt = tf.keras.optimizers.Adam(learning_rate=0.0002 ,beta_1=0.5)
discriminator.compile(loss='binary_crossentropy', optimizer= opt , metrics = ['accuracy'])
return(discriminator)
model_discriminator = images_discriminator()
model_discriminator.summary()
from tensorflow.keras.datasets import cifar10
import random
import tensorflow as tf
import numpy as np
from tensorflow.keras import backend
import pandas as pd
import matplotlib.pyplot as plt
import keras
%matplotlib inline
(Xtrain, Ytrain), (testx, testy) = cifar10.load_data()
def load_images():
(Xtrain, Ytrain), (testx, testy) = cifar10.load_data()
# we stayed with the dogs
indice = np.where(Ytrain == 0)
indice = indice[0]
Xtrain = Xtrain[indice, :,:,:]
# Normalize the data
X = Xtrain.astype('float32')
X = (X - 127.5) / 127.5
return X
print(load_images().shape)
def load_data_reales(dataset, n_samples):
ix = np.random.randint(0, dataset.shape[0], n_samples)
X = dataset[ix]
y = np.ones((n_samples, 1))
return X,y
def load_data_fake(n_samples):
X = np.random.rand(32 * 32 * 3 * n_samples)
X = -1 + X * 2
X = X.reshape((n_samples, 32,32,3))
y = np.zeros((n_samples, 1))
return X,y
def train_discriminator(model, dataset, n_iterations=20, batch = 128):
medium_batch = int(batch/2)
for i in range(n_iterations):
X_real, y_real = load_data_reales(dataset, medium_batch)
los_real, acc_real = model.train_on_batch(X_real, y_real)
X_fake, y_fake = load_data_fake(medium_batch)
los_fake, acc_fake = model.train_on_batch(X_fake, y_fake)
print(str(i+1) + ' Real:' + str(acc_real*100) + ', Fake:' + str(acc_fake*100))
dataset = load_images()
train_discriminator(model_discriminator, dataset)
def my_loss_function(x_real,x_fake):
la=tf.keras.losses.BinaryCrossentropy()
my_loss_function = la(x_real, x_fake)
return my_loss_function
def create_gan(discriminator, generator):
discriminator.trainable=False
gan = Sequential()
gan.add(generator)
gan.add(discriminator)
opt = tf.keras.optimizers.Adam(learning_rate=0.0002,beta_1=0.5)
return gan
gan = create_gan(model_discriminator,model_generator)
gan.summary()
def show_images_generated(data_fake, epoch):
now = datetime.now()
now = now.strftime("%Y%m%d_%H%M%S")
# we make the data go from 0 to 1
data_fake = (data_fake + 1) / 2.0
for i in range(10):
plt.imshow(data_fake[i])
plt.axis('off')
name = str(epoch) + '_generated_image_' + str(i) + '.png'
plt.savefig(name, bbox_inches='tight')
plt.close()
def evaluate_and_save(model_generator, epoch, medium_dataset):
# We save the model
now = datetime.now()
now = now.strftime("%Y%m%d_%H%M%S")
name = str(epoch) + '_' + str(now)+"_model_generator_" + '.h5'
model_generator.save(name)
# We generate new data
X_real,Y_real = load_data_reales(dataset, medium_dataset)
X_fake, Y_fake = create_data_fake(model_generator,medium_dataset)
# We evaluate the model
_, acc_real = model_discriminator.evaluate(X_real, Y_real)
_, acc_fake = model_discriminator.evaluate(X_fake, Y_fake)
print('Acc Real:' + str(acc_real*100) + '% Acc Fake:' + str(acc_fake*100)+'%')
def training(data, model_generator, model_discriminator, epochs, n_batch, beginning = 0):
dimension_batch = int(data.shape[0]/n_batch)
medium_dataset = int(n_batch/2)
# We iterate over the epochs
for epoch in range(beginning, beginning + epochs):
# We iterate over all batches
for batch in range(n_batch):
# We load all the real data
X_real,Y_real = load_data_reales(dataset, medium_dataset)
# We train the discriminator withEnrenamos discriminator with data reales
cost_discriminator_real, _ = model_discriminator.train_on_batch(X_real, Y_real)
X_fake, Y_fake = create_data_fake(model_generator,medium_dataset)
cost_discriminator_fake, _ = model_discriminator.train_on_batch(X_fake, Y_fake)
# We generate input images for the GAN
X_gan = generate_data_entry(medium_dataset)
Y_gan = np.ones((medium_dataset, 1))
# We train the GAN with fake data
cost_gan = gan.train_on_batch(X_gan, Y_gan)
# Every 10 epochs we show the results and cost
if (epoch+1) % 10 == 0:
evaluate_and_save(model_generator,epoch = epoch, medium_dataset= medium_dataset)
show_images_generated(X_fake, epoch = epoch)
training(dataset, model_generator, model_discriminator, epochs = 10, n_batch=128, beginning = 0)
X_fake, _ = create_data_fake(n_samples=49, model_generator=model_generator)
X_fake = (X_fake+1)/2
for i in range(49):
plt.subplot(7,7,i+1)
plt.axis('off')
plt.imshow(X_fake[i])
# We generate new data
X_real1,Y_real1 = load_data_reales(dataset, int(15))
X_fake1, Y_fake1 = create_data_fake(model_generator,int(15))
# We evaluate the model
_, acc_real = model_discriminator.evaluate(X_real1, Y_real1)
_, acc_fake = model_discriminator.evaluate(X_fake1, Y_fake1)
print('Acc Real:' + str(acc_real*100) + '% Acc Fake:' + str(acc_fake*100)+'%')
I am doing a nested plot but my labels are repeating as many times as a loop, although it is out of the loop. In fact, I need three labels which correspond to 6.96K, 0.268K, 0.02025K
import matplotlib.pyplot as plt
import numpy as np
Freq = np.arange(1, 10, 0.1)
T = 6.96
for f in Freq:
def quanta(f,T):
return (f*T)
final = quanta(f,T)
plt.plot(f, final, 'o', color='red', linewidth=1.5, markersize=4, label = '6.96K')
plt.legend(shadow=True, loc=(0.5, 0.2) )
T = 0.268
for f in Freq:
def quanta(f,T):
return (f*T)
final = quanta(f,T)
plt.plot(f, final, 'o', color='orange', linewidth=1.5, markersize=4,label = '0.268K')
plt.legend(shadow=True, loc=(0.5, 0.2) )
T = 0.02025
for f in Freq:
def quanta(f,T):
return (f*T)
final = quanta(f,T)
plt.plot(f, final, 'o', color='Blue', linewidth=1.5, markersize=4,label = '0.02025K')
plt.legend(shadow=True, loc=(0.5, 0.2) )
plt.show()
I have this python code:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
from matplotlib import cm
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot solid of revolution along x-axis
def sor_x(ll, ul):
u = np.linspace(ll, ul, 60)
v = np.linspace(0, 2 * np.pi, 60)
U, V = np.meshgrid(u, v)
X = U
Y = (U**2)*np.cos(V)
Z = (U**2)*np.sin(V)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)
if __name__ == '__main__':
ll, ul = 0, 1
sor_x(ll, ul)
plt.show()
This plots the solid of revolution of function y = x**2 along x-axis. Now I have to change this to a 3D animation like this:
The code for this animation in mathematica is:
f[r_, ϕ_, z_] := {(2 + Tan[z])Cos[ϕ], (2 + Cos[z]) Sin[ϕ], z}
vase[α_] :=
ParametricPlot3D[f[r, ϕ, z], {z, 0, 2 Pi}, {ϕ, 0, α},
AspectRatio -> Automatic, PlotRange -> {{-3, 3}, {-3, 3}, {0, 6}}];
animation = Table[
vase[ϕ],
{ϕ, 0.1, 2π, π/12}];
Export["rotationskoerper_animation.gif", animation,
ConversionOptions -> {"AnimationDisplayTime" -> 0.1, "Loop" -> True},
ImageSize -> {1000, 1000}]
I am trying to run the following example from the Bokeh docs in a Jupyter notebook as opposed to through the Bokeh server, however I can't figure out what to change in order for this to work? Right now I simply get a blank output. I feel like it should be easy and I'm just missing something...Here is the code from the Bokeh docs.
import numpy as np
from bokeh.models import BoxSelectTool, LassoSelectTool, Paragraph
from bokeh.plotting import figure, hplot, vplot
# create three normal population samples with different parameters
x1 = np.random.normal(loc=5.0, size=400) * 100
y1 = np.random.normal(loc=10.0, size=400) * 10
x2 = np.random.normal(loc=5.0, size=800) * 50
y2 = np.random.normal(loc=5.0, size=800) * 10
x3 = np.random.normal(loc=55.0, size=200) * 10
y3 = np.random.normal(loc=4.0, size=200) * 10
x = np.concatenate((x1, x2, x3))
y = np.concatenate((y1, y2, y3))
TOOLS="pan,wheel_zoom,box_select,lasso_select"
# create the scatter plot
p = figure(tools=TOOLS, plot_width=600, plot_height=600, title=None, min_border=10, min_border_left=50)
r = p.scatter(x, y, size=3, color="#3A5785", alpha=0.6)
p.select(BoxSelectTool).select_every_mousemove = False
p.select(LassoSelectTool).select_every_mousemove = False
# create the horizontal histogram
hhist, hedges = np.histogram(x, bins=20)
hzeros = np.zeros(len(hedges)-1)
hmax = max(hhist)*1.1
LINE_ARGS = dict(color="#3A5785", line_color=None)
ph = figure(toolbar_location=None, plot_width=p.plot_width, plot_height=200, x_range=p.x_range,
y_range=(-hmax, hmax), title=None, min_border=10, min_border_left=50)
ph.xgrid.grid_line_color = None
ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hhist, color="white", line_color="#3A5785")
hh1 = ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hzeros, alpha=0.5, **LINE_ARGS)
hh2 = ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hzeros, alpha=0.1, **LINE_ARGS)
# create the vertical histogram
vhist, vedges = np.histogram(y, bins=20)
vzeros = np.zeros(len(vedges)-1)
vmax = max(vhist)*1.1
th = 42 # need to adjust for toolbar height, unfortunately
pv = figure(toolbar_location=None, plot_width=200, plot_height=p.plot_height+th-10, x_range=(-vmax, vmax),
y_range=p.y_range, title=None, min_border=10, min_border_top=th)
pv.ygrid.grid_line_color = None
pv.xaxis.major_label_orientation = -3.14/2
pv.quad(left=0, bottom=vedges[:-1], top=vedges[1:], right=vhist, color="white", line_color="#3A5785")
vh1 = pv.quad(left=0, bottom=vedges[:-1], top=vedges[1:], right=vzeros, alpha=0.5, **LINE_ARGS)
vh2 = pv.quad(left=0, bottom=vedges[:-1], top=vedges[1:], right=vzeros, alpha=0.1, **LINE_ARGS)
pv.min_border_top = 80
pv.min_border_left = 0
ph.min_border_top = 10
ph.min_border_right = 10
p.min_border_right = 10
layout = vplot(hplot(p, pv), hplot(ph, Paragraph(width=200)), width=800, height=800)
def update(attr, old, new):
inds = np.array(new['1d']['indices'])
if len(inds) == 0 or len(inds) == len(x):
hhist1, hhist2 = hzeros, hzeros
vhist1, vhist2 = vzeros, vzeros
else:
neg_inds = np.ones_like(x, dtype=np.bool)
neg_inds[inds] = False
hhist1, _ = np.histogram(x[inds], bins=hedges)
vhist1, _ = np.histogram(y[inds], bins=vedges)
hhist2, _ = np.histogram(x[neg_inds], bins=hedges)
vhist2, _ = np.histogram(y[neg_inds], bins=vedges)
hh1.data_source.data["top"] = hhist1
hh2.data_source.data["top"] = -hhist2
vh1.data_source.data["right"] = vhist1
vh2.data_source.data["right"] = -vhist2
r.data_source.on_change('selected', update)
Thanks in advance!
You have to run the output_notebook command first to set notebook operation (https://docs.bokeh.org/en/latest/docs/user_guide/notebook.html), then use the show command
tl;dr
from bokeh.io import output_notebook, show
output_notebook()
... make plots and stuff ...
show(plots)
I have a plot of 3 data sets that have datetime objetcs on the x axis.
I want to have a cursor that snaps to the data and shows the precise x and y value.
I already have a "snap to cursor", but that only works for scalar x axes.
Can anyone help me to modify the snap to cursor so that it works for datetime x axes as well?
Here are my data plots:
import numpy as np
import matplotlib.pyplot as plot
import matplotlib.ticker as mticker
import matplotlib.dates as dates
import datetime
import Helpers
fig = plot.figure(1)
DAU = ( 2, 20, 25, 60, 190, 210, 18, 196, 212)
WAU = ( 50, 160, 412, 403, 308, 379, 345, 299, 258)
MAU = (760, 620, 487, 751, 612, 601, 546, 409, 457)
firstDay = datetime.datetime(2012,1,15)
#create an array with len(DAU) entries from given starting day
dayArray = [firstDay + datetime.timedelta(days = i) for i in xrange(len(DAU))]
line1 = plot.plot(dayArray, DAU, 'o-', color = '#336699')
line2 = plot.plot(dayArray, WAU, 'o-', color = '#993333')
line3 = plot.plot(dayArray, MAU, 'o-', color = '#89a54e')
ax = plot.subplot(111)
dateLocator = mticker.MultipleLocator(2)
dateFormatter = dates.DateFormatter('%d.%m.%Y')
ax.xaxis.set_major_locator(dateLocator)
ax.xaxis.set_major_formatter(dateFormatter)
fig.autofmt_xdate(rotation = 90, ha = 'center')
yMax = max(np.max(DAU), np.max(WAU), np.max(MAU))
yLimit = 100 - (yMax % 100) + yMax
plot.yticks(np.arange(0, yLimit + 1, 100))
plot.title('Active users', weight = 'bold')
plot.grid(True, axis = 'both')
plot.subplots_adjust(bottom = 0.2)
plot.subplots_adjust(right = 0.82)
legend = plot.legend((line1[0], line2[0], line3[0]),
('DAU',
'WAU',
'MAU'),
'upper left',
bbox_to_anchor = [1, 1],
shadow = True)
frame = legend.get_frame()
frame.set_facecolor('0.80')
for t in legend.get_texts():
t.set_fontsize('small')
#THIS DOES NOT WORK
cursor = Helpers.SnaptoCursor(ax, dayArray, DAU, 'euro daily')
plot.connect('motion_notify_event', cursor.mouse_move)
plot.show()
And this is my module "Helper" that contains the "SnaptoCursor" class:
(I got the basic SnaptoCursor class from somewhere else and modified it a little bit)
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plot
def minsec(sec, unused):
"""
Returns a string of the input seconds formatted as mm'ss''.
"""
minutes = sec // 60
sec = sec - minutes * 60
return '{0:02d}\'{1:02d}\'\''.format(int(minutes), int(sec))
class SnaptoCursor():
"""
A cursor with crosshair snaps to the nearest x point.
For simplicity, I'm assuming x is sorted.
"""
def __init__(self, ax, x, y, formatting, z = None):
"""
ax: plot axis
x: plot spacing
y: plot data
formatting: string flag for desired formatting
z: optional second plot data
"""
self.ax = ax
self.lx = ax.axhline(color = 'k') #the horiz line
self.ly = ax.axvline(color = 'k') #the vert line
self.x = x
self.y = y
self.z = z
# text location in axes coords
self.txt = ax.text(0.6, 0.9, '', transform = ax.transAxes)
self.formatting = formatting
def format(self, x, y):
if self.formatting == 'minsec':
return 'x={0:d}, y='.format(x) + minsec(y, 0)
elif self.formatting == 'daily euro':
return u'day {0:d}: {1:.2f}€'.format(x, y)
def mouse_move(self, event):
if not event.inaxes: return
mouseX, mouseY = event.xdata, event.ydata
#searchsorted: returns an index or indices that suggest where x should be inserted
#so that the order of the list self.x would be preserved
indx = np.searchsorted(self.x, [mouseX])[0]
mouseX = self.x[indx]
#if z wasn't defined
if self.z == None:
mouseY = self.y[indx]
#if z was defined: compare the distance between mouse and the two plots y and z
#and use the nearest one
elif abs(mouseY - self.y[indx]) < abs(mouseY - self.z[indx]):
mouseY = self.y[indx]
else:
mouseY = self.z[indx]
#update the line positions
self.lx.set_ydata(mouseY)
self.ly.set_xdata(mouseX)
self.txt.set_text(self.format(mouseX, mouseY))
plot.draw()
Of course this does not work since I am calling the SnaptoCursor with the datetime array "dayArray", which is supposed to be compared to the mouse coordinates later on. And these data types are not comparable.
I got it!!!
The problems where these two lines in the init method of the SnaptoCursor class:
self.lx = ax.axhline(color = 'k') #the horiz line
self.ly = ax.axvline(color = 'k') #the vert line
They were somehow messing up the datetime x axis (that has ordinals up to 730,000 e.g.), so you just have to initialize the lines' coordinates:
self.lx = ax.axhline(y = min(y), color = 'k') #the horiz line
self.ly = ax.axvline(x = min(x), color = 'k') #the vert line
Then it works just fine!
I'll be posting my complete SnaptoCursor class now that I have modified so it accepts individual formatting strings, and it can take up to 3 input data plots - that get snapped to according to your mouse position.
def percent(x, unused):
"""
Returns a string of the float number x formatted as %.
"""
return '{0:1.2f}%'.format(x * 100)
def minsec(sec, unused):
"""
Returns a string of the input seconds formatted as mm'ss''.
"""
minutes = sec // 60
sec = sec - minutes * 60
return '{0:02d}\'{1:02d}\'\''.format(int(minutes), int(sec))
class SnaptoCursor():
"""
A cursor with crosshair snaps to the nearest x point.
For simplicity, I'm assuming x is sorted.
"""
def __init__(self, ax, x, y, formatting, y2 = None, y3 = None):
"""
ax: plot axis
x: plot spacing
y: plot data
formatting: string flag for desired formatting
y2: optional second plot data
y3: optional third plot data
"""
self.ax = ax
self.lx = ax.axhline(y = min(y), color = 'k') #the horiz line
self.ly = ax.axvline(x = min(x), color = 'k') #the vert line
self.x = x
self.y = y
self.y2 = y2
self.y3 = y3
# text location in axes coords
self.txt = ax.text(0.6, 0.9, '', transform = ax.transAxes)
self.formatting = formatting
def format(self, x, y):
if self.formatting == 'minsec':
return 'x={0:d}, y='.format(x) + minsec(y, 0)
if self.formatting == 'decimal':
return 'x={0:d}, y={1:d}'.format(x, int(y))
elif self.formatting == 'date decimal':
return 'x={0:%d.%m.%Y}, y={1:d}'.format(x, int(y))
elif self.formatting == 'decimal percent':
return 'x={0:d}, y={1:d}%'.format(x, int(y * 100))
elif self.formatting == 'float':
return 'x={0:d}, y={1:.2f}'.format(x, y)
elif self.formatting == 'float percent':
return 'x={0:d}, y='.format(x) + percent(y, 0)
elif self.formatting == 'daily euro':
return u'day {0:d}: {1:.2f}€'.format(x, y)
def mouse_move(self, event):
if not event.inaxes:
return
mouseX, mouseY = event.xdata, event.ydata
if type(self.x[0]) == datetime.datetime:
mouseX = dates.num2date(int(mouseX)).replace(tzinfo = None)
#searchsorted: returns an index or indices that suggest where mouseX should be inserted
#so that the order of the list self.x would be preserved
indx = np.searchsorted(self.x, [mouseX])[0]
#if indx is out of bounds
if indx >= len(self.x):
indx = len(self.x) - 1
#if y2 wasn't defined
if self.y2 == None:
mouseY = self.y[indx]
#if y2 was defined AND y3 wasn't defined
elif self.y3 == None:
if abs(mouseY - self.y[indx]) < abs(mouseY - self.y2[indx]):
mouseY = self.y[indx]
else:
mouseY = self.y2[indx]
#if y2 AND y3 were defined
elif abs(mouseY - self.y2[indx]) < abs(mouseY - self.y[indx]):
if abs(mouseY - self.y2[indx]) < abs(mouseY - self.y3[indx]):
mouseY = self.y2[indx]
else:
mouseY = self.y3[indx]
#lastly, compare y with y3
elif abs(mouseY - self.y[indx]) < abs(mouseY - self.y3[indx]):
mouseY = self.y[indx]
else:
mouseY = self.y3[indx]
#update the line positions
self.lx.set_ydata(mouseY)
self.ly.set_xdata(mouseX)
self.txt.set_text(self.format(mouseX, mouseY))
plot.draw()