Skip to content

Commit

Permalink
Add random seed to ensure reproducibility (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
ni9999 authored Oct 10, 2024
1 parent 032f07e commit 4318296
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
6 changes: 5 additions & 1 deletion healthchain/data_generators/cdsdatagenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def generate(
constraints: Optional[list] = None,
free_text_path: Optional[str] = None,
column_name: Optional[str] = None,
random_seed: Optional[int] = None,
) -> BaseModel:
"""
Generates CDS data based on the current workflow, constraints, and optional free text data.
Expand All @@ -83,6 +84,7 @@ def generate(
constraints (Optional[list]): A list of constraints to apply to the data generation.
free_text_path (Optional[str]): The path to a CSV file containing free text data.
column_name (Optional[str]): The column name in the CSV file to use for free text data.
random_seed (Optional[int]): The random seed to use for reproducible data generation.
Returns:
BaseModel: The generated CDS FHIR data.
Expand All @@ -95,7 +97,9 @@ def generate(
for resource in self.mappings[self.workflow]:
generator_name = resource["generator"]
generator = self.fetch_generator(generator_name)
result = generator.generate(constraints=constraints)
result = generator.generate(
constraints=constraints, random_seed=random_seed
)

results.append(BundleEntry(resource=result))

Expand Down
2 changes: 2 additions & 0 deletions healthchain/data_generators/conditiongenerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def generate(
subject_reference: Optional[str] = None,
encounter_reference: Optional[str] = None,
constraints: Optional[list] = None,
random_seed: Optional[int] = None,
):
Faker.seed(random_seed)
subject_reference = subject_reference or "Patient/123"
encounter_reference = encounter_reference or "Encounter/123"
code = generator_registry.get("SnomedCodeGenerator").generate(
Expand Down
6 changes: 4 additions & 2 deletions healthchain/data_generators/encountergenerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ class EncounterGenerator(BaseGenerator):
A generator class for creating FHIR Encounter resources.
Methods:
generate(constraints: Optional[list] = None) -> Encounter:
Generates a FHIR Encounter resource with optional constraints.
generate(constraints: Optional[list] = None, random_seed: Optional[int] = None) -> Encounter:
Generates a FHIR Encounter resource with optional constraints and random_seed.
"""

@staticmethod
def generate(
constraints: Optional[list] = None,
random_seed: Optional[int] = None,
) -> Encounter:
Faker.seed(random_seed)
patient_reference = "Patient/123"
return Encounter(
resourceType="Encounter",
Expand Down
2 changes: 2 additions & 0 deletions healthchain/data_generators/medicationrequestgenerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class MedicationRequestGenerator(BaseGenerator):
@staticmethod
def generate(
constraints: Optional[list] = None,
random_seed: Optional[int] = None,
):
Faker.seed(random_seed)
subject_reference = "Patient/123"
encounter_reference = "Encounter/123"
contained_medication = Medication(
Expand Down
6 changes: 5 additions & 1 deletion healthchain/data_generators/patientgenerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def generate():
@register_generator
class PatientGenerator(BaseGenerator):
@staticmethod
def generate(constraints: Optional[list] = None):
def generate(
constraints: Optional[list] = None,
random_seed: Optional[int] = None,
) -> Patient:
Faker.seed(random_seed)
return Patient(
resourceType="Patient",
id=generator_registry.get("IdGenerator").generate(),
Expand Down
2 changes: 2 additions & 0 deletions healthchain/data_generators/proceduregenerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def generate(
subject_reference: Optional[str] = None,
encounter_reference: Optional[str] = None,
constraints: Optional[list] = None,
random_seed: Optional[int] = None,
):
Faker.seed(random_seed)
subject_reference = subject_reference or "Patient/123"
encounter_reference = encounter_reference or "Encounter/123"
code = generator_registry.get("ProcedureSnomedCodeGenerator").generate(
Expand Down

0 comments on commit 4318296

Please sign in to comment.