Skip to content

Allow is_valid to be called on unfit constraints #2578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 24, 2025
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
2 changes: 1 addition & 1 deletion sdv/cag/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _get_is_valid_dict(data, table_name):
return {
table: pd.Series(True, index=table_data.index)
for table, table_data in data.items()
if table != table_name
if table != table_name or table_name is None
}


Expand Down
23 changes: 15 additions & 8 deletions sdv/cag/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ def reverse_transform(self, data):

return reverse_transformed

def _is_valid(self, data):
def _is_valid(self, data, metadata):
raise NotImplementedError

def is_valid(self, data):
def is_valid(self, data, metadata=None):
"""Say whether the given table rows are valid.

Args:
Expand All @@ -267,14 +267,21 @@ def is_valid(self, data):
pd.Series or dict[pd.Series]:
Series of boolean values indicating if the row is valid for the constraint or not.
"""
if not self._fitted:
if not self._fitted and metadata is None:
raise NotFittedError(
'Constraint must be fit using ``fit`` before determining if data is valid.'
'Constraint must be fit using ``fit`` before determining if data is valid '
'without providing metadata.'
)

data = self._convert_data_to_dictionary(data, self.metadata)
is_valid_data = self._is_valid(data)
if self._single_table:
return is_valid_data[self._table_name]
metadata = self.metadata if metadata is None else metadata
data_dict = self._convert_data_to_dictionary(data, metadata)
is_valid_data = self._is_valid(data_dict, metadata)
if isinstance(data, pd.DataFrame) or self._single_table:
table_name = (
self._get_single_table_name(metadata)
if getattr(self, '_table_name', None) is None
else self._table_name
)
return is_valid_data[table_name]

return is_valid_data
5 changes: 4 additions & 1 deletion sdv/cag/fixed_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ def _reverse_transform(self, data):

return data

def _is_valid(self, data):
def _is_valid(self, data, metadata):
"""Determine whether the data matches the constraint."""
if not self._fitted:
return _get_is_valid_dict(data, table_name=None)

table_name = self._get_single_table_name(self.metadata)
is_valid = _get_is_valid_dict(data, table_name)
merged = data[table_name].merge(
Expand Down
4 changes: 2 additions & 2 deletions sdv/cag/fixed_increments.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _fit(self, data, metadata):
table_name = self._get_single_table_name(metadata)
self._dtype = data[table_name][self.column_name].dtype

def _is_valid(self, data):
def _is_valid(self, data, metadata):
"""Determine if the data is evenly divisible by the increment.

Args:
Expand All @@ -169,7 +169,7 @@ def _is_valid(self, data):
number of tables in the data and contain the same
table names.
"""
table_name = self._get_single_table_name(self.metadata)
table_name = self._get_single_table_name(metadata)
is_valid = _get_is_valid_dict(data, table_name)
valid = self._check_if_divisible(data, table_name, self.column_name, self.increment_value)
is_valid[table_name] = valid
Expand Down
45 changes: 16 additions & 29 deletions sdv/cag/inequality.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,10 @@ def _get_is_datetime(self, metadata, table_name):
def _get_datetime_format(self, metadata, table_name, column_name):
return metadata.tables[table_name].columns[column_name].get('datetime_format')

def _validate_constraint_with_data(self, data, metadata):
"""Validate the data is compatible with the constraint.

Validate that the inequality requirement is met between the high and low columns.
"""
table_name = self._get_single_table_name(metadata)
data = data[table_name]
low, high = self._get_data(data)
def _get_valid_table_data(self, table_data, metadata, table_name):
low, high = self._get_data(table_data)
is_datetime = self._get_is_datetime(metadata, table_name)
if is_datetime and is_object_dtype(data[self._low_column_name]):
if is_datetime and is_object_dtype(table_data[self._low_column_name]):
low_format = self._get_datetime_format(metadata, table_name, self._low_column_name)
high_format = self._get_datetime_format(metadata, table_name, self._high_column_name)
low = cast_to_datetime64(low, low_format)
Expand All @@ -143,7 +137,15 @@ def _validate_constraint_with_data(self, data, metadata):
high_datetime_format=high_format,
)

valid = pd.isna(low) | pd.isna(high) | self._operator(high, low)
return pd.isna(low) | pd.isna(high) | self._operator(high, low)

def _validate_constraint_with_data(self, data, metadata):
"""Validate the data is compatible with the constraint.

Validate that the inequality requirement is met between the high and low columns.
"""
table_name = self._get_single_table_name(metadata)
valid = self._get_valid_table_data(data[table_name], metadata, table_name)
if not valid.all():
invalid_rows = _get_invalid_rows(valid)
raise ConstraintNotMetError(
Expand Down Expand Up @@ -293,7 +295,7 @@ def _reverse_transform(self, data):

return data

def _is_valid(self, data):
def _is_valid(self, data, metadata):
"""Check whether `high` is greater than `low` in each row.

Args:
Expand All @@ -304,24 +306,9 @@ def _is_valid(self, data):
dict[str, pd.Series]:
Whether each row is valid.
"""
table_name = self._get_single_table_name(self.metadata)
table_name = self._get_single_table_name(metadata)
is_valid = _get_is_valid_dict(data, table_name)
table_data = data[table_name]
low, high = self._get_data(table_data)
if self._is_datetime and is_object_dtype(self._dtype):
low = cast_to_datetime64(low, self._low_datetime_format)
high = cast_to_datetime64(high, self._high_datetime_format)

format_matches = bool(self._low_datetime_format == self._high_datetime_format)
if not format_matches:
low, high = match_datetime_precision(
low=low,
high=high,
low_datetime_format=self._low_datetime_format,
high_datetime_format=self._high_datetime_format,
)

valid = pd.isna(low) | pd.isna(high) | self._operator(high, low)
is_valid[table_name] = valid
valid_table_rows = self._get_valid_table_data(data[table_name], metadata, table_name)
is_valid[table_name] = valid_table_rows

return is_valid
4 changes: 2 additions & 2 deletions sdv/cag/one_hot_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _reverse_transform(self, data):

return data

def _is_valid(self, data):
def _is_valid(self, data, metadata):
"""Check whether the data satisfies the one-hot constraint.

Args:
Expand All @@ -138,7 +138,7 @@ def _is_valid(self, data):
dict[str, pd.Series]:
Whether each row is valid.
"""
table_name = self._get_single_table_name(self.metadata)
table_name = self._get_single_table_name(metadata)
is_valid = _get_is_valid_dict(data, table_name)
is_valid[table_name] = self._get_valid_table_data(data[table_name])

Expand Down
7 changes: 4 additions & 3 deletions sdv/cag/programmable_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,14 @@ def _reverse_transform(self, data):

return reverse_transformed

def _is_valid(self, data):
def _is_valid(self, data, metadata):
if self._is_single_table:
data = data[self._table_name]
table_name = self._get_single_table_name(metadata)
data = data[table_name]

is_valid = self.programmable_constraint.is_valid(data)

if self._is_single_table:
return {self._table_name: is_valid}
return {table_name: is_valid}
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, should we update the if condition to be the same as in the base cag class:
if isinstance(data, pd.DataFrame) or self._single_table:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this is a different check - the _is_single_table attribute on the harness controls whether we expect a single dataframe or a dataframe dict from the programmable constraint (based on whether it's a SingleTableProgrammableConstraint or a ProgrammableConstraint).


return is_valid
4 changes: 2 additions & 2 deletions sdv/cag/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _reverse_transform(self, data):

return data

def _is_valid(self, data):
def _is_valid(self, data, metadata):
"""Check whether the `middle` column is between the `low` and `high` columns.

Args:
Expand All @@ -365,7 +365,7 @@ def _is_valid(self, data):
dict[str, pd.Series]:
Whether each row is valid.
"""
table_name = self._get_single_table_name(self.metadata)
table_name = self._get_single_table_name(metadata)
is_valid = _get_is_valid_dict(data, table_name)
is_valid[table_name] = self._get_valid_table_data(data[table_name])

Expand Down
4 changes: 3 additions & 1 deletion tests/integration/cag/test_programmable_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def __init__(self, column_names, table_name):
self.table_name = table_name
self._joint_column = '#'.join(self.column_names)
self._combinations = None
self._fitted = False
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need it so we can use the FixedCombinations methods on this class. The _is_valid method for FixedCombinations now checks whether or not the constraint has been fit, so we need it set on this class for the method to actually work without erroring.


def _get_single_table_name(self, metadata):
# Have to define this so that we can re-use existing methods on the constraint
Expand All @@ -114,6 +115,7 @@ def fit(self, data, metadata):
self.metadata = metadata
data = {self.table_name: data}
FixedCombinations._fit(self, data, metadata)
self._fitted = True

def transform(self, data):
data = {self.table_name: data}
Expand All @@ -130,7 +132,7 @@ def reverse_transform(self, transformed_data):

def is_valid(self, synthetic_data):
synthetic_data = {self.table_name: synthetic_data}
is_valid = FixedCombinations._is_valid(self, synthetic_data)
is_valid = FixedCombinations._is_valid(self, synthetic_data, self.metadata)
return is_valid[self.table_name]

return MyConstraint
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/cag/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ def test_is_valid_errors_if_not_fitted(self, data):
# Setup
instance = BaseConstraint()
expected_msg = re.escape(
'Constraint must be fit using ``fit`` before determining if data is valid.'
'Constraint must be fit using ``fit`` before determining '
'if data is valid without providing metadata.'
)

# Run and assert
Expand All @@ -573,12 +574,13 @@ def test_is_valid(self, data):
instance = BaseConstraint()
instance._is_valid = Mock()
instance._fitted = True
instance.metadata = Mock()

# Run
is_valid_result = instance.is_valid(data)

# Assert
instance._is_valid.assert_called_once_with(data)
instance._is_valid.assert_called_once_with(data, instance.metadata)
assert is_valid_result == instance._is_valid.return_value

def test_is_valid_single_table(self, data):
Expand All @@ -591,10 +593,13 @@ def test_is_valid_single_table(self, data):
instance._is_valid = Mock()
instance._is_valid.return_value = {'table1': data.copy()}
instance._fitted = True
instance.metadata = Mock()

# Run
is_valid_result = instance.is_valid(data)

# Assert
instance._is_valid.assert_called_once_with(DataFrameDictMatcher({'table1': data}))
instance._is_valid.assert_called_once_with(
DataFrameDictMatcher({'table1': data}), instance.metadata
)
pd.testing.assert_frame_equal(is_valid_result, data)
31 changes: 31 additions & 0 deletions tests/unit/cag/test_fixed_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,34 @@ def test__is_valid_with_nans(self):

expected_invalid_out = pd.Series([False] * 3, name='b#c')
pd.testing.assert_series_equal(expected_invalid_out, invalid_out)

def test__is_valid_unfit(self):
"""Test the ``_is_valid`` method when the constraint has not been fit."""
# Setup
metadata = Metadata.load_from_dict({
'tables': {
'table': {
'columns': {
'a': {'sdtype': 'categorical'},
'b': {'sdtype': 'categorical'},
'c': {'sdtype': 'categorical'},
'd': {'sdtype': 'categorical'},
}
}
}
})
data = pd.DataFrame({
'a': ['a', 'b', 'c'],
'b': ['d', 'e', 'f'],
'c': ['g', 'h', 'i'],
})

columns = ['b', 'c']
instance = FixedCombinations(column_names=columns)

# Run
valid_out = instance.is_valid(data, metadata)

# Assert
expected_valid_out = pd.Series([True, True, True])
pd.testing.assert_series_equal(expected_valid_out, valid_out)
Loading
Loading