Parameters
This module contains classes related to parameters and parameter generation.
The goal of this module is to abstract away how parameters are generated from the user. In terms of parameters the only things a user should see is the ParameterGenerator that offers an API for managing parameters and generates individual Combinations (the second object a user should ever see).
Combination
Bases: object
Class representing a combination of parameters.
This class represents a combination of parameters generated by a class of type ParameterGenerator. The only time a user should ever get an instance of a Combination from the ParameterGenerator is when a combination of parameters is VALID.
Source code in maestrowf/datastructures/core/parameters.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
__init__(token='$')
Initialize an empty Combination class.
A Combination comes packed with the following: - Corresponding values for each parameter in the instance. - A name for each parameter in instance. - Labels for each parameter-value combination in the instance.
The 'token' method parameter defines the character(s) that are expected in front of user parameterized values in strings when an instance of a Combination is applied. For example, assume that we have an instance 'c' of the Combination class that has had the parameter 'PARAM1' added. 'PARAM1' is named 'COMPONENT1' and 'token' is left at its default value of '$'. 'PARAM1' has some arbitrary value that was set. In order to substitute the different variations of 'PARAM1' in a string when the apply method is called, the user would include the following mark up:
- The value of 'PARAM1': '$(PARAM1)'
- The label of 'PARAM1': '$(PARAM1.label)'
- The name of 'PARAM1': '$(PARAM1.name)'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token |
Token expected to be found in front of a parameter. |
'$'
|
Source code in maestrowf/datastructures/core/parameters.py
__str__()
Generate the string representation of a Combination object.
Returns:
Type | Description |
---|---|
A string representing the combination. |
add(key, name, value, label)
Add a parameter to the Combination object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Parameter key that identifies a replacement. |
required | |
name |
Custom name that identifies a parameter. |
required | |
value |
Value of the parameter in this combination. |
required | |
label |
Value of the parameter label for this combination. |
required |
Source code in maestrowf/datastructures/core/parameters.py
apply(item)
Apply the combination to an item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
String that may contain parameters to be substituted. |
required |
Returns:
Type | Description |
---|---|
String equal to item, except with parameters replaced. |
Source code in maestrowf/datastructures/core/parameters.py
get_param_string(params)
Get the combination string for the specified parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
A set of parameters to be used in the string. |
required |
Returns:
Type | Description |
---|---|
A string containing the labels for the parameters in params. |
Source code in maestrowf/datastructures/core/parameters.py
get_param_values(params)
Get the values for the specified parameters.
:yields: Tuples of param names and values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
A set of parameters to be used in the string. |
required |
Source code in maestrowf/datastructures/core/parameters.py
ParameterGenerator
Class for containing parameters and generating combinations.
The goal of this class is to provide one centralized location for managing and storing parameters. This implementation of the ParameterGenerator, currently, is very basic. It takes lists of parameters and uses those to construct combinations, meaning that if you were to view this as an Excel table, you would have a row for each valid combination you wanted to study.
The other goal is to make it so that by having the ParameterGenerator manage parameters, functionality can be added without affecting how the end user interacts with this class. The ParameterGenerator has an Iterator defined and will generate each combination one by one. The end user should NEVER SEE AN INVALID COMBINATION. Because this class generates the combinations as specified by the parameters added (eventually with types or enforced inheritance), and eventually constraints, it opens up being able to quietly change how this class generates its combinations.
Easily convert studies to other types of studies. Because the API doesn't change from its nice Pythonic style, you can in theory swap out a ParameterGenerator that performs completely differently. All of a sudden, you can get the following for simply deriving from this class:
- Uncertainty Quantification (UQ): Add the ability to statistically sample parameters behind the scenes. Let the ParameterGenerator constraint solve behind the scenes and return the Combination objects it was going to return in the first place. If you can't find a valid sampling, just return nothing and the study won't run.
- Boundary and constraint testing: Like UQ above, hide the solving from the user. Simply add parameters to be constraint solved on behind the API and all the user sees is combinations on the frontend.
Ideally, all parameter generation schemes should boil down as follows:
- Derive from this class, add constraint solving.
- Construct a study how you would otherwise do so, just use the new ParameterGenerator and add parameters.
- Setup, stage, and execute your study.
- Profit.
Source code in maestrowf/datastructures/core/parameters.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
|
__bool__()
Override for the bool operator.
Returns:
Type | Description |
---|---|
True if the ParameterGenerator instance has values, False otherwise. |
__init__(token='$', ltoken='%%')
Initialize an empty ParameterGenerator object.
The ParameterGenerator is instantiated with two token values, one for parameters and one for labels. The 'token' parameter represents the character(s) expected in front of parameterized strings. For example, if 'token' is left at its default of '$' and we have a parameter named 'COMP1', then the instance of the ParameterGenerator will replace the value '$(COMP1)' in any item passed to the apply method. The 'ltoken' parameter functions in much the same way, except that instead of substituting for a parameter, this character(s) is what is found in a parameter label. The label for the parameter 'COMP1' is specified as '$(COMP.label)' where the label may have a value of 'COMP1.%%' (where %% is the default value of ltoken). For any combination, '%%' will be replaced by the value of the parameter 'COMP1' for that given instance when the label is specified in a item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token |
Leading token that denotes a parameter (Default: '$'). |
'$'
|
|
ltoken |
Token that represents where to place a value in a label (Default: '%%'). |
'%%'
|
Source code in maestrowf/datastructures/core/parameters.py
__iter__()
Return the iterator for the ParameterGenerator.
Returns:
Type | Description |
---|---|
Iterator for walking parameter combinations. |
add_parameter(key, values, label=None, name=None)
Add a parameter to the ParameterGenerator.
Currently, all parameters added to a ParameterGenerator instance must have a list of values that are the same length. Future improvements will add the ability to specify either types of parameters or provide different ParameterGenerators derivations that have unique behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Parameter key to find for replacement. |
required | |
values |
List of values the parameter can take. |
required | |
label |
Label string for labeling the parameter. |
None
|
|
name |
Custom name for identifying parameter. |
None
|
Source code in maestrowf/datastructures/core/parameters.py
get_combinations()
Generate all combinations of parameters.
Returns:
Type | Description |
---|---|
A generator with all combinations of parameters. |
Source code in maestrowf/datastructures/core/parameters.py
get_metadata()
Produce metadata for the parameters in a generator instance.
Returns:
Type | Description |
---|---|
A dictionary containing metadata about the instance. |
Source code in maestrowf/datastructures/core/parameters.py
get_used_parameters(step)
Return the parameters used by a StudyStep.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step |
A StudyStep instance to be checked. |
required |
Returns:
Type | Description |
---|---|
A set of the parameter names used within the step parameter. |