metawards.VariableSet¶
-
class
metawards.
VariableSet
(variables: Dict[str, float] = None, repeat_index: int = 1, names: List[str] = None, values: List[float] = None)[source]¶ This class holds a single set of adjustable variables that are used to adjust the variables as part of a model run
Examples
>>> v = VariableSet() >>> v["beta[1]"] = 0.95 >>> v["beta[2]"] = 0.9 >>> print(v.fingerprint()) (beta[1]=0.95, beta[2]=0.9)[repeat 1] >>> params = Parameters() >>> params.set_disease("ncov") >>> v.adjust(params) >>> print(params.disease_params.beta[1], >>> params.disease_params.beta[2]) 0.95 0.9
-
__init__
(variables: Dict[str, float] = None, repeat_index: int = 1, names: List[str] = None, values: List[float] = None)[source]¶ Construct a new VariableSet from the passed adjusted variable values.
Parameters: - names (List[str]) – The list of the names of the variables to adjust
- values (List[float]) – The values of the variables to adjust (same order as names)
- variables (Dict[str, float]) – names and values of variables to adjust passed as a dictionary
- repeat_index (int) – the index used to distinguish different repeats of the same VariableSet from one another
Examples
>>> v = VariableSet() >>> v["beta[1]"] = 0.95 >>> v["beta[2]"] = 0.9 >>> print(v.fingerprint()) (beta[1]=0.95, beta[2]=0.9)[repeat 1]
Methods
__init__
(variables, float] = None, …)Construct a new VariableSet from the passed adjusted variable values. adjust
(params)Use the variables in this set to adjust the passed parameters. adjustable_help
()Return a string that contains some help that is useful for finding out which variables can be adjusted create_fingerprint
(vals, index, include_index)Create the fingerprint for the passed values extract_values
(fingerprint)Return the original values from the passed fingerprint or filename. fingerprint
(include_index)Return a fingerprint for this VariableSet. make_compatible_with
(other)Return a copy of this VariableSet which has been made compatible with ‘other’. read
(filename)Read a single set of adjustable variables from the passed file. repeat_index
()Return the repeat index of this set. variable_names
()Return the names of the variables that will be adjusted by this VariableSet variable_values
()Return the values that the variables will be adjusted to. variables
()Return the variables (name and values) to be adjusted -
adjust
(params)[source]¶ Use the variables in this set to adjust the passed parameters. Note that this directly modifies ‘params’
Parameters: params (Parameters) – The parameters whose variables will be adjusted Returns: Return type: None Examples
>>> v = VariableSet() >>> v["beta[1]"] = 0.95 >>> v["beta[2]"] = 0.9 >>> print(v.fingerprint()) (beta[1]=0.95, beta[2]=0.9)[repeat 1] >>> params = Parameters() >>> params.set_disease("ncov") >>> v.adjust(params) >>> print(params.disease_params.beta[1], >>> params.disease_params.beta[2]) 0.95 0.9
-
static
adjustable_help
()[source]¶ Return a string that contains some help that is useful for finding out which variables can be adjusted
-
static
create_fingerprint
(vals: List[float], index: int = None, include_index: bool = False)[source]¶ Create the fingerprint for the passed values
-
static
extract_values
(fingerprint: str)[source]¶ Return the original values from the passed fingerprint or filename. This assumes that the fingerprint was created using the ‘fingerprint’ function, namely that any integers are actually 0.INTEGER
Parameters: fingerprint (str) – The fingerprint (or filename) to decode Returns: (values, repeat) – The list of values of the variables and the repeat index. The repeat index is None if it wasn’t included in the fingerprint Return type: (List[float], int)
-
fingerprint
(include_index: bool = False)[source]¶ Return a fingerprint for this VariableSet. This can be used to quickly identify and distinguish the values of the variables in this set from the values in other VariableSets which have the same adjustable variables, but different parameters
Parameters: include_index (bool) – Whether or not to include the repeat_index in the fingerprint Returns: fingerprint – The fingerprint for this VariableSet Return type: str
-
make_compatible_with
(other)[source]¶ Return a copy of this VariableSet which has been made compatible with ‘other’. This means that it will change the same variables as ‘other’, e.g. by adding ‘None’ changes for missing variables. This will raise an error if it is not possible to make this set compatible
Parameters: other (VariableSet) – The passed VariableSet for which this should be made compatible Returns: result – A copy of this VariableSet which is now compatible with ‘other’ Return type: VariableSet Example
>>> v1 = VariableSet() >>> v1["beta[1]"] = 0.9 >>> v1["beta[2]"] = 0.8
>>> v2 = VariableSet() >>> v2["beta[1]"] = 0.6 >>> v2 = v2.make_compatible_with(v1) >>> print(v2) (beta[1]=0.6, beta[2]=0.8)[repeat 1]
-
static
read
(filename: str)[source]¶ Read a single set of adjustable variables from the passed file. The file can either write the variables in horizontal or vertical mode, using space or comma separated values.
This is useful for when you want to set a global set of parameters at the start of a calculation and don’t want to use a large VariableSets
Parameters: filename (str) – The name of the file containing the VariableSet Returns: variables – The VariableSet that has been read Return type: VariableSet
-
repeat_index
()[source]¶ Return the repeat index of this set. The repeat index is the ID of this set if the VariableSet is repeated. The index should range from 1 to nrepeats
Returns: index – The repeat index of this set Return type: int
-
variable_names
()[source]¶ Return the names of the variables that will be adjusted by this VariableSet
Returns: names – The list of names of variables to be adjusted Return type: List[str]
-