Skip to content

Commit

Permalink
GaussianCopula get_learned_distributions crashes if nothing was lea…
Browse files Browse the repository at this point in the history
…rned (#2339)
  • Loading branch information
R-Palazzo authored Jan 13, 2025
1 parent 7384de0 commit 6b47f9d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
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

0 comments on commit 6b47f9d

Please sign in to comment.