sweetpea.primitives module¶
This module provides the fundamental primitives used by the SweetPea domain-specific language.
A Note on Deprecations¶
A number of functions, classes, methods, and attributes in this file are marked as deprecated. These are preserved for backwards compatibility, but will eventually be removed in favor of alternative forms.
- class sweetpea.primitives.Level(*_, **__)¶
Bases:
objectA discrete value that a
Factorcan hold.Note
Do not directly instantiate
Level. Instead, construct one of the subclasses:For more on levels, consult the guide.
- internal_name: str¶
The internal name, which is meant to be unique among levels.
Deprecated since version 0.1.0: This attribute will be removed. It should not be used outside of this module.
- factor: sweetpea.primitives.Factor¶
The factor to which this level belongs.
- property weight: int¶
The level’s weight.
- property external_name: str¶
An alias for
Level.name.Deprecated since version 0.1.0: This property will be removed in favor of
Level.name.
- class sweetpea.primitives.SimpleLevel(*_, **__)¶
Bases:
sweetpea.primitives.LevelA simple
Level, which only has a name.For example, in a simple Stroop experiment, a
colorFactormay consist of a few simple levels such asred,blue, andgreen.- Parameters
name – The name of the level.
- weight: dataclasses.InitVar = 1¶
- class sweetpea.primitives.DerivedLevel(*_, **__)¶
Bases:
sweetpea.primitives.LevelA
Levelrepresenting the intersection of other levels, potentially from differentFactors. These are produced through use ofDerivationWindows, which are constraints on level selection during trial sampling.For more information on when to use derived levels, consult the Derivations section of the SweetPea guide.
- Parameters
name – The name of the level.
window – A
DerivationWindowused in producing a cross product of this level.
- window: sweetpea.primitives.DerivationWindow¶
The derivation window corresponding to this level.
- weight: dataclasses.InitVar = 1¶
- get_dependent_cross_product()¶
Produces a list of n-tuples, where each tuple represents a unique combination of
Levelselections among all possibilities.For instance, if we have two
Factors, each with someLevels:Factor
Levels
color
red, blue, green
value
1, 2
Then the following list of tuples is returned:
[(red, 1), (red, 2), (blue, 1), (blue, 2), (green, 1), (green, 2)]
Tip
You can access a
Level’s correspondingFactorvia theLevel.factorattribute.- Return type
typing.List[typing.Tuple[Level, …]]
- class sweetpea.primitives.ElseLevel(*_, **__)¶
Bases:
sweetpea.primitives.LevelA
Levelfor… something.- Parameters
name – The name of the level.
- weight: dataclasses.InitVar = 1¶
- derive_level_from_levels(other_levels)¶
Converts the
ElseLevelinto aDerivedLevelby combining it with other specifiedDerivedLevels.- Parameters
other_levels (List[sweetpea.primitives.DerivedLevel]) – A list of
DerivedLevelsfor use in constructing the new level.- Return type
- class sweetpea.primitives.Factor(name, initial_levels, *_, **__)¶
Bases:
objectAn independent variable in a factorial experiment. Factors are composed of
Levelsand come in two flavors:However, both classes can be implicitly constructed by the base
Factorconstructor, so we recommend you always use that for creating factors.During
Factorconstruction, the firstLevelin theinitial_levelsis dynamically type-checked. If it’s aDerivedLevelorElseLevel, a .DerivedFactor will be initialized. Otherwise, a .SimpleFactor will be initialized.In all cases, the
initial_levelswill be processed. This step ensures that all of a factor’slevelswill always beLevels. The levels are processed according to these rules:A
Levelinstance is left alone.A
strinstance is converted into aSimpleLevel.A
tupleorlistconsisting of exactly onestrfollowed by oneintwill be converted into aSimpleLevelwith thestras its name and theintas its weight.Anything else is converted into a
SimpleLevelby using its string representation as a level name.
Note
The
DerivedFactorsubclass does additional processing after these steps.- Parameters
name (str) – The name of this factor.
initial_levels (typing.Sequence[Any]) – The levels comprising this factor. The list can be made of anything, but any values in the list that are not instances of
Levelor one of its subclasses will be converted intoSimpleLevelinstances by using their string representation, as determined bySimpleLevel(str(value)).
- Return type
Tip
See the Factorial Experiment Design section of the SweetPea guide for more about factors, levels, and how to use them.
- initial_levels: dataclasses.InitVar¶
The levels used during factor initialization.
- levels: Sequence[sweetpea.primitives.Level]¶
The discrete values that this factor can have.
- get_level(name)¶
Returns a
Levelinstance corresponding to the given name, if it exists within this factor. Otherwise, returnsNone.- Parameters
name (str) –
- Return type
Optional[sweetpea.primitives.Level]
- is_derived()¶
Whether this factor is derived.
Deprecated since version 0.1.0: Instead of using this function, we recommend doing a dynamic type check with
isinstance(). This provides the same semantic information to the programmer while also providing greater type guarantees when using a static type checker, such as mypy.factor: Factor = ... if isinstance(factor, DerivedFactor): # Code requiring a derived factor. ... else: # Code if it's not a derived factor. ...
- Return type
- property has_complex_window: bool¶
Whether this factor has a complex derivation window.
A complex derivation window is a window of derived factors whose first-level derivations are themselves considered complex.
- applies_to_trial(trial_number)¶
Whether this factor applies to the given trial. For example, factors with
TransitionDerivationderivations in their levels do not apply to trial number1, but do apply to all subsequent trials.Tip
Trials start their numbering at
1.
- property factor_name: str¶
An alias for
Factor.namefor backwards compatibility.Deprecated since version 0.1.0: This property will be removed in favor of
Factor.name.
- class sweetpea.primitives.SimpleFactor(name, initial_levels, *_, **__)¶
Bases:
sweetpea.primitives.FactorA
Factorcomprised ofSimpleLevels. If a subclass ofLevelis passed in that is not aSimpleLevel, an error is raised during initialization.- Parameters
name (str) – The name of this factor.
initial_levels (typing.Sequence[SimpleLevel]) – The levels comprising this factor.
- Return type
- levels: Sequence[sweetpea.primitives.SimpleLevel]¶
The discrete values that this factor can have.
- get_level(name)¶
Returns a
Levelinstance corresponding to the given name, if it exists within this factor. Otherwise, returnsNone.- Parameters
name (str) –
- Return type
Optional[sweetpea.primitives.SimpleLevel]
- class sweetpea.primitives.DerivedFactor(name, initial_levels, *_, **__)¶
Bases:
sweetpea.primitives.FactorA
Factorcomposed ofDerivedLevels.After doing the level processing described by
Factor, an additional step is taken: allElseLevelsare converted intoDerivedLevels. This is done by callingElseLevel.derive_level_from_levels(), supplying all the naturalDerivedLevelsthat were passed in.- Parameters
name (str) – The name of this factor.
initial_levels (typing.Sequence[typing.Union[DerivedLevel, ElseLevel]]) –
The levels comprising this factor. The levels may be instances of either
DerivedLevelsorElseLevels.Note
Any given
ElseLevelwill be converted to aDerivedLevelby way ofElseLevel.derive_level_from_levels(), so that theFactor.levelslist will only containDerivedLevels.
- Return type
- levels: Sequence[sweetpea.primitives.DerivedLevel]¶
The discrete values that this factor can have.
- get_level(name)¶
Returns a
Levelinstance corresponding to the given name, if it exists within this factor. Otherwise, returnsNone.- Parameters
name (str) –
- Return type
Optional[sweetpea.primitives.DerivedLevel]
- class sweetpea.primitives.DerivationWindow(predicate, factors, width, stride)¶
Bases:
objectA mechanism to specify the window of a derivation. Derivation windows are used to inform a derivation based on levels from other factors in the current trial and, potentially, multiple preceding trials.
For detailed information, see the SweetPea guide’s section on derivation.
- Parameters
predicate (typing.Callable[[Any, ...], bool]) –
A predicate function used during derivation. Different derivation windows require different forms of predicates.
Warning
The type of arguments passed to the
predicatevaries by Derivation subclass. Read their documentation carefully!factors (typing.Sequence[Factor]) – The factors upon which this derivation window depends.
width (int) – The number of trials that this derivation window depends upon. The current trial must be included, so the number must be greater than or equal to
1.stride (int) – TODO DOC
- Return type
- predicate: Callable¶
The predicate used during derivation with this window.
- factors: List[sweetpea.primitives.Factor]¶
The factors upon which this derivation depends.
- initial_factor_count: int¶
The number of factors that originally came in, disregarding any expansion caused by a
DerivedLevelchanging things.
- property size: Tuple[int, int]¶
The width and stride of this derivation window as a pair.
- property first_factor: sweetpea.primitives.Factor¶
The first
Factorin the internal list of Factors.- Return type
- property is_complex: bool¶
Whether this derivation window is considered complex. A complex derivation windowis one for which at least one of the following is true:
The window’s
widthis greater than1.The window’s
strideis greater than1.The window’s
first_factoris a complex derived factor.
- Return type
- property args: List[sweetpea.primitives.Factor]¶
An alias for
DerivationWindow.factors.- Return type
Deprecated since version 0.1.0: This property will be removed in favor of
DerivationWindow.factors.
- property fn: Callable¶
An alias for
DerivationWindow.predicate.- Return type
Callable
Deprecated since version 0.1.0: This property will be removed in favor of
DerivationWindow.predicate.
- class sweetpea.primitives.WithinTrialDerivationWindow(predicate, factors)¶
Bases:
sweetpea.primitives.DerivationWindowA derivation window that depends on levels from other factors, all within the same trial.
- Parameters
- Return type
- class sweetpea.primitives.TransitionDerivationWindow(predicate, factors)¶
Bases:
sweetpea.primitives.DerivationWindowA derivation window that depends on levels from other factors in the current trial and the immediately preceding trial.
TransitionDerivationsare used to constrain the transition points between trials.- Parameters
predicate (typing.Callable[[typing.Sequence[Factor], ...], bool]) – A predicate that takes as many lists of factors as factors in this derivation window.
factors (typing.Sequence[Factor]) – The factors upon which this derivation window depends.
- Return type
- sweetpea.primitives.get_external_level_name(level)¶
Returns
Level.name.Deprecated since version 0.1.0: This function will be removed in favor of
Level.name.- Parameters
level (sweetpea.primitives.Level) –
- Return type
- sweetpea.primitives.get_internal_level_name(level)¶
Returns
Level.internal_name.Deprecated since version 0.1.0: This function will be removed. It should not be used outside of this module.
- Parameters
level (sweetpea.primitives.Level) –
- Return type
- sweetpea.primitives.WithinTrial¶
An alias for
WithinTrialDerivationWindow.Deprecated since version 0.1.0: This class will be removed in favor of
WithinTrialDerivationWindow.
- sweetpea.primitives.Transition¶
An alias for
TransitionDerivationWindow.Deprecated since version 0.1.0: This class will be removed in favor of
TransitionDerivationWindow.
- sweetpea.primitives.Window¶
An alias for
DerivationWindow.Deprecated since version 0.1.0: This class will be removed in favor of
DerivationWindow.alias of
sweetpea.primitives.DerivationWindow
- sweetpea.primitives.simple_level(name, weight=None)¶
A function alias for
SimpleLevel.Deprecated since version 0.1.2: This function will be removed in favor of direct instantiation of
SimpleLevel.- Parameters
- Return type
- sweetpea.primitives.derived_level(name, derivation, weight=None)¶
A function alias for
DerivedLevel.Deprecated since version 0.1.2: This function will be removed in favor of direct instantiation of
DerivedLevel.- Parameters
name (str) – The name of the level.
derivation (sweetpea.primitives.DerivationWindow) – The derivation window for this level.
weight (Optional[int]) – An optional weight for the level.
- Return type
- sweetpea.primitives.else_level(name, weight=None)¶
A function alias for
ElseLevel.Deprecated since version 0.1.2: This function will be removed in favor of direct instantiation of
ElseLevel.- Parameters
- Return type
- sweetpea.primitives.factor(name, levels)¶
A function alias for
Factor.Deprecated since version 0.1.2: This function will be removed in favor of direct instantiation of
Factor.- Parameters
name (str) – The name of the factor.
levels (Sequence[Any]) – A sequence of levels for the factor.
- Return type
- sweetpea.primitives.within_trial(fn, args)¶
A function alias for
WithinTrialDerivationWindow.Deprecated since version 0.1.2: This function will be removed in favor of direct instantiation of
WithinTrialDerivationWindow.- Parameters
fn (Callable) – A predicate function. See
WithinTrialDerivationWindow.args (List[sweetpea.primitives.Factor]) – A list of
Factors. SeeWithinTrialDerivationWindow.
- Return type
- sweetpea.primitives.transition(fn, args)¶
A function alias for
TransitionDerivationWindow.Deprecated since version 0.1.2: This function will be removed in favor of direct instantiation of
TransitionDerivationWindow.- Parameters
fn (Callable) – A predicate function. See
TransitionDerivationWindow.args (List[sweetpea.primitives.Factor]) – A list of
Factors. SeeTransitionDerivationWindow.
- Return type
- sweetpea.primitives.window(fn, args, width, stride)¶
A function alias for
DerivationWindow.Deprecated since version 0.1.2: This function will be removed in favor of direct instantiation of
DerivationWindow.- Parameters
fn (Callable) – A predicate function. See
DerivationWindow.args (List[sweetpea.primitives.Factor]) – A list of
Factors. SeeDerivationWindow.width (int) – The width of the window.
stride (int) – The stride of the window.
- Return type