interval module
Wrapping array into intervals.
Contains IntervalArray structure wrapping array and allowing to access it by providing interval value.
- class IntervalArray(a: ndarray | List, n: int = 1)
Bases:
object
Wrap 1-D array into 2-D interval structure of length n.
Reshapes array a of size n into 2-D array of shape (len(a)/n, n). Elements are accessed using __getitem__ and __setitem__ providing interval and element number a[i, j] where it denotes j-th element of i-th interval, i.e., i * n + j element
Skipping j-th value is equivalent to selecting element in flat array (without intervals): a[i] == a[i // n, i % n].
- Parameters:
a (1D-array) – Input array.
n (int, default: 1) – Interval size. f interval size is 1, it behaves like a normal array.
Examples
>>> from traffic_weaver.interval import IntervalArray >>> import numpy as np >>> x = np.arange(9) >>> a = IntervalArray(x, 5) >>> print(a[1, 2]) 7 >>> print(a[1]) 1 >>> a[1, 2] = 15 >>> a[1, 2].item() 15
- extend_linspace(direction='both')
Extend one interval in given direction with linearly spaced values.
- Parameters:
direction (str, default='both') – Possible values are ‘both’, ‘left’, ‘right’.
Examples
>>> import numpy as np >>> x = np.arange(10) >>> a = IntervalArray(x, 5) >>> a.to_2d_array() array([[0., 1., 2., 3., 4.], [5., 6., 7., 8., 9.]]) >>> a.extend_linspace() >>> a.to_2d_array() array([[-5., -4., -3., -2., -1.], [ 0., 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., 14.]])
- extend_constant(direction='both')
Extend one interval in given direction with constant value.
- Parameters:
direction (str, default='both') – Possible values are ‘both’, ‘left’, ‘right’.
Examples
>>> import numpy as np >>> x = np.arange(10) >>> a = IntervalArray(x, 5) >>> a.to_2d_array() array([[0., 1., 2., 3., 4.], [5., 6., 7., 8., 9.]]) >>> a.extend_constant() >>> a.to_2d_array() array([[0., 0., 0., 0., 0.], [0., 1., 2., 3., 4.], [5., 6., 7., 8., 9.], [9., 9., 9., 9., 9.]])
- nr_of_full_intervals()
Number of intervals that contain all n items.
Examples
>>> import numpy as np >>> x = np.arange(14) >>> a = IntervalArray(x, 5) >>> a.to_2d_array() array([[ 0., 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., nan]]) >>> a.nr_of_full_intervals() 2
- property array
Array storing values of intervals.
- to_2d_array()
Convert intervals to 2D array.
Examples
Let assume that the IntervalArray has the following representation:
IntervalArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], n=4)
Converting it to 2D array results in creating the following ndarray:
array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., nan, nan]])
Examples
>>> import numpy as np >>> x = np.arange(10) >>> a = IntervalArray(x, 4) >>> a.to_2d_array() array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., nan, nan]])
- to_2d_array_closed_intervals(drop_last=True)
Convert to 2D array, each row ends with the first value from the next one.
- Parameters:
drop_last (bool, default: True) – Whether to drop the last row. Usually last row contains the last value of an array and is filled up with NaNs.
Examples
Let assume that the IntervalArray has the following representation:
IntervalArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], n=4)
Converting it to 2D array results in creating the following ndarray:
array([[ 0., 1., 2., 3., 4.], [ 4., 5., 6., 7., 8..]])
Examples
>>> import numpy as np >>> x = np.arange(10) >>> a = IntervalArray(x, 4) >>> a.to_2d_array() array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., nan, nan]]) >>> a.to_2d_array_closed_intervals() array([[0., 1., 2., 3., 4.], [4., 5., 6., 7., 8.]])
- oversample(num: int, method: Callable[[list[float] | ndarray, int], ndarray])
Oversample array n times using provided method.
- Parameters:
num (int) – Number of additional samples.
method (Callable) – Method used to oversample the array.
- Returns:
Oversampled IntervalArray.
- Return type:
See also
- oversample_linspace(num: int)
Concrete implementation of oversample using linspace.
- Parameters:
num (int) – Number of additional samples.
- Returns:
Oversampled IntervalArray.
- Return type:
Examples
>>> import numpy as np >>> x = np.arange(10) >>> a = IntervalArray(x, 4) >>> a.to_2d_array() array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., nan, nan]]) >>> a.oversample_linspace(2).to_2d_array() array([[0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5], [4. , 4.5, 5. , 5.5, 6. , 6.5, 7. , 7.5], [8. , 8.5, 9. , nan, nan, nan, nan, nan]])
- oversample_piecewise(num: int)
Concrete implementation of oversample using piecewise constants.
- Parameters:
num (int) – Number of additional samples.
- Returns:
Oversampled IntervalArray.
- Return type:
Examples
>>> import numpy as np >>> x = np.arange(10) >>> a = IntervalArray(x, 4) >>> a.to_2d_array() array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., nan, nan]]) >>> a.oversample_piecewise(2).to_2d_array() array([[ 0., 0., 1., 1., 2., 2., 3., 3.], [ 4., 4., 5., 5., 6., 6., 7., 7.], [ 8., 8., 9., nan, nan, nan, nan, nan]])