anonymous-submission-acl2025's picture
add 17
8a79f2e
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