Add exposure cache and keep absolute time only
This commit is contained in:
@@ -35,6 +35,17 @@ import pandas as pd # Pandas for data manipulation
|
||||
import tqdm # Progress bar for chunk processing
|
||||
|
||||
|
||||
def _unique_preserve_order(values):
|
||||
"""Return unique values while preserving first-seen order."""
|
||||
seen = set()
|
||||
out = []
|
||||
for value in values:
|
||||
if value not in seen:
|
||||
seen.add(value)
|
||||
out.append(value)
|
||||
return out
|
||||
|
||||
|
||||
# CSV mapping field IDs to human-readable names
|
||||
field_map_file = "field_ids_enriched.csv"
|
||||
|
||||
@@ -72,7 +83,7 @@ basic_info_fields = [
|
||||
|
||||
# Fields needed for tabular extraction from raw CSV.
|
||||
tabular_fields = _unique_preserve_order(
|
||||
basic_info_fields + assessment_fields + exposure_fields
|
||||
basic_info_fields + assessment_fields + exposure_fields + ["date_of_birth"]
|
||||
)
|
||||
|
||||
# TSV mapping field IDs to ICD10-related date columns
|
||||
@@ -144,6 +155,7 @@ for ukb_chunk in tqdm.tqdm(ukb_iterator, desc="Processing UK Biobank data"):
|
||||
),
|
||||
errors="coerce",
|
||||
)
|
||||
ukb_chunk["date_of_birth"] = dob.dt.strftime("%Y-%m-%d")
|
||||
|
||||
# Use only date variables that actually exist in the current chunk
|
||||
present_date_vars = [c for c in date_vars if c in ukb_chunk.columns]
|
||||
@@ -253,8 +265,11 @@ data = data[np.isin(data[:, 0], valid_eids)]
|
||||
final_tabular = final_tabular.loc[valid_eids]
|
||||
final_tabular = final_tabular.convert_dtypes()
|
||||
|
||||
# Save basic sex information separately.
|
||||
basic_info = final_tabular[["sex"]].copy()
|
||||
# Save basic information needed by the model and exposure-date alignment.
|
||||
basic_cols = ["sex"]
|
||||
if "date_of_birth" in final_tabular.columns:
|
||||
basic_cols.append("date_of_birth")
|
||||
basic_info = final_tabular[basic_cols].copy()
|
||||
basic_info.to_csv("ukb_basic_info.csv")
|
||||
|
||||
# Save event data
|
||||
|
||||
Reference in New Issue
Block a user