-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreBehavioralStudy.py
197 lines (167 loc) · 6.68 KB
/
ScoreBehavioralStudy.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
import os
import importlib
import sys
import pandas as pd
import csv
import datetime
import NCMPartv2
import ScoreNIHToolbox
import glob
import numpy as np
importlib.reload(ScoreNIHToolbox)
# importlib.reload(NCMPartv2)
BaseDir = '/home/jsteffen'
BaseDir = '/Users/jasonsteffener'
sys.path.append(os.path.join(BaseDir,'Documents','GitHub','CognitiveTasks','DataHandlingScripts'))
import ProcessBehavioralFunctions
importlib.reload(ProcessBehavioralFunctions)
# importlib.reload(DataHandlingScriptsPart1)
# import DataHandlingBehavioral
# importlib.reload(DataHandlingBehavioral)
OutDataFolder = os.path.join(BaseDir, 'Dropbox/steffenercolumbia/Projects/MyProjects/NeuralCognitiveMapping/NeuroPsychData/CassParticipantData/data')
df = ProcessBehavioralFunctions.CycleOverBehDataFolders(OutDataFolder)
# subid = '1001003'
# VisitFolder = os.path.join(OutDataFolder,subid)
# ScoreOneBeh(VisitFolder,subid)
# Data = DataHandlingBehavioral.ReadBehFile(VisitFolder, '*DMS_Block', subid)
#
#
# Data = DataHandlingBehavioral.CheckDMSDataFrameForLoad(Data)
def ScoreOneBeh(subdir, subid):
Results = DataHandlingBehavioral.LoadRawBehData(subdir, subid)
return Results
# Load the NIH data
dfNIH = ScoreNIHToolbox.Run(BaseDir)
inputFileName = os.path.join(BaseDir,'Dropbox/steffenercolumbia/Projects/MyProjects/NeuralCognitiveMapping/data/SurveyMonkeyExports/Participant Questionnaire.csv')
# open the file
fid = open(inputFileName,'r', encoding="ISO-8859-1")
data = csv.reader(fid)
#data = pandas.read_csv(fid, sep=',', encoding='latin-1')
# Read whole file into a list
LL = list(data)
fid.close()
NPart = len(LL) - 2
HeaderLine1 = LL[0]
HeaderLine2 = LL[1]
PartData = LL[2:]
PartCount = 0
DataList = []
for i in PartData:
print("=================================")
# Skip rows that are test subjects
if len(i[9]) == 8:
if not i[9][5] == '9':
try:
# 14 has missing NIH data
# 15 has missing block data
# 16 has data that does not look good
# Empty rows in the survey monkey file need to be removed
part = NCMPartv2.NCMParticipant()
part.MakeParticipant(i)
part.ReadBlockDataLong('DMS_Block','DMS',6)
part.ReadStairData('DMS')
DataList.append(part)
# print str(RowCount)+" "+part.subid
PartCount += 1
except:
print(str(PartCount)+" "+part.subid)
print("############# >>>>> Uhh Oh <<<<< ############")
PartCount += 1
else:
print("Skipping: %s"%(i[9]))
# Map the psychopy ans SM data together
for i in DataList:
SMsubid = i.subid
dfLOC = (df['AAsubid'] == SMsubid)
dfLOC = [i for i, x in enumerate(dfLOC) if x]
if len(dfLOC) > 0:
df.loc[dfLOC[0],'ageSM'] = i.age
df.loc[dfLOC[0],'ageGroupSM'] = i.ageGroup
df.loc[dfLOC[0],'eduSM'] = i.edu
df.loc[dfLOC[0],'sex'] = i.sex
df.loc[dfLOC[0],'BDIscore'] = i.BDIscore
df.loc[dfLOC[0],'GDSscore'] = i.GDSscore
df.loc[dfLOC[0],'FOSC'] = i.FOSC
df.loc[dfLOC[0],'PAAerobic'] = i.PAAerobicMin
df.loc[dfLOC[0],'PABicycling'] = i.PABicyclingMin
df.loc[dfLOC[0],'PAJogging'] = i.PAJoggingMin
df.loc[dfLOC[0],'PALapSwim'] = i.PALapSwimMin
df.loc[dfLOC[0],'PALowIntensity'] = i.PALowIntensityMin
df.loc[dfLOC[0],'PARunning'] = i.PARunningMin
df.loc[dfLOC[0],'PATennis'] = i.PATennisMin
df.loc[dfLOC[0],'PAWalkHike'] = i.PAWalkHikeMin
else: # the part is not in the tasks DF
pass
dfAll = df.merge(dfNIH, left_on='AAsubid', right_on='AAsubid', how='outer')
dfAll['Checked'] = np.zeros([len(dfAll)])
BaseFileName = 'NCM_BehavStudy_Tasks_NIH_SM'
now = datetime.datetime.now()
NowString = now.strftime("_updated_%b-%d-%Y_%H-%M.csv")
NewOutFileName = BaseFileName + NowString
OutFile = os.path.join(OutDataFolder, NewOutFileName)
dfAll.to_csv(OutFile, index = False)
dfOld = LoadExistingData(OutDataFolder, BaseFileName)
dfUpdated = SeeIfDataHasBeenChecked(dfAll, dfOld)
def LoadExistingData(OutDataFolder, BaseFileName):
Files = glob.glob(os.path.join(OutDataFolder, BaseFileName + '*.csv'))
df = pd.read_csv(Files[-1])
return df
def SeeIfDataHasBeenChecked(dfAll, dfOld):
NewPartList = ScoreNIHToolbox.ExtractUniquePartIDs(dfAll['AAsubid'])
OldPartList = ScoreNIHToolbox.ExtractUniquePartIDs(dfOld['AAsubid'])
for i in NewPartList:
print(i)
try:
# is the sub from dfAll in dfOld
if len(find(OldPartList==i)) > 0:
# this subject IS in the old table
# pull out their new data
# indexOld = dfOld.index[dfOld['AAsubid']==i].tolist()
tempOld = dfOld[dfOld['AAsubid'] == i]
if (tempOld['Checked'] == 0).all():
# update old DF with the new data
tempNew = dfAll[dfAll['AAsubid'] == i]
#replace old with new
indexNew = dfAll.index[dfAll['AAsubid']==i].tolist()
indexOld = dfOld.index[dfOld['AAsubid']==i].tolist()
dfOld.loc[indexOld[0]] = dfAll.loc[indexNew[0]]
else:
# do not change old data
pass
else:
# no .. add them to dfOld
indexNew = dfAll.index[dfAll['AAsubid']==i].tolist()
dfOld = dfOld.append(dfAll.loc[indexNew])
except:
print("problem with: %s"%(i))
return dfOld
#
# yes
# is Checked in dfOld == 1?
# yes, do nothing
# else, update dfOld
#check existing data file to see i
# inputFileName = [u'/Users/jasonsteffener/Dropbox/steffenercolumbia/Projects/MyProjects/NeuralCognitiveMapping/data/SurveyMonkeyExports/Participant Questionnaire.csv']
# # open the file
# fid = open(inputFileName[0],'r', encoding="ISO-8859-1")
#
# data = csv.reader(fid)
# data = pd.read_csv(fid, sep=',', encoding='latin-1')
# #
# df = pd.read_csv(inputFileName[0], sep=',', encoding='latin-1')
# indices = [i for i, c in enumerate(df.columns) if not c.startswith('Unnamed')]
# questions = [c for c in df.columns if not c.startswith('Unnamed')]
# slices = [slice(i, j) for i, j in zip(indices, indices[1:] + [None])]
# for q in slices:
# print(df.iloc[:, q]) # Use `display` if using Jupyter
#
# def parse_response(s):
# try:
# return s[~s.isnull()][0]
# except IndexError:
# return np.nan
#
# data = [df.iloc[:, q].apply(parse_response, axis=1)[1:] for q in slices]
# dfOUT = pd.concat(data, axis=1)
# dfOUT.columns = questions
####