File size: 18,491 Bytes
8a79f2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import sys
import os
import pandas as pd
import random
import re
import string
from functools import reduce
import numpy as np
import math

from datetime import datetime, timedelta

from lib.data_helpers import data_utils
from lib.experiment_specs import study_config
from lib.utilities import codebook
from lib.utilities import serialize
from lib.data_helpers.confidential import Confidential
from lib.data_helpers.manual_changes import ManualChanges
from lib.data_helpers.builder_utils import BuilderUtils

random.seed(23555)

"""
Purpose: Cleans Qualtrics surveys based on their configs in study_config
Input:
 - Survey Name only input survey names found in data/{study}/configurations/study_config
 - input_dir: directory of raw file
 - output_dir: directory of clean_master file
 - remove_embedded: if True, removes the embedded data from a raw qualtrics survey
 - test: bool -  if True, keeps all banned email responses
 - codebook: bool - if True, the qualtrics cleaner will update the master codebook. for check_survey.py, this is set to False, and for the main pipeline, this is set to true
 - helper_paths : the dictionary specifies the absolute path locations of 'Manual Changes' an excel spreadsheet where specify
 changes to the survey answers, and 'Testers' where we specify information for the PhoneAddictionTeam
 {'ManualChanges':manual_changes_path, 'Testers':testers_path}

MainFunction: clean_survey

Output: Clean survey file

Note: DON'T MAKE ANY NEW VARS -- THEY WILL BE DELETED BECAUSE WE CROP ALL DATA BEFORE THE FIRST VAR AND ALL DATA AFTER THE LAST VAR

Note:  all variables are converted to strings, and NaN will be coded as "nan"
"""


