Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdv/single_table/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ def add_constraints(self, constraints):
except ConstraintNotMetError:
raise e

self.metadata.validate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this fix it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data processor uses the _valid_column_relationships attribute here to iterate over column relationships. That attribute only gets set here when validating the column relationships.

When we add constraints at the single table level, we use the updated metadata from the constraints for the data processor, but it was missing the _valid_column_relationships attribute because the updated metadata hadn't been validated.

self._data_processor = DataProcessor(
metadata=self.metadata._convert_to_single_table(),
enforce_rounding=self.enforce_rounding,
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/single_table/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,56 @@ def demo_metadata():
return DEMO_METADATA


def test_column_relationships_with_constraints():
"""Test constraints with column relationships. GH#2768"""
# Setup
metadata = Metadata.load_from_dict({
'tables': {
'table': {
'columns': {
'id': {'sdtype': 'id'},
'street': {'sdtype': 'street_address'},
'city': {'sdtype': 'city'},
'state': {'sdtype': 'administrative_unit'},
'zip': {'sdtype': 'postcode'},
'code': {'sdtype': 'categorical'},
'description': {'sdtype': 'categorical'},
}
},
},
})
metadata.add_column_relationship(
table_name='table',
relationship_type='address',
column_names=['street', 'city', 'state', 'zip'],
)
data = pd.DataFrame({
'id': [f'id_{i}' for i in range(6)],
'street': ['123 Street'] * 6,
'city': ['Boston', 'LA', 'Cambridge', 'San Francisco', 'Boston', 'LA'],
'state': ['MA', 'CA'] * 3,
'zip': ['72801'] * 6,
'code': ['000', '001', '002'] * 2,
'description': ['code 0', 'code 1', 'code 2'] * 2,
})
constraint = FixedCombinations(table_name='table', column_names=['code', 'description'])
synth = GaussianCopulaSynthesizer(metadata)

# Run
synth.add_constraints([constraint])
synth.fit(data)
samples = synth.sample(100)

# Assert
assert samples.columns.tolist() == data.columns.to_list()
expected_combinations = {('000', 'code 0'), ('001', 'code 1'), ('002', 'code 2')}
sampled_combinations = {
(code, description)
for code, description in samples[['code', 'description']].drop_duplicates().to_numpy()
}
assert expected_combinations == sampled_combinations


def test_conditional_sampling_with_constraints(demo_data, demo_metadata):
"""Test constraints with conditional sampling. GH#1737"""
# Setup
Expand Down