oTree Forum >

Help sorting Particpants into Treatments

#1 by lmayr

Hello all, 

I am very new to Otree and have a (hopefully) fairly simple question. I have two treatments that I am trying to sort participants into respectively. I thought the simplest way to do this in my case, is define a treatment variable using 

def creating_session(subsession):
    for player in subsession.get_players():
        treatments = itertools.cycle([1, 2])
        
and then use the is_displayed function to show pages to participants in the different treatment groups. However when I call the is_dispalyed function using 

    def is_displayed(player: Player):
        return player.treatments == 1
        
    I get the error code 'AttributeError: 'Player' object has no attribute 'treatment''

I hope someone can help me figure this out, it is driving me crazy.

#2 by BonnEconLab

You need to replace “treatments” by “player.treatments” in creating_session:

def creating_session(subsession):
    for player in subsession.get_players():
        player.treatments = itertools.cycle([1, 2])

This assumes that you have defined the respective field in the Player class:

class Player(BasePlayer):

    treatments = models.IntegerField()

#3 by BonnEconLab

By the way, if this is described in detail on https://otree.readthedocs.io/en/latest/treatments.html#creating-session.

Importantly, if your experiment consists of multiple rounds AND you do not want to assign a potentially different treatment in each round, you must do the assignment only once, in round 1.

That is, you need to add the condition “if subsession.round_number == 1”. Plus, you should use a participant field instead of a player field, since participant fields store information that can be accessed across different rounds. See https://otree.readthedocs.io/en/latest/treatments.html#treatment-groups-multiple-rounds:

def creating_session(subsession):

    if subsession.round_number == 1:
        for player in subsession.get_players():
            player.participant.treatments = ...

#4 by BonnEconLab

By the way: You wrote,

> I get the error code 'AttributeError: 'Player' object has no attribute 'treatment''

If this is true, then you’re mixing “treatments” in the plural and “treatment” in the singular.

#5 by BonnEconLab

Last but not least, I am not sure that “itertools.cycle([1, 2])” is correct here. I guess it will be executed for each player anew so that all players will end up in treatment “1”.

If you want the players to be assigned to the two treatments alternatingly, the following should work:

player.participant.treatments = 2 - (player.id_in_subsession % 2)

% is the modulo operator in Python.

This will assign treatment 1 to all odd-numbered player IDs and treatment 2 to all even-numbered player IDs:

– treatment 1 to player 1,
– treatment 2 to player 2,
– treatment 1 to player 3,
– treatment 2 to player 4,
– and so on.

#6 by lmayr

Thank you so much for your help! It seems it was a combination of all problems, I ended up using this
 
def creating_session(subsession):
    if subsession.round_number == 1:
        treatments = itertools.cycle([1, 2, 3])
        for player in subsession.get_players():
            player.treatment = next(treatments)
            
          In the actual experiment there are three treatments, will the itertools problem still be present in this case?
          
          
Thank you so much for your help! Beste grüße aus Heidelberg!

Write a reply

Set forum username