Copy group structure in other rounds and apps

227 views
Skip to first unread message

AK

unread,
Nov 17, 2021, 5:26:59 AM11/17/21
to oTree help & discussion

Dear all,

at the moment I try to shuffle groups in the first of several apps via:

def creating_session(subsession: Subsession):
import random
if subsession.round_number == 1:
subsession.group_randomly()
matrix = subsession.get_group_matrix()
subsession.set_group_matrix(matrix)
subsession.session.vars['id_matrix'] = matrix
else:
subsession.group_like_round(1)

and would like to have the same group structure in all rounds and all apps (and their rounds). It works fine fo rthe first App but it does not work automatically in the next Apps. I tried to copy the structure via try-except  and subsession.set_group_matrix(subsession.session.vars['id_matrix']) both in "def creating_session(subsession: Subsession):" of the second App. It gives me a warning message "SAWarning: Identity map already had an identity for (<class 'redistribute.Group'>, (46,), None), replacing it with newly
flushed object.   Are there load operations occurring inside of an event handler within the flush?  "within the flush?" % (instance_key,)".
How can I fix this or is there an easy work around? I would not like to do without the shuffle at the beginning because I do not want to group the participants according to their entry into the experiment but randomly.

Thank you in advance for your help.

Best,
AK

Chris @ oTree

unread,
Nov 17, 2021, 10:37:52 AM11/17/21
to AK, oTree help & discussion
I will look into this ASAP.

Sent from my phone

On Nov 17, 2021, at 2:27 AM, AK <ann-katr...@fhgr.ch> wrote:


--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/3690fc0f-dd44-4c81-b26e-a35b9a289b33n%40googlegroups.com.

Chris @ oTree

unread,
Nov 17, 2021, 9:24:47 PM11/17/21
to oTree help & discussion
It seems to be working fine for me. Can you share your project with me, and provide a screenshot demonstrating that it is not grouping participants properly? (you can look in the session's 'data' tab, which has a group column).

I do get the warning also, but it may not be significant.

AK

unread,
Nov 18, 2021, 1:49:57 AM11/18/21
to oTree help & discussion
Hi Chris,

Thank you very much for your help. If you say I can ignore the warning than  it is OK for me. I just hope it is not a problem when running a session with 60 participants.

I get the warning with the following code in app 1:
# FUNCTIONS
def creating_session(subsession: Subsession):
import random
if subsession.round_number == 1:
subsession.group_randomly(fixed_id_in_group=True)

matrix = subsession.get_group_matrix()
subsession.set_group_matrix(matrix)
subsession.session.vars['id_matrix'] = matrix
print(matrix)
print(subsession.session.vars['id_matrix'])
print(subsession.get_group_matrix())
else:
subsession.set_group_matrix(subsession.session.vars['id_matrix'])#subsession.group_like_round(1)#subsession.set_group_matrix(subsession.session.vars['id_matrix'])
print(subsession.session.vars['id_matrix'])
print(subsession.get_group_matrix())

and the following code in app 2:
# FUNCTIONS
def creating_session(subsession: Subsession):
import random
if "id_matrix" in subsession.session.vars:
subsession.set_group_matrix(subsession.session.vars['id_matrix'])
else:
subsession.group_randomly()
print("new app data")
print(subsession.get_group_matrix())
print(subsession.session.vars['id_matrix'])

The grouping works fine but the warning appears as you can see on the following output of the terminal of the test run with 4 participants:
[[3, 2], [1, 4]]
[[3, 2], [1, 4]]
[[3, 2], [1, 4]]
[[3, 2], [1, 4]]
[[3, 2], [1, 4]]
c:\users\...\pycharmprojects\otree_development\venv\lib\site-packages\sqlalchemy\orm\session.py:1956: SAWarning: Identity map already had an identity for (<class 'redistribute.Group'>, (112,), None), replacing it with newly

 flushed object.   Are there load operations occurring inside of an event handler within the flush?
  "within the flush?" % (instance_key,)
c:\users\...\pycharmprojects\otree_development\venv\lib\site-packages\sqlalchemy\orm\session.py:1956: SAWarning: Identity map already had an identity for (<class 'redistribute.Group'>, (113,), None), replacing it with newly

 flushed object.   Are there load operations occurring inside of an event handler within the flush?
  "within the flush?" % (instance_key,)
new app data
[[3, 2], [1, 4]]
[[3, 2], [1, 4]]

and the sessions data:
participant id   group.id_in_session
P1                      2
P2                      1
P3                      1
P4                      2

Thank you and best,
AK

beat...@gmail.com

unread,
May 26, 2022, 12:58:34 PM5/26/22
to oTree help & discussion
Hello, I also have the same problem, and I am not happy with my solution, but I found out that when I refer to the old group matrix before, the flush error messages vanish (at least in my code):

print(subsession.get_group_matrix())
subsession.set_group_matrix(subsession.session.vars['id_matrix'])

or

unusedvariable = subsession.get_group_matrix()
subsession.set_group_matrix(subsession.session.vars['id_matrix'])
Reply all
Reply to author
Forward
0 new messages