Coverage for C:\Users\t590r\Documents\GitHub\suppy\suppy\utils\_decorators.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2025-02-05 10:12 +0100

1"""File that includes somme decorators used throughout the module.""" 

2 

3import warnings 

4from typing import Callable 

5import numpy as np 

6 

7 

8def ensure_float_array(func: Callable) -> Callable: 

9 """ 

10 Decorator to ensure that the input array is of type float32 or float64. 

11 If the input array is not of type float32 or float64, it will be converted 

12 to float64. 

13 

14 Parameters 

15 ---------- 

16 func : Callable 

17 The function to be decorated. 

18 

19 Returns 

20 ------- 

21 Callable 

22 The decorated function which ensures the input array is of type float32 or float64. 

23 

24 Raises 

25 ------ 

26 TypeError 

27 If the input array cannot be converted to float64. 

28 

29 Warnings 

30 -------- 

31 UserWarning 

32 If the input array is not of type float32 or float64 and needs to be converted. 

33 """ 

34 

35 def wrapper(self, arr, *args, **kwargs): 

36 if arr.dtype not in [np.float32, np.float64]: 

37 warnings.warn("Array is not of type float32 or float64, converting to float64") 

38 try: 

39 arr = arr.astype(np.float64) 

40 except Exception as e: 

41 raise TypeError("Failed to convert array to float64") from e 

42 return func(self, arr, *args, **kwargs) 

43 

44 return wrapper