Select random image from a pull

192 views
Skip to first unread message

nicolas rochat

unread,
Mar 2, 2022, 10:03:37 AM3/2/22
to oTree help & discussion
Dear Chris,

I'm trying to use different images for different participants in my experiment.
For each image, the result should be recorded. 
To do so at each creating-session participant are match with a set of 5 images on for each round and save.

However, I'm not able to retrieve the image for each round, so obviously i miss something.

here is my code, any ideas? 
______________________________________
from otree.api import *
from glob2 import glob
import pandas as pd
import random

doc = """
Your app description
"""

round = 5
class C(BaseConstants):
NAME_IN_URL = 'faceDescription'
PLAYERS_PER_GROUP = None
NUM_ROUNDS = round

class Subsession(BaseSubsession):
pass

def creating_session(subsession: Subsession):
PicturePool = [i.split("/")[-1] for i in glob('_static/faceDescription/*jpg')]
if subsession.round_number == 1:
for p in subsession.get_players():
selected_img = random.choices(PicturePool,k=5)
PicturePool = [e for e in PicturePool if e not in selected_img]
print('player', p.id_in_subsession)
print(selected_img)
print(len(PicturePool))
p.participant.images = selected_img



class Group(BaseGroup):
pass


class Player(BasePlayer):
Nez_forme = models.StringField(choices = ["Normale","Épatée","Arrondie","Aquilin"],
label="Forme du nez :",
widget=widgets.RadioSelectHorizontal)
### Lèvre
levre_type = models.StringField(choices = ["Normales","Pulpeuses","Charnues","Fines"],
label="Type de lèvres :",
widget=widgets.RadioSelectHorizontal)

### Information
email = models.StringField(blank=True)
pseudo = models.StringField()
sexe_p = models.StringField(choices = ["Homme","Femme","Autre"], label = "Sexe", widget=widgets.RadioSelectHorizontal)
age_p = models.IntegerField(label = "Age")
photo_ID = models.StringField()

# PAGES

class Openning(Page):
def is_displayed(self):
return self.round_number == 1
pass

class ConsentForm(Page):
def is_displayed(self):
return self.round_number == 1
pass

class ConsignePage(Page):
def is_displayed(self):
return self.round_number == 1
pass


class participant(Page):
form_model = "player"
form_fields = ["pseudo", "sexe_p", "age_p"]
def is_displayed(self):
return self.round_number == 1


class MyPage(Page):
form_model = 'player'
form_fields = ['Nez_forme', 'levre_type']
@staticmethod
def vars_for_template(player: Player):
participant = player.participant
image = participant.images[player.round_number - 1]
player.photo_ID = image
return {'img_to_show': "faceDescription/{}".format(image)}



class Cloture(Page):
form_model = "player"
form_fields = ["email"]
def is_displayed(self):
return self.round_number == round ## fin de l'expérience



page_sequence = [
# Openning, ConsentForm, participant,
# ConsignePage,
MyPage, Cloture]


____________ setting part :
SESSION_CONFIGS = [
# dict(
# name='public_goods',
# app_sequence=['public_goods'],
# num_demo_participants=3,
# ),
dict(
name='faceDescription',
num_demo_participants=3,
app_sequence=['faceDescription'],
PARTICIPANT_FIELDS = ["images"]
),
]
______

Chris @ oTree

unread,
Mar 2, 2022, 10:10:31 AM3/2/22
to nicolas rochat, oTree help & discussion
What is the error you are getting? Or how does the behavior of the program differ from your expectation?

Sent from my phone

On Mar 2, 2022, at 11:03 PM, nicolas rochat <nicolas....@gmail.com> wrote:

Dear Chris,
--
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/47041a0d-086d-4c64-84f7-a9f40a633f73n%40googlegroups.com.

nicolas rochat

unread,
Mar 2, 2022, 10:42:00 AM3/2/22
to oTree help & discussion
Seem Participant as no attribute images Traceback (most recent call last):
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/middleware/base.py", line 26, in __call__
    await response(scope, receive, send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/responses.py", line 226, in __call__
    (self.listen_for_disconnect, {"receive": receive}),
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/concurrency.py", line 24, in run_until_first_complete
    [task.result() for task in done]
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/concurrency.py", line 24, in <listcomp>
    [task.result() for task in done]
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/responses.py", line 216, in stream_response
    async for chunk in self.body_iterator:
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/middleware/base.py", line 56, in body_stream
    task.result()
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/middleware/base.py", line 38, in coro
    await self.app(scope, receive, send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/errorpage.py", line 247, in __call__
    raise exc  # from None
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/errorpage.py", line 227, in __call__
    await self.app(scope, receive, _send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/middleware/base.py", line 25, in __call__
    response = await self.dispatch_func(request, self.call_next)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/middleware.py", line 42, in dispatch
    response = await call_next(request)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/middleware/base.py", line 45, in call_next
    task.result()
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/middleware/base.py", line 38, in coro
    await self.app(scope, receive, send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/middleware/sessions.py", line 75, in __call__
    await self.app(scope, receive, send_wrapper)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/patch.py", line 41, in __call__
    raise exc  # from None
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/patch.py", line 29, in __call__
    await self.app(scope, receive, sender)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/routing.py", line 582, in __call__
    await route.handle(scope, receive, send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/routing.py", line 243, in handle
    await self.app(scope, receive, send)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/views/abstract.py", line 111, in dispatch
    response = await run_in_threadpool(self.inner_dispatch, request)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/starlette/concurrency.py", line 40, in run_in_threadpool
    return await loop.run_in_executor(None, func, *args)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/views/abstract.py", line 418, in inner_dispatch
    return self.get()
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/views/abstract.py", line 481, in get
    context = self.get_context_data(form=form)
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/views/abstract.py", line 165, in get_context_data
    user_vars = self.call_user_defined('vars_for_template')
  File "/home/nrochat/anaconda3/envs/otree_lineup/lib/python3.7/site-packages/otree/views/abstract.py", line 88, in call_user_defined
    return getattr(type(self), method_name)(self.player, *args, **kwargs)
  File "/mnt/3A3E7D9E3E7D53BB/fac/Recherche/01_Crimino/01_Lineup/Database/otree_exp/otree_lineup/faceDescription/__init__.py", line 105, in vars_for_template

    image = participant.images[player.round_number - 1]
AttributeError: 'Participant' object has no attribute 'images'

Chris @ oTree

unread,
Mar 2, 2022, 11:17:24 AM3/2/22
to nicolas rochat, oTree help & discussion
PARTICIPANT_FIELDS doesn’t go inside the session config, it is a separate setting.

Sent from my phone

On Mar 2, 2022, at 11:03 PM, nicolas rochat <nicolas....@gmail.com> wrote:

PARTICIPANT_FIELDS

nicolas rochat

unread,
Mar 2, 2022, 1:27:08 PM3/2/22
to oTree help & discussion
Thanks, this solved this problem.

Any ideas why i don't have the data for each round?
 I think I'm lost here.
Reply all
Reply to author
Forward
0 new messages