from otree.api import * import pandas as pd import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'eval' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 # Info = pd.read_csv('eval/example3.csv') BlfJ = pd.read_csv('eval_control/junk_belief.csv') # belief for junk BlfP = pd.read_csv('eval_control/belief_post.csv') # belief for posterior BlfW = pd.read_csv('eval_control/wtp.csv') # belief for willingness to pay type2word = pd.read_csv('eval_control/type2word.csv') GroupN = pd.read_csv('eval_control/gnum.csv') CandiID = pd.read_csv('eval_control/num_all.csv') CandiR = pd.read_csv('eval_control/race_all.csv') InfoAF = pd.read_csv('eval_control/AsianFemale2.csv') InfoAM = pd.read_csv('eval_control/AsianMale2.csv') InfoBF = pd.read_csv('eval_control/BlackFemale2.csv') InfoBM = pd.read_csv('eval_control/BlackMale2.csv') InfoWF = pd.read_csv('eval_control/WhiteFemale2.csv') InfoWM = pd.read_csv('eval_control/WhiteMale2.csv') class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): eid = models.IntegerField() consent_choice = models.StringField( label="", choices=["Consent", "Do not consent"], widget=widgets.RadioSelect ) prolific_id = models.StringField(label="What is your Prolific ID?") # with basic info mh = models.IntegerField(min=0, max=100) sh = models.IntegerField(min=0, max=100) ch = models.IntegerField(min=0, max=100) mh2 = models.IntegerField(min=0, max=100) sh2 = models.IntegerField(min=0, max=100) ch2 = models.IntegerField(min=0, max=100) mh3 = models.IntegerField(min=0, max=100) sh3 = models.IntegerField(min=0, max=100) ch3 = models.IntegerField(min=0, max=100) mh4 = models.IntegerField(min=0, max=100) sh4 = models.IntegerField(min=0, max=100) ch4 = models.IntegerField(min=0, max=100) mh5 = models.IntegerField(min=0, max=100) sh5 = models.IntegerField(min=0, max=100) ch5 = models.IntegerField(min=0, max=100) mh6 = models.IntegerField(min=0, max=100) sh6 = models.IntegerField(min=0, max=100) ch6 = models.IntegerField(min=0, max=100) # with additional info mh7 = models.IntegerField(min=0, max=100) sh7 = models.IntegerField(min=0, max=100) ch7 = models.IntegerField(min=0, max=100) mh8 = models.IntegerField(min=0, max=100) sh8 = models.IntegerField(min=0, max=100) ch8 = models.IntegerField(min=0, max=100) mh9 = models.IntegerField(min=0, max=100) sh9 = models.IntegerField(min=0, max=100) ch9 = models.IntegerField(min=0, max=100) mh10 = models.IntegerField(min=0, max=100) sh10 = models.IntegerField(min=0, max=100) ch10 = models.IntegerField(min=0, max=100) # willingness to pay wtpM11 = models.IntegerField() wtpS11 = models.IntegerField() wtpC11 = models.IntegerField() wtpM12 = models.IntegerField() wtpS12 = models.IntegerField() wtpC12 = models.IntegerField() wtpM13 = models.IntegerField() wtpS13 = models.IntegerField() wtpC13 = models.IntegerField() wtpM14 = models.IntegerField() wtpS14 = models.IntegerField() wtpC14 = models.IntegerField() att7 = models.StringField() att8 = models.StringField() att9 = models.StringField() att10 = models.StringField() mathlog = models.IntegerField() soclog = models.IntegerField() comblog = models.IntegerField() compG1 = models.IntegerField() compG2 = models.IntegerField() compG3 = models.IntegerField() pay_n = models.IntegerField() pay_t = models.IntegerField() pay_t2 = models.IntegerField() pay_t3 = models.IntegerField() # PAGES class Consent(Page): pass class Consent2(Page): form_model = "player" form_fields = ['consent_choice'] class EndNowC(Page): @staticmethod def is_displayed(player): return player.consent_choice == 'Do not consent' class ProlificID(Page): form_model = "player" form_fields = ["prolific_id"] class InstG(Page): form_model = 'player' form_fields = ['compG1', 'mathlog', 'soclog', 'comblog'] @staticmethod def before_next_page(player: Player, timeout_happened): player.eid = player.participant.id_in_session list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] list2 = [1, 2, 3] random.shuffle(list1) random.shuffle(list2) player.pay_n = list1[0] player.pay_t = list2[0] # player.pay_n = 11 # player.pay_t = 1 # print(C.Info[0]) @staticmethod def error_message(player, values): if values['compG1'] != 2: return 'Please try again.' class InstG2(Page): form_model = 'player' form_fields = ['compG2', 'compG3'] @staticmethod def error_message(player, values): if values['compG2'] != 3: return 'Please try again: Note that the first question asks to choose "True" choice and the second question asks to choose "False" choice. Also, you should submit the answers for both questions again. Hint: You would see the explanation once you click a choice. Green-colored text explanation means a choice is True, and the red-colored choice text explanation means a choice is False.' if values['compG3'] != 2: return 'Please try again: Note that the first question asks to choose "True" choice and the second question asks to choose "False" choice. Also, you should submit the answers for both questions again. Hint: You would see the explanation once you click a choice. Green-colored text explanation means a choice is True, and the red-colored choice text explanation means a choice is False. ' class InstG3(Page): pass class InstB2(Page): pass class InstB3(Page): pass class Belief1(Page): form_model = 'player' form_fields = ['mh', 'sh', 'ch'] def vars_for_template(player: Player): infoJ = C.BlfJ.values k = player.eid-1 a = 1 num1 = infoJ[k][a*3-2] num2 = infoJ[k][a*3-1] num3 = infoJ[k][a*3] num1_1=100-num1 num2_1=100-num2 num3_1=100-num3 return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, ) class Belief2(Page): form_model = 'player' form_fields = ['mh2', 'sh2', 'ch2'] def vars_for_template(player: Player): infoJ = C.BlfJ.values k = player.eid - 1 a = 2 num1 = infoJ[k][a * 3 - 2] num2 = infoJ[k][a * 3 - 1] num3 = infoJ[k][a * 3] num1_1 = 100 - num1 num2_1 = 100 - num2 num3_1 = 100 - num3 return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, ) class Belief3(Page): form_model = 'player' form_fields = ['mh3', 'sh3', 'ch3'] def vars_for_template(player: Player): infoJ = C.BlfJ.values k = player.eid - 1 a = 3 num1 = infoJ[k][a * 3 - 2] num2 = infoJ[k][a * 3 - 1] num3 = infoJ[k][a * 3] num1_1 = 100 - num1 num2_1 = 100 - num2 num3_1 = 100 - num3 return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, ) class Belief4(Page): form_model = 'player' form_fields = ['mh4', 'sh4', 'ch4'] def vars_for_template(player: Player): infoJ = C.BlfJ.values k = player.eid - 1 a = 4 num1 = infoJ[k][a * 3 - 2] num2 = infoJ[k][a * 3 - 1] num3 = infoJ[k][a * 3] num1_1 = 100 - num1 num2_1 = 100 - num2 num3_1 = 100 - num3 return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, ) class Belief5(Page): form_model = 'player' form_fields = ['mh5', 'sh5', 'ch5'] def vars_for_template(player: Player): infoJ = C.BlfJ.values k = player.eid - 1 a = 5 num1 = infoJ[k][a * 3 - 2] num2 = infoJ[k][a * 3 - 1] num3 = infoJ[k][a * 3] num1_1 = 100 - num1 num2_1 = 100 - num2 num3_1 = 100 - num3 return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, ) class Belief6(Page): form_model = 'player' form_fields = ['mh6', 'sh6', 'ch6'] def vars_for_template(player: Player): infoJ = C.BlfJ.values k = player.eid - 1 a = 6 num1 = infoJ[k][a * 3 - 2] num2 = infoJ[k][a * 3 - 1] num3 = infoJ[k][a * 3] num1_1 = 100 - num1 num2_1 = 100 - num2 num3_1 = 100 - num3 return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, ) class Belief7(Page): form_model = 'player' form_fields = ['mh7', 'sh7', 'ch7', 'att7'] def vars_for_template(player: Player): infoP = C.BlfP.values infoT= C.type2word.values k = player.eid - 1 a = 1 num1 = infoP[k][a * 3 - 2] num2 = infoP[k][a * 3 - 1] num3 = infoP[k][a * 3] tp1 = infoT[k][a * 2 - 1] tp2 = infoT[k][a * 2] return dict( num1=num1, num2=num2, num3=num3, tp1=tp1, tp2=tp2 ) class Belief8(Page): form_model = 'player' form_fields = ['mh8', 'sh8', 'ch8', 'att8'] def vars_for_template(player: Player): infoP = C.BlfP.values infoT= C.type2word.values k = player.eid - 1 a = 2 num1 = infoP[k][a * 3 - 2] num2 = infoP[k][a * 3 - 1] num3 = infoP[k][a * 3] tp1 = infoT[k][a * 2 - 1] tp2 = infoT[k][a * 2] return dict( num1=num1, num2=num2, num3=num3, tp1=tp1, tp2=tp2 ) class Belief9(Page): form_model = 'player' form_fields = ['mh9', 'sh9', 'ch9', 'att9'] def vars_for_template(player: Player): infoP = C.BlfP.values infoT= C.type2word.values k = player.eid - 1 a = 3 num1 = infoP[k][a * 3 - 2] num2 = infoP[k][a * 3 - 1] num3 = infoP[k][a * 3] tp1 = infoT[k][a * 2 - 1] tp2 = infoT[k][a * 2] return dict( num1=num1, num2=num2, num3=num3, tp1=tp1, tp2=tp2 ) class Belief10(Page): form_model = 'player' form_fields = ['mh10', 'sh10', 'ch10', 'att10'] def vars_for_template(player: Player): infoP = C.BlfP.values infoT= C.type2word.values k = player.eid - 1 a = 4 num1 = infoP[k][a * 3 - 2] num2 = infoP[k][a * 3 - 1] num3 = infoP[k][a * 3] tp1 = infoT[k][a * 2 - 1] tp2 = infoT[k][a * 2] return dict( num1=num1, num2=num2, num3=num3, tp1=tp1, tp2=tp2 ) class InstW(Page): pass class WTP1(Page): form_model = 'player' form_fields = ['wtpM11', 'wtpS11', 'wtpC11'] def vars_for_template(player: Player): infoW = C.BlfW.values infoG = C.GroupN.values k = player.eid - 1 a = 1 num1 = infoW[k][a * 3 - 2] num2 = infoW[k][a * 3 - 1] num3 = infoW[k][a * 3] num1_1 = round(100-num1, 3) num2_1 = round(100-num2, 3) num3_1 = round(100-num3, 3) g = infoG[k][a] return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, g=g ) class WTP2(Page): form_model = 'player' form_fields = ['wtpM12', 'wtpS12', 'wtpC12'] def vars_for_template(player: Player): infoW = C.BlfW.values infoG = C.GroupN.values k = player.eid - 1 a = 2 num1 = infoW[k][a * 3 - 2] num2 = infoW[k][a * 3 - 1] num3 = infoW[k][a * 3] num1_1 = round(100 - num1, 3) num2_1 = round(100 - num2, 3) num3_1 = round(100 - num3, 3) g = infoG[k][a] return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, g=g ) class WTP3(Page): form_model = 'player' form_fields = ['wtpM13', 'wtpS13', 'wtpC13'] def vars_for_template(player: Player): infoW = C.BlfW.values infoG = C.GroupN.values k = player.eid - 1 a = 3 num1 = infoW[k][a * 3 - 2] num2 = infoW[k][a * 3 - 1] num3 = infoW[k][a * 3] num1_1 = round(100 - num1, 3) num2_1 = round(100 - num2, 3) num3_1 = round(100 - num3, 3) g = infoG[k][a] return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, g=g ) class WTP4(Page): form_model = 'player' form_fields = ['wtpM14', 'wtpS14', 'wtpC14'] def vars_for_template(player: Player): infoW = C.BlfW.values infoG = C.GroupN.values k = player.eid - 1 a = 4 num1 = infoW[k][a * 3 - 2] num2 = infoW[k][a * 3 - 1] num3 = infoW[k][a * 3] num1_1 = round(100 - num1, 3) num2_1 = round(100 - num2, 3) num3_1 = round(100 - num3, 3) g = infoG[k][a] return dict( num1=num1, num2=num2, num3=num3, num1_1=num1_1, num2_1=num2_1, num3_1=num3_1, g=g ) class Results(Page): form_model = 'player' def before_next_page(player: Player, timeout_happened): infoID = C.CandiID.values infoR = C.CandiR.values k = player.eid - 1 if infoR[k][player.pay_n] == 1: infoCall = C.InfoAF.values if infoR[k][player.pay_n] == 2: infoCall = C.InfoAM.values if infoR[k][player.pay_n] == 3: infoCall = C.InfoBF.values if infoR[k][player.pay_n] == 4: infoCall = C.InfoBM.values if infoR[k][player.pay_n] == 5: infoCall = C.InfoWF.values if infoR[k][player.pay_n] == 6: infoCall = C.InfoWM.values idc = infoID[k][player.pay_n] - 1 numbers = [20, 0] mhprob = [0.01*player.mh, 0.01*player.mh2, 0.01*player.mh3, 0.01*player.mh4, 0.01*player.mh5, 0.01*player.mh6, 0.01*player.mh7, 0.01*player.mh8, 0.01*player.mh9, 0.01*player.mh10] shprob = [0.01 * player.sh, 0.01 * player.sh2, 0.01 * player.sh3, 0.01 * player.sh4, 0.01 * player.sh5, 0.01 * player.sh6, 0.01 * player.sh7, 0.01 * player.sh8, 0.01 * player.sh9, 0.01 * player.sh10] chprob = [0.01 * player.ch, 0.01 * player.ch2, 0.01 * player.ch3, 0.01 * player.ch4, 0.01 * player.ch5, 0.01 * player.ch6, 0.01 * player.ch7, 0.01 * player.ch8, 0.01 * player.ch9, 0.01 * player.ch10] if player.pay_n < 7: player.pay_t2 = -1 infoJ = C.BlfJ.values a = player.pay_n if player.pay_t == 1: num = infoJ[k][a * 3 - 2] * 0.01 distb = [num, 1-num] ball = random.choices([0, 1], distb, k=1) player.pay_t3 = ball[0] x = mhprob[player.pay_n - 1] if ball == 0: dist = [1 - (1 - x) ** 2, (1 - x) ** 2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if ball == 1: dist = [1 - x ** 2, x ** 2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if player.pay_t == 2: num = infoJ[k][a * 3 - 1] * 0.01 distb = [num, 1-num] ball = random.choices([0, 1], distb, k=1) player.pay_t3 = ball[0] x = shprob[player.pay_n - 1] if ball == 0: dist = [1 - (1 - x) ** 2, (1 - x) ** 2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if ball == 1: dist = [1 - x ** 2, x ** 2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if player.pay_t == 3: num = infoJ[k][a * 3] * 0.01 distb = [num, 1-num] ball = random.choices([0, 1], distb, k=1) player.pay_t3 = ball[0] x = chprob[player.pay_n - 1] if ball == 0: dist = [1 - (1 - x) ** 2, (1 - x) ** 2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if ball == 1: dist = [1 - x ** 2, x ** 2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if player.pay_n < 11: if player.pay_n > 6: player.pay_t2 = -1 if player.pay_t == 1: player.pay_t3 = infoCall[idc][8] x = mhprob[player.pay_n-1] if infoCall[idc][8] == 1: dist = [1 - (1 - x)**2, (1 - x)**2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if infoCall[idc][8] == 0: dist = [1 - x**2, x**2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if player.pay_t == 2: player.pay_t3 = infoCall[idc][9] x = shprob[player.pay_n-1] if infoCall[idc][9] == 1: dist = [1 - (1 - x)**2, (1 - x)**2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if infoCall[idc][9] == 0: dist = [1 - x**2, x**2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if player.pay_t == 3: player.pay_t3 = infoCall[idc][10] x = chprob[player.pay_n-1] if infoCall[idc][10] == 1: dist = [1 - (1 - x)**2, (1 - x)**2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if infoCall[idc][10] == 0: dist = [1 - x**2, x**2] y = random.choices(numbers, dist, k=1) player.payoff = y[0] if player.pay_n > 10: list0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] random.shuffle(list0) player.pay_t2 = list0[0] wage = player.pay_t2 Mwtp = [player.wtpM11, player.wtpM12, player.wtpM13, player.wtpM14] Swtp = [player.wtpS11, player.wtpS12, player.wtpS13, player.wtpS14] Cwtp = [player.wtpC11, player.wtpC12, player.wtpC13, player.wtpC14] if player.pay_t==1: player.pay_t3 = infoCall[idc][8] if wage > Mwtp[player.pay_n-11]: player.payoff = 0 if wage < Mwtp[player.pay_n-11]: if infoCall[idc][8] == 0: player.payoff = 10-wage elif infoCall[idc][8] == 1: player.payoff = 20-wage if wage == Mwtp[player.pay_n-11]: if infoCall[idc][8] == 0: player.payoff = 10-wage elif infoCall[idc][8] == 1: player.payoff = 20-wage if player.pay_t==2: player.pay_t3 = infoCall[idc][9] if wage > Swtp[player.pay_n-11]: player.payoff = 0 if wage < Swtp[player.pay_n-11]: if infoCall[idc][9] == 0: player.payoff = 10-wage elif infoCall[idc][9] == 1: player.payoff = 20-wage if wage == Swtp[player.pay_n-11]: if infoCall[idc][9] == 0: player.payoff = 10-wage elif infoCall[idc][9] == 1: player.payoff = 20-wage if player.pay_t==3: player.pay_t3 = infoCall[idc][10] if wage > Cwtp[player.pay_n-11]: player.payoff = 0 if wage < Cwtp[player.pay_n-11]: if infoCall[idc][10] == 0: player.payoff = 10-wage elif infoCall[idc][10] == 1: player.payoff = 20-wage if wage == Cwtp[player.pay_n-11]: if infoCall[idc][10] == 0: player.payoff = 10-wage elif infoCall[idc][10] == 1: player.payoff = 20-wage class Results2(Page): def vars_for_template(player: Player): n1=player.pay_n n2=player.pay_t n3=player.pay_t2 n4=player.pay_t3 if n1 > 6: n1 = n1-6 if player.pay_n > 6: if n4 == 1: n4 = 'High' elif n4 == 0: n4 = 'Low' if player.pay_n < 7: if n4 == 0: n4 = 'Black' elif n4 == 1: n4 = 'White' if player.pay_n > 6: if n2 == 1: n2 = 'Math' elif n2 == 2: n2 = 'Social' elif n2 == 3: n2 = 'Combined' if player.pay_n < 7: if n2 == 1: n2 = 'Green' elif n2 == 2: n2 = 'Blue' elif n2 == 3: n2 = 'Purple' return dict( n1=n1, n2=n2, n3=n3, n4=n4 ) class Belief1_example(Page): form_model = 'player' form_fields = ['mh', 'sh', 'ch', 'att'] def vars_for_template(player: Player): infoG='Male' infoR='White' infoH='Littleton' infoM='Proficient' infoA='Novice' return dict( info1=infoG, info2=infoR, info3=infoH, info4=infoM, info5=infoA, ) # page_sequence = [Consent, Consent2, EndNowC, ProlificID, InstG, InstW,WTP1, InstB3, Belief7] # page_sequence = [InstG, InstG2, InstB2, Belief1, Belief2, Belief3, Belief4, # Belief5, Belief6, InstB3, Belief7, Belief8, Belief9, Belief10, # InstW, WTP1, WTP2, WTP3, WTP4, Results, Results2] page_sequence = [InstG, InstG2, InstB2, Belief1, Belief2, Belief3, Belief4, Belief5, Belief6, InstB3, Belief7, Belief8, Belief9, Belief10, InstW, WTP1, WTP2, WTP3, WTP4, Results, Results2]