ListRandomizer.get_condition()#

ListRandomizer.get_condition(raise_exception: bool = False) str[source]#

Returns a condition.

Parameters

raise_exception (bool) – If True, the function raises the AllSlotsFull exception instead of automatically aborting the experiment if all slots are full. This allows you to catch the exception and customize the experiment’s behavior in this case.

If all conditions are full, the experiment will be aborted. New participants will be redirected to an abort page. The text displayed on this page can be customized with the arguments ‘full_page_title’ and ‘full_page_text’.

Returns

A condition name, taken from the randomized conditions list.

Return type

str

Raises
  • AllSlotsFull – If raise_exception is True and

  • all conditions are full.

  • SlotInconsistency – If slot validation fails.

Examples

An example using the default behavior:

import alfred3 as al
exp = al.Experiment()

@exp.setup
def setup(exp):
    randomizer = al.ListRandomizer(("cond1", 10), ("cond2", 10), exp=exp)
    exp.condition = randomizer.get_condition()

@exp.member
class DemoPage(al.Page):

    def on_exp_access(self):

        if self.exp.condition == "cond1":
            lab = "label in condition 1"

        elif self.exp.condition == "cond2":
            lab = "label in condition 2"

        self += al.TextEntry(leftlab=lab, name="t1")

This is an example of customizing the ‘full’ behavior. It basically re-implements the default behavior, but showcases how you can customize behavior by catching the “AllSlotsFull” exception:

import alfred3 as al
from alfred3 import exceptions as alexcept
exp = al.Experiment()

@exp.setup
def setup(exp):
    randomizer = al.ListRandomizer(("cond1", 10), ("cond2", 10), exp=exp)
    try:
        exp.condition = randomizer.get_condition(raise_exception=True)
    except alexcept.AllSlotsFull:
        full_page = al.Page(title="Experiment closed.", name="fullpage")
        full_page += al.Text("Sorry, the experiment currently does not accept any further participants")
        exp.abort(reason="full", page=full_page)


@exp.member
class DemoPage(al.Page):

    def on_exp_access(self):

        if self.exp.condition == "cond1":
            lab = "label in condition 1"

        elif self.exp.condition == "cond2":
            lab = "label in condition 2"

        self += al.TextEntry(leftlab=lab, name="t1")

The following is an example of a custom page displayed when the experiment is full. Note that it, basically implements the same behavior as the previous example, it is just a little more convenient:

import alfred3 as al
exp = al.Experiment()

@exp.setup
def setup(exp):
    full_page = al.Page(title="Experiment closed.", name="fullpage")
    full_page += al.Text("Sorry, the experiment currently does not accept any further participants.")

    randomizer = al.ListRandomizer(("cond1", 10), ("cond2", 10), exp=exp, abort_page=full_page)

    exp.condition = randomizer.get_condition()


@exp.member
class DemoPage(al.Page):

    def on_exp_access(self):

        if self.exp.condition == "cond1":
            lab = "label in condition 1"

        elif self.exp.condition == "cond2":
            lab = "label in condition 2"

        self += al.TextEntry(leftlab=lab, name="t1")