/
task.py
305 lines (264 loc) · 13.2 KB
/
task.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# encoding: UTF-8
from sys import argv
from random import uniform, expovariate
from datetime import date
from time import time
import urllib, urllib2
import hmac, json
import wx
from psychopy.visual import Rect
import schizoidpy
from schizoidpy import init_wx, box, okay
import commitments
from commitments import long_wrapped_text, message
par = dict(zip(argv[1::2], argv[2::2])) # DEPLOYMENT SCRIPT EDITS THIS LINE
o = schizoidpy.Task(
button_radius = .12,
okay_button_pos = (0, -.7))
o.set_pyglet_visible(False)
o.save('task', par['task'])
small_dialog_width = 300
# ------------------------------------------------------------
# Subroutines
# ------------------------------------------------------------
def d8(d):
return d.year * 10000 + d.month * 100 + d.day
class EmailDialog(wx.Dialog):
def __init__(self, prompt):
wx.Dialog.__init__(self, None, style =
wx.DEFAULT_DIALOG_STYLE & ~wx.CLOSE_BOX)
self.input1 = wx.TextCtrl(self)
#self.input.Bind(wx.EVT_CHAR, generic_keypress(self))
self.input1.SetFocus()
self.input2 = wx.TextCtrl(self)
box(self, wx.VERTICAL,
wx.Size(small_dialog_width + 20, 10),
(long_wrapped_text(self, prompt), 0, wx.ALIGN_CENTER_HORIZONTAL),
(self.input1, 0, wx.EXPAND | wx.ALL, 10),
(self.input2, 0, wx.EXPAND | wx.ALL, 10),
(okay(self, True), 0, wx.ALIGN_CENTER_HORIZONTAL)).Fit(self)
def server_send(subject, email, activity_names):
'Send email addresses and activities to the server.'
if not par['use_server']: return
export_json = json.dumps(dict(
time = int(time()),
subject = subject,
email = email,
first_d8 = d8(commitments.dateplus(date.today(), 1)),
last_d8 = d8(commitments.dateplus(date.today(), commitments.checkin_range)),
activities = activity_names))
f = urllib2.urlopen(par['receiver_url'], data = urllib.urlencode(dict(
hmac = hmac.new(par['hmac_key'], export_json).hexdigest(),
json = export_json)))
if par['debug']: print f.read()
f.close()
divider_bar = Rect(o.win,
fillColor = 'black', lineColor = 'black',
width = 1.5, height = .01)
prompt = o.text(0, .8, 'Which option would you prefer?')
dec_rate = 1 / 0.25
inc_rate = 1 / 0.523852201187
trials = 26
# Note that the catch-trials specifications implicitly rely on
# this.
def econ_test(dkey_prefix, instructions, text_top, text_bottom,
initial_discount_guess, catch_big, catch_small):
with o.dkey_prefix(('econ', dkey_prefix)):
o.instructions('instructions', instructions, wrap = 1.5)
with o.showing(divider_bar, prompt):
discount_guess = initial_discount_guess
for trial in range(trials):
catchtype = (
'small' if trial in catch_small else
'big' if trial in catch_big else
None)
o.save(('catchtype', trial), catchtype)
discount = (
# On catch_small trials, make the small option
# dominate, so all subjects should choose the
# small option.
1.13 if catchtype == "small" else
# On catch_big trials, make the difference
# between the options lopsided so that only
# very extreme preferences would be consistent
# with choosing the small option.
.03 if catchtype == "big" else
# On normal trials, use our current guess
# of the discount factor.
discount_guess)
o.save(('discount', trial), discount)
big = uniform(5, 95)
small = big * discount
big = round(big, 2)
small = round(small, 2)
if small <= .01:
small = .01
if not catchtype and big <= small:
big = small + .01
o.save(('big', trial), big)
o.save(('small', trial), small)
big = '${:.2f}'.format(big)
small = '${:,.2f}'.format(small)
o.wait_screen(.5)
choice = o.keypress_screen(('took_big', trial),
dict(up = False, down = True),
o.text(0, .25, text_top(big, small)),
o.text(0, -.25, text_bottom(big, small)))
if not catchtype:
v = discount_guess / (1 - discount_guess)
if choice:
v *= 1 + expovariate(inc_rate)
else:
v /= 1 + expovariate(dec_rate)
discount_guess = v / (v + 1)
# ------------------------------------------------------------
# Preliminaries
# ------------------------------------------------------------
# Check for a network connection.
if par['use_server'] and not par['debug']:
assert '<title>Example Domain</title>' in urllib2.urlopen('http://example.org').read()
if par['debug_serverside']:
server_send('test1', par['debug_email'], ['Studying', 'Jogging', 'Eating'])
exit()
init_wx()
if par['debug']:
o.data['subject'] = 'test'
else:
o.get_subject_id('Decision-Making')
o.start_clock()
# ------------------------------------------------------------
# Get an email address and commitments
# ------------------------------------------------------------
with o.timestamps('email'):
while True:
dlg = EmailDialog("After you leave the lab, we'll communicate with you by email for the rest of the study.\n\nEnter your email address twice.")
dlg.CenterOnScreen(wx.BOTH)
dlg.ShowModal()
dlg.Destroy()
s = dlg.input1.GetValue().strip()
if s != dlg.input2.GetValue().strip():
message("The addresses you entered don't match.")
elif ' ' in s:
message("Spaces aren't allowed in email addresses.")
elif '@' not in s:
message('Your email address is missing an "@".')
else:
o.save('email', s)
break
with o.timestamps('activities'):
activities = commitments.get_activities()
server_send(o.data['subject'], o.data['email'], activities)
with o.timestamps('commitments'):
o.save('commitments', commitments.get_commitments(activities))
# ------------------------------------------------------------
# Administer econometric tests
# ------------------------------------------------------------
o.set_pyglet_visible(True)
econ_test('patience',
'In this task, you will make a series of hypothetical choices.\n\n'
'Each trial will present you with a hypothetical choice between two amounts of money delivered to you at a given time in the future. Press the up arrow key if you would prefer the upper option and the down arrow key if you would prefer the lower option.\n\n'
'Even though these are completely hypothetical decisions, try your best to imagine what you would choose if you were really offered these choices.',
text_top = lambda llr, ssr: '{} today'.format(ssr),
text_bottom = lambda llr, ssr: '{} in one month'.format(llr),
initial_discount_guess = .5,
catch_big = (5, 14, 23),
catch_small = (2, 6, 9))
econ_test('shifted_patience',
'The next task is like the previous one, except the top option is delayed by one month and the bottom option is delayed by two months.',
text_top = lambda llr, ssr: '{} in one month'.format(ssr),
text_bottom = lambda llr, ssr: '{} in two months'.format(llr),
initial_discount_guess = .5,
catch_big = (3, 6, 8),
catch_small = (1, 16, 22))
econ_test('risk_aversion',
'In this task, you will choose between a gamble and a sure gain.\n\n'
'The gamble has a a 95% chance of giving you money and a 5% chance of yielding nothing. The other option has a 100% chance of yielding the indicated amount.',
text_top = lambda risky, sure: '{} (100% chance)'.format(sure),
text_bottom = lambda risky, sure: '{} (95% chance) or\nnothing (5% chance)'.format(risky),
initial_discount_guess = .95,
catch_big = (10, 14, 25),
catch_small = (5, 17, 20))
econ_test('loss_aversion',
'In this task, you will choose whether or not to take a gamble.\n\n'
'The gamble is equally likely to make you gain or lose money, but the amount to be gained and the amount to be lost may differ.',
text_top = lambda gain, loss: 'Nothing (100% chance)',
text_bottom = lambda gain, loss: 'Gain {} (50% chance) or\nlose {} (50% chance)'.format(gain, loss),
initial_discount_guess = .5,
catch_big = (1, 2, 8),
catch_small = (0, 16, 23))
# ------------------------------------------------------------
# Administer questionnaires
# ------------------------------------------------------------
o.questionnaire_screen('bfi',
# John, O. P., Naumann, L. P., & Soto, C. J. (2008). Paradigm shift to the integrative Big Five trait taxonomy: History, measurement, and conceptual issues. In O. P. John, R. W. Robins, & L. A. Pervin (Eds.), Handbook of personality: Theory and research (3rd ed., pp. 114–158). New York, NY: Guilford Press. ISBN 978-1-59385-836-0
u'How much would you agree that you are someone who…?',
scale_levels = ('Disagree\nstrongly', 'Disagree\na little', 'Neither agree\nnor disagree', 'Agree\na little', 'Agree\nstrongly'),
questions = [
'Is talkative',
'Tends to find fault with others',
'Does a thorough job',
'Is depressed, blue',
'Is original, comes up with new ideas',
'Is reserved',
'Is helpful and unselfish with others',
'Can be somewhat careless',
'Is relaxed, handles stress well',
'Is curious about many different things',
'Is full of energy',
'Starts quarrels with others',
'Is a reliable worker',
'Can be tense',
'Is ingenious, a deep thinker',
'Generates a lot of enthusiasm',
'Has a forgiving nature',
'Tends to be disorganized',
'Worries a lot',
'Has an active imagination',
'Tends to be quiet',
'Is generally trusting',
'Tends to be lazy',
'Is emotionally stable, not easily upset',
'Is inventive',
'Has an assertive personality',
'Can be cold and aloof',
'Perseveres until the task is finished',
'Can be moody',
'Values artistic, aesthetic experiences',
'Is sometimes shy, inhibited',
'Is considerate and kind to almost everyone',
'Does things efficiently',
'Remains calm in tense situations',
'Prefers work that is routine',
'Is outgoing, sociable',
'Is sometimes rude to others',
'Makes plans and follows through with them',
'Gets nervous easily',
'Likes to reflect, play with ideas',
'Has few artistic interests',
'Likes to cooperate with others',
'Is easily distracted',
'Is sophisticated in art, music, or literature'])
o.questionnaire_screen('cfc',
# Strathman, A., Gleicher, F., Boninger, D. S., & Edwards, C. S. (1994). The consideration of future consequences: Weighing immediate and distant outcomes of behavior. Journal of Personality and Social Psychology, 66(4), 742–752. doi:10.1037/0022-3514.66.4.742
'How characteristic of you are each of these statements?',
scale_levels = ('Extremely\nuncharacteristic', 'Somewhat\nuncharacteristic', 'Uncertain', 'Somewhat\ncharacteristic', 'Extremely\ncharacteristic'),
questions = [
'I consider how things might be in the future, and try to influence those things with my day to day behavior.',
'Often I engage in a particular behavior in order to achieve outcomes that may not result for many years.',
'I only act to satisfy immediate concerns, figuring the future will take care of itself.',
'My behavior is only influenced by the immediate (i.e., a matter of days or weeks) outcomes of my actions.',
'My convenience is a big factor in the decisions I make or the actions I take.',
'I am willing to sacrifice my immediate happiness or well-being in order to achieve future outcomes.',
'I think it is important to take warnings about negative outcomes seriously even if the negative outcome will not occur for many years.',
'I think it is more important to perform a behavior with important distant consequences than a behavior with less-important immediate consequences.',
'I generally ignore warnings about possible future problems because I think the problems will be resolved before they reach crisis level.',
'I think that sacrificing now is usually unnecessary since future outcomes can be dealt with at a later time.',
'I only act to satisfy immediate concerns, figuring that I will take care of future problems that may occur at a later date.',
'Since my day to day work has specific outcomes, it is more important to me than behavior that has distant outcomes.'])
# ------------------------------------------------------------
# Done!
# ------------------------------------------------------------
o.done(par['py_output_path_fmt'].format(**o.data))
o.wait_screen(1,
o.text(0, 0, 'Done!\n\nPlease let the experimenter know you are done.'))