-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpseudo.py
More file actions
82 lines (57 loc) · 2.59 KB
/
pseudo.py
File metadata and controls
82 lines (57 loc) · 2.59 KB
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
import os
import pandas as pd
pseudo_dict = {}
def detect_separator(filepath):
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
first_line = f.readline()
for delimiter in [',', ';']:
if delimiter in first_line:
return delimiter
def get_pseudo_dict(pseudo_path):
pseudo_file_path = None
for file_name in os.listdir(pseudo_path):
if "gPAS" in file_name and file_name.endswith(".csv"):
pseudo_file_path = os.path.join(pseudo_path, file_name)
if pseudo_file_path:
sep = detect_separator(pseudo_file_path)
try:
df = pd.read_csv(pseudo_file_path, sep=sep)
pseudonym_map = dict(zip(df.iloc[:, 0].astype(str), df.iloc[:, 1].astype(str)))
return pseudonym_map
except Exception as e:
return print(f"Error reading file {pseudo_file_path}:", e)
def pseudo_ids_process(pseudo_map_path, original_output_path):
study_file_paths = []
parent_directory = os.path.dirname(original_output_path)
pseudo_output_path = os.path.join(parent_directory, 'pseudo')
if not os.path.exists(pseudo_output_path):
os.makedirs(pseudo_output_path)
pseudo_map = get_pseudo_dict(pseudo_map_path)
print("Mapping obtained:", pseudo_map)
for file_name in os.listdir(original_output_path):
if file_name.endswith(".csv") and not file_name.endswith("_no_headers.csv"):
study_file_paths.append(os.path.join(original_output_path, file_name))
unmapped_report = {} # {filename: [ids]}
for study_file in study_file_paths:
try:
sep = detect_separator(study_file)
df = pd.read_csv(study_file, sep=sep)
mapped_ids = df['participant_identifier'].map(pseudo_map)
missing_mask = mapped_ids.isna()
missing_ids = (
df.loc[missing_mask, 'participant_identifier']
.dropna()
.unique()
.tolist()
)
if missing_ids:
unmapped_report[os.path.basename(study_file)] = missing_ids
df_cleaned = df.loc[~missing_mask].copy()
df_cleaned['participant_identifier'] = mapped_ids[~missing_mask]
output_file_path = os.path.join(pseudo_output_path, os.path.basename(study_file))
df_cleaned.to_csv(output_file_path, sep=sep, index=False)
return pseudo_output_path
except Exception as e:
print(f"Error reading file {study_file}: {e}")
for filename, ids in unmapped_report.items():
print(f"[UNMAPPED IDS] {filename}: {ids}")