Kossisoroyce's picture
Upload folder using huggingface_hub
beaf88a verified
"""
Cancer Pathology & Histopathology - Sub-Saharan Africa
Author: Electric Sheep Africa
Based on African pathology registry data and WHO Classification of Tumours
"""
import numpy as np, pandas as pd, argparse, os
np.random.default_rng(42)
CANCER_TYPES = ['Breast', 'Cervix', 'Prostate', 'Colorectal', 'Liver', 'Lung', 'Ovary', 'Stomach', 'Thyroid', 'Bladder']
COUNTRIES = ['Kenya', 'Uganda', 'Nigeria', 'Ghana', 'Tanzania', 'South Africa', 'Ethiopia']
YEAR = {y: 0.1 for y in range(2018, 2026)}
# IHC markers availability
IHC_MARKERS = ['ER', 'PR', 'HER2', 'Ki67', 'p53', 'EGFR', 'CK7', 'CK20', 'Vimentin', 'CD45']
AVAILABILITY = {'National referral': 0.92, 'Regional': 0.72, 'District': 0.35}
# Morphology codes distribution
MORPHOLOGY_CODES = {
'8140/3': ('Adenocarcinoma', 0.35),
'8070/3': ('Squamous cell carcinoma', 0.25),
'8500/3': ('Ductal carcinoma', 0.15),
'8510/3': ('Medullary carcinoma', 0.05),
'8260/3': ('Papillary carcinoma', 0.08),
'8310/3': ('Clear cell carcinoma', 0.04),
'8430/3': ('Mucoepidermoid carcinoma', 0.03),
'8720/3': ('Melanoma', 0.03),
'8890/3': ('Leiomyosarcoma', 0.02),
}
def sc(p, rng):
a = np.array(list(p.values()))
return rng.choice(list(p.keys()), p=a/a.sum())
def gen(n=4500, seed=42):
rng = np.random.default_rng(seed)
recs = []
for i in range(n):
country = sc(dict.fromkeys(COUNTRIES, 1/len(COUNTRIES)), rng)
cancer = sc(dict.fromkeys(CANCER_TYPES, 1/len(CANCER_TYPES)), rng)
year = sc(YEAR, rng)
facility = sc({'National referral': 0.25, 'Regional': 0.40, 'District': 0.35}, rng)
# Get morphology
morph_code = sc({k: v[1] for k, v in MORPHOLOGY_CODES.items()}, rng)
morph_name = [v[0] for k, v in MORPHOLOGY_CODES.items() if k == morph_code][0]
# IHC markers available based on facility
avail_markers = [m for m in IHC_MARKERS if rng.random() < AVAILABILITY[facility]]
if not avail_markers:
avail_markers = ['Not done']
recs.append({
'pathology_id': f'PATH-{country[:3].upper()}-{year}-{i+1:05d}',
'country': country, 'year': year, 'facility_type': facility,
'cancer_type': cancer, 'icd_o_morphology_code': morph_code,
'morphology_description': morph_name,
'grade': sc({'Well differentiated': 0.12, 'Moderately differentiated': 0.35,
'Poorly differentiated': 0.38, 'Undifferentiated': 0.15}, rng),
'lymphovascular_invasion': rng.choice(['Present', 'Absent', 'Not documented'], p=[0.32, 0.48, 0.20]),
'perineural_invasion': rng.choice(['Present', 'Absent', 'Not documented'], p=[0.18, 0.52, 0.30]),
'margin_status': sc({'Negative': 0.62, 'Close': 0.18, 'Positive': 0.12, 'Not applicable': 0.08}, rng),
'ihc_er': sc({'Positive': 0.55, 'Negative': 0.35, 'Not done': 0.10}, rng),
'ihc_pr': sc({'Positive': 0.48, 'Negative': 0.42, 'Not done': 0.10}, rng),
'ihc_her2': sc({'Positive': 0.15, 'Negative': 0.72, 'Equivocal': 0.05, 'Not done': 0.08}, rng),
'ihc_ki67_index': round(rng.uniform(5, 90), 1) if 'Ki67' in avail_markers else 'Not done',
'ihc_p53': sc({'Positive': 0.45, 'Negative': 0.40, 'Not done': 0.15}, rng),
'molecular_subtype': sc({'Luminal A': 0.38, 'Luminal B': 0.15, 'HER2+': 0.12, 'Triple negative': 0.25, 'N/A': 0.10}, rng) if cancer == 'Breast' else 'N/A',
'specimen_type': sc({'Core needle biopsy': 0.42, 'Excisional biopsy': 0.28, 'Resection': 0.22, 'Fine needle aspirate': 0.08}, rng),
'diagnosis_confirmed': rng.choice(['Yes', 'No - inconclusive', 'No - suboptimal'], p=[0.85, 0.12, 0.03]),
'turnaround_time_days': rng.integers(3, 28)
})
return pd.DataFrame(recs)
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument('--n', type=int, default=4500)
p.add_argument('--output', type=str, default='.')
a = p.parse_args()
for sn, m, s in [('low_burden', 0.8, 42), ('moderate_burden', 1.0, 43), ('high_burden', 1.2, 44)]:
d = gen(int(a.n * m), s)
d['scenario'] = sn
d.to_csv(os.path.join(a.output, f'cancer_pathology_histopathology_{sn}.csv'), index=False)
print(f"Saved: cancer_pathology_histopathology_{sn}.csv, n={len(d)}")