Created
August 23, 2011 23:12
-
-
Save theY4Kman/1166866 to your computer and use it in GitHub Desktop.
Parses the JSON events list for the <Trading Game> Mechanical Turk experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
FIRST_MTURK = 1313994874 | |
GAME_LENGTH = 5*60+5 | |
START_CASH = 20.0 | |
APPROVED_MTURK = ('25fead42', '5d7ef660', '23cf8ae3', '2d1402d1', '4c8f688c', | |
'09be3a98', '682550c5', '3ce59c03') | |
def parse_events(fname): | |
'''Takes a filename and parses all its events''' | |
with open(fname, 'r') as fp: | |
return extract_all(json.load(fp)) | |
def extract_all(all_events): | |
'''Takes the dictionary of users->events and returns a dictionary containing | |
the users as keys and the useful lists in a tuple as values''' | |
results = {} | |
for user,events in all_events.items(): | |
if user not in APPROVED_MTURK: | |
continue | |
result = extract_data(events) | |
if result is not None: | |
results[user] = result | |
return results | |
def extract_data(events): | |
'''Generates four lists--time, wallet, value, and net worth--from a list of | |
events. It first goes backward in time calculating the wallet and change in | |
value. Then it works from start to finish calculating the actual value. Next, | |
it goes through again and determines net worth. Finally, it converts the | |
timestamps to time since the start of the game.''' | |
start = None | |
end = None | |
times = [] | |
deltavalues = [] | |
values = [] | |
wallet = [] | |
networth = [] | |
class Value: | |
wallet = START_CASH | |
value = 0.0 | |
value = Value() | |
def add(time, netwallet=None, netvalue=None): | |
times.append(time) | |
if netwallet is not None: | |
value.wallet += netwallet | |
if netvalue is not None: | |
value.value += netvalue | |
values.append(value.value) | |
wallet.append(value.wallet) | |
networth.append(value.value + value.wallet) | |
for event in reversed(events): | |
event = json.loads(event) | |
if event['name'] == 'Began game': | |
start = event['time'] | |
add(start) | |
elif event['name'] == 'EndGame': | |
end = event['time'] | |
add(end) | |
elif event['name'] == 'AuctionSold': | |
add(event['time'], event['data']['price'], -event['data']['item']['value']) | |
elif event['name'] == 'ItemBought': | |
add(event['time'], -event['data']['price'], event['data']['item']['value']) | |
if end is None or start is None: | |
return None | |
after_game = None | |
before_game = 0 | |
# Convert timestamps | |
for x,timestamp in enumerate(times): | |
times[x] -= start | |
if after_game is None and times[x] > GAME_LENGTH: | |
after_game = x | |
if times[x] < 0: | |
before_game = x+1 | |
if before_game > 0 and after_game is not None: | |
break | |
# Remove any entries after the end of the game | |
times = times[before_game:after_game] | |
values = values[before_game:after_game] | |
wallet = wallet[before_game:after_game] | |
networth = networth [before_game:after_game] | |
return (times, wallet, values, networth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment