Is it possible in OpenTurns to create a discrete distribution containing non-numeric (string) values?
One application would be to select different turbulence models in my CFD code, that are given as string-values (e.g. ‘k-e’, ‘k-om’, ‘WALE’).
I am thinking of something like this, but it gives an error:
points = ot.Sample([['k-e'], ['k-om'], ['WALE']])
weights = ot.Point([1, 1, 1])
my_distribution = ot.UserDefined(points, weights)
9376 def __init__(self, *args):
-> 9377 _typ.Sample_swiginit(self, _typ.new_Sample(*args))
9378
9379 def __eq__(self, *args):
TypeError: Object passed as argument is not convertible to a Sample
If is not possible to handle non-numeric variables in OT, I would have to do a mapping of numerical values to the given strings in my Simulator Code.
Hello,
To the best of my knowledge, all of OpenTURNS’ distributions can only return numerical values (e.g., floats, integers, arrays), and therefore you would probably need to resort to a mapping before running the simulator code, as you suggest.
However, I’ll let the more experienced users correct me if I’m wrong.
Cheers
Hi,
@JPelamatti is perfectly right: OT is dedicated to the modeling of real-valued random vectors or random fields (to now). You can define discrete distributions (e.g. the UserDefined class you used) but you will have to map the real valued realizations of such distributions into your set of strings. Be aware of the fact that any distribution in OT is seen as the distribution of a random vector, so the corresponding realizations are vectors even if these vectors are of dimension 1. You can use the Description class to store your set of strings, but a pure Python list is the most natural option IMO. The conversion between these two types is automatic.
You can use:
labels = ['k-e', 'k-om', 'WALE']
index = [[0], [1], [2]]
my_distribution = ot.UserDefined(index)
sample_labels = [labels[int(x[0])] for x in my_distribution.getSample(12)]
to generate a list of 12 randomly chosen labels:
['k-e', 'WALE', 'k-e', 'k-e', 'k-om', 'WALE', 'k-om', 'WALE', 'k-e', 'k-om', 'WALE', 'k-e']
Cheers
Régis