-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreSurveyMonkey_NCM002.py
185 lines (154 loc) · 7.02 KB
/
ScoreSurveyMonkey_NCM002.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
# -*- coding: utf-8 -*-
# import pandas as pd
import csv
import os
import numpy as np
import tkinter
from tkinter import messagebox
import datetime
# from dateutil.parser import parse
import sys
sys.path.insert(1,'/Users/jasonsteffener/Documents/GitHub/ScoringCognitiveTasks/code')
sys.path.insert(1,'/Users/jasonsteffener/Documents/GitHub/CognitiveTasks/ConfigFiles')
# import collections
import glob
import shutil
import Class_PANAS
import Class_Demog
import Class_Lifestyle
import importlib
# importlib.reload(Class_PANAS)
importlib.reload(Class_Lifestyle)
BaseDir = '/home/jsteffen/'
BaseDir = '/Users/jasonsteffener/Documents/'
# What folder is this file in?
dir_path = os.path.dirname(os.path.realpath(__file__))
# This will load the config file containing the location of the data folder
# If there is an error it means that the GUI program has not been run.
# The GUI checks to see if thie config file exists. If it does not then it is created.
print(dir_path)
# sys.path.append(os.path.join(dir_path,'..','ConfigFiles'))
import NeuropsychDataFolder
# Load up the data location as a global variable
AllInDataFolder = NeuropsychDataFolder.NeuropsychDataFolder
# Where to put the summary data
AllOutDataFolder = os.path.join(os.path.split(AllInDataFolder)[0], 'SummaryData')
# From the Neuropsych folder change to the SurveyMonkey Folder
SurveyMonkeyDataFolder = os.path.split(AllInDataFolder)[0]
SurveyMonkeyDataFolder = os.path.join(SurveyMonkeyDataFolder, 'SurveyMonkey')
# At home lifestyle questionnaire
LifestyleFileName = 'Royal LifestyleBehavior.csv'
# At the lab demographics questionnaire
DemographicsFileName = 'Royal Participant Questionnaire.csv'
# At the lab PANAS questionnaire
PANASFileName = 'PANAS Questionnaire.csv'
LifestyleInputFile = os.path.join(SurveyMonkeyDataFolder, LifestyleFileName)
DemographicsInputFile = os.path.join(SurveyMonkeyDataFolder, DemographicsFileName)
PANASInputFile = os.path.join(SurveyMonkeyDataFolder, PANASFileName)
def main():
# Check to see if all three files are present
AreAllThreeFilesPresent(LifestyleInputFile, DemographicsInputFile, PANASInputFile)
# Load the data from all the demographics data file
DemoH1, DemoH2, DemoData = ReadSMFileAsCSV(DemographicsInputFile)
# Load the data from the PANAS file
PANASH1, PANASH2, PANASData = ReadSMFileAsCSV(PANASInputFile)
# Load the data from the Lifestyle file
LifeH1, LifeH2, LifeData = ReadSMFileAsCSV(LifestyleInputFile)
## PANAS
AllPANAS = Class_PANAS.PANAS()
AllPANAS.ProcessDataFile(PANASData)
# Create a file name for PANAS data
# UpdatedDataFileName = CreateOutFileName('NCM002_PANAS', AllOutDataFolder)
# ExistingDataFileName = LocateOutDataFile(AllOutDataFolder,'NCM002_PANAS')
# # Write PANAS to file
# WriteOutNewdataMoveOldData(AllPANAS.AllPANAS, UpdatedDataFileName, ExistingDataFileName)
# AllPANAS.AllParts.to_csv(UpdatedDataFileName)
## DEMOGRAPHICS
AllDemog = Class_Demog.Demograhics()
AllDemog.ProcessDataFile(DemoData)
# Create a file name for Demog data
# UpdatedDataFileName = CreateOutFileName('NCM002_Demog', AllOutDataFolder)
# ExistingDataFileName = LocateOutDataFile(AllOutDataFolder,'NCM002_Demog')
# # Write Demographics to file
# WriteOutNewdataMoveOldData(AllDemog.AllParts, UpdatedDataFileName, ExistingDataFileName)
# AllDemog.AllParts.to_csv(UpdatedDataFileName)
## LIFESTYLE
AllLife = Class_Lifestyle.Lifestyle()
AllLife.ProcessData(LifeData)
# Create a file name for Demog data
# UpdatedDataFileName = CreateOutFileName('NCM002_Life', AllOutDataFolder)
# ExistingDataFileName = LocateOutDataFile(AllOutDataFolder,'NCM002_Life')
# # Write Demographics to file
# WriteOutNewdataMoveOldData(AllLife.AllLife, UpdatedDataFileName, ExistingDataFileName)
# AllLife.AllLife.to_csv(UpdatedDataFileName)
# Merge all together
# MM01 = pd.merge(left = AllDemog.AllParts, right = AllLife.AllLife, how = 'outer', left_on='PartID',right_on='PartID')
# MM02 = pd.merge(left = MM01, right = AllPANAS.AllPANAS, how = 'outer', left_on='PartID', right_on = 'PartID')
return AllLife, AllDemog, AllPANAS
def CreateOutFileName(BaseFileName, AllOutDataFolder):
# Create a file to hold processed data using the time and date
# to indicate when it was made
now = datetime.datetime.now()
NowString = now.strftime("_updated_%b-%d-%Y_%H-%M.csv")
NewOutFileName = os.path.join(AllOutDataFolder, BaseFileName + NowString)
return NewOutFileName
def LocateOutDataFile(AllOutDataFolder, BaseFileName):
# Locate an existing processed data file and if it does not exist, then make it.
# What files exist with this name?
Files = glob.glob(os.path.join(AllOutDataFolder, BaseFileName + '*.csv'))
now = datetime.datetime.now()
NowString = now.strftime("_updated_%b-%d-%Y_%H-%M.csv")
NewOutFileName = BaseFileName + NowString
if len(Files) == 0:
FileName = os.path.join(AllOutDataFolder, NewOutFileName)
else:
# this will open an existing file
FileName = Files[-1]
return FileName
def WriteOutNewdataMoveOldData(UpdatedData, UpdatedDataFileName, ExistingDataFileName):
# Move the old file
OldDataFolder = os.path.join(AllOutDataFolder, 'OldResultFiles')
# if the folder for old data does not exist, then make it
if not os.path.exists(OldDataFolder):
os.mkdir(OldDataFolder)
# change the name of the results file so it is not confused with current data
MovedDataFile = os.path.join(OldDataFolder, 'X_'+os.path.basename(ExistingDataFileName))
shutil.move(ExistingDataFileName, MovedDataFile)
# Now that the old data is moved, write out the updated data
UpdatedData.to_csv(UpdatedDataFileName, index = False, float_format='%.3f')
def AreAllThreeFilesPresent(File1, File2, File3):
# Check to see if all three datafiles are where they belong
root = tkinter.Tk()
root.withdraw()
AllFilesFlag = True
if not os.path.exists(File1):
AllFilesFlag = False
messagebox.showerror('Error', 'Missing Lifestyle questionnaire data file')
if not os.path.exists(File2):
AllFilesFlag = False
messagebox.showerror('Error', 'Missing demographics data file')
if not os.path.exists(File3):
AllFilesFlag = False
messagebox.showerror('Error', 'Missing PANAS data file')
return AllFilesFlag
def ReadSMFileAsCSV(InputFile):
# Open the file
fid = open(InputFile,'r', encoding="ISO-8859-1")
data = csv.reader(fid)
# Read whole file into a list
LL = list(data)
# Close the file
fid.close()
# How many participants are in this data set
NPart = len(LL) - 2
# The surveyMonkey data has two header lines.
# This makes it hard to use dataframes
# Extract header line 1
HeaderLine1 = LL[0]
# Extract header line 1
HeaderLine2 = LL[1]
# Extract the rest of the rows as the actual participant data
PartData = LL[2:]
return HeaderLine1, HeaderLine2, PartData
if __name__ == "__main__":
main()