Export OT Interval to 2-point Sample

Is there a way to export OpenTURNS Intervals to Samples or numpy arrays? I figure that since Intervals have 2 boundaries in every dimension, they could in theory be turned to Samples of size 2 (with the dimension staying the same).

Let us consider an example.

import openturns as ot
ot.Interval(0.6, 1.2)

Is there a way to convert ot.Interval(0.6, 1.2) into ot.Sample([[0.6], [1.2]])?

There is no such service but it could easily be added. What do you prefer? A constructor of Sample based on an Interval (mySample=Sample(myInterval)) or a conversion method (mySample=myInterval.toSample())?

Here is a way:

import openturns as ot
interval = ot.Interval(0.6, 1.2)
lower = interval.getLowerBound()
upper = interval.getUpperBound()
sample = ot.Sample([lower, upper])

This should work in any dimension.
Perhaps you searched for a more straightforward method?

Regards,

Michaël

I also use such transformation as shown by Michaël. If a service is provided, in my case, I would prefer to have a constructor based on an Interval.
Antoine

I agree with Antoine, having a Sample constructor based on an Interval would be great.

Yes, I would like to avoid writing this code block.

I dont agree we need a constructor from interval for 1 line:

sample = ot.Sample([interval.getLowerBound(), interval.getUpperBound()])

My real goal is in fact to turn an OpenTURNS Interval into a numpy array. In fact, what I initially tried was arr = np.array(interval) which of course doesn’t work.
Having arr = np.array(ot.Sample(interval)) work would be good for me, but I think
arr = np.array(ot.Sample([interval.getLowerBound(), interval.getUpperBound()]))
is really long…

Agreed with @schueller : no need to have a new method or ctor.
If you usually needs such transformation within your applications, I suggest you overload the Interval class when loading openturns for example

import openturns as ot
def interval_to_sample(interval):
    return ot.Sample([interval.getLowerBound(), 
                      interval.getUpperBound()])
ot.Interval.asSample = interval_to_sample

In that way, we get a new method that suits your needs …

interval = ot.Interval(0.6, 1.2)
print(interval.asSample())

Thanks @sofianehaddad for the tip.

Perhaps I should have stated the true goal of this request immediately: to convert array-like OpenTURNS objects to numpy arrays. This is easy to do for Sample objects (arr = np.array(mySample)) and that is the reason why I asked for a way to convert Intervals to Samples.

Wouldn’t it be great to able to simply write arr = np.array(myPoint) or arr = np.array(myInterval) though?

This way it would become easier to feed OT results to other Python libraries.