COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
shesha.config.config_setter_utils Namespace Reference

Functions

def enforce_int (n)
 
def enforce_float (f)
 
def enforce_or_cast_bool (b)
 
def enforce_array (data, size, dtype=np.float32, scalar_expand=False)
 
def enforce_arrayMultiDim (data, shape, dtype=np.float32)
 

Function Documentation

◆ enforce_array()

def shesha.config.config_setter_utils.enforce_array (   data,
  size,
  dtype = np.float32,
  scalar_expand = False 
)

Definition at line 66 of file config_setter_utils.py.

66 def enforce_array(data, size, dtype=np.float32, scalar_expand=False):
67  # Scalar case
68  if isinstance(data, (int, float, complex)):
69  data = [data]
70 
71  # Singleton case
72  if len(data) == 1:
73  if scalar_expand or size == 1:
74  return np.full(size, data[0], dtype=dtype)
75  else:
76  raise TypeError("This non-singleton array cannot " + \
77  "be initialized with a scalar.")
78 
79  if len(data) != size:
80  raise TypeError("Input argument has wrong number of elements.")
81  if isinstance(data, np.ndarray) and len(data.shape) > 1:
82  raise TypeError("Multidimensional ndarray input is not allowed")
83  if isinstance(data,
84  list) and not all([isinstance(x, (float, int, complex))
85  for x in data]):
86  raise TypeError("Input list may only contain numerical values.")
87 
88  # OK, now let's try it
89  return np.array(data, dtype=dtype)
90 
91 

◆ enforce_arrayMultiDim()

def shesha.config.config_setter_utils.enforce_arrayMultiDim (   data,
  shape,
  dtype = np.float32 
)

Definition at line 92 of file config_setter_utils.py.

92 def enforce_arrayMultiDim(data, shape, dtype=np.float32):
93  if not isinstance(data, np.ndarray):
94  raise TypeError("Input argument must be a np.ndarray")
95  else:
96  doRaise = False
97  if len(data.shape) != len(shape):
98  doRaise = True
99  for (i, j) in zip(data.shape, shape):
100  if j != -1 and i != j:
101  doRaise = True
102  if doRaise:
103  raise TypeError(
104  "Input has wrong dimensions, expect multi dimensional arrays")
105 
106  return np.array(data, dtype=dtype)

◆ enforce_float()

def shesha.config.config_setter_utils.enforce_float (   f)

Definition at line 47 of file config_setter_utils.py.

47 def enforce_float(f):
48  if not (isinstance(f, float) or isinstance(f, int)):
49  raise TypeError("Value should be float.")
50  return float(f)
51 
52 

◆ enforce_int()

def shesha.config.config_setter_utils.enforce_int (   n)

Definition at line 41 of file config_setter_utils.py.

41 def enforce_int(n):
42  if not isinstance(n, int):
43  raise TypeError("Value should be integer.")
44  return n
45 
46 

◆ enforce_or_cast_bool()

def shesha.config.config_setter_utils.enforce_or_cast_bool (   b)

Definition at line 53 of file config_setter_utils.py.

53 def enforce_or_cast_bool(b):
54  if isinstance(b, bool):
55  return b
56  if isinstance(b, (int, float)):
57  if b == 0:
58  return False
59  elif b == 1:
60  return True
61  else:
62  raise ValueError("Will not cast non 0/1 int or float to boolean.")
63  raise TypeError("Will only cast int and float to booleans.")
64 
65