Coverage for kwave/utils/checkutils.py: 31%
32 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-24 11:55 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-24 11:55 -0700
1from typing import List
3import numpy as np
6def 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']
12def 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'
18def 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'
24def num_dim(x):
25 # get the size collapsing any singleton dimensions
26 return len(x.squeeze().shape)
29def num_dim2(x: np.ndarray):
30 # get the size collapsing any singleton dimensions
31 sz = np.squeeze(x).shape
33 if len(sz) > 2:
34 return len(sz)
35 else:
36 return np.sum(np.array(sz) > 1)
39def 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:
47 Returns:
49 """
50 return isinstance(value, str) and value == target
53def 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:
62 Returns:
64 """
65 # added by Farid
66 return isinstance(value, str) and value in target
69def 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)