Coverage for mlprodict/onnxrt/ops_cpu/op_rfft.py: 93%

28 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-04 02:28 +0100

1# -*- encoding: utf-8 -*- 

2# pylint: disable=E0203,E1101,C0111 

3""" 

4@file 

5@brief Runtime operator. 

6""" 

7import numpy 

8from numpy.fft import rfft 

9from ._op import OpRun 

10from ._new_ops import OperatorSchema 

11 

12 

13class RFFT(OpRun): 

14 

15 atts = {'axis': -1} 

16 

17 def __init__(self, onnx_node, desc=None, **options): 

18 OpRun.__init__(self, onnx_node, desc=desc, 

19 expected_attributes=RFFT.atts, 

20 **options) 

21 

22 def _find_custom_operator_schema(self, op_name): 

23 if op_name == "RFFT": 

24 return RFFTSchema() 

25 raise RuntimeError( # pragma: no cover 

26 f"Unable to find a schema for operator '{op_name}'.") 

27 

28 def _run(self, a, fft_length=None, attributes=None, verbose=0, fLOG=None): # pylint: disable=W0221 

29 if fft_length is not None: 

30 fft_length = fft_length[0] 

31 y = rfft(a, fft_length, axis=self.axis) 

32 if a.dtype == numpy.float32: 

33 return (y.astype(numpy.complex64), ) 

34 if a.dtype == numpy.float64: 

35 return (y.astype(numpy.complex128), ) 

36 raise TypeError( # pragma: no cover 

37 f"Unexpected input type: {a.dtype!r}.") 

38 

39 def to_python(self, inputs): 

40 if len(inputs) == 1: 

41 return ('from numpy.fft import rfft', 

42 f"return rfft({inputs[0]}, axis={self.axis})") 

43 return ('from numpy.fft import rfft', 

44 f"return rfft({inputs[0]}, {inputs[1]}[0], axis={self.axis})") 

45 

46 

47class RFFTSchema(OperatorSchema): 

48 """ 

49 Defines a schema for operators added in this package 

50 such as @see cl FFT. 

51 """ 

52 

53 def __init__(self): 

54 OperatorSchema.__init__(self, 'RFFT') 

55 self.attributes = RFFT.atts