UQ[py]Lab (PCE display)


I’m currently working with UQ[py]Lab and have come across an issue. When I try to use uq.display(myPCE) to generate a visual representation of the coefficient distribution in myPCEmetamodel, it doesn’t seem to work. However, other functions like uq.print(myPCE) are functioning correctly after creating the metamodel.

The error that I see is:

Traceback (most recent call last):

line 72, in <module>
line 394, in display
    fig = self.make_fig(obj, name, type, **kwargs)

 line 403, in make_fig
    return helpers.display(obj, self, **kwargs)

 line 119, in display
    return display_util.PCE(obj)

 line 358, in PCE
    Coefficients = np.array(Model['PCE']['Coefficients'])
TypeError: list indices must be integers or slices, not str

Could you provide any suggestions on how to resolve this?

Best regards, Eilidh

Hi @Eilidh_Radcliff

Thanks for reporting this. Can you please provide a minimal reproducible example?

Best regards

Hi Styfen,

Thank you for the fast response!

I discovered this while experimenting with one of the examples from the documentation of UQLab. I will paste below:

from uqpylab import sessions, display_util
import numpy as np
import matplotlib.pyplot as plt
import scipy.io

mySession = sessions.cloud()

ModelOpts = {
    'Type': 'Model',
    'ModelFun': 'simply_supported_beam_9points.model',
    'isVectorized': 'true'

myModel = uq.createModel(ModelOpts)

InputOpts = {
    'Marginals': [
        'Name': 'b', # beam width
        'Type': 'Lognormal',
        'Moments': [0.15, 0.0075] # (m)
        'Name': 'h', # beam height
        'Type': 'Lognormal',
        'Moments': [0.3, 0.015] # (m)
        'Name': 'L', # beam length
        'Type': 'Lognormal',
        'Moments': [5, 0.05] # (m)
        'Name': 'E', # Young's modulus
        'Type': 'Lognormal',
        'Moments': [3e10, 4.5e9] # (Pa)
        'Name': 'p', # uniform load
        'Type': 'Lognormal',
        'Moments': [1e4, 1e3] # (N/m)

myInput = uq.createInput(InputOpts)

MetaOpts = {
    'Type': 'Metamodel',
    'MetaType': 'PCE'

MetaOpts['TruncOptions'] = {'qNorm': 0.75}
MetaOpts['Degree'] = np.arange(2,11).tolist()

MetaOpts['ExpDesign'] = {
    "NSamples" : 10,
    "Sampling" : "LHS"


myPCE = uq.createModel(MetaOpts)



Can you also provide the code for simply_supported_beam_9points.model?

I tried it with a dummy function, but could not reproduce the error. Can you also tell me which Python version you are using and if you have installed UQ[py]Lab in a fresh Python environment? Can you also list the output of pip list?

the code for simply_supported_beam_9points:

import numpy as np

def model(X):
    """calculates the deflection of a Simply Supported Beam on 9 equally-spaced 
    points along the beam length. X refers to a sample of the input random variables: [b h L E p]. 
    The points for which the deflection is calculated are xi = (1:9)/10*L.
    The vector Y contains the displacement at each of the 9 points.

    X: ndarray
        5-column matrix
        x[:,0]: beam width (m)
        x[:,1]: beam height (m)
        x[:,2]: length (m)
        x[:,3]: Young modulus (Pa)
        x[:,4]: uniform load (N)
        Y[:,0]: deflection at xi=1/10*L
        Y[:,8]: deflection at xi=9/10*L
    X = np.array(X,ndmin=2)
    b = X[:, 0]; # beam width  (m)
    h = X[:, 1]; # beam height (m)
    L = X[:, 2]; # Length (m)
    E = X[:, 3]; # Young modulus (Pa)
    p = X[:, 4]; # uniform load (N)
    # The beam is considered primatic, therefore:
    I = b* np.power(h,3) / 12; # the moment of intertia

    Y = np.empty((X.shape[0],9))
    for j in np.arange(0,9):
        xi = (j+1)/10*L
        Y[:,j] = -p*xi*(np.power(L,3)-2*np.power(xi,2)*L + xi**3)/(24*E*I);
    print('X type', type(X))   
    print('X shape', X.shape)   
    print('b type', type(b))
    print('Y type',type(Y))
    print('Y shape', Y.shape)   
    return Y

My pip list is:

Package Version

appdirs 1.4.4
asttokens 2.4.1
certifi 2023.11.17
charset-normalizer 3.3.2
colorama 0.4.6
comm 0.2.0
contourpy 1.2.0
cycler 0.12.1
decorator 5.1.1
executing 2.0.1
fonttools 4.47.0
idna 3.6
ipython 8.19.0
ipywidgets 8.1.1
jedi 0.19.1
jupyterlab-widgets 3.0.9
kiwisolver 1.4.5
matplotlib 3.8.2
matplotlib-inline 0.1.6
numpy 1.26.2
packaging 23.2
pandas 1.5.3
parso 0.8.3
Pillow 10.1.0
pip 22.3.1
plotly 5.18.0
prompt-toolkit 3.0.43
pure-eval 0.2.2
Pygments 2.17.2
pyparsing 3.1.1
python-dateutil 2.8.2
pytz 2023.3.post1
requests 2.31.0
scipy 1.11.4
setuptools 65.5.0
six 1.16.0
stack-data 0.6.3
tenacity 8.2.3
traitlets 5.14.0
uqpylab 0.95
urllib3 2.1.0
wcwidth 0.2.12
widgetsnbextension 4.0.9

I am using Python version: Python 3.11.3

I am working in a fresh venv.

Thank you for providing the code. I can now reproduce the error. It seems that uq.display is not yet implemented for multi-output PCE. Unfortunately, the error message is not really informative here.

UQLab calculates a separate PCE for each output. So if uq.display is important for you, you can fix it for now using:

def display_ith(pce: dict, index: int) -> None:
    pce_ith = pce.copy()
    pce_ith["PCE"] = pce_ith["PCE"][index]

for i in range(len(myPCE["PCE"])):
    display_ith(myPCE, i)

Also note that uq.print only prints the first output. However, you should see this in the form of a warning message.

Let us know if you have any further problems.

1 Like

Thank you - this resolves my error for this example too! :smile:

When using the function uq.print, why is it only possible to print the first output?