metawards.VariableSet¶
- 
class metawards.VariableSet(variables: Optional[Dict[str, float]] = None, repeat_index: int = 1, names: Optional[List[str]] = None, values: Optional[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: Optional[Dict[str, float]] = None, repeat_index: int = 1, names: Optional[List[str]] = None, values: Optional[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 - __delattr__(name, /)- Implement delattr(self, name). - __dir__()- Default dir() implementation. - __eq__(other)- Return self==value. - __format__(format_spec, /)- Default object formatter. - __ge__(value, /)- Return self>=value. - __getattribute__(name, /)- Return getattr(self, name). - __getitem__(key)- __gt__(value, /)- Return self>value. - __init__([variables, repeat_index, names, …])- Construct a new VariableSet from the passed adjusted variable values. - __init_subclass__- This method is called when a class is subclassed. - __le__(value, /)- Return self<=value. - __len__()- __lt__(value, /)- Return self<value. - __ne__(value, /)- Return self!=value. - __new__(**kwargs)- Create and return a new object. - __reduce__()- Helper for pickle. - __reduce_ex__(protocol, /)- Helper for pickle. - __repr__()- Return repr(self). - __setattr__(name, value, /)- Implement setattr(self, name, value). - __setitem__(key, value)- __sizeof__()- Size of object in memory, in bytes. - __str__()- Return a printable representation of the variables to be adjusted - __subclasshook__- Abstract classes can override this to customize issubclass(). - _add(name, value)- Internal function to add a new variable called ‘name’ to be varied - it will be set equal to ‘value’ - _extract_values(fingerprint)- adjust(params)- Use the variables in this set to adjust the passed parameters. - 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. - from_data(data)- Build a VariableSet from the passed data dictionary (which may have been deserialised from json - from_json(s)- Construct and return VariableSet loaded from the passed json file - get_value(key)- Return the value of ‘key’ - is_empty()- Return whether or not there is nothing to change - make_compatible_with(other)- Return a copy of this VariableSet which has been made compatible with ‘other’. - Return the output directory in which runs using this variable set should be placed. - read(filename)- Read a single set of adjustable variables from the passed file. - Return the repeat index of this set. - set_value(key, value)- Set the value of ‘key’ to ‘value’ - to_data()- Return a data dictionary that can be serialised to json - to_json([filename, indent, auto_bzip])- Serialise the VariableSet to JSON. - Return the names of the variables that will be adjusted by this VariableSet - Return the values that the variables will be adjusted to. - Return the variables (name and values) to be adjusted - Attributes - __dict__- __doc__- __hash__- __module__- __weakref__- list of weak references to the object (if defined) - 
__init__(variables: Optional[Dict[str, float]] = None, repeat_index: int = 1, names: Optional[List[str]] = None, values: Optional[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] 
 - 
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: Optional[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 
 
 - 
static from_data(data)[source]¶
- Build a VariableSet from the passed data dictionary (which may have been deserialised from json 
 - 
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
 - 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] 
 - 
output_dir()[source]¶
- Return the output directory in which runs using this variable set should be placed. Normally this would be the fingerprint of the variable set, but users may prefer to specify their own naming scheme, which can be added via a design file 
 - 
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
 
 - 
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 
 
 - 
to_json(filename: Optional[str] = None, indent: Optional[int] = None, auto_bzip: bool = True) → str[source]¶
- Serialise the VariableSet to JSON. This will write to a file if filename is set, otherwise it will return a JSON string. - Parameters
- filename (str) – The name of the file to write the JSON to. The absolute path to the written file will be returned. If filename is None then this will serialise to a JSON string which will be returned. 
- indent (int) – The number of spaces of indent to use when writing the json 
- auto_bzip (bool) – Whether or not to automatically bzip2 the written json file 
 
- Returns
- Returns either the absolute path to the written file, or the json-serialised string 
- Return type
- str 
 
 - 
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] 
 
 
-