# Export OT Interval to 2-point Sample

**URL:** <https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36>\
**Category:** Python usage\
**Tags:** feature-request, api\
**Created:** [October 3, 2020, 6:06pm UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36 "2020-10-03T18:06:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![josephmure](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/josephmure/32/114_2.png) [@josephmure](https://openturns.discourse.group/u/josephmure)\
**Post date:** [October 3, 2020, 6:06pm UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/1 "2020-10-03T18:06:17Z")

</div>

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.

```auto
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]])`?

---

<div class="post-metadata">

**Author:** ![regislebrun](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/regislebrun/32/154_2.png) [@regislebrun](https://openturns.discourse.group/u/regislebrun)\
**Post date:** [October 5, 2020, 12:31pm UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/2 "2020-10-05T12:31:53Z")

</div>

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())?

---

<div class="post-metadata">

**Author:** ![MichaelBaudin](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/michaelbaudin/32/9_2.png) [@MichaelBaudin](https://openturns.discourse.group/u/MichaelBaudin)\
**Post date:** [October 5, 2020, 12:34pm UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/3 "2020-10-05T12:34:18Z")

</div>

Here is a way:

```auto
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

---

<div class="post-metadata">

**Author:** ![dumas](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/dumas/32/12_2.png) [@dumas](https://openturns.discourse.group/u/dumas)\
**Post date:** [October 5, 2020, 12:41pm UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/4 "2020-10-05T12:41:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josephmure](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/josephmure/32/114_2.png) [@josephmure](https://openturns.discourse.group/u/josephmure)\
**Post date:** [October 6, 2020, 9:35am UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/5 "2020-10-06T09:35:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josephmure](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/josephmure/32/114_2.png) [@josephmure](https://openturns.discourse.group/u/josephmure)\
**Post date:** [October 6, 2020, 9:36am UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/6 "2020-10-06T09:36:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![schueller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/schueller/32/3_2.png) [@schueller](https://openturns.discourse.group/u/schueller)\
**Post date:** [October 8, 2020, 7:52am UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/7 "2020-10-08T07:52:20Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![josephmure](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/josephmure/32/114_2.png) [@josephmure](https://openturns.discourse.group/u/josephmure)\
**Post date:** [October 8, 2020, 8:57am UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/8 "2020-10-08T08:57:35Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![sofianehaddad](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/sofianehaddad/32/46_2.png) [@sofianehaddad](https://openturns.discourse.group/u/sofianehaddad)\
**Post date:** [October 19, 2020, 8:09pm UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/9 "2020-10-19T20:09:51Z")

</div>

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

```python
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 …

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

```

---

<div class="post-metadata">

**Author:** ![josephmure](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/josephmure/32/114_2.png) [@josephmure](https://openturns.discourse.group/u/josephmure)\
**Post date:** [October 20, 2020, 7:41am UTC](https://openturns.discourse.group/t/export-ot-interval-to-2-point-sample/36/10 "2020-10-20T07:41:29Z")

</div>

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 `Interval`s to `Sample`s.

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.
