Download scripts/package.py from KeremTT/hemato: direct link, hf CLI and curl.
- Browser
- Download file 4.35 kB
-
https://huggingface.co/datasets/KeremTT/hemato/resolve/main/scripts/package.py
- Command line
-
hf download hf://datasets/KeremTT/hemato/scripts/package.py
-
curl -L -o package.py https://huggingface.co/datasets/KeremTT/hemato/resolve/main/scripts/package.py
4.35 kB
| """Write final manifest (meta/), split each config into standalone <=300 MB ZIPs (classes never span parts).""" | |
| import csv, json, os, zipfile | |
| from collections import Counter, defaultdict | |
| LIMIT = 300_000_000 | |
| os.makedirs('meta', exist_ok=True); os.makedirs('out', exist_ok=True) | |
| TR = {'Blast': 'Blast', 'Promyelocyte': 'Promiyelosit', 'Myelocyte': 'Miyelosit', 'Metamyelocyte': 'Metamiyelosit', | |
| 'Band_neutrophil': 'Çomak nötrofil', 'Segmented_neutrophil': 'Segmentli nötrofil', 'Eosinophil': 'Eozinofil', 'Basophil': 'Bazofil', | |
| 'Lymphocyte': 'Lenfosit', 'Monocyte': 'Monosit', 'Proerythroblast': 'Proeritroblast', 'Erythroblast': 'Eritroblast', | |
| 'Plasma_cell': 'Plazma hücresi', 'Hairy_cell': 'Tüylü hücre (hairy cell)', 'Platelet': 'Trombosit', 'Artefact': 'Artefakt', | |
| 'Not_identifiable': 'Tanımlanamayan hücre', 'ALL': 'Akut lenfoblastik lösemi hücresi', 'Normal': 'Normal hücre', | |
| 'Parasitized': 'Plasmodium ile enfekte eritrosit', 'Uninfected': 'Enfekte olmayan eritrosit'} | |
| SRC = {'bone_marrow_mll': ('Matek et al. 2021, Bone Marrow Cytomorphology (MLL / Helmholtz / Fraunhofer)', 'CC BY 4.0', 'https://doi.org/10.7937/TCIA.AXH3-T579'), | |
| 'pbc_acevedo': ('Acevedo et al. 2020, PBC dataset (Hospital Clinic of Barcelona)', 'CC BY 4.0', 'https://doi.org/10.17632/snkd93bnjr.1'), | |
| 'c_nmc_2019': ('Gupta & Gupta 2019, C-NMC 2019 (ISBI challenge)', 'CC BY 3.0', 'https://doi.org/10.7937/tcia.2019.dc64i46r'), | |
| 'nih_malaria': ('Rajaraman et al. 2018, NIH/NLM Malaria cell images', 'Public (U.S. NLM)', 'https://lhncbc.nlm.nih.gov/LHC-research/LHC-projects/image-processing/malaria-datasheet.html'), | |
| 'bccd': ('BCCD (Shenggan 2017), Roboflow 100 version', 'MIT (original) / CC BY 4.0 (RF100)', 'https://github.com/Shenggan/BCCD_Dataset')} | |
| R = [r for r in csv.DictReader(open('manifest.csv')) if os.path.exists(os.path.join('dataset', r['file']))] | |
| for r in R: | |
| r['label_tr'] = TR.get(r['label'], '') | |
| r['source_citation'], r['source_license'], r['source_url'] = SRC[r['source']] | |
| fields = ['config', 'split', 'label', 'label_tr', 'file', 'source', 'original_label', 'original_file', 'patient_id', 'source_citation', 'source_license', 'source_url'] | |
| with open('meta/manifest.csv', 'w', newline='', encoding='utf-8-sig') as f: | |
| w = csv.DictWriter(f, fieldnames=fields, extrasaction='ignore'); w.writeheader(); w.writerows(R) | |
| stats = {} | |
| for cfg in ('cls', 'all', 'malaria', 'det'): | |
| rs = [r for r in R if r['config'] == cfg] | |
| stats[cfg] = dict(images=len(rs), splits=dict(Counter(r['split'] for r in rs)), | |
| classes={k: dict(Counter(r['split'] for r in rs if r['label'] == k)) for k in sorted({r['label'] for r in rs if r['label']})}, | |
| sources=dict(Counter(r['source'] for r in rs)), patients=len({r['patient_id'] for r in rs if r['patient_id']})) | |
| json.dump(stats, open('meta/stats.json', 'w'), indent=1, ensure_ascii=False) | |
| NAMES = {'cls': 'HEMATO-CLS', 'all': 'HEMATO-ALL', 'malaria': 'HEMATO-MALARIA', 'det': 'HEMATO-DET'} | |
| for cfg in ('cls', 'all', 'malaria'): | |
| by = defaultdict(list) | |
| for r in R: | |
| if r['config'] == cfg: by[r['label']].append(r['file']) | |
| # every part must contain several classes (a single-class ZIP is imported as unlabeled "flat" data), | |
| # so files are dealt round-robin across parts: each part gets a slice of every class. | |
| allf = [f for lab in sorted(by) for f in sorted(by[lab])] | |
| total = sum(os.path.getsize(os.path.join('dataset', f)) for f in allf) | |
| n = max(1, -(-total // LIMIT)) | |
| parts = [allf[i::n] for i in range(n)] | |
| for i, files in enumerate(parts, 1): | |
| zn = f'out/{NAMES[cfg]}_part{i:02d}-of-{len(parts):02d}.zip' | |
| with zipfile.ZipFile(zn, 'w', zipfile.ZIP_STORED) as z: | |
| for f in files: z.write(os.path.join('dataset', f), f.split('/', 1)[1]) # drop the config prefix | |
| print(zn, round(os.path.getsize(zn) / 1e6), 'MB', len(files)) | |
| with zipfile.ZipFile('out/HEMATO-DET.zip', 'w', zipfile.ZIP_DEFLATED) as z: | |
| for root, _, files in os.walk('dataset/det'): | |
| for fn in files: | |
| p = os.path.join(root, fn); z.write(p, os.path.relpath(p, 'dataset/det')) | |
| print('out/HEMATO-DET.zip', round(os.path.getsize('out/HEMATO-DET.zip') / 1e6), 'MB') | |
| print(json.dumps({k: (v['images'], v['splits']) for k, v in stats.items()})) | |