diff --git a/docs/includes/generated_docs/language__measures.md b/docs/includes/generated_docs/language__measures.md index 0583898a2..7f4b018cc 100644 --- a/docs/includes/generated_docs/language__measures.md +++ b/docs/includes/generated_docs/language__measures.md @@ -80,7 +80,7 @@ this method more than once is an error.
- configure_dummy_data(population_size=10, legacy=False, timeout=60) + configure_dummy_data(population_size=10, legacy=False, timeout=60, additional_population_constraint=None) 🔗
@@ -98,6 +98,19 @@ Use legacy dummy data. _timeout_
Maximum time in seconds to spend generating dummy data. +_additional_population_constraint_
+An additional ehrQL query that can be used to constrain the population that will +be selected for dummy data. This is incompatible with legacy mode. + +For example, if you wanted to ensure that two dates appear in a particular order in your +dummy data, you could add ``additional_population_constraint = dataset.first_date < +dataset.second_date``. + +You can also combine constraints with ``&`` as normal in ehrQL. +e.g. ``additional_population_constraint = patients.sex.is_in(['male', 'female']) & ( +patients.age_on(some_date) < 80)`` would give you dummy data consisting of only men +and women who were under the age of 80 on some particular date. + ```py measures.configure_dummy_data(population_size=10000) ``` diff --git a/ehrql/measures/measures.py b/ehrql/measures/measures.py index 07d549b7a..82518ab6d 100644 --- a/ehrql/measures/measures.py +++ b/ehrql/measures/measures.py @@ -292,6 +292,7 @@ def configure_dummy_data( population_size=DummyDataConfig.population_size, legacy=DummyDataConfig.legacy, timeout=DummyDataConfig.timeout, + additional_population_constraint=None, ): """ Configure the dummy data to be generated. @@ -308,6 +309,19 @@ def configure_dummy_data( _timeout_
Maximum time in seconds to spend generating dummy data. + _additional_population_constraint_
+ An additional ehrQL query that can be used to constrain the population that will + be selected for dummy data. This is incompatible with legacy mode. + + For example, if you wanted to ensure that two dates appear in a particular order in your + dummy data, you could add ``additional_population_constraint = dataset.first_date < + dataset.second_date``. + + You can also combine constraints with ``&`` as normal in ehrQL. + e.g. ``additional_population_constraint = patients.sex.is_in(['male', 'female']) & ( + patients.age_on(some_date) < 80)`` would give you dummy data consisting of only men + and women who were under the age of 80 on some particular date. + ```py measures.configure_dummy_data(population_size=10000) ``` @@ -315,6 +329,9 @@ def configure_dummy_data( self.dummy_data_config.population_size = population_size self.dummy_data_config.legacy = legacy self.dummy_data_config.timeout = timeout + self.dummy_data_config.set_additional_population_constraint( + additional_population_constraint + ) def configure_disclosure_control(self, *, enabled=True): """ diff --git a/ehrql/query_language.py b/ehrql/query_language.py index be0a0b6ca..ee395ff6e 100644 --- a/ehrql/query_language.py +++ b/ehrql/query_language.py @@ -50,6 +50,21 @@ class DummyDataConfig: timeout: int = 60 additional_population_constraint: "qm.Series[bool] | None" = None + def set_additional_population_constraint(self, additional_population_constraint): + if additional_population_constraint is not None: + validate_patient_series_type( + additional_population_constraint, + types=[bool], + context="additional population constraint", + ) + self.additional_population_constraint = ( + additional_population_constraint._qm_node + ) + if self.legacy and self.additional_population_constraint is not None: + raise ValueError( + "Cannot provide an additional population constraint in legacy mode." + ) + class Dataset: """ @@ -149,21 +164,9 @@ def configure_dummy_data( self.dummy_data_config.population_size = population_size self.dummy_data_config.legacy = legacy self.dummy_data_config.timeout = timeout - if additional_population_constraint is not None: - validate_patient_series_type( - additional_population_constraint, - types=[bool], - context="additional population constraint", - ) - self.dummy_data_config.additional_population_constraint = ( - additional_population_constraint._qm_node - ) - else: - self.dummy_data_config.additional_population_constraint = None - if legacy and additional_population_constraint is not None: - raise ValueError( - "Cannot provide an additional population constraint in legacy mode." - ) + self.dummy_data_config.set_additional_population_constraint( + additional_population_constraint + ) def __setattr__(self, name, value): if name == "population":