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

sdv-issue-331: Fixing Duplicate IDs when using reject-sampling #382

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions sdv/metadata/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,23 @@ def filter_valid(self, data):

return data

def make_ids_unique(self, data):
"""Repopulate any id fields in provided data to guarantee uniqueness.

Args:
data (pandas.DataFrame):
Table data.

Returns:
pandas.DataFrame:
Table where all id fields are unique.
"""
for name, field_metadata in self._fields_metadata.items():
if field_metadata['type'] == 'id':
data[name] = self._make_ids(field_metadata, len(data))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This returns all the ids in order. Idk if we want to randomize them, or provide the option for that

Copy link
Member

Choose a reason for hiding this comment

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

There is an issue about this #329 , we will solve that later.


return data

# ###################### #
# Metadata Serialization #
# ###################### #
Expand Down
1 change: 1 addition & 0 deletions sdv/tabular/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def _sample_batch(self, num_rows=None, max_retries=100, max_rows_multiplier=10,

counter += 1

sampled = self._metadata.make_ids_unique(sampled)
return sampled.head(min(len(sampled), num_rows))

def _make_conditions_df(self, conditions, num_rows):
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/metadata/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ def test__make_ids_fail(self):
metadata = {'subtype': 'string', 'regex': '[a-d]'}
with pytest.raises(ValueError):
Table._make_ids(metadata, 20)

def test_make_ids_unique(self):
"""Test that id columns contain all unique values"""
metadata_dict = {
'fields': {
'item 0': {'type': 'id', 'subtype': 'integer'},
'item 1': {'type': 'boolean'}
},
'primary_key': 'item 0'
}
metadata = Table.from_dict(metadata_dict)
data = pd.DataFrame({
'item 0': [0, 1, 1, 2, 3, 5, 5, 6],
'item 1': [True, True, False, False, True, False, False, True]
})

new_data = metadata.make_ids_unique(data)

assert new_data['item 1'].equals(data['item 1'])
assert new_data['item 0'].is_unique