Coverage for /home/andrew/Documents/Research/gwent/gwent/utils.py : 90%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import astropy.units as u
2import numpy as np
5def make_quant(param, default_unit):
6 """Convenience function to intialize a parameter as an astropy quantity.
8 Parameters
9 ----------
10 param : float, or Astropy Quantity
11 Parameter to initialize
12 default_unit : str
13 Astropy unit string, sets as default for param.
15 Returns
16 -------
17 an astropy quantity
19 Examples
20 --------
21 self.f0 = make_quant(f0,'MHz')
23 Notes
24 -----
25 Taken from <https://github.com/Hazboun6/hasasia/blob/master/hasasia/sensitivity.py#L834>
27 """
28 default_unit = u.core.Unit(default_unit)
29 if hasattr(param, "unit"):
30 try:
31 quantity = param.to(default_unit)
32 except u.UnitConversionError:
33 raise ValueError(
34 "Quantity {0} with incompatible unit {1}".format(param, default_unit)
35 )
36 else:
37 quantity = param * default_unit
39 return quantity
42def Get_Var_Dict(obj, value):
43 """Updates and initializes variable dictionaries used to keep track of
44 current values and variable minima and maxima.
46 Parameters
47 ----------
48 obj : object
49 Instance of class with parameter variables
50 value : array-like
51 value(s) that are assigned into dictionary
53 Notes
54 -----
55 value contains the variable name in the first index
56 the next is the current value of the variable
57 the last two are optional and contain the variable min and max
59 Examples
60 --------
61 obj.var_dict = ['M',value]
62 where obj is in this case an instance of a BinaryBlackHole
64 """
65 if not hasattr(obj, "var_dict"):
66 obj._var_dict = {}
67 if isinstance(value, list):
68 if len(value) == 2 and isinstance(value[0], str):
69 var_name = value[0]
70 vals = value[1]
71 if isinstance(vals, u.Quantity):
72 no_unit_vals = vals.value
73 else:
74 no_unit_vals = vals
76 if isinstance(no_unit_vals, list) and len(no_unit_vals) == 3:
77 if hasattr(obj, "n_p"):
78 if obj.n_p == len(no_unit_vals):
79 raise ValueError(LenError_1())
80 if (
81 isinstance(vals[0], (float, int, u.Quantity))
82 and isinstance(vals[1], (float, int, u.Quantity))
83 and isinstance(vals[2], (float, int, u.Quantity))
84 ):
85 obj._return_value = vals[0]
86 obj._var_dict[var_name] = {
87 "val": vals[0],
88 "min": vals[1],
89 "max": vals[2],
90 "sampled": False,
91 }
92 else:
93 raise ValueError(DictError_3())
94 elif (
95 isinstance(no_unit_vals, (list, np.ndarray)) and len(no_unit_vals) != 3
96 ):
97 if var_name in obj._var_dict.keys():
98 obj._var_dict[var_name]["val"] = vals
99 else:
100 if len(no_unit_vals) == 2:
101 obj.var_dict[var_name] = {
102 "val": vals,
103 "min": None,
104 "max": None,
105 "sampled": True,
106 }
107 else:
108 if hasattr(obj, "n_p"):
109 if obj.n_p == len(no_unit_vals):
110 obj.var_dict[var_name] = {
111 "val": vals,
112 "min": None,
113 "max": None,
114 "sampled": False,
115 }
116 else:
117 raise ValueError(LenError_2())
118 else:
119 raise ValueError(LenError_2())
121 obj._return_value = vals
122 elif isinstance(no_unit_vals, (float, int, np.int64)):
123 if var_name in obj._var_dict.keys():
124 obj._var_dict[var_name]["val"] = vals
125 else:
126 obj.var_dict[var_name] = {
127 "val": vals,
128 "min": None,
129 "max": None,
130 "sampled": False,
131 }
132 obj._return_value = vals
133 else:
134 raise ValueError(DictError_2())
135 else:
136 raise ValueError(DictError_Full())
137 else:
138 raise ValueError(DictError_Full())
141def DictError_Full():
142 return 'Must assign either: \n\
143 - A name and value in a list (ie. ["name",val]), or \n\
144 - A name, a value, a minimum value, and maximum value in a list (ie. ["name",val,min,max]), \n\
145 where where name is a string, and val,min,and max are either floats, ints, or an astropy Quantity.'
148def DictError_3():
149 return 'Must assign a name, a value, a minimum value, and maximum value in a list (ie. ["name",val,min,max]), \n\
150 where name is a string, and val, min, and max are either floats, ints, or astropy Quantities.'
153def DictError_2():
154 return 'Must assign a name and value in a list (ie. ["name",val]) \n\
155 where name is a string, and val is either a float, an int, or an astropy Quantity.'
158def LenError_1():
159 return "Could not tell if values are pulsar values or min/maxes. Try using more than 3 pulsars."
162def LenError_2():
163 return "To assign an array of values, it must be either [min,max], or an array of individual pulsar parameters of length n_p."