class CleanSurvey():

    default_helper_paths = {"Intermediate": os.path.join("data", "external", "intermediate"),
                            "Confidential": os.path.join("data", "external", "dropbox_confidential")}


    def __init__(self, survey_name: str, input_dir: str, output_dir: str,
                 remove_embedded: bool = True, test: bool = False, codebook_bool: bool = True,
                 helper_paths: dict = default_helper_paths):

        self.survey_name = survey_name
        self.open_date_time = study_config.surveys[survey_name]["Start"]
        self.close_date_time = study_config.surveys[survey_name]["End"]
        self.code = study_config.surveys[survey_name]["Code"]
        self.raw_file_path = os.path.join(input_dir, survey_name + ".csv")
        self.clean_file_path = os.path.join(output_dir, survey_name + ".csv")

        self.intermediate_dir = helper_paths["Intermediate"]
        self.confidential_dir = helper_paths["Confidential"]
        self.testers = pd.read_excel(os.path.join(self.confidential_dir,"Testers.xlsm"), sheet_name="Testers")

        self.timezones_path = os.path.join(self.intermediate_dir,"Timezones")
        self.manual_changes_path = os.path.join(self.confidential_dir,"ManualChanges.xlsx")
        self.pii_path = Confidential.id_file


        self.remove_embedded = remove_embedded
        self.test = test
        self.codebook_bool = codebook_bool

    def clean_survey(self):
        raw_data_file = os.path.join(self.raw_file_path)
        print(f"\n Processing {self.survey_name}")
        df = pd.read_csv(raw_data_file, dtype=str)

        if len(df)==0:
            print(f"No Data in{self.survey_name}")
            return pd.DataFrame()

        print(f"obs before processing {len(df)-2}")
        df = self._process_col_names(df)
        df = self._process_col_values(df)

        """ we know the qualtricts datetimes are in eastern, so we
         adjust the qualtrics times to local using the timezones found in the PD data"""
        df = self._process_datetimes(df)
        df = self._filter_timeframe(df)
        df = self._filter_emails(df)
        df = self._validate_completes(df)

        if self.survey_name in study_config.text_surveys:
            df = self._reshape_text_survey(df)
            df = ManualChanges.manual_clean(df, self.survey_name, self.manual_changes_path)
            df.to_csv(self.clean_file_path, index=False)
            return df

        df = self._filter_duplicates(df)
        df = ManualChanges.manual_clean(df, self.survey_name, self.manual_changes_path)
        df = df.reset_index(drop=True)


        Confidential.build_id_map(df,self.survey_name, self.pii_path)
        Confidential.anonymize_cols(df)

        if self.code is not None:
            df = data_utils.add_survey_code(df, self.code)

        #if self.test:
        #    self.clean_file_path = self.clean_file_path.replace(".csv", "TEST.csv")

        df.to_csv(self.clean_file_path, index=False)

        """in prep for merge, remove embedded data. keep in csv exported above just for comparison"""
        if self.remove_embedded:
            df = self._remove_embedded_data(df)

        print("Created {0} with {1} entries".format(self.survey_name, len(df.index)))
        return df

    def _process_col_names(self, df: pd.DataFrame):
        df.columns = df.columns.str.replace(' ', '')
        df.columns = df.columns.str.replace('_', '')

        df = codebook.rename_vars(df)

        if self.codebook_bool:
            self._update_codebook(df)

        df = df.iloc[2:, :]
        df_dup = df.loc[:, df.columns.duplicated()]
        if len(list(df_dup.columns)) > 0:
            print(f" \t Duplicate Columns to delete: {df_dup.columns}")
        df = df.loc[:, ~df.columns.duplicated()]
        return df

    def _process_col_values(self, df):
        df = df.astype(str).applymap(lambda x: x.strip())

        for p_col in ["PhoneNumber","PhoneNumberConfirm","FriendContact"]:
            if p_col in df.columns.values:
                df[p_col] = df[p_col].apply(lambda x: re.sub("[^0-9]", "", str(x)))
                df[p_col] = df[p_col].apply(lambda x: "1"+x if len(x)==10 else x)
                df[p_col] = df[p_col].apply(lambda x: x if len(x) == 11 else 'nan')

        #silly bug
        if self.survey_name == "Baseline":
            print("\t dealing with dumb baseline bug to ensure appcode assertion passes")
            print(f"\t Len before dropping sherry {len(df)}")
            df = df.loc[df["MainEmail"]!="xy1087@nyu.edu"]
            print(f"\t len after dropping sherry {len(df)}")

        # ADD APPCODES to survey without appcode
        if "AppCode" not in df.columns.values:
            print("\t No AppCode in Survey")
            # make the main email col in pii the raw email col in the survey w/o appcode
            pii = serialize.open_pickle(self.pii_path).reset_index().rename(columns = {"index":"AppCode"})
            email_col = study_config.surveys[self.survey_name]["RawEmailCol"]
            pii = pii.loc[pii["MainEmail"] != "nan",["AppCode", "MainEmail"]].rename(columns = {"MainEmail":email_col})

            df = df.merge(pii, on=email_col, how='left')
            print("done merger")

        for appcode_col in ["AppCode", "AppCodeConfirm"]:
            if appcode_col in df.columns.values:
                df = data_utils.add_A_to_appcode(df,appcode_col)
                "If no appcode, assign 'UNASSIGNED_' + ResponseID as Appcode"
                df.loc[df[appcode_col]=="nan", appcode_col] = 'UNASSIGNED_'+df["ResponseID"]

        if "AppCode" not in df.columns:
            print("AppCode not in df.columns. Anonymize with Response ID")
            df["AppCode"] = 'UNASSIGNED_'+df["ResponseID"]

         #Temptation Stratification for SurveyChecks
        if self.survey_name =="Recruitment":
            df["Age"] = df["Age"].astype(float)
            df.loc[(df["Age"]>=18)&(df["Age"]<=34),"AgeStrat"] = "18-34"
            df.loc[(df["Age"] >= 35) & (df["Age"] <= 50), "AgeStrat"] = "35-50"
            df.loc[(df["Age"] > 50), "AgeStrat"] = "50+"
            df["Age"] = df["Age"].fillna("nan").astype(str)
            

        return df

    def _process_datetimes(self,df):
        df['SurveyStartEasternDatetime'] = pd.to_datetime(df['SurveyStartDatetime'], infer_datetime_format=True)
        df['SurveyEndEasternDatetime'] = pd.to_datetime(df['SurveyEndDatetime'], infer_datetime_format=True)
        df.loc[:, 'OpenEasternDateTime'] = self.open_date_time
        df.loc[:, 'CloseEasternDateTime'] = self.close_date_time

        # Create Local Datetime
        try:
            # Get the modal timezone for each user to adjust the easter survey times to local time of user
            timezones = serialize.open_pickle(self.timezones_path)
            df = df.merge(timezones, on = "AppCode", how = 'left')
            for time_var in ['SurveyStartEasternDatetime','SurveyEndEasternDatetime','OpenEasternDateTime','CloseEasternDateTime']:
                df[time_var.replace("Eastern","")]= df[time_var]+df["EastToLocal"]

                #if we can't find timezone, let local timezone equal eastern timezone
                df.loc[df[time_var.replace("Eastern","")].isnull(),time_var.replace("Eastern","")] = df[time_var]
                df.loc[df["EastToLocal"].isnull(),"EastToLocal"] = timedelta(0)
        except:
            print("Could not merge status exporter likely because Appcode not in survey or because timezones file not in path")
            df['SurveyStartDatetime'] = pd.to_datetime(df['SurveyStartDatetime'], infer_datetime_format=True)
            df['SurveyEndDatetime'] = pd.to_datetime(df['SurveyEndDatetime'], infer_datetime_format=True)
            return df

        return df

    """for each possible email column, drop nan's, drop banned emails, keep last of duplicate"""

    def _filter_emails(self, df):
        print(f"obs before filtering tester emails {len(df)}")
        other_email_cols = ['SchoolEmail', 'PreferEmail', 'RecipientEmail', "Email", "EmailConfirm", "Email.1"]

        if not self.test:
            for email_col in other_email_cols+["ParentEmail"]:
                if email_col in list(df.columns.values):
                    df = df.loc[~df[email_col].isin(list(self.testers["Email"]))]
        else:
            print("Because self.test == True, all banned emails will be included!!")

        # ensure the main email col is "MainEmail"
        raw_email_col = study_config.surveys[self.survey_name]["RawEmailCol"]
        if raw_email_col != "MainEmail":

            # drop the current MainEmail Version, and create a new one using the raw email data
            if "MainEmail" in df.columns:
                df = df.drop(columns=["MainEmail"])
            df = df.rename(columns={raw_email_col: "MainEmail"})

        # drop other email cols
        for col in other_email_cols:
            if col in df.columns:
                df = df.drop(columns=[col])
        return df

    # creates new variable "Complete" which indicates if the participant completed the survey, or the last column they filled in
    def _validate_completes(self, df):
        last_question = study_config.surveys[self.survey_name]["LastQuestion"]

        # Assert the last question will not be filled by an anonymous code, regardless if the cell were empty
        id_cols = list(study_config.id_cols.values())
        assert last_question not in sum(id_cols, [])

        question_index = list(df.columns).index(last_question)
        survey_cols = list(df.columns)[:question_index+1:]
        reverse_survey_cols = survey_cols[::-1]

        complete_q = study_config.surveys[self.survey_name]["CompleteQuestion"]
        df.loc[(df["Finished"] == 'True') & (df[complete_q] != 'nan'), "Complete"] = "Complete"
        df.loc[df["Complete"] != 'Complete', "Complete"] = "UnfinishedOther"

        # Find the last completed survey variable
        df_dict = df.to_dict(orient = 'index')
        for key, value in df_dict.copy().items():
            if value["Complete"] == "Complete":
                continue
            else:
                for col in reverse_survey_cols:

                    #exclude empty cols
                    if (value[col] != "nan"):
                        try:
                            #exclude cols that are automatically filled in
                            if "UNASSIGNED" not in str(value[col]):
                                df_dict[key]["Complete"] = col
                                break
                        except:
                            print("bug")
        df = pd.DataFrame.from_dict(df_dict, orient = "index")
        return df

    def _filter_duplicates(self, df):
        # filter out duplicates-- prioritize keeping complete obs, then the later obs
        ranks = range(0,len(df.columns))
        rank_of_cols = dict(zip(list(df.columns),ranks))
        rank_of_cols["Complete"] = 9999999

        df["CompleteRank"] = df["Complete"].apply(lambda x: rank_of_cols[x])

        scratch_path = os.path.join(self.intermediate_dir, "Scratch")

        for dup_col in ["MainEmail","AppCode"]:
            if dup_col not in df.columns:
                print(f"{self.survey_name} doesn't have an {dup_col} column")
            else:
                print(f"obs before dropping dups of {dup_col}: {len(df)}")

                #sort obs by, appcode, how far they completed the survey, then time survey was started
                df = df.sort_values(by = [dup_col,"CompleteRank","SurveyStartDatetime"])

                #if dup_col == "MainEmail":
                #    dup = df[(df.duplicated(subset=["MainEmail"], keep=False)) & (df['MainEmail'] != "nan")]
                #    dup.to_csv(os.path.join(scratch_path, f"RecruitDups.csv"))

                # mark all entries as duplicates, except for the last one
                df = df.loc[(~df.duplicated(subset=[dup_col], keep='last')) | (df[dup_col] == "nan")]

                #if dup_col == "MainEmail":
                #    df.to_csv(os.path.join(scratch_path, f"Keeps.csv"))

        print(f"obs after dropping dups of {dup_col}: {len(df)}")
        df = df.sort_values("SurveyStartDatetime")
        return df

    def _filter_timeframe(self, df):
        print(f"obs before droppingpeople that began before survey start or ended after close {len(df)}")
        if self.test == False:
            df = df.loc[df['SurveyStartEasternDatetime'] >= df['OpenEasternDateTime']]
            df = df.loc[df['SurveyEndEasternDatetime'] <= df['CloseEasternDateTime']]
        return df

    def _update_codebook(self, df):
        codebook_dic = {}
        for col in df.columns:
            codebook_dic[col] = {
                "VariableLabel": str(df.loc[0,col]),
                "DataType": df.dtypes[col],
                "PrefixEncoding": "Survey"
            }
        #Remove Timing Data from Codebook
        timing_vars = ["Timing-ClickCount",
                       "Timing-PageSubmit",
                       "Timing-FirstClick",
                       "Timing-LastClick",
                       "BrowserMetaInfo"]
        for varname, chars in codebook_dic.copy().items():
            if len([x for x in timing_vars if x in chars["VariableLabel"].replace(" ","")]) > 0:
                del codebook_dic[varname]

        codebook.add_vardic_to_codebook(codebook_dic)

    def _remove_embedded_data(self, df_f):

        #first remove all the qualtrics tracker vars
        unimportant_vars = [y for y in df_f if
                            any(x in y for x in ["FirstClick", "LastClick", "PageSubmit", "ClickCount"])]
        df_f = df_f.drop(columns=unimportant_vars)

        #then remove the embedded data
        non_survey_vars_to_keep = study_config.main_cols + [f"{self.code}_{x}" for x in study_config.kept_survey_data]
        for question_type in ["FirstQuestion", "LastQuestion"]:
            if question_type in study_config.surveys[self.survey_name]:
                last_question = study_config.surveys[self.survey_name]["Code"] + "_" + \
                                study_config.surveys[self.survey_name][question_type]
                question_index = list(df_f.columns).index(last_question)
                if question_type == "FirstQuestion":
                    keep_cols = list(set(non_survey_vars_to_keep + list(df_f.columns)[question_index:]))
                else:
                    keep_cols = list(set(non_survey_vars_to_keep + list(df_f.columns)[:question_index + 1]))
                df = df_f[[x for x in df_f.columns if x in keep_cols]]

        # re order columns (put main cols in front and maintain order of survey columns
        var_order = [x for x in non_survey_vars_to_keep if x in df_f.columns] + [x for x in df_f.columns if x not in non_survey_vars_to_keep]
        kept_var_order = [x for x in var_order if x in df.columns]
        df = df[kept_var_order]

        # remove hyphens from df, but if var is prefixed, drop IF the non prefix var is df
        """
        for var in df.columns:
            if f"{self.code}_{self.code}" in var:
                non_pref_var = var.replace(f'{self.code}_', '')
                if var.replace(f"{self.code}_", "") in df.columns:
                    # if df[var] == df[var.replace(f"{self.code}_","")]:
                    if df.equals(df[[var, non_pref_var]]):
                        df = df.drop(columns=[var])
                        print(f"Dropping {var} b/c {non_pref_var} and identical")
                    else:
                        test = df[[var, non_pref_var]]
                        print(f"Keeping {var} b/c {var} != {non_pref_var}")
                else:
                    print(f"Keeping {var} b/c unprefixed var still in df")
         """
        return df

    def _reshape_text_survey(self,df):
        df["SurveyStartDate"] = df["SurveyStartDatetime"].dt.date
        df = BuilderUtils.add_phase_label(raw_df = df,raw_df_date="SurveyStartDate", start_buffer=0, end_buffer=-1)

        # Replace Values of phase with the start survey code
        codes = [study_config.phases[x]["StartSurvey"]["Code"] for x in list(study_config.phases.keys())]
        rename_dic = dict(zip(list(study_config.phases.keys()), codes))
        df["Phase"] = df["Phase"].apply(lambda x: rename_dic[x] if x in rename_dic else x)

        keep_vars =[study_config.surveys[self.survey_name]["CompleteQuestion"],"SurveyStartDatetime","SurveyEndDatetime","Complete"]
        df_p = df.pivot_table(index=["AppCode"],
                                values=keep_vars,
                                columns=["Phase"],
                                aggfunc='first')


        df_p.columns = [f'_{self.code}'.join(col[::-1]).strip() for col in df_p.columns.values]
        df_p = df_p.reset_index()

        return df_p