Factors and Levels
- class sweetpea.Factor(name, levels)
A factor for use in an experiment design.
By default
Factor
in SweetPea always creates aDiscreteFactor
which contains a finite number of levels (Refer toContinuousFactor
for non-discrete factors). The levels of a factor can be plainLevel
values, any kind of non-Level
value (which is implicitly coerced to aLevel
value), orDerivedLevel
values. In the last case, the result is a derived factor. The levels list must either contain all derived levels or all values that are not derived levels, and derived levels must all use a compatible derivation as described in Derivations. The names of the levels must be distinct; create a level with a weight to get the effect of multiple levels with he same name.- Parameters:
name – the factor’s name
levels (List[Level]) – the factor’s levels
- get_level(name)
Finds a returns a level of the factor with a given name. If the factor has multiple levels with the same name, any one of them might be returned.
Indexing a factor with [] is the same as calling the get_level method.
- Parameters:
name – the level’s name
- Returns:
a level with the given name
- Return type:
- class sweetpea.Level(name, weight=1)
A level for use in a non-derived factor. A level object can be used for only one factor.
If weight is provided as a value greater than 1, it affects how the level is used in crossings, causing it to be combined weight times with each combination of other factors’ levels in a crossing. That’s conceptually similar to having multiple levels with the same name, but as long as the level’s factor is part of a block’s crossing, the weight crossing occurrences of the level are not considered distinct. Consequently, a sampling strategy without replacement (see
Gen
) will produce fewer samples than it would for separate levels. Along similar lines, aDerivedLevel
can have a weight greater than 1 to affect crossings, but cannot be included in a level multiple times, because each derived level’s predicate must match a different set of inputs.For a non-derived level whose factor is not crossed (or, more generally, is not in all crossings in a
MultiCrossBlock
), a weight value has the same effect as duplicating the level’s name. That is, the would-be copies are treated as distinct, which means that sampling with replacement is biased toward levels with greater weight. For sampling strategies without replacement, the weight thus increases the number of samples that are considered distinct.- Parameters:
name – the level’s name, which can be any value
weight (int) – the level’s weight
- Return type:
- property name
The level’s name, which can be any kind of value.
- class sweetpea.DerivedLevel(name, derivation, weight=1)
Creates a derived level, which depends on the levels of other factors in a design. All derived levels for one factor must use compatible derivations as described in Derivations.
- Parameters:
name – the level’s name, which can be any value
derivation (Derivation) – a condition on other factors’ levels; see Derivations
weight (int) – the level’s weight
- Returns:
a derived level
- Return type:
- class sweetpea.ElseLevel(name, weight=1)
Creates a derived level that acts as an “else” case, matching any arguments that other derived levels do not match. An “else” derived level can appear only once among the levels supplied to
Factor
, and only in combination with other derived levels. It is compatible with any derivation described in Derivations.
- class sweetpea.ContinuousFactor(name, distribution)
Sweetpea also supports a
ContinuousFactor
for factors without finite levels, which sample continuously at runtime. This is different fromDiscreteFactor
that requires a finite discrete levels during its initialization. AContinuousFactor
can dynamically generate values at runtime based on the input distribution.To initialize a
ContinuousFactor
, a distribution is required in order to generate values at runtime. The distribution must be an instance of aDistribution
. Several built-in types are available forDistribution
.UniformDistribution
Samples values from a uniform distribution within a given range.GaussianDistribution
Samples values from a normal distribution with a specified mean and standard deviation.ExponentialDistribution
Samples values from an exponential distribution with a given rate parameter.LogNormalDistribution
Samples values from a log-normal distribution with a specified mean and standard deviation.CustomDistribution
Samples values by calling a user-defined input function.If
UniformDistribution
,GaussianDistribution
,LogNormalDistribution
, orLogNormalDistribution
is used to initialize theContinuousFactor
, the factor will generate values following the corresponding distribution at runtime throughContinuousFactor.generate()
. In this case the factor is always a non-derived continuousfactor.The user can also use
CustomDistribution
, in which case the user needs to provide a custom func(Callable) to initialize theContinuousFactor
. The factor will then call func to generate values at runtime throughContinuousFactor.generate()
. In addition,CustomDistribution
can also accept an additional argument dependents that contains a list of factors that the currentContinuousFactor
depends on. The list of factors can be eitherDiscreteFactor
orContinuousFactor
in the design. In such cases, theContinuousFactor
initialized is considered a derived continuousfactor and it also suggests that func would require additional values in order to generate values at runtime. For example, when dependents constains aFactor
Color in the design, the factor_values for Color needs to be passed toContinuousFactor.generate()
.If distribution is not set or recognized, an error will be raised.
- Parameters:
name (str) – The name of the continuous factor.
distribution (Distribution) – A distribution used to generate values dynamically.
- generate(factor_values=[])
Generate values for the continuousfactor based on the input distribution.
- Parameters:
factor_values (List[Any]) – optional factor values when generating values with
CustomDistribution
. The length of factor_values needs to be the same as the dependents when intializingCustomDistribution
.- Returns:
the value for the factor
- Return type:
Any
- class sweetpea.DiscreteFactor(name, levels)
In contrast to
ContinuousFactor
that generate values dynamically, aDiscreteFactor
takes on a finite set of distinct, separate values (or levels) during its initialization. Each level can be represented using theLevel
class. ADiscreteFactor
is initialized using theFactor
class.