Coverage for mlprodict/onnxrt/ops_cpu/op_imputer.py: 100%

26 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 ._op import OpRunUnaryNum, RuntimeTypeError 

9 

10 

11class Imputer(OpRunUnaryNum): 

12 

13 atts = {'imputed_value_floats': numpy.empty(0, dtype=numpy.float32), 

14 'imputed_value_int64s': numpy.empty(0, dtype=numpy.int64), 

15 'replaced_value_float': 0., 

16 'replaced_value_int64': 0} 

17 

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

19 OpRunUnaryNum.__init__(self, onnx_node, desc=desc, 

20 expected_attributes=Imputer.atts, 

21 **options) 

22 if len(self.imputed_value_floats) > 0: 

23 self.values = self.imputed_value_floats 

24 self.replace = self.replaced_value_float 

25 elif len(self.imputed_value_int64s) > 0: 

26 self.values = self.imputed_value_int64s 

27 self.replace = self.replaced_value_int64 

28 else: 

29 raise ValueError("Missing are not defined.") # pragma: no cover 

30 

31 def _run(self, x, attributes=None, verbose=0, fLOG=None): # pylint: disable=W0221 

32 if len(x.shape) != 2: 

33 raise RuntimeTypeError( 

34 f"x must be a matrix but shape is {x.shape}") 

35 if self.values.shape[0] not in (x.shape[1], 1): 

36 raise RuntimeTypeError( # pragma: no cover 

37 f"Dimension mismatch {self.values.shape[0]} != {x.shape[1]}") 

38 x = x.copy() 

39 if numpy.isnan(self.replace): 

40 for i in range(0, x.shape[1]): 

41 val = self.values[min(i, self.values.shape[0] - 1)] 

42 x[numpy.isnan(x[:, i]), i] = val 

43 else: 

44 for i in range(0, x.shape[1]): 

45 val = self.values[min(i, self.values.shape[0] - 1)] 

46 x[x[:, i] == self.replace, i] = val 

47 

48 return (x, )