Download scripts/build_dataset.py from HiTZ/saei-clinddx: direct link, hf CLI and curl.
- Browser
- Download file 3.91 kB
-
https://huggingface.co/datasets/HiTZ/saei-clinddx/resolve/main/scripts/build_dataset.py
- Command line
-
hf download hf://datasets/HiTZ/saei-clinddx/scripts/build_dataset.py
-
curl -L -o build_dataset.py https://huggingface.co/datasets/HiTZ/saei-clinddx/resolve/main/scripts/build_dataset.py
3.91 kB
| #!/usr/bin/env python3 | |
| """Build aligned ES/EN clinical case dataset from two 11-column TSV/CSV files. | |
| Usage: python scripts/build_dataset.py --es path/es.tsv --en path/en.tsv --out . | |
| No dependency beyond Python standard library. Review copyright, consent and PHI before release. | |
| """ | |
| import argparse,csv,json,pathlib,re,hashlib,collections,statistics | |
| FIELDS=['year','title','authors','chapter','summary','clinical case','differential diagnosis','evolution','final diagnosis','discussion','bibliography'] | |
| FIELDS_TEXT=['title','chapter','summary','clinical case','differential diagnosis','evolution','final diagnosis','discussion','bibliography'] | |
| FIELD_MAP={'clinical case':'clinical_case','differential diagnosis':'differential_diagnosis','final diagnosis':'final_diagnosis'} | |
| def read_table(path): | |
| with open(path,encoding='utf-8-sig',newline='') as f: | |
| sample=f.read(4096);f.seek(0) | |
| dialect=csv.excel_tab if path.lower().endswith('.tsv') else csv.Sniffer().sniff(sample,delimiters=',;\t') | |
| rows=list(csv.DictReader(f,dialect=dialect)) | |
| if not rows or not set(FIELDS).issubset(rows[0]):raise ValueError('Input must have all 11 columns: '+path) | |
| return [{k:(r.get(k) or '').strip() for k in FIELDS} for r in rows] | |
| def norm(s):return ' '.join(s.lower().split()) | |
| def wordcount(s):return len(re.findall(r"\b[\wÀ-ÿ]+(?:[-'][\wÀ-ÿ]+)*\b",s)) | |
| def record(r,id_,lang): | |
| return {'id':id_,'language':lang,'year':int(r['year']) if r['year'].isdigit() else None,'title':r['title'],'authors':r['authors'],'chapter':r['chapter'],'summary':r['summary'],'clinical_case':r['clinical case'],'differential_diagnosis':r['differential diagnosis'],'evolution':r['evolution'],'final_diagnosis':r['final diagnosis'],'discussion':r['discussion'],'bibliography':r['bibliography']} | |
| def write_jsonl(path,records): | |
| path.parent.mkdir(parents=True,exist_ok=True) | |
| with open(path,'w',encoding='utf-8') as f: | |
| for obj in records:f.write(json.dumps(obj,ensure_ascii=False)+'\n') | |
| def main(): | |
| ap=argparse.ArgumentParser();ap.add_argument('--es',required=True);ap.add_argument('--en',required=True);ap.add_argument('--out',default='.');args=ap.parse_args() | |
| root=pathlib.Path(args.out);a=read_table(args.es);b=read_table(args.en) | |
| if len(a)!=len(b):raise ValueError('Different row counts; manually align cases before building') | |
| mismatches=[i+1 for i,(x,y) in enumerate(zip(a,b)) if x['year']!=y['year'] or norm(x['authors'])!=norm(y['authors'])] | |
| if mismatches:raise ValueError('Year/author alignment mismatch rows: '+str(mismatches)) | |
| ids=[f'saei-{x["year"]}-{i:04d}' for i,x in enumerate(a,1)] | |
| es=[record(r,ids[i],'es') for i,r in enumerate(a)];en=[record(r,ids[i],'en') for i,r in enumerate(b)] | |
| for name,rows in [('es',es),('en',en)]: | |
| write_jsonl(root/'data'/name/'test.jsonl',rows) | |
| manifest={'dataset_name':'SAEI-CLINDDx','pronunciation':'say-ee clin dix','repository_slug':'SAEI-CLINDDx','release_status':'draft; testing and exploratory evaluation only','intended_use':'testing and exploratory evaluation only; not for model training','configs':['es','en'],'split':'test','number_of_aligned_cases':len(ids),'case_ids_are_row_based':True,'source_files':{'es':pathlib.Path(args.es).name,'en':pathlib.Path(args.en).name},'sha256':{lang:hashlib.sha256(pathlib.Path(p).read_bytes()).hexdigest() for lang,p in [('es',args.es),('en',args.en)]},'cautions':['Source row order and aligned year/authors must be preserved when extending.','The test split denotes intended experimental evaluation use only; it is not a validated independent benchmark. Do not train on these cases if evaluating on them.','Do not publish until copyright, privacy, and medical review are complete.']} | |
| (root/'manifest.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2),encoding='utf-8') | |
| print(json.dumps({'aligned_cases':len(ids),'missing_ddx_cases':sum(not r['differential_diagnosis'] for r in es)},indent=2)) | |
| if __name__=='__main__':main() | |