Skip to content
Closed
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
14 changes: 8 additions & 6 deletions dpsynth/experimental/nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ def __call__(
"""Runs the mechanism on per-type DataFrames, returns synthetic records."""
shared_cols = list(self.schema.shared_schema.attributes)

types = list(self.detail_synths.keys())
types = list(self.type_vocabulary)
data = dict(data)
for t in types:
if t not in data:
columns = list(self.schema.per_type_schemas[t].attributes)
if t not in data or data[t].empty:
columns = shared_cols + list(self.schema.per_type_schemas[t].attributes)
data[t] = pd.DataFrame(columns=columns)

shared_dfs = [
Expand All @@ -167,10 +167,10 @@ def __call__(

synthetic_details: dict[str, pd.DataFrame] = {}
detail_results: dict[str, data_generation_v3.DataGenerationResult] = {}
for type_name in types:
for type_name, synth in self.detail_synths.items():
detail_cols = list(self.schema.per_type_schemas[type_name].attributes)
sub_df = data[type_name][detail_cols]
res = self.detail_synths[type_name](rng, sub_df)
sub_df = data[type_name].reindex(columns=detail_cols)
res = synth(rng, sub_df)
synthetic_details[type_name] = res.synthetic_data
detail_results[type_name] = res

Expand Down Expand Up @@ -299,6 +299,8 @@ def configure( # pyrefly: ignore[bad-override]

detail_synths = {}
for type_name, type_schema in schema.per_type_schemas.items():
if not type_schema.attributes:
continue
config = data_generation_v3.TabularConfig(
domains=type_schema.attributes,
discrete_mechanism=self.detail_mechanism,
Expand Down
38 changes: 38 additions & 0 deletions tests/experimental/nested_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,44 @@ def test_max_records_per_user_requires_bypass(self):
with self.assertRaises(ValueError):
synth.configure(schema, zcdp_rho=10.0, max_records_per_user=5)

def test_empty_detail_schema(self):
shared_schema = domain.Schema({
'platform': domain.CategoricalAttribute(
possible_values=['web', 'mobile']
),
})
per_type_schemas = {
'click': domain.Schema({
'element': domain.CategoricalAttribute(
possible_values=['button', 'link']
),
}),
'marker': domain.Schema({}),
}
schema = nested.NestedSchema(
shared_schema=shared_schema,
per_type_schemas=per_type_schemas,
)
synth = nested.NestedTabularConfig()
calibrated = synth.configure(schema, zcdp_rho=100.0)
self.assertNotIn('marker', calibrated.detail_synths)
self.assertIn('click', calibrated.detail_synths)

rng = np.random.default_rng(42)
data = {
'click': pd.DataFrame({
'platform': ['web', 'mobile'] * 10,
'element': ['button', 'link'] * 10,
}),
'marker': pd.DataFrame({
'platform': ['web', 'mobile'] * 10,
}),
}
result = calibrated(rng, data)
self.assertIn('marker', result.synthetic_data)
self.assertIn('platform', result.synthetic_data['marker'].columns)
self.assertLen(result.synthetic_data['marker'].columns, 1)


def load_tests(loader, tests, ignore):
del loader, ignore # Unused.
Expand Down
Loading