Download 17/replication_package/code/lib/data_helpers/confidential.py from chuxuan/REPRO-Bench: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/datasets/chuxuan/REPRO-Bench/resolve/main/17/replication_package/code/lib/data_helpers/confidential.py
- Command line
-
hf download hf://datasets/chuxuan/REPRO-Bench/17/replication_package/code/lib/data_helpers/confidential.py
-
curl -L -o confidential.py https://huggingface.co/datasets/chuxuan/REPRO-Bench/resolve/main/17/replication_package/code/lib/data_helpers/confidential.py
3.13 kB
| import sys | |
| import os | |
| import pandas as pd | |
| from lib.experiment_specs import study_config | |
| from lib.utilities import serialize | |
| """ | |
| Class that contains functions to anonomize all PII info or de-anonymize PII columns | |
| - all PII columns are replace with the appcode value | |
| """ | |
| class Confidential: | |
| id_file = os.path.join("data","external", "dropbox_confidential","ContactLists","Generator","PII") | |
| """populate the PII dataframe with column values for the given survey""" | |
| def build_id_map(df, survey_name, id_file = id_file): | |
| for survey, id_cols in study_config.id_cols.items(): | |
| if survey in survey_name: | |
| id_dict = serialize.soft_df_open(id_file).to_dict(orient = 'index') | |
| id_cols = ["AppCode"] + [x for x in df.columns if x in study_config.id_cols[survey]] | |
| new_pii = df.loc[df["AppCode"].notnull(), id_cols] | |
| new_pii.index = new_pii["AppCode"] | |
| new_pii_dict = new_pii.drop(columns = "AppCode").to_dict("index") | |
| if len(id_dict) ==0: | |
| """if id dict is empty, replace with with the new data""" | |
| id_dict = new_pii_dict.copy() | |
| else: | |
| """update pii dict""" | |
| for appcode in new_pii_dict.keys(): | |
| """if appcode not in the id_dict, add it""" | |
| if appcode not in id_dict: | |
| id_dict[appcode] = new_pii_dict[appcode] | |
| else: | |
| """if appcode is in the id_dict, add or update the columns""" | |
| for col,val in new_pii_dict[appcode].items(): | |
| """if col is not in the id_dict, add it (UNCLEAR HOW THIS WILL WORK WITH THE DELAYED SURVEY""" | |
| id_dict[appcode][col] = val | |
| id_df = pd.DataFrame.from_dict(id_dict, orient= 'index') | |
| serialize.save_pickle(id_df,id_file,test_override=True) | |
| break | |
| """anonymize pii columns in df """ | |
| def anonymize_cols(df): | |
| all_pii_cols = sum(list(study_config.id_cols.values()),[]) | |
| for col in df.columns: | |
| if col in all_pii_cols: | |
| df[col] = df["AppCode"] | |
| return df | |
| """ Adds PII back to data frame by replacing the values of all anonymized columns with the pii""" | |
| def add_pii(df, id_file = id_file,only_main_cols = False): | |
| id_df = serialize.soft_df_open(id_file) | |
| all_pii_cols = list(id_df.columns) | |
| relevant_pii = [x for x in df.columns if x in all_pii_cols] | |
| # Drop Cols with the anonymized pii | |
| df = df.drop(columns = relevant_pii) | |
| # Merge in the relevant pii | |
| id_df = id_df.reset_index().rename(columns = {"index":"AppCode"}) | |
| if only_main_cols==True: | |
| id_main_cols = [x for x in study_config.main_cols if x in id_df.columns] | |
| id_df = id_df[id_main_cols] | |
| df_pii = id_df.merge(df, on = "AppCode", how = 'right') | |
| return df_pii | |