oTree Forum >

Balanced treatment assignment

#1 by Tanmoy

Hi,

I have two treatments - T1 and T2. 
I wanted to assign subjects randomly and equally to these two treatments.
In each treatment players per group is 2 again.

So, I tried following -

class Constants(BaseConstants):
    name_in_url = 'test'
    players_per_group = 2
    num_rounds = 1
    ...

class Player(BasePlayer):
    treatment = models.StringField()
    ....
    
    def creating_session(subsession):
        import itertools
        treatments = itertools.cycle(['1', '2'])
        for player in subsession.get_players():
            player.treatment = next(treatments)

class Page1(Page):
    form_model = 'player'
    form_fields = ['variable1']

    def is_displayed(player: Player):
        return player.treatment == '1'

class Page2(Page):
    form_model = 'player'
    form_fields = ['variable2']

    def is_displayed(player: Player):
        return player.treatment == '2'
        
        ...
page_sequence = [Page1, Page2,...]      


I am getting the followng Error-
"TypeError: player.treatment is None. Accessing a null field is generally considered an error. Or, if this is intentional, use field_maybe_none()"

I understand that as the 'treatment' variable is not generating, calling it later is considered as 'null'. And I confirmed it. In the dataset treatment variable is empty. What wrong did I do?

Any help would be appreciated.
Thank you.

#2 by tim3178

Hello Tanmoy,
You might have already found another solution, but your creating_session function should not be within the player class.

Do this instead, your function will be called when launching your app.

class Player(BasePlayer):
    treatment = models.StringField()

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

Write a reply

Set forum username