from otree.api import * import random import os import csv from otree.forms import widgets doc = """ does communicating uncertainty improve trust """ class C(BaseConstants): PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 NAME_IN_URL = 'turst_uncertainty' JACKPOT = cu(100) GUESS_MAX = 100 class Subsession(BaseSubsession): pass class Group(BaseGroup): two_thirds_avg = models.FloatField() best_guess = models.IntegerField() num_winners = models.IntegerField() class Con(BaseConstants): form_model = 'player' form_fields = ['my_field'] #price1 cost 30*4 #price range cost 10*4-40*4 data = [ {'day': 'Mon', 'price1': 156, 'price_range1': [143, 174],'probability1': 73, 'prob_range1':[90, 30], 'price2': 156, 'price_range2': [143, 174],'probability2': 73, 'prob_range2':[90, 30],'price3': 137, 'probability3': 43}, {'day': 'Tue', 'price1': 196, 'price_range1': [189, 203],'probability1': 87, 'prob_range1':[93, 77], 'price2': 196, 'price_range2': [189, 203],'probability2': 87, 'prob_range2':[93, 77],'price3': 132, 'probability3': 18}, {'day': 'Wed', 'price1': 167, 'price_range1': [157, 180],'probability1': 79, 'prob_range1':[91, 51], 'price2': 167, 'price_range2': [157, 180],'probability2': 79, 'prob_range2':[91, 51],'price3': 132, 'probability3': 22}, {'day': 'Thu', 'price1': 163, 'price_range1': [151, 178],'probability1': 76, 'prob_range1':[91, 43], 'price2': 163, 'price_range2': [151, 178],'probability2': 76, 'prob_range2':[91, 43],'price3': 138, 'probability3': 45}, {'day': 'Fri', 'price1': 161, 'price_range1': [150, 177],'probability1': 76, 'prob_range1':[90, 40], 'price2': 161, 'price_range2': [150, 177],'probability2': 76, 'prob_range2':[90, 40],'price3': 137, 'probability3': 43}, {'day': 'Sat', 'price1': 179, 'price_range1': [170, 189],'probability1': 83, 'prob_range1':[92, 65], 'price2': 179, 'price_range2': [170, 189],'probability2': 83, 'prob_range2':[92, 65],'price3': 169, 'probability3': 79}, {'day': 'Sun', 'price1': 169, 'price_range1': [159, 182],'probability1': 80, 'prob_range1':[91, 54], 'price2': 169, 'price_range2': [159, 182],'probability2': 80, 'prob_range2':[91, 54],'price3': 145, 'probability3': 60}, ] class Player(BasePlayer): # guess = models.IntegerField( # min=0, max=C.GUESS_MAX # ) instruction_version = models.StringField() #pic_version = models.IntegerField() random_sequence = models.StringField() number1 = models.IntegerField(label="Sept 1", min=0, max=1000) number2 = models.IntegerField(label="Sept 2", min=0, max=1000) number3 = models.IntegerField(label="Sept 3", min=0, max=1000) data_Mon_1 = models.IntegerField(label="Mon_1") data_Tue_1 = models.IntegerField(label="Tue_1") data_Wed_1 = models.IntegerField(label="Wed_1") data_Thu_1 = models.IntegerField(label="Thu_1") data_Fri_1 = models.IntegerField(label="Fri_1") data_Sat_1 = models.IntegerField(label="Sat_1") data_Sun_1 = models.IntegerField(label="Sun_1") round1 = models.StringField data_Mon_2 = models.IntegerField(label="Mon_2") data_Tue_2 = models.IntegerField(label="Tue_2") data_Wed_2 = models.IntegerField(label="Wed_2") data_Thu_2 = models.IntegerField(label="Thu_2") data_Fri_2 = models.IntegerField(label="Fri_2") data_Sat_2 = models.IntegerField(label="Sat_2") data_Sun_2 = models.IntegerField(label="Sun_2") data_Mon_3 = models.IntegerField(label="Mon_3") data_Tue_3 = models.IntegerField(label="Tue_3") data_Wed_3 = models.IntegerField(label="Wed_3") data_Thu_3 = models.IntegerField(label="Thu_3") data_Fri_3 = models.IntegerField(label="Fri_3") data_Sat_3 = models.IntegerField(label="Sat_3") data_Sun_3 = models.IntegerField(label="Sun_3") experience_short_term_rental = models.StringField( label="Please select your experience with Short-Term Rentals (e.g., Airbnb):", choices=['Hosting', 'Renting', 'Both', 'None'], widget=widgets.RadioSelect ) education = models.StringField( label="What is your highest level of education completed?", choices=['High School', 'University Degree'], widget=widgets.RadioSelect ) experience_probability_statistics = models.StringField( label="Please indicate your experience with Probability and Statistics:", choices=['Completed a high school course', 'Completed a university course', 'None'], widget=widgets.RadioSelect ) numerical_skills = models.StringField( label="How would you rate your numerical skills?", choices=['Low', 'Medium', 'High'], widget=widgets.RadioSelect ) age_group = models.StringField( label="Please select your age group:", choices=['Under 18', '18-24', '25-34', '35-44', '45-54', '55 and over'], widget=widgets.RadioSelect ) # FUNCTIONS def set_payoffs(group: Group): players = group.get_players() guesses = [p.guess for p in players] two_thirds_avg = (2 / 3) * sum(guesses) / len(players) group.two_thirds_avg = round(two_thirds_avg, 2) group.best_guess = min(guesses, key=lambda guess: abs(guess - group.two_thirds_avg)) winners = [p for p in players if p.guess == group.best_guess] group.num_winners = len(winners) for p in winners: p.is_winner = True p.payoff = C.JACKPOT / group.num_winners # PAGES class Welcome(Page): @staticmethod def is_displayed(player: Player): player.instruction_version = random.choice(['a', 'b', 'c','d']) #player.pic_version = random.randint(1, 10) return player.round_number == 1 class demographic(Page): form_model = 'player' form_fields = [ 'experience_short_term_rental', 'education', 'experience_probability_statistics', 'numerical_skills', ] def vars_for_template(self): return { # Define any variables to pass to the template here } class predictionInput1(Page): form_model = 'player' form_fields = ['data_Mon_1','data_Tue_1','data_Wed_1','data_Thu_1','data_Fri_1','data_Sat_1','data_Sun_1'] def vars_for_template(player: Player): return {'data': Con.data} class predictionInput2(Page): form_model = 'player' form_fields = ['data_Mon_2','data_Tue_2','data_Wed_2','data_Thu_2','data_Fri_2','data_Sat_2','data_Sun_2'] def vars_for_template(player: Player): return {'data': Con.data} class predictionInput3(Page): form_model = 'player' form_fields = ['data_Mon_3','data_Tue_3','data_Wed_3','data_Thu_3','data_Fri_3','data_Sat_3','data_Sun_3'] def vars_for_template(player: Player): return {'data': Con.data} class read(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class neighberhoud(Page): form_model = 'player' #form_fields = ['guess'] @staticmethod def vars_for_template(player: Player): imgg_pp=f'http://localhost:8000/static/{player.id_in_group}_plot.png' #plt.clf() #print(img_path) return { 'image_path': imgg_pp} class Guess(Page): form_model = 'player' #form_fields = ['guess'] @staticmethod def vars_for_template(player: Player): group = player.group #random_numbers = [str(random.randint(1, 100)) for _ in range(30)] # Generate 10 random numbers #random_sequence = ', '.join(random_numbers) # Store the numbers as a comma-separated string print(player.subsession.session.config) csv_file_path = os.path.join(player.subsession.session.config['name'], 'Book1.csv') print(csv_file_path) with open(csv_file_path, 'r') as csv_file: csv_reader = csv.DictReader(csv_file) all_rows = list(csv_reader) random_row = random.choice(all_rows) x_values = [] y_values = [] for key, value in random_row.items(): if value.isdigit(): x_values.append(key) y_values.append(int(value)) # Convert the stored random numbers back to a list #random_numbers = [int(x) for x in player.random_sequence.split(', ')] # Create a simple line plot # plt.plot(y_values) # plt.xlabel('Index') # plt.ylabel('Random Number') # plt.title('Random Number Sequence') #return player.round_number == 1 #def vars_for_template(self): #player = self.player # # Convert the stored random numbers back to a list # random_numbers = [int(x) for x in random_sequence.split(', ')] # # Create a simple line plot # plt.plot(random_numbers) # plt.xlabel('Index') # plt.ylabel('Random Number') # plt.title('Random Number Sequence') # Save the figure as an image print({player.id_in_group}) #img_path = f'_static/{player.id_in_group}_plot.png' #plt.savefig(img_path) imgg_pp=f'http://localhost:8000/static/{player.id_in_group}_plot.png' #plt.clf() #print(img_path) return { 'image_path': imgg_pp} class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class payment(Page): @staticmethod def vars_for_template(player: Player): #group = player.group return {'round_n' : 1} page_sequence = [Welcome, read, Introduction, neighberhoud, predictionInput1, predictionInput2, demographic, payment]