Coverage for kwave/utils/checkutils.py: 31%

1 from typing import List

2

3 import numpy as np

4

5

6 def enforce_fields(dictionary, *fields):

7 # from kwave

8 for f in fields:

9 assert f in dictionary.keys(), [f'The field {f} must be defined in the given dictionary']

10

11

12 def enforce_fields_obj(obj, *fields):

13 # from kwave

14 for f in fields:

15 assert getattr(obj, f) is not None, f'The field {f} must be not None in the given object'

16

17

18 def check_field_names(dictionary, *fields):

19 # from kwave

20 for k in dictionary.keys():

21 assert k in fields, f'The field {k} is not a valid field for the given dictionary'

22

23

24 def num_dim(x):

25 # get the size collapsing any singleton dimensions

26 return len(x.squeeze().shape)

27

28

29 def num_dim2(x: np.ndarray):

30 # get the size collapsing any singleton dimensions

31 sz = np.squeeze(x).shape

32

33 if len(sz) > 2:

34 return len(sz)

35 else:

36 return np.sum(np.array(sz) > 1)

37

38

39 def check_str_eq(value, target: str):

40 """

41 String equality check only if the value is string. Helps to avoid FutureWarnings when value is not a string.

42 Added by @Farid

43 Args:

44 value:

45 target:

46

47 Returns:

48

49 """

50 return isinstance(value, str) and value == target

51

52

53 def check_str_in(value, target: List[str]):

54 """

55 Check if value is in the given list only if the value is string.

56 Helps to avoid FutureWarnings when value is not a string.

57 Added by @Farid

58 Args:

59 value:

60 target:

61

62 Returns:

63

64 """

65 # added by Farid

66 return isinstance(value, str) and value in target

67

68

69 def is_number(value):

70 if value is None:

71 return False

72 if isinstance(value, (int, float)):

73 return True

74 if isinstance(value, str):

75 return False

76 if value.dtype in [np.float32, np.float64]:

77 return True

78 return np.issubdtype(np.array(value), np.number)