oTree Forum >

how to keep same id_in_group across apps

#1 by tae_yn

Hi, I randomised people's id_in_group in the first app in order to assign different roles. But in the second app, people's id_in_group changes to automatic sequential id_in_group (1, 2, 3, 4 ~). How can I keep the same id_in_group from the first app to the second app? I read that I can use participant_fields but I couldn't find a practical code example. Thank you in advance.

#2 by Javier

Hi, my solution for something similar was as follows:
- Randomise the people's id_in_group in the first app/subsession.
- Store that id_in_group in a participant's field.
- In the next rounds/apps, when creating the subsession do something like:
    - player.id_in_group = participant.aux_field
I hope this might be of help to you, best.

#3 by tae_yn

Hi, Thank you! may I ask how to store id_in_group in a participant's field. I tried to follow the tutorial but i couldn't make it happen. I am doing the below for randomising the id in group.

class Subsession(BaseSubsession):
    def creating_session(subsession):

#4 by Javier

In your settings.py file you need to set up the participant's variable or field in which to store that info. Something like this:

PARTICIPANT_FIELDS = ['id_in_group']

Then, when creating the subsession:

class Subsession(BaseSubsession):
    def creating_session(subsession):
        if subsession.round_number == 1:
            for player in subsession.get_players():
                player.participant.id_in_group = player.id_in_group

And you can recover that in later apps by accessing player.participant.id_in_group

Best, Javier.

#5 by tae_yn

Hi, I have followed your advice but it gives me error saying 'Participant has no field "id_in_group".' :/ I do have PARTICIPANT_FIELDS in my settings.py I don't know why it is not working..

#6 by ccrabbe

Hi tae_yn -

The most straightforward way to store information for a participant is to use participant.vars.  If 'p' is an instance of player, then:

p.participant.vars['keyname'] = value 

will store information, and:

previous_value = p.participant.vars['keyname']

will retrieve it.

Participant fields are a way to make your code look nicer, but lack the flexibility of just using participant.vars directly.

However, for your original question, the way that I would solve it would be to store the group matrix in session.vars (which works similar to participant.vars except instead of one per participant there's one for the whole oTree session) using subsession.get_group_matrix(), and then set the groups of the second app using subsession.set_group_matrix(...).

If you're not using group_by_arrival_time and each of your apps have the same PLAYERS_PER_GROUP you can call each of those in creating_session.  See the documentation here:  https://otree.readthedocs.io/en/latest/multiplayer/groups.html#get-group-matrix

Something like:

in the first app:
subsession.session.vars['matching_matrix'] = subsession.get_group_matrix()

in the subsequent app:


#7 by tae_yn

Thank you, Javier and Chris!

Write a reply

Set forum username