❖ Auto-Detect Constraints
SDV is able to automatically identify common business rules in your dataset and add them as constraints. Use the functionality described in this guide to auto-detect, inspect, update, and add constraints to your synthesizer.
from sdv.multi_table import HSASynthesizer
# create a synthesizer
synthesizer = HSASynthesizer(metadata)
# detect constraints based on the data and add them to the synthesizer
detected_constraints = synthesizer.detect_constraints(data)
synthesizer.add_constraints(detected_constraints)
# now fit and sample the synthesizer like usual
synthesizer.fit(data)
synthetic_data = synthesizer.sample()Detect Constraints
SDV detects constraints using your synthesizer and data.
<synthesizer instance>.detect_constraints
Use this method with an SDV synthesizer in order to detect constraints. All single- and multi-table SDV synthesizers are supported except for DayZSynthesizer.
Running the auto-detection does not automatically apply the constraints. This algorithm provides you a list of constraints that you can review and modify before officially adding to the synthesizer.
Parameters:
(required)
data: Your data. For single-table synthesizers, this is a single pd.DataFrame object. For multi-table synthesizers, your data is a dictionary that maps each table name to a pd.DataFrame object that contains the data.verbose: A boolean that controls whether to print out information during the detection.(default)
True: Show a progress bar during detection and print out a list of all all the constraints that are found.False: Do not print anything out during detection.
constraint_classes: A list of strings describing which constraint classes to detect(default)
['OneHotEncoding', 'FixedNullCombinations', 'ChainedInequality', 'DenormalizedTable']: Detect constraints for these predefined constraint classes. Note that DenormalizedTable will not be applied to single-table synthesizers.[<string>]: Provide a list of strings with the class names of the Predefined constraints. Currently, SDV offers support for auto-detecting all predefined constraints except for ForeignToForeignKey and ReferenceTable.None: Do not limit the constraint classes. SDV will detect all available constraint classes.
table_names: A list of table names describing which tables to use when detecting constraints.(default)
None: Use all the tables names from the dataset[<string>]: Provide a list of strings with the table names. If provided, SDV will only detect constraints within these tables.
detection_params: A dictionary describing any additional parameters to use when detecting each constraint class. This advanced functionality can be used to control the exact specifications of what should be detected.(default)
None: Use the default detection capabilities for each of the constraint classes.<dictionary>: A dictionary that maps each constraint class name (string) to a dictionary of parameters for detection. If available, these parameters are listed the API for each individual constraint class. (See Predefined Constraints.)
Returns: A ConstraintList object. This object is similar to a list of constraints. However, it has specialized methods for adding and removing constraints, as the order of constraints may impact the algorithm.
Inspect Constraints
After detecting constraint, you can inspect them by printing out your constraint set object.
print(<constraint_list_instance>)
Print out your ConstraintList object to see a list of constraints. Each constraint will be printed out next to its index, which indicates the order in which it will be applied.
Remove Constraints
You can remove any of the auto-detected constraints in from your ConstraintList object.
<constraint_list_instance>.remove_constraint
Use this function to remove any of the constraints in your ConstraintList object. Keep in mind that the constraint list is designed to be applied in order. Removing a constraint from the middle of the list may mean that some of the constraints that follow it are no longer applicable.
Parameters:
(required)
index: An integer describing the position of the constraint that you want to remove. To find the position for a constraint, print out the list.redetect: Whether to re-detect constraints that occur after the one you've removed.(default)
False: Remove the constraint and any other constraints that come after it if they are no longer valid.True: Remove the constraint and any other constraints that come after it if they are no longer valid. Then, re-detect constraints in case there are more.
verbose: A boolean that controls whether to print out information during removal/re-detection.(default)
True: Show a progress bar during deletion and re-detection and print out a list of all all the constraints that are found.False: Do not print anything out during detection.
Returns None. The ConstraintList instance will no longer have the constraint, and may have some new constraints based on re-detection.
Add Auto-Detected Constraints to Your Synthesizer
Finally, when all the auto-detected constraints look good, you can add them to a single- or multi-table SDV synthesizer.
<synthesizer_instance>.add_constraints
Use this method to add the auto-detected constraints to your synthesizer instance. All single- and multi-table SDV synthesizers are supported except for DayZSynthesizer.
Parameters:
(required)
constraints: AConstraintListobject containing your auto-detected constraints.
Returns None. The constraints are now added to the synthesizer. Please make sure to fit the synthesizer first. When you sample synthetic data, it is guaranteed to follow the constraints.
FAQ
Last updated