Update utils/preprocess_umls_data.py
Browse files
utils/preprocess_umls_data.py
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Preprocess raw UMLS data into a simplified CSV for FAISS indexing.
|
| 3 |
+
Accepts a TSV/CSV with columns CUI, STR (string), DEF, SAB (source).
|
| 4 |
+
Outputs a clean CSV with headers: cui,name,definition,source.
|
| 5 |
+
|
| 6 |
+
Usage:
|
| 7 |
+
python preprocess_umls_data.py \
|
| 8 |
+
--input raw_umls.tsv \
|
| 9 |
+
--output processed/concepts.csv \
|
| 10 |
+
--sep '\t'
|
| 11 |
+
"""
|
| 12 |
+
import argparse
|
| 13 |
+
import pandas as pd
|
| 14 |
+
|
| 15 |
+
def main():
|
| 16 |
+
parser = argparse.ArgumentParser(description="Preprocess UMLS raw data.")
|
| 17 |
+
parser.add_argument('--input', required=True,
|
| 18 |
+
help='Raw UMLS file (TSV/CSV)')
|
| 19 |
+
parser.add_argument('--output', required=True,
|
| 20 |
+
help='Output CSV for concepts')
|
| 21 |
+
parser.add_argument('--sep', default='\t',
|
| 22 |
+
help='Separator for input file')
|
| 23 |
+
args = parser.parse_args()
|
| 24 |
+
|
| 25 |
+
# Read raw file
|
| 26 |
+
df = pd.read_csv(args.input, sep=args.sep, dtype=str)
|
| 27 |
+
|
| 28 |
+
# Expect columns: CUI, STR, DEF, SAB
|
| 29 |
+
df = df.rename(columns={
|
| 30 |
+
'CUI': 'cui',
|
| 31 |
+
'STR': 'name',
|
| 32 |
+
'DEF': 'definition',
|
| 33 |
+
'SAB': 'source'
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
# Drop rows missing CUI or name
|
| 37 |
+
df = df.dropna(subset=['cui', 'name'])
|
| 38 |
+
|
| 39 |
+
# Fill missing definitions/sources
|
| 40 |
+
df['definition'] = df['definition'].fillna('')
|
| 41 |
+
df['source'] = df['source'].fillna('')
|
| 42 |
+
|
| 43 |
+
# Deduplicate by CUI + name
|
| 44 |
+
df = df.drop_duplicates(subset=['cui', 'name'])
|
| 45 |
+
|
| 46 |
+
# Output
|
| 47 |
+
df.to_csv(args.output, index=False)
|
| 48 |
+
print(f"Processed {len(df)} concepts to {args.output}")
|
| 49 |
+
|
| 50 |
+
if __name__ == '__main__':
|
| 51 |
+
main()
|