File size: 2,759 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
import os
import sys
import pandas as pd

from lib.experiment_specs import study_config
from lib.utilities import codebook
from lib.data_helpers.confidential import Confidential


class ManualChanges():

    @staticmethod
    def manual_clean(df,survey,manual_changes_path):
        survey_code = study_config.surveys[survey]["Code"]

        appcode_changes = pd.read_excel(manual_changes_path, sheet_name="AppCodes").to_dict(orient='index')
        for index, value in appcode_changes.items():
            if value["Old"][0] != "A":
                print("AppCodes must begin with 'A'")
                sys.exit()

            if value["Old"] not in list(df["AppCode"].unique()):
                print(f"Couldn't find old appcode {value['Old']}")
            else:
                df.loc[df["AppCode"] == value["Old"], "AppCode"] = value["New"]
                print(f"Replaced AppCode {value['Old']} with {value['New']}")

        manual_changes = pd.read_excel(manual_changes_path, sheet_name="General")
        man_c = manual_changes.to_dict(orient='index')
        survey_codes = [study_config.surveys[x]["Code"] + "_" for x in study_config.surveys]

        for key, value in man_c.items():
            variable = value["Variable"]

            # Remove the survey specific code, change in survey data if the survey codes match (i.e. B_ is with Baseline)
            if variable.startswith(tuple(survey_codes)):
                prefix_code = variable.split("_", 1)[0]
                variable = variable.split("_", 1)[1]

                #if the prefix matches the survey's code we are cleaning
                if prefix_code == survey_code:
                    df = ManualChanges.manual_replace(df,value,variable)

            # No Need to Modify Variable if the variable is a main column or a treatment column
            elif (variable in study_config.main_cols+study_config.embedded_main_cols) and (variable in df.columns):
                df = ManualChanges.manual_replace(df,value,variable)
            else:
                continue

        return df

    @staticmethod
    def manual_replace(df, value, variable):
        if variable not in df.columns:
            return df
        else:
            try:
                if value["AppCode"] not in list(df["AppCode"].unique()):
                    print(f"Manual change did not occur for {value['AppCode']}'s {variable} to {value['NewValue']} b/c appcode was not found")

                else:
                    df.loc[df["AppCode"] == value["AppCode"], variable] = value["NewValue"]
                    print(f"Changed {variable} for {value['AppCode']} to {value['NewValue']}")
            except:
                print(f"{variable} does not seem to be in dataframe")
            return df