from otree.api import * import numpy as np import math from itertools import combinations import pandas as pd import os doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'binarychoice' PLAYERS_PER_GROUP = None NUM_ROUNDS = 5 NUM_PER_PAIR = 3 NUM_PRODUCTS = 5 NUM_PRODUCT_TYPES = 4 class Subsession(BaseSubsession): pass # class Product(ExtraModel): # name = models.StringField() # fame = models.StringField() # popularity = models.StringField() # img_name = models.StringField() def creating_session(subsession: Subsession): round = subsession.round_number if round == 1: subsession.session.productImgs = {} # gambles = read_csv("binarychoice/tversky.csv", BinaryGamble) # rows = read_csv("binarychoice/yougovdata.csv", Product) # df = pd.read_csv("binarychoice/tversky.csv") # parties_all = ["Sinn Féin", "Alliance Party", "Conservative Party", "Labour and Co-operative Party", "Democratic Unionist Party", "Green Party", "Liberal Democrats", "Plaid Cymru", "Scottish National Party", "Social Democratic and Labour Party"] choice_types = ["movies", "dinners", "dining", "charities", "cars", "holidays", "magazines", "fruits"] dfs_dict = {} for choice_type in choice_types: print(choice_type) choice_type_df = pd.read_csv(f"binarychoice/{choice_type}_data.csv", encoding='latin1') if os.path.exists(f"binarychoice/excluded_{choice_type}.csv"): excluded_df = pd.read_csv(f"binarychoice/excluded_{choice_type}.csv") choice_type_df = choice_type_df[~choice_type_df["name"].isin(excluded_df["name"])] if choice_type != "movies": choice_type_df["fame_val"] = choice_type_df["fame"].str.replace("%", "").astype(float) choice_type_df = choice_type_df[choice_type_df["fame_val"] > 50] choice_type_df = choice_type_df[choice_type_df["image"] != "No image available"] dfs_dict[choice_type] = choice_type_df.reset_index(drop=True) # pd.set_option('display.max_rows', 300) for choice_type in choice_types: choice_type_df = dfs_dict[choice_type] products = choice_type_df["name"].values subsession.session.productImgs[choice_type] = {} for product in products: img_name = choice_type_df[choice_type_df["name"] == product]["image"].values[0] subsession.session.productImgs[choice_type][product] = img_name print(subsession.session.productImgs) # dfs_dict["dining"] # gambles = df.to_dict('records') for p in subsession.get_players(): participant_choice_types = np.random.choice(choice_types, C.NUM_PRODUCT_TYPES, replace=False) p.participant.choiceTypes = participant_choice_types p.participant.chosenProducts = {} p.participant.productPairs = {} p.participant.productIdx = {} for choice_type in participant_choice_types: choice_type_df = dfs_dict[choice_type] product_idxs = np.random.choice(len(choice_type_df),C.NUM_PRODUCTS, replace=False) p.participant.productIdx[choice_type] = 0 # product_idx = np.random.choice(len(rows),C.NUM_PRODUCTS, replace=False) products = [choice_type_df.loc[idx, "name"] for idx in product_idxs] p.participant.chosenProducts[choice_type] = products product_pairs = list(combinations(products, 2)) # repeat the vector of product_pairs three times product_pairs = product_pairs * C.NUM_PER_PAIR np.random.shuffle(product_pairs) p.participant.productPairs[choice_type] = product_pairs # p.participant.products = products # p.participant.productPairs = product_pairs # p.participant.productIdx = 0 # parties_idx = np.random.choice(len(parties_all),C.NUM_PRODUCTS, replace=False) # parties = [parties_all[idx] for idx in parties_idx] # party_pairs = list(combinations(parties, 2)) # party_pairs = party_pairs * C.NUM_PER_PAIR # np.random.shuffle(party_pairs) # p.participant.parties = parties # p.participant.partyPairs = party_pairs # p.participant.partyIdx = 0 # p.participant.tverskys = gambles # tverskyPairs = list(combinations(gambles, 2)) # tverskyPairs = tverskyPairs * C.NUM_PER_PAIR # np.random.shuffle(tverskyPairs) # p.participant.tverskyPairs = tverskyPairs # p.participant.tverskyIdx = 0 # p.participant.chosenGambles = [] for p in subsession.get_players(): choice_type = p.participant.choiceTypes[round % C.NUM_PRODUCT_TYPES] p.choicetype = choice_type print(p.participant.productIdx[choice_type]) print(p.participant.productPairs[choice_type][p.participant.productIdx[choice_type]]) pair = list(p.participant.productPairs[choice_type][p.participant.productIdx[choice_type]]) np.random.shuffle(pair) p.left = pair[0] p.right = pair[1] print(subsession.session.productImgs) p.leftImg = subsession.session.productImgs[choice_type][p.left] p.rightImg = subsession.session.productImgs[choice_type][p.right] p.participant.productIdx[choice_type] += 1 # if round % 3 == 0: # p.choicetype = "candy" # pair = list(p.participant.productPairs[p.participant.productIdx]) # np.random.shuffle(pair) # p.left = pair[0] # p.right = pair[1] # p.participant.productIdx += 1 # elif round % 3 == 2: # p.choicetype = "party" # pair = list(p.participant.partyPairs[p.participant.partyIdx]) # np.random.shuffle(pair) # p.left = pair[0] # p.right = pair[1] # p.participant.partyIdx += 1 # elif round % 3 == 1: # p.choicetype = "tversky" # pair = list(p.participant.tverskyPairs[p.participant.tverskyIdx]) # np.random.shuffle(pair) # leftG = pair[0] # rightG = pair[1] # p.left = leftG["name"] # p.leftP = leftG["p"] # p.leftW = leftG["w"] # p.leftD = leftG["d"] # p.right = rightG["name"] # p.rightP = rightG["p"] # p.rightW = rightG["w"] # p.rightD = rightG["d"] # p.participant.tverskyIdx += 1 # # for r in range(1,Constants.num_rounds): # class Group(BaseGroup): pass class Player(BasePlayer): choicetype = models.StringField() choice = models.StringField() left = models.StringField() right = models.StringField() leftImg = models.StringField() rightImg = models.StringField() pass # PAGES class BinaryChoice(Page): form_model = 'player' form_fields = ['choice'] @staticmethod def is_displayed(player): print(player.participant.choiceTypes) print(player.participant.choiceTypes[player.round_number % C.NUM_PRODUCT_TYPES]) print(player.participant.productIdx[player.participant.choiceTypes[player.round_number % C.NUM_PRODUCT_TYPES]]) return True def vars_for_template(player: Player): return dict(left=player.left, right=player.right, choice_type=player.choicetype, leftImg=player.leftImg, rightImg=player.rightImg) page_sequence = [BinaryChoice]