from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import numpy as np # from mpl.config import * from random import randrange import random import csv import pandas as pd import yaml import re import os author = 'Mohsen Foroughifar' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'Study1_Interaction2' players_per_group = None num_rounds = 21 participation_fee = 6 lottery_a_hi = 12 lottery_a_lo = 0 lottery_b_hi = 12 lottery_b_lo = 0 num_choices = 20 certain_choice = True one_choice_per_page = False random_order = False enforce_consistency = False percentage = False small_pies = False large_pies = False progress_bar = True instructions = True results = False class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: print('\nStudy1_Interaction2') for p in self.get_players(): info_structure = {1: [5 / 10, 5 / 10], 2: [6 / 10, 5 / 10], 3: [7 / 10, 5 / 10], 4: [8 / 10, 5 / 10], 5: [9 / 10, 5 / 10], 6: [10 / 10, 5 / 10], 7: [6 / 10, 6 / 10], 8: [7 / 10, 6 / 10], 9: [8 / 10, 6 / 10], 10: [9 / 10, 6 / 10], 11: [10 / 10, 6 / 10], 12: [7 / 10, 7 / 10], 13: [8 / 10, 7 / 10], 14: [9 / 10, 7 / 10], 15: [10 / 10, 7 / 10], 16: [8 / 10, 8 / 10], 17: [9 / 10, 8 / 10], 18: [10 / 10, 8 / 10], 19: [9 / 10, 9 / 10], 20: [10 / 10, 9 / 10], 21: [10 / 10, 10 / 10]} try: p.participant.vars['flag'] == 1 p.participant.vars['treatment'] = 'D_I_wo' except: p.participant.vars['treatment'] = 'I_D_wo' seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] random.shuffle(seq) p.participant.vars['seq'] = seq # print('\nseq:', p.participant.vars['seq']) p.participant.vars['state_realized'] = [] p.participant.vars['draw'] = [] draw = [] for i in seq: structure = info_structure[i] state = [1, 2] random.shuffle(state) if state[0] == 1: draw = np.random.binomial(1, structure[0], 1) else: draw = np.random.binomial(1, 1 - structure[1], 1) p.participant.vars['state_realized'].append(state[0]) if draw == 1: p.participant.vars['draw'].append(1) else: p.participant.vars['draw'].append(2) for j in range(1, Constants.num_rounds + 1): p.participant.vars['neighbor_id' + str(j)] = None # print('\nstate_realized:', p.participant.vars['state_realized']) # print('\ndraw:', p.participant.vars['draw']) round_order = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] random.shuffle(round_order) for t in range(1, Constants.num_rounds + 1): # print('\nPLAYER:',p.id_in_group) directory_temp = './_static/global/data_wo_info' folders = os.listdir(directory_temp) folder = random.choice(folders) directory = directory_temp + '/' + folder files = os.listdir(directory) file = random.choice(files) data = pd.read_csv(directory+'/'+file) neighbor = random.choice(data.index) # ============================================================================= # p.participant.vars['neighbor_age' + str(t)] = data['Age'][neighbor] # p.participant.vars['neighbor_edu' + str(t)] = data['Education'][neighbor] # p.participant.vars['neighbor_gender' + str(t)] = data['Gender'][neighbor] # p.participant.vars['neighbor_probcourse' + str(t)] = data['Prob_course'][neighbor] # ============================================================================= if (folder == 'old'): neighbor_seq = [int(i) for i in re.sub('[\[\]\s]','',data['seq'][neighbor]).split(',')] neighbor_round = neighbor_seq.index(round_order[t - 1]) + 1 # print('\nneighbor_round:',neighbor_round) p.participant.vars['neighbor_file' + str(t)] = file p.participant.vars['neighbor_id' + str(t)] = neighbor p.participant.vars['neighbor_round' + str(t)] = neighbor_round p.participant.vars['neighbor_seq' + str(t)] = round_order[t - 1] neighbor_structure = info_structure[round_order[t - 1]] neighbor_state_realized = [int(i) for i in re.sub('[\[\]\s]','',data['state_realized'][neighbor]).split(',')] neighbor_state = neighbor_state_realized[neighbor_round - 1] neighbor_draw_list = [int(i) for i in re.sub('[\[\]\s]','',data['state_realized'][neighbor]).split(',')] neighbor_draw = neighbor_draw_list[neighbor_round - 1] neighbor_Guess_list = yaml.load(data['Guesses'][neighbor]) neighbor_guess = re.sub('Picture B','Box T (Tail)',re.sub('Picture A','Box H (Head)',neighbor_Guess_list[str(neighbor_round - 1)])) else: neighbor_data = yaml.load(data['player.participant_vars_dump'][neighbor]) neighbor_seq = neighbor_data['seq'] neighbor_round = neighbor_seq.index(round_order[t - 1]) + 1 # print('\nneighbor_round:',neighbor_round) p.participant.vars['neighbor_file' + str(t)] = file p.participant.vars['neighbor_id' + str(t)] = neighbor p.participant.vars['neighbor_round' + str(t)] = neighbor_round p.participant.vars['neighbor_seq' + str(t)] = round_order[t - 1] neighbor_structure = info_structure[round_order[t - 1]] neighbor_state_realized = neighbor_data['state_realized'] neighbor_state = neighbor_state_realized[neighbor_round - 1] neighbor_draw_list = neighbor_data['state_realized'] neighbor_draw = neighbor_draw_list[neighbor_round - 1] neighbor_guess = neighbor_data['Guesses'][neighbor_round - 1] p.participant.vars['neighbor_guess'+ str(t)] = neighbor_guess p.participant.vars['neighbor_structure'+ str(t)] = neighbor_structure p.participant.vars['neighbor_state'+ str(t)] = neighbor_state p.participant.vars['neighbor_draw'+ str(t)] = neighbor_draw p.participant.vars['flag'] = 0 n = Constants.num_choices class Group(BaseGroup): pass class Player(BasePlayer): Guess_About_Neighbor = models.IntegerField( choices=[ [1, 'Box H (Head)'], [2, 'Box T (Tail)'], ], widget=widgets.RadioSelect ) Confidence_for_neighbor = models.IntegerField(min=0, max=100) Switching_point_for_neighbor = models.IntegerField(min=0, max=100) Major = models.StringField() Age = models.IntegerField(min=0, max=100) Prob_course = models.BooleanField() Education = models.IntegerField() Gender = models.IntegerField( choices=[ [1, 'Male '], [2, 'Female '], [3, 'Other '], ], widget=widgets.RadioSelectHorizontal ) Comment_ball = models.LongStringField() Comment_neighbor = models.LongStringField() Feedback = models.LongStringField() participant_vars_dump = models.LongStringField() # set player's payoff # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: def set_payoffs(self): # random draw to pick one round from each part # ------------------------------------------------------------------------------------------------------------ random_part1 = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]) # random_part2 = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]) random_part3 = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]) # set player's payoff part 1 # ------------------------------------------------------------------------------------------------------------ guess_or_post = 1 self.participant.vars['pay_off_guess_or_post'] = [guess_or_post] if guess_or_post == 1: if ((self.participant.vars['Guesses'][random_part1 - 1]=='Box T (Tail)')+1 == self.participant.vars['state_realized'][random_part1 - 1]): payoff = [Constants.lottery_b_hi] else: payoff = [Constants.lottery_b_lo] else: X = random.choice(range(1, 101)) if self.participant.vars['switching_rows'][random_part1 - 1] <= X: if ((self.participant.vars['Guesses'][random_part1 - 1] == 'Box T (Tail)') + 1 == self.participant.vars['state_realized'][random_part1 - 1]): payoff = [Constants.lottery_b_hi] else: payoff = [Constants.lottery_b_lo] else: draw_for_posterior_lottery = np.random.binomial(1, X / 100, 1) if draw_for_posterior_lottery == 1: payoff = [Constants.lottery_b_hi] else: payoff = [Constants.lottery_b_lo] guess_or_post = 1 self.participant.vars['pay_off_guess_or_post'].append(guess_or_post) if guess_or_post == 1: if ((self.participant.vars['Guesses_About_Neighbor'][random_part3 - 1]=='Box T (Tail)')+1 == self.participant.vars['neighbor_state'+ str(random_part3)]): payoff.append(Constants.lottery_b_hi) else: payoff.append(Constants.lottery_b_lo) else: X = random.choice(range(1, 101)) if self.participant.vars['switching_rows_neighbor'][random_part3 - 1] <= X: if ((self.participant.vars['Guesses_About_Neighbor'][random_part3 - 1]=='Box T (Tail)')+1 == self.participant.vars['neighbor_state'+ str(random_part3)]): payoff.append(Constants.lottery_b_hi) else: payoff.append(Constants.lottery_b_lo) else: draw_for_posterior_lottery = np.random.binomial(1, X / 100, 1) if draw_for_posterior_lottery == 1: payoff.append(Constants.lottery_b_hi) else: payoff.append(Constants.lottery_b_lo) # set payoff as global variable # ------------------------------------------------------------------------------------------------------------ self.participant.vars['payoff'] = payoff self.participant.vars['pay_off_randrounds']=[random_part1,random_part3] self.participant.vars['Check_Guesses'] = self.sorted_check(self.participant.vars['Guesses'], self.participant.vars['state_realized'], self.participant.vars['seq']) self.participant.vars['state_realized_sorted'] = self.sort(self.participant.vars['state_realized'], self.participant.vars['seq']) # self.participant.vars['switching_rows_sorted'] = self.sort(self.participant.vars['switching_rows'], # self.participant.vars['seq']) self.participant.vars['Guesses_sorted'] = self.sort(self.participant.vars['Guesses'], self.participant.vars['seq']) self.participant.vars['Guesses_conf_sorted'] = self.sort(self.participant.vars['Guesses_confidence'], self.participant.vars['seq']) self.participant.vars['draw_sorted'] = self.sort(self.participant.vars['draw'], self.participant.vars['seq']) neighbors = [self.participant.vars['neighbor_id' + str(kk)] for kk in range(1, Constants.num_rounds + 1)] neighbors_states = [self.participant.vars['neighbor_state' + str(kk)] for kk in range(1, Constants.num_rounds + 1)] neighbors_round = [self.participant.vars['neighbor_round' + str(kk)] for kk in range(1, Constants.num_rounds + 1)] neighbors_draw = [self.participant.vars['neighbor_draw' + str(kk)] for kk in range(1, Constants.num_rounds + 1)] neighbors_guess = [self.participant.vars['neighbor_guess' + str(kk)] for kk in range(1, Constants.num_rounds + 1)] neighbors_round_seq = [self.participant.vars['neighbor_seq' + str(kk)] for kk in range(1, Constants.num_rounds + 1)] self.participant.vars['Check_Guess_neighbor'] = self.sorted_check(self.participant.vars['Guesses_About_Neighbor'], neighbors_states, neighbors_round_seq) self.participant.vars['neighbor_state_sorted'] = self.sort(neighbors_states, neighbors_round_seq) # self.participant.vars['switching_rows_I_sorted'] = self.sort(self.participant.vars['switching_rows_I'], # neighbors_round_seq) self.participant.vars['Guesses_neighbor_sorted'] = self.sort(self.participant.vars['Guesses_About_Neighbor'], neighbors_round_seq) self.participant.vars['Guesses_conf_neighbor_sorted'] = self.sort( self.participant.vars['Guesses_condifence_About_Neighbor'], neighbors_round_seq) self.participant.vars['neighbor_draw_sorted'] = self.sort(neighbors_draw, neighbors_round_seq) self.participant.vars['neighbor_guess_sorted'] = self.sort(neighbors_guess, neighbors_round_seq) def sorted_check(self, guesses, states, seq): output = [] for i in range(1, len(states)+1): Ind = seq.index(i) output.append(((guesses[Ind] == 'Box T (Tail)') + 1 == states[Ind]) * 1) return output def sort(self, input, orders): temp = [] for i in range(1, len(orders)+1): Ind = orders.index(i) temp.append(input[Ind]) return temp