Coverage for suppy\utils\_decorators.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2026-05-08 13:56 +0200

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

2 

3import warnings 

4import functools 

5from typing import Callable 

6import numpy as np 

7 

8 

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

10 """ 

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

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

13 to float64. 

14 

15 Parameters 

16 ---------- 

17 func : Callable 

18 The function to be decorated. 

19 

20 Returns 

21 ------- 

22 Callable 

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

24 

25 Raises 

26 ------ 

27 TypeError 

28 If the input array cannot be converted to float64. 

29 

30 Warnings 

31 -------- 

32 UserWarning 

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

34 """ 

35 

36 @functools.wraps(func) 

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

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

39 warnings.warn( 

40 "Array is not of type float32 or float64, converting to float64", 

41 stacklevel=2, 

42 ) 

43 try: 

44 arr = arr.astype(np.float64) 

45 except (TypeError, ValueError) as e: 

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

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

48 

49 return wrapper