1
- use argus:: signals:: interpolation:: Linear ;
1
+ use argus:: signals:: interpolation:: { Constant , Linear } ;
2
2
use argus:: signals:: Signal ;
3
+ use pyo3:: exceptions:: PyValueError ;
3
4
use pyo3:: prelude:: * ;
4
5
use pyo3:: types:: PyType ;
5
6
@@ -10,6 +11,7 @@ use crate::{DType, PyArgusError};
10
11
pub enum PyInterp {
11
12
#[ default]
12
13
Linear ,
14
+ Constant ,
13
15
}
14
16
15
17
#[ derive( Debug , Clone , derive_more:: From , derive_more:: TryInto ) ]
@@ -122,31 +124,49 @@ macro_rules! impl_signals {
122
124
impl [ <$ty_name Signal >] {
123
125
/// Create a new empty signal
124
126
#[ new]
125
- #[ pyo3( signature = ( ) ) ]
126
- fn new( ) -> ( Self , PySignal ) {
127
- ( Self , PySignal :: new( Signal :: <$ty>:: new( ) , PyInterp :: Linear ) )
127
+ #[ pyo3( signature = ( * , interpolation_method = "linear" ) ) ]
128
+ fn new( interpolation_method: & str ) -> PyResult <( Self , PySignal ) > {
129
+ let interp = match interpolation_method {
130
+ "linear" => PyInterp :: Linear ,
131
+ "constant" => PyInterp :: Constant ,
132
+ _ => return Err ( PyValueError :: new_err( format!( "unsupported interpolation method `{}`" , interpolation_method) ) ) ,
133
+ } ;
134
+ Ok ( ( Self , PySignal :: new( Signal :: <$ty>:: new( ) , interp) ) )
128
135
}
129
136
130
137
/// Create a new signal with constant value
131
138
#[ classmethod]
132
- fn constant( _: & PyType , py: Python <' _>, value: $ty) -> PyResult <Py <Self >> {
139
+ #[ pyo3( signature = ( value, * , interpolation_method = "linear" ) ) ]
140
+ fn constant( _: & PyType , py: Python <' _>, value: $ty, interpolation_method: & str ) -> PyResult <Py <Self >> {
141
+ let interp = match interpolation_method {
142
+ "linear" => PyInterp :: Linear ,
143
+ "constant" => PyInterp :: Constant ,
144
+ _ => return Err ( PyValueError :: new_err( format!( "unsupported interpolation method `{}`" , interpolation_method) ) ) ,
145
+ } ;
133
146
Py :: new(
134
147
py,
135
- ( Self , PySignal :: new( Signal :: constant( value) , PyInterp :: Linear ) )
148
+ ( Self , PySignal :: new( Signal :: constant( value) , interp ) )
136
149
)
137
150
}
138
151
139
152
/// Create a new signal from some finite number of samples
140
153
#[ classmethod]
141
- fn from_samples( _: & PyType , samples: Vec <( f64 , $ty) >) -> PyResult <Py <Self >> {
154
+ #[ pyo3( signature = ( samples, * , interpolation_method = "linear" ) ) ]
155
+ fn from_samples( _: & PyType , samples: Vec <( f64 , $ty) >, interpolation_method: & str ) -> PyResult <Py <Self >> {
142
156
let ret: Signal :: <$ty> = Signal :: <$ty>:: try_from_iter( samples
143
157
. into_iter( )
144
158
. map( |( t, v) | ( core:: time:: Duration :: try_from_secs_f64( t) . unwrap_or_else( |err| panic!( "Value = {}, {}" , t, err) ) , v) )
145
159
) . map_err( PyArgusError :: from) ?;
160
+
161
+ let interp = match interpolation_method {
162
+ "linear" => PyInterp :: Linear ,
163
+ "constant" => PyInterp :: Constant ,
164
+ _ => return Err ( PyValueError :: new_err( format!( "unsupported interpolation method `{}`" , interpolation_method) ) ) ,
165
+ } ;
146
166
Python :: with_gil( |py| {
147
167
Py :: new(
148
168
py,
149
- ( Self , PySignal :: new( ret, PyInterp :: Linear ) )
169
+ ( Self , PySignal :: new( ret, interp ) )
150
170
)
151
171
} )
152
172
}
@@ -171,6 +191,7 @@ macro_rules! impl_signals {
171
191
let time = core:: time:: Duration :: from_secs_f64( time) ;
172
192
match super_. interpolation {
173
193
PyInterp :: Linear => signal. interpolate_at:: <Linear >( time) ,
194
+ PyInterp :: Constant => signal. interpolate_at:: <Constant >( time) ,
174
195
}
175
196
}
176
197
0 commit comments