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

GaussianCopula get_learned_distributions crashes if nothing was learned #2339

Merged
merged 4 commits into from
Jan 13, 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
6 changes: 4 additions & 2 deletions sdv/single_table/copulagan.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,11 @@ def get_learned_distributions(self):
"Distributions have not been learned yet. Please fit your model first using 'fit'."
)

field_transformers = self._gaussian_normalizer_hyper_transformer.field_transformers

learned_distributions = {}
if not hasattr(self._gaussian_normalizer_hyper_transformer, 'field_transformers'):
return learned_distributions

field_transformers = self._gaussian_normalizer_hyper_transformer.field_transformers
for column_name, transformer in field_transformers.items():
if isinstance(transformer, rdt.transformers.GaussianNormalizer):
learned_params = deepcopy(transformer._univariate.to_dict())
Expand Down
3 changes: 3 additions & 0 deletions sdv/single_table/copulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ def get_learned_distributions(self):
"Distributions have not been learned yet. Please fit your model first using 'fit'."
)

if not hasattr(self._model, 'to_dict') or not self._model.to_dict():
return {}

parameters = self._model.to_dict()
columns = parameters['columns']
univariates = deepcopy(parameters['univariates'])
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/single_table/test_copulagan.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,29 @@ def test_get_learned_distributions(self):
},
}

def test_get_learned_distributions_nothing_learned(self):
"""Test that ``get_learned_distributions`` returns an empty dict when nothing is learned."""
# Setup
metadata = Metadata().load_from_dict({
'tables': {
'table1': {
'columns': {
'col_1': {'sdtype': 'id'},
'col_2': {'sdtype': 'credit_card_number'},
},
}
}
})
data = pd.DataFrame({'col_1': range(100), 'col_2': range(100)})
synthesizer = CopulaGANSynthesizer(metadata, default_distribution='beta')
synthesizer.fit(data)

# Run
result = synthesizer.get_learned_distributions()

# Assert
assert result == {}

def test_get_learned_distributions_raises_an_error(self):
"""Test that ``get_learned_distributions`` raises an error."""
# Setup
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/single_table/test_copulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,29 @@ def test_get_learned_distributions(self):
'one': {'distribution': 'uniform', 'learned_parameters': {'loc': 1.0, 'scale': 0.0}},
}

def test_get_learned_distributions_nothing_learned(self):
"""Test that ``get_learned_distributions`` returns an empty dict when nothing is learned."""
# Setup
metadata = Metadata().load_from_dict({
'tables': {
'table1': {
'columns': {
'col_1': {'sdtype': 'id'},
'col_2': {'sdtype': 'credit_card_number'},
},
}
}
})
data = pd.DataFrame({'col_1': range(100), 'col_2': range(100)})
synthesizer = GaussianCopulaSynthesizer(metadata, default_distribution='beta')
synthesizer.fit(data)

# Run
result = synthesizer.get_learned_distributions()

# Assert
assert result == {}

def test_get_learned_distributions_raises_an_error(self):
"""Test that ``get_learned_distributions`` returns a dict.

Expand Down
Loading