Skip to content
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

Log columns in numerical_distributions which have been renamed #1235

Merged
merged 7 commits into from
Feb 8, 2023
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
12 changes: 9 additions & 3 deletions sdv/single_table/copulagan.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""Combination of GaussianCopula transformation and GANs."""
import logging
from copy import deepcopy

import rdt

from sdv.single_table.copulas import GaussianCopulaSynthesizer
from sdv.single_table.ctgan import CTGANSynthesizer
from sdv.single_table.utils import validate_numerical_distributions
from sdv.single_table.utils import (
log_numerical_distributions_error, validate_numerical_distributions)

LOGGER = logging.getLogger(__name__)


class CopulaGANSynthesizer(CTGANSynthesizer):
Expand Down Expand Up @@ -145,7 +149,7 @@ def __init__(self, metadata, enforce_min_max_values=True, enforce_rounding=True,
)
self._numerical_distributions = {
field: GaussianCopulaSynthesizer.get_distribution_class(distribution)
for field, distribution in (numerical_distributions or {}).items()
for field, distribution in self.numerical_distributions.items()
}

def _create_gaussian_normalizer_config(self, processed_data):
Expand Down Expand Up @@ -179,8 +183,10 @@ def _fit(self, processed_data):
processed_data (pandas.DataFrame):
Data to be learned.
"""
gaussian_normalizer_config = self._create_gaussian_normalizer_config(processed_data)
log_numerical_distributions_error(
self.numerical_distributions, processed_data.columns, LOGGER)

gaussian_normalizer_config = self._create_gaussian_normalizer_config(processed_data)
self._gaussian_normalizer_hyper_transformer = rdt.HyperTransformer()
self._gaussian_normalizer_hyper_transformer.set_config(gaussian_normalizer_config)
processed_data = self._gaussian_normalizer_hyper_transformer.fit_transform(processed_data)
Expand Down
13 changes: 10 additions & 3 deletions sdv/single_table/copulas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Wrappers around copulas models."""
import logging
import warnings
from copy import deepcopy

Expand All @@ -12,7 +13,11 @@

from sdv.errors import NonParametricError
from sdv.single_table.base import BaseSingleTableSynthesizer
from sdv.single_table.utils import flatten_dict, unflatten_dict, validate_numerical_distributions
from sdv.single_table.utils import (
flatten_dict, log_numerical_distributions_error, unflatten_dict,
validate_numerical_distributions)

LOGGER = logging.getLogger(__name__)


class GaussianCopulaSynthesizer(BaseSingleTableSynthesizer):
Expand Down Expand Up @@ -97,7 +102,7 @@ def __init__(self, metadata, enforce_min_max_values=True, enforce_rounding=True,
self._default_distribution = self.get_distribution_class(self.default_distribution)
self._numerical_distributions = {
field: self.get_distribution_class(distribution)
for field, distribution in (numerical_distributions or {}).items()
for field, distribution in self.numerical_distributions.items()
}
self._num_rows = None

Expand All @@ -108,9 +113,11 @@ def _fit(self, processed_data):
processed_data (pandas.DataFrame):
Data to be learned.
"""
log_numerical_distributions_error(
self.numerical_distributions, processed_data.columns, LOGGER)
self._num_rows = len(processed_data)
numerical_distributions = deepcopy(self._numerical_distributions)

numerical_distributions = deepcopy(self._numerical_distributions)
for column in processed_data.columns:
if column not in numerical_distributions:
numerical_distributions[column] = self._numerical_distributions.get(
Expand Down
11 changes: 11 additions & 0 deletions sdv/single_table/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,14 @@ def validate_numerical_distributions(numerical_distributions, metadata_columns):
f'{invalid_columns}. The column names you provide must be present '
'in the metadata.'
)


def log_numerical_distributions_error(numerical_distributions, processed_data_columns, logger):
"""Log error when numerical distributions columns don't exist anymore."""
unseen_columns = numerical_distributions.keys() - set(processed_data_columns)
for column in unseen_columns:
logger.info(
f"Requested distribution '{numerical_distributions[column]}' "
f"cannot be applied to column '{column}' because it no longer "
'exists after preprocessing.'
)
27 changes: 26 additions & 1 deletion tests/unit/single_table/test_copulagan.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,31 @@ def test__create_gaussian_normalizer_config(self, mock_rdt):
assert config == expected_config
assert mock_rdt.transformers.GaussianNormalizer.call_args_list == expected_calls

@patch('sdv.single_table.copulagan.LOGGER')
@patch('sdv.single_table.copulagan.CTGANSynthesizer._fit')
@patch('sdv.single_table.copulagan.rdt')
def test__fit_logging(self, mock_rdt, mock_ctgansynthesizer__fit, mock_logger):
"""Test a message is logged.

A message should be logged if the columns passed in ``numerical_distributions``
were renamed/dropped during preprocessing.
"""
# Setup
metadata = SingleTableMetadata()
metadata.add_column('col', sdtype='numerical')
numerical_distributions = {'col': 'gamma'}
instance = CopulaGANSynthesizer(metadata, numerical_distributions=numerical_distributions)
processed_data = pd.DataFrame()

# Run
instance._fit(processed_data)

# Assert
mock_logger.info.assert_called_once_with(
"Requested distribution 'gamma' cannot be applied to column 'col' "
'because it no longer exists after preprocessing.'
)

@patch('sdv.single_table.copulagan.CTGANSynthesizer._fit')
@patch('sdv.single_table.copulagan.rdt')
def test__fit(self, mock_rdt, mock_ctgansynthesizer__fit):
Expand All @@ -248,7 +273,7 @@ def test__fit(self, mock_rdt, mock_ctgansynthesizer__fit):
metadata = SingleTableMetadata()
instance = CopulaGANSynthesizer(metadata)
instance._create_gaussian_normalizer_config = Mock()
processed_data = Mock()
processed_data = pd.DataFrame()

# Run
instance._fit(processed_data)
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/single_table/test_copulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ def test_get_parameters(self):
'default_distribution': 'beta'
}

@patch('sdv.single_table.copulas.LOGGER')
def test__fit_logging(self, mock_logger):
"""Test a message is logged.

A message should be logged if the columns passed in ``numerical_distributions``
were renamed/dropped during preprocessing.
"""
# Setup
metadata = SingleTableMetadata()
metadata.add_column('col', sdtype='numerical')
numerical_distributions = {'col': 'gamma'}
instance = GaussianCopulaSynthesizer(
metadata, numerical_distributions=numerical_distributions)
processed_data = pd.DataFrame({'updated_col': [1, 2, 3]})

# Run
instance._fit(processed_data)

# Assert
mock_logger.info.assert_called_once_with(
"Requested distribution 'gamma' cannot be applied to column 'col' "
'because it no longer exists after preprocessing.'
)

@patch('sdv.single_table.copulas.warnings')
@patch('sdv.single_table.copulas.multivariate')
def test__fit(self, mock_multivariate, mock_warnings):
Expand Down