from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import numpy as np import csv import matplotlib import pandas as pd import matplotlib.pyplot as plt import matplotlib matplotlib.use('Agg') from io import StringIO from matplotlib import pyplot as plt from io import BytesIO from base64 import b64encode import urllib doc = """ """ class Constants(BaseConstants): name_in_url = 'a_trust_game' players_per_group = None num_rounds = 1 endowment = 10 multiplier = 3 instructions_template = 'trust_game_andrej/instructions.html' ############################### # SUBSESSION MODELS ############################### class Subsession(BaseSubsession): pass ############################### # GROUP MODELS ############################### class Group(BaseGroup): pass ############################### # PLAYER MODELS ############################### class Player(BasePlayer): ################### # Trustor decisions ################### sent_amount = models.IntegerField( choices=[[0, 'Keep your 10 monetary units'], [10, 'Transfer your 10 monetary units']], widget=widgets.RadioSelect, label='Please make a choice', doc="""Trsutor transfer""" ) ################### # Trustee decisions ################### sent_back_amount = models.IntegerField( min=0, max=30, doc="""Amount trustee""") ################### # Questionnaire items ################### q1 = models.IntegerField(label='', initial=None, widget=widgets.RadioSelect, choices=[[0, '18-25'], [1, '26-35'], [2, '36-45'], [3, '46-55'], [4, '56-65'], [5, '>66']]) q2 = models.IntegerField(label='', initial=None, widget=widgets.RadioSelect, choices=[[0, 'Male'], [1, 'Female'], [2, 'Non-binary']]) q3 = models.IntegerField(label='', initial=None, widget=widgets.RadioSelect, choices=[[0, 'Banking'], [1, 'Health'], [2, 'Tourism'], [3, 'Logistics'], [4, 'IT/communication'], [5, 'Engineering'], [6, 'Car manufacturing'], [7, 'Insurance'], [8, 'Retail'], [9, 'Public service'], [10, 'Consulting'], [11, 'Other']]) # banking, health, tourism, logistics, IT/communication, engineering, electronics, car manufacturing, insurance, energy, retail, public service, consulting, auditing, other ################### # PLOTTING ################### def aggregates(self): others_ = self.get_others_in_group() obs_ = [] for p in others_: obs_.append([p.sent_amount, p.sent_back_amount, p.q1, p.q2, p.q3] ) #, p.c1, p.c2, p.c3, p.c4, p.c5]) df_ = pd.DataFrame(obs_, columns=['trustor', 'trustee', 'q1', 'q2', 'q3']) #, 'c1', 'c2', 'c3', 'c4', 'c5']) ##### IMAGE TRUSTOR OVERALL ##### # Actual plot plt.figure(figsize=(4, 3)) plt.bar(1, df_['trustor'].mean(), width=0.8) plt.ylabel('Avg. monetary units transferred', fontsize=9) plt.xlim(0, 2) plt.ylim(0, 11) plt.xticks([]) plt.yticks([2, 4, 6, 8, 10]) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image'] = urllib.parse.quote(string) # clearing current window plt.clf plt.close ##### IMAGE TRUSTEE OVERALL ##### plt.figure(figsize=(4, 3)) plt.bar(1, df_['trustee'].mean(), width=0.8) plt.ylabel('Avg. monetary units transferred back', fontsize=9) plt.xlim(0, 2) plt.ylim(0, 31) plt.yticks([5,10,15,20,25,30]) plt.xticks([]) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image2'] = urllib.parse.quote(string) # clearing current window plt.close plt.clf ##### IMAGE TRUSTEE AGE ##### df_q1 = df_.groupby(['q1']).mean() plt.figure(figsize=(4, 3)) plt.bar(df_q1.index, df_q1['trustee'], width=0.8) plt.ylabel('Avg. monetary units transferred back', fontsize=9) plt.ylim(0, 31) plt.yticks([5,10,15,20,25,30]) plt.xlim(-1, 6) plt.xticks([0,1,2,3,4,5], ['18-25', '26-35', '36-45', '46-55', '56-65', '>66'], rotation=45) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image3'] = urllib.parse.quote(string) # clearing current window plt.close plt.clf ##### IMAGE TRUSTOR AGE ##### df_q1 = df_.groupby(['q1']).mean() plt.figure(figsize=(4, 3)) plt.bar(df_q1.index, df_q1['trustor'], width=0.8) plt.ylabel('Avg. monetary units transferred back', fontsize=9) plt.ylim(0, 11) plt.yticks([2,4,6,8,10]) plt.xlim(-1, 6) plt.xticks([0, 1, 2, 3, 4, 5], ['18-25', '26-35', '36-45', '46-55', '56-65', '>66'], rotation=45) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image4'] = urllib.parse.quote(string) # clearing current window plt.close plt.clf ##### IMAGE TRUSTEE GENDER ##### df_q1 = df_.groupby(['q2']).mean() plt.figure(figsize=(4, 3)) plt.bar(df_q1.index, df_q1['trustee'], width=0.8) plt.ylabel('Avg. monetary units transferred back', fontsize=9) plt.ylim(0, 31) plt.yticks([5,10,15,20,25,30]) plt.xlim(-1, 3) plt.xticks([0, 1, 2], ['Male', 'Female', 'Non-binary'], rotation=45) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image5'] = urllib.parse.quote(string) # clearing current window plt.close plt.clf ##### IMAGE TRUSTOR GENDER ##### df_q1 = df_.groupby(['q2']).mean() plt.figure(figsize=(4, 3)) plt.bar(df_q1.index, df_q1['trustor'], width=0.8) plt.ylabel('Avg. monetary units transferred back', fontsize=9) plt.ylim(0, 11) plt.yticks([2,4,6,8,10]) plt.xlim(-1, 3) plt.xticks([0, 1, 2], ['Male', 'Female', 'Non-binary'], rotation=45) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image6'] = urllib.parse.quote(string) # clearing current window plt.close plt.clf ##### IMAGE TRUSTEE INDUSTRY ##### df_q1 = df_.groupby(['q3']).mean() plt.figure(figsize=(4, 3)) plt.bar(df_q1.index, df_q1['trustee'], width=0.8) plt.ylabel('Avg. monetary units transferred back', fontsize=9) plt.ylim(0, 31) plt.yticks([5,10,15,20,25,30]) plt.xlim(-1, 12) plt.xticks([0, 1, 2,3,4,5,6,7,8,9,10,11], ['Banking', 'Health', 'Tourism','Logistics', 'IT/communication', 'Engineering', 'Car manufacturing', 'Insurance', 'Retail', 'Public service', 'Consulting', 'Other'], rotation=90) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image7'] = urllib.parse.quote(string) # clearing current window plt.close plt.clf ##### IMAGE TRUSTOR INDUSTRY ##### df_q1 = df_.groupby(['q3']).mean() plt.figure(figsize=(4, 3)) plt.bar(df_q1.index, df_q1['trustor'], width=0.8) plt.ylabel('Avg. monetary units transferred back', fontsize=9) plt.ylim(0, 11) plt.yticks([2,4,6,8,10]) plt.xlim(-1, 12) plt.xticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], ['Banking', 'Health', 'Tourism', 'Logistics', 'IT/communication', 'Engineering', 'Car manufacturing', 'Insurance', 'Retail', 'Public service', 'Consulting', 'Other'], rotation=90) # translation of plot fig = plt.gcf() buf = BytesIO() fig.savefig(buf, format='png', bbox_inches='tight') buf.seek(0) string = b64encode(buf.read()) self.participant.vars['image8'] = urllib.parse.quote(string) # clearing current window plt.close plt.clf # buf.seek(0) # encoded = b64encode(buf.getvalue()) # # self.participant.vars['image'] = ''.format(encoded.decode('utf-8')) #urllib.parse.quote(string) #imgdata = StringIO() #fig.savefig(imgdata, format='svg') #imgdata.seek(0) #self.participant.vars['image'] = imgdata.getvalue() # def create_figure(self): # plt.figure(figsize=(4, 3)) # plt.plot(range(3), range(3), color='blue', marker='d', markersize=10) # # fig = plt.gcf() # buf = BytesIO() # fig.savefig(buf, format='png', bbox_inches='tight') # buf.seek(0) # # string = b64encode(buf.read()) # # return urllib.parse.quote(string)