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

Always create a sample size of 1 no matter how small the scale. #2062

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 12 additions & 0 deletions sdv/sampling/hierarchical_sampler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Hierarchical Samplers."""
import logging
import warnings

import pandas as pd

Expand Down Expand Up @@ -273,13 +274,24 @@ def _sample(self, scale=1.0):
# DFS to sample roots and then their children
non_root_parents = set(self.metadata._get_parent_map().keys())
root_parents = set(self.metadata.tables.keys()) - non_root_parents
send_min_sample_warning = False
for table in root_parents:
num_rows = round(self._table_sizes[table] * scale)
if num_rows <= 0:
send_min_sample_warning = True
num_rows = 1
synthesizer = self._table_synthesizers[table]
LOGGER.info(f'Sampling {num_rows} rows from table {table}')
sampled_data[table] = self._sample_rows(synthesizer, num_rows)
self._sample_children(table_name=table, sampled_data=sampled_data, scale=scale)

if send_min_sample_warning:
warn_msg = (
"The 'scale' parameter it too small. Some tables may have 1 row."
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: "The 'scale' parameter it too small." -> "The 'scale' parameter is too small."

' For better quality data, please choose a larger scale.'
)
warnings.warn(warn_msg)

added_relationships = set()
for relationship in self.metadata.relationships:
parent_name = relationship['parent_table_name']
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/multi_table/test_hma.py
Original file line number Diff line number Diff line change
Expand Up @@ -1839,3 +1839,27 @@ def test_disjointed_tables():
# Assert
for table in real_data:
assert list(real_data[table].columns) == list(disjoin_synthetic_data[table].columns)


def test_small_sample():
"""Test that the sample function still works with a small scale"""
# Setup
data, metadata = download_demo(
modality='multi_table',
dataset_name='fake_hotels'
)
synthesizer = HMASynthesizer(metadata)
synthesizer.fit(data)

# Run and Assert
warn_msg = re.escape(
"The 'scale' parameter it too small. Some tables may have 1 row."
' For better quality data, please choose a larger scale.'
)
with pytest.warns(Warning, match=warn_msg):
synthetic_data = synthesizer.sample(scale=0.01)

assert (len(synthetic_data['hotels']) == 1)
assert (len(synthetic_data['guests']) >= len(data['guests']) * .01)
assert synthetic_data['hotels'].columns.tolist() == data['hotels'].columns.tolist()
assert synthetic_data['guests'].columns.tolist() == data['guests'].columns.tolist()
Loading