Generate random test data with Faker including a custom provider for a specific domain like medical codes. Create a CSV with 1 million rows maintaining referential integrity where patient IDs in visits must exist in patients.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Providing Python example for large synthetic data generation
A clear Python script is ready that defines a custom medical code provider with Faker, generates patient data (e.g., 100,000 patients), and then generates 1 million visit records referencing existing patients to maintain referential integrity, writing both datasets efficiently to CSV files.
Below is a self‑contained Python example using Faker with a custom provider for medical codes. It writes two CSV files:
patients.csv — a set of patients (e.g., 100 000 patients).
visits.csv — 1 000 000 visit records, each referencing an existing patient ID to maintain referential integrity.
You can adjust the counts as needed. The code streams directly to CSV to avoid excessive memory use.
python
import csv
import random
from faker import Faker
from faker.providers import BaseProvider
# --- Custom provider for medical codes ---
class MedicalProvider(BaseProvider):
# A small sample of ICD-10 codes; extend or replace with your own list
icd10_codes = [
("A00", "Cholera"),
("B20", "HIV disease"),
("E11", "Type 2 diabetes mellitus"),
("I10", "Essential (primary) hypertension"),
("J45", "Asthma"),
("M54.5", "Low back pain"),
("N39.0", "Urinary tract infection"),
("R51", "Headache"),
("Z00.0", "General medical exam"),
("Z23", "Immunization")
]
def medical_code(self):
code, desc = self.random_element(self.icd10_codes)
return code
def medical_description(self):
code, desc = self.random_element(self.icd10_codes)
return desc
# --- Initialize Faker and add provider ---
fake = Faker()
fake.add_provider(MedicalProvider)
# --- Configuration ---
NUM_PATIENTS = 100_000
NUM_VISITS = 1_000_000
PATIENTS_CSV = "patients.csv"
VISITS_CSV = "visits.csv"
# --- Generate patients ---
def generate_patients(num_patients):
"""
Returns a list of patient IDs and writes patient info to CSV.
"""
patient_ids = []
with open(PATIENTS_CSV, mode="w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["patient_id", "first_name", "last_name", "dob", "gender"])
for pid in range(1, num_patients + 1):
patient_ids.append(pid)
writer.writerow([
pid,
fake.first_name(),
fake.last_name(),
fake.date_of_birth(minimum_age=0, maximum_age=100).isoformat(),
random.choice(["M", "F", "O"])
])
return patient_ids
# --- Generate visits ---
def generate_visits(patient_ids, num_visits):
"""
Writes visit data to CSV, ensuring patient_id references existing patients.
"""
with open(VISITS_CSV, mode="w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow([
"visit_id",
"patient_id",
"visit_date",
"medical_code",
"medical_description",
"visit_reason"
])
for vid in range(1, num_visits + 1):
pid = random.choice(patient_ids)
writer.writerow([
vid,
pid,
fake.date_between(start_date="-2y", end_date="today").isoformat(),
fake.medical_code(),
fake.medical_description(),
fake.sentence(nb_words=6)
])
if __name__ == "__main__":
print("Generating patients...")
patient_ids = generate_patients(NUM_PATIENTS)
print("Generating visits...")
generate_visits(patient_ids, NUM_VISITS)
print("Done.")
Notes
Performance: Writing 1 000 000 rows will take some time. Running on a decent machine, this should complete within a few minutes, but you can reduce NUM_VISITS/NUM_PATIENTS to test quickly.
Memory: The code stores patient_ids in memory; for 100 000 patients, this is modest. If you need even larger sets, consider generating IDs on the fly or writing the patients.csv first, then sampling directly from its ID range without storing all rows (e.g., pid = random.randint(1, NUM_PATIENTS)).
Custom codes: Replace or extend icd10_codes with your domain-specific list if needed.
This ensures that every patient_id in visits.csv corresponds to an existing record in patients.csv.