forked from djangraw/MoodDrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadRutledgeGbeData.py
360 lines (302 loc) · 13.6 KB
/
LoadRutledgeGbeData.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
LoadRutledgeGbeData.py
Code to load "mobile app" data from Robb Rutledge's Great Brain Experiment
Created on Tue Jun 23 12:17:41 2020
@author: jangrawdc
-Updated 7/31/20 by DJ - added pctNoMoves column
-Updated 1/22/20 by DJ - added secondTimeSubmitted column to summary
-Updated 4/8/21 by DJ - adapted for shared code structure.
-Updated 4/22/21 by DJ - changed to use new dataset published publicly by Rutledge lab
"""
# %% Extract data points of interest from .mat file
# Import packages
import scipy.io
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
# Get in & out directories
dataDir = '../Data' # directory where data is found
outDir = '%s/OutFiles'%dataDir # directory where processed data files go
outFigDir = '../Figures' # directory where figures should go
# %% Load new data
inFile = '%s/PilotData/Rutledge_GBE_risk_data.mat'%dataDir # Path to mobile app data file
# Load full dataset
print('Loading full dataset from %s...'%inFile)
allData = scipy.io.loadmat(inFile, mdict=None, squeeze_me=True)['subjData']
# crop dataset to make it match the original version used in our paper
inFile = '../Data/PilotData/Rutledge_GBE_IDs.csv'
print('Loading IDs of included subjects from %s...'%inFile)
subjIDs = pd.read_csv(inFile,header=None).values[:,0]
print('Cropping...')
allData_new = allData[subjIDs-1] # IDs from Rutledge lab are 1-based, so we subtract 1 for python's zero-based numbering
# New dataset distributed by Rutledge lab has 14 fields:
# ID, age, isFemale, location, lifeSatisfaction, education, nativeLanguage, deviceType, nPlays, timesPlayed, dayNumber, designVersion, datHeader, data
# The original dataset used in this paper had 6 fields:
# Location, timeSubmitted, appversion, data, noPlays, life satisfaction
old_field_indices = [3,0,11,13,8,4] # time submitted not present in new dataset... Subbing ID instead.
allData = [[thisData[index] for index in old_field_indices] for thisData in allData_new]
print('Done!')
# %% Extract fields
# get one-time data
noPlays = np.array([thisData[4] for thisData in allData])
lifeSatisfaction = np.array([thisData[5] for thisData in allData])/10.0 # rescale to 0-1
# get each-play data from FIRST run
location = np.array([thisData[0] for thisData in allData])
location[noPlays>1] = [loc for loc in location[noPlays>1]]
allTimesSubmitted = np.array([thisData[1] for thisData in allData])
timeSubmitted = allTimesSubmitted.copy()
timeSubmitted[noPlays>1] = [loc for loc in timeSubmitted[noPlays>1]]
secondTimeSubmitted = np.array(['']*len(timeSubmitted),dtype='object')
secondTimeSubmitted[noPlays>1] = [loc for loc in allTimesSubmitted[noPlays>1]]
appVersion = np.array([thisData[2] for thisData in allData])
appVersion[noPlays>1] = [loc for loc in appVersion[noPlays>1]]
# Get happyData from FIRST run
happyData_list = np.array([thisData[3] for thisData in allData])
nSubj = happyData_list.size
nTrials = happyData_list[0].shape[0]
nCols = happyData_list[0].shape[1]
happyData = np.zeros((nSubj,nTrials,nCols))*np.nan
for i in range(nSubj):
if noPlays[i]==1:
happyData[i,:,:] = happyData_list[i]
else:
happyData[i,:,:] = happyData_list[i][0]
# rescale happiness ratings to 0-1 (only true for new Dryad published dataset)
happyData[:,:,9:10] = happyData[:,:,9:10]/100.0
#%% Extract trial-wise rating info
iRating = np.where(~np.isnan(happyData[0,:,9]))[0]
nRatings = len(iRating)
allRatings = np.zeros((nRatings,nSubj))
allWinnings = np.zeros((nTrials,nSubj))
# amounts
allWins = np.zeros((nTrials,nSubj))
allLosses = np.zeros((nTrials,nSubj))
allCertains = np.zeros((nTrials,nSubj))
allRPEs = np.zeros((nTrials,nSubj))
allGamble = np.zeros((nTrials,nSubj),dtype=int)
allTimes = np.zeros((nTrials,nSubj))
allRTs = np.zeros((nTrials,nSubj))
allRatingMoves = np.zeros((nRatings,nSubj))
allRatingRTs = np.zeros((nRatings,nSubj))
staticTrialDur = 2 # rough estimate from viewing game
for iSubj in range(nSubj):
allCertains[:,iSubj] = happyData[iSubj,:,2]
allWins[:,iSubj] = happyData[iSubj,:,3]
allLosses[:,iSubj] = happyData[iSubj,:,4]
isWin = (happyData[iSubj,:,7]==happyData[iSubj,:,3])
isLoss = (happyData[iSubj,:,7]==happyData[iSubj,:,4])
allGamble[:,iSubj] = happyData[iSubj,:,6]
allWinnings[:,iSubj] = happyData[iSubj,:,7]
allRPEs[isWin,iSubj] = happyData[iSubj,isWin,3] - happyData[iSubj,isWin,4]
allRPEs[isLoss,iSubj] = happyData[iSubj,isLoss,4] - happyData[iSubj,isLoss,3]
allRTs[:,iSubj] = happyData[iSubj,:,8]
allTimes[:,iSubj] = np.cumsum(np.nan_to_num(np.abs(happyData[iSubj,:,13])) +
np.nan_to_num(happyData[iSubj,:,11]) +
np.nan_to_num(happyData[iSubj,:,8]) +
staticTrialDur)
allRatings[:,iSubj] = happyData[iSubj,iRating,9]
allRatingMoves[:,iSubj] = happyData[iSubj,iRating,9] - happyData[iSubj,iRating,10];
allRatingRTs[:,iSubj] = happyData[iSubj,iRating,11]
allRatingTimes = allTimes[iRating,:]
allRatingTimes[0,:] = 0;
print('Adjusting iRating to be trial before which rating was presented')
iRating[1:] = iRating[1:]+1
print('Done!')
pctNoMoves = np.mean(allRatingMoves==0,axis=0)*100
# %% Save subject info
participants = np.arange(nSubj)+50000 # first subject = 50000
columns = ['participant','location','timeSubmitted','appVersion','lifeHappy','noPlays','secondTimeSubmitted']
dfSummary = pd.DataFrame(np.zeros((nSubj,len(columns))),columns=columns)
dfSummary['participant'] = participants
dfSummary['location'] = location
dfSummary['timeSubmitted'] = timeSubmitted
dfSummary['secondTimeSubmitted'] = secondTimeSubmitted
dfSummary['appVersion'] = appVersion
dfSummary['lifeHappy'] = lifeSatisfaction
dfSummary['noPlays'] = noPlays
dfSummary['pctNoMoves'] = pctNoMoves # % ratings where slider wasn't moved
# save results
outFile = '%s/Mmi-GBE_Summary.csv'%(outDir)
print('Saving subject info as %s...'%outFile)
dfSummary.to_csv(outFile)
print('Done!')
# %% Place ratings & trial results in pandas dataframes
columns = ['participant','iBlock','iTrial','time','rating','RT']
dfRatings = pd.DataFrame(np.zeros((nSubj*nRatings,len(columns))),columns=columns)
dfRatings['participant'] = dfRatings['participant'].astype(int)
dfRatings['iBlock'] = dfRatings['iBlock'].astype(int)
dfRatings['iTrial'] = dfRatings['iTrial'].astype(int)
columns = ['participant', 'iBlock', 'iTrial', 'trialType',
'lastHappyRating', 'targetHappiness', 'time', 'choice', 'RT',
'RPE', 'outcome', 'winAmount','loseAmount','certainAmount',
'outcomeAmount', 'currentWinnings','isRatingTrial', 'rating',
'ratingRT', 'ratingTime']
dfTrial = pd.DataFrame(np.zeros((nSubj*nTrials,len(columns))),columns=columns)
dfTrial['participant'] = dfTrial['participant'].astype(int)
dfTrial['iTrial'] = dfTrial['iTrial'].astype(int)
dfTrial['isRatingTrial'] = dfTrial['isRatingTrial'].astype(bool)
dfTrial['iBlock'] = dfTrial['iBlock'].astype(int)
#dfTrial['choice'] = ''
dfTrial['trialType'] = 'random'
dfTrial[['lastHappyRating', 'targetHappiness', 'time', 'choice', 'RT',
'RPE', 'outcome', 'outcomeAmount', 'currentWinnings',
'isRatingTrial', 'rating', 'ratingRT', 'ratingTime']] = np.nan
isRating = ~np.isnan(happyData[0,:,9])
choices = np.array(['certain','gamble'])
iTrials = np.arange(nTrials)
dfRatings['participant'] = np.repeat(participants,nRatings)
dfRatings['iBlock'] = 0
dfRatings['iTrial'] = np.tile(iRating-1, nSubj)
dfRatings['time'] = allRatingTimes.flatten('F')
dfRatings['rating'] = allRatings.flatten('F')
dfRatings['RT'] = allRatingRTs.flatten('F')
dfTrial['participant'] = np.repeat(participants,nTrials)
dfTrial['iTrial'] = np.tile(iTrials,nSubj)
dfTrial['time'] = allTimes.flatten('F')
dfTrial['choice'] = allGamble.flatten('F')
dfTrial['RT'] = allRTs.flatten('F')
dfTrial['winAmount'] = allWins.flatten('F')
dfTrial['loseAmount'] = allLosses.flatten('F')
dfTrial['certainAmount'] = allCertains.flatten('F')
dfTrial['RPE'] = allRPEs.flatten('F')
dfTrial['outcomeAmount'] = allWinnings.flatten('F')
dfTrial['currentWinnings'] = np.cumsum(allWinnings,axis=0).flatten('F')
dfTrial['isRatingTrial'] = np.tile(isRating,nSubj)
dfTrial['outcome'] = 'certain'
dfTrial.loc[dfTrial.outcomeAmount==dfTrial.winAmount,'outcome'] = 'win'
dfTrial.loc[dfTrial.outcomeAmount==dfTrial.loseAmount,'outcome'] = 'lose'
dfTrial.loc[dfTrial.isRatingTrial,'rating'] = allRatings.flatten('F')
dfTrial.loc[dfTrial.isRatingTrial,'ratingRT'] = allRatingRTs.flatten('F')
dfTrial.loc[dfTrial.isRatingTrial,'ratingTime'] = allRatingTimes.flatten('F')
print('Converting choice column from int to string...')
dfTrial.loc[dfTrial.choice==0,'choice'] = 'certain'
dfTrial.loc[dfTrial.choice==1,'choice'] = 'gamble'
print('Done!')
# %% Select 5000 participants as exploratory sample
# Randomly (but repeatably) select 5k subjects
nExp = 5000 # number of exploratory participants
randSeed = 24567 # seed used to get a repeatable set of participants
np.random.seed(randSeed)
# use these 5k subjects as the exploratory mobile app cohort
exploratoryParticipants = np.random.choice(participants,nExp,replace=False)
dfRatings_exp = dfRatings.loc[np.isin(dfRatings.participant,exploratoryParticipants),:]
dfTrial_exp = dfTrial.loc[np.isin(dfTrial.participant,exploratoryParticipants),:]
dfSummary_exp = dfSummary.loc[np.isin(dfSummary.participant,exploratoryParticipants),:]
# Use the rest as confirmatory sample
dfRatings_conf = dfRatings.loc[~np.isin(dfRatings.participant,exploratoryParticipants),:]
dfTrial_conf = dfTrial.loc[~np.isin(dfTrial.participant,exploratoryParticipants),:]
dfSummary_conf = dfSummary.loc[~np.isin(dfSummary.participant,exploratoryParticipants),:]
# %% Save results as csv files
# Save exploratory mobile app cohort
outFile = '%s/Mmi-GbeExplore_Ratings.csv'%(outDir)
print('Saving ratings as %s...'%outFile)
dfRatings_exp.to_csv(outFile)
print('Done!')
outFile = '%s/Mmi-GbeExplore_Trial.csv'%(outDir)
print('Saving trials as %s...'%outFile)
dfTrial_exp.to_csv(outFile)
print('Done!')
outFile = '%s/Mmi-GbeExplore_Summary.csv'%(outDir)
print('Saving subject info as %s...'%outFile)
dfSummary_exp.to_csv(outFile)
print('Done!')
# Same for confirmatory mobile app cohort
outFile = '%s/Mmi-GbeConfirm_Ratings.csv'%(outDir)
print('Saving ratings as %s...'%outFile)
dfRatings_conf.to_csv(outFile)
print('Done!')
outFile = '%s/Mmi-GbeConfirm_Trial.csv'%(outDir)
print('Saving trials as %s...'%outFile)
dfTrial_conf.to_csv(outFile)
print('Done!')
outFile = '%s/Mmi-GbeConfirm_Summary.csv'%(outDir)
print('Saving subject info as %s...'%outFile)
dfSummary_conf.to_csv(outFile)
print('Done!')
# %% Find runs where mood ratings were ignored (could be used to exclude later)
#isFlat = np.all(np.diff(allRatings,axis=0)==0, axis=0)
isFlat = np.all(allRatingMoves==0, axis=0)
print('%d flat subjects.'%np.sum(isFlat))
# %% Plot ratings
# collect summary stats
meanRatings = np.nanmean(allRatings,axis=1)
steRatings = np.nanstd(allRatings,axis=1)/np.sqrt(nSubj)
medianRatings = np.nanmedian(allRatings,axis=1)
medianRatingTimes = np.nanmedian(allRatingTimes,axis=1)
plt.figure(623,figsize=[10,10],dpi=180); plt.clf();
#Plot mean ratings
plt.subplot(2,2,1)
plt.plot(medianRatingTimes, meanRatings,'.-', label='mean+/- ste',zorder=5)
plt.fill_between(medianRatingTimes,meanRatings-steRatings,meanRatings+steRatings,
alpha=0.5,zorder=0)
plt.plot(medianRatingTimes,medianRatings,'.-',label='median')
# Annotate plot
plt.xlabel('mean trial time')
plt.ylabel('mean happiness rating')
plt.title('Rutledge Data (n=%d)'%nSubj)
plt.legend()
# Examine skew of 1st, 2nd, & last ratings
plt.subplot(2,2,2)
xHist = np.linspace(-0.025,1.025,22)
plt.hist(allRatings[0,:],xHist,alpha=0.5,label='first')
plt.hist(allRatings[1,:],xHist,alpha=0.5,label='second')
plt.hist(allRatings[-1,:],xHist,alpha=0.5,label='last')
plt.xlabel('happiness rating')
plt.ylabel('# subjects')
plt.title('Individual happiness ratings (n=%d)'%nSubj)
plt.legend()
# Make 2D histogram
plt.subplot(2,2,3)
# Create heatmap
x = np.tile(np.arange(nRatings),(nSubj,1)).T.flatten();
y = allRatings.flatten();
xedges = np.arange(nRatings+1)-0.5
yedges = np.linspace(-0.025,1.025,22)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=(xedges,yedges))
heatmap = heatmap/nSubj*100;
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
# plot
plt.imshow(np.rot90(heatmap), extent=extent, cmap='jet',aspect='auto') # plot heatmap (must be rotated 90deg for some reason!)
# annotate plot
plt.colorbar().set_label('% subjects');
plt.axhline(y=0.5,linestyle=':',color='w')
plt.title('2D histogram of ratings (n=%d)'%(nSubj))
plt.xlabel('rating number')
plt.ylabel('happiness rating');
# Make spaghetti plot of a few subjects
plt.subplot(2,2,4)
nToPlot = 30
plt.plot(iRating, allRatings[:,:nToPlot],'.-')
plt.xlabel('trial number')
plt.ylabel('happiness rating')
plt.title('Individual ratings of first %d subjects'%nToPlot)
plt.tight_layout()
# Save result
outFile = '%s/RutledgeGbeRatings.png'%outFigDir
print('Saving figure as %s...'%outFile)
plt.savefig(outFile)
print('Done!')
# %% Plot winnings
# collect summary stats
meanWinnings = np.nanmean(allWinnings,axis=1)
steWinnings = np.nanstd(allWinnings,axis=1)/np.sqrt(nSubj)
medianWinnings = np.nanmedian(allWinnings,axis=1)
plt.figure(624); plt.clf();
#Plot winnings
plt.plot(range(nTrials),meanWinnings,'.-', label='mean+/- ste',zorder=5)
plt.fill_between(range(nTrials),meanWinnings-steWinnings,meanWinnings+steWinnings,
alpha=0.5,zorder=0)
plt.plot(range(nTrials),medianWinnings,'.-',label='median')
# Annotate plot
plt.xlabel('trial number')
plt.ylabel('mean winnings')
plt.title('Rutledge Data (n=%d)'%nSubj)
plt.legend()
plt.tight_layout()
# Save result
outFile = '%s/RutledgeGbeWinnings.png'%outFigDir
print('Saving figure as %s...'%outFile)
plt.savefig(outFile)
print('Done!')