Skip to content

Commit

Permalink
Include unit of privacy in graphs and output (#205)
Browse files Browse the repository at this point in the history
* include contributions in chart title

* pull contributions into variable

* contributions in output
  • Loading branch information
mccalluc authored Jan 7, 2025
1 parent 4c217fb commit ec26413
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 14 deletions.
7 changes: 6 additions & 1 deletion dp_wizard/app/components/column_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,14 @@ def column_plot():
contributions=contributions,
weighted_epsilon=epsilon * weight / weights_sum,
)
s = "s" if contributions > 1 else ""
title = (
f"Simulated {name}: normal distribution, "
f"{contributions} contribution{s} / invidual"
)
return plot_histogram(
histogram,
error=accuracy,
cutoff=0, # TODO
title=f"Simulated {name}, assuming normal distribution",
title=title,
)
7 changes: 3 additions & 4 deletions dp_wizard/utils/code_generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def _make_queries(self):

def _make_query(self, column_name):
indentifier = name_to_identifier(column_name)
title = f"DP counts for {column_name}"
accuracy_name = f"{indentifier}_accuracy"
histogram_name = f"{indentifier}_histogram"
return (
Expand All @@ -118,19 +117,19 @@ def _make_query(self, column_name):
)
.fill_blocks(
OUTPUT_BLOCK=self._make_output(
title=title,
column_name=column_name,
accuracy_name=accuracy_name,
histogram_name=histogram_name,
)
)
.finish()
)

def _make_output(self, title: str, accuracy_name: str, histogram_name: str):
def _make_output(self, column_name: str, accuracy_name: str, histogram_name: str):
return (
Template(f"{self.root_template}_output")
.fill_values(
TITLE=title,
COLUMN_NAME=column_name,
)
.fill_expressions(
ACCURACY_NAME=accuracy_name,
Expand Down
7 changes: 6 additions & 1 deletion dp_wizard/utils/code_generators/no-tests/_notebook_output.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# CONFIDENCE_NOTE
plot_histogram(HISTOGRAM_NAME, error=ACCURACY_NAME, cutoff=0, title=TITLE)
column_name = COLUMN_NAME
title = (
f"DP counts for {column_name}, "
f"assuming {contributions} contributions per invidual"
)
plot_histogram(HISTOGRAM_NAME, error=ACCURACY_NAME, cutoff=0, title=title)
3 changes: 2 additions & 1 deletion dp_wizard/utils/code_generators/no-tests/_privacy_unit.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
privacy_unit = dp.unit_of(contributions=CONTRIBUTIONS)
contributions = CONTRIBUTIONS
privacy_unit = dp.unit_of(contributions=contributions)
1 change: 1 addition & 0 deletions dp_wizard/utils/code_generators/no-tests/_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def flatten_dict(dictionary, parent_key=""):
"data": CSV_PATH,
"epsilon": EPSILON,
"columns": COLUMNS,
"contributions": contributions,
},
"outputs": OUTPUTS,
}
Expand Down
6 changes: 3 additions & 3 deletions dp_wizard/utils/code_generators/no-tests/_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
COLUMNS_BLOCK


def get_context(csv_path):
def get_context_contributions(csv_path):
CONTEXT_BLOCK
return context
return context, contributions


if __name__ == "__main__":
Expand All @@ -21,6 +21,6 @@ def get_context(csv_path):
"--csv", required=True, help="Path to csv containing private data"
)
args = parser.parse_args()
context = get_context(csv_path=args.csv)
context, contributions = get_context_contributions(csv_path=args.csv)

QUERIES_BLOCK
6 changes: 5 additions & 1 deletion dp_wizard/utils/code_generators/no-tests/_script_output.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
print(TITLE)
column_name = COLUMN_NAME
print(
f"DP counts for {column_name}, "
f"assuming {contributions} contributions per invidual"
)
print(CONFIDENCE_NOTE, ACCURACY_NAME)
print(HISTOGRAM_NAME)
6 changes: 3 additions & 3 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def expect_no_error():
expect_not_visible(download_results_text)
page.get_by_label("Contributions").fill("42")
page.get_by_text("Code sample: Unit of Privacy").click()
expect_visible("dp.unit_of(contributions=42)")
expect_visible("contributions = 42")
expect_no_error()

# Button disabled until upload:
Expand Down Expand Up @@ -156,7 +156,7 @@ def expect_no_error():

script_download = script_download_info.value
script = script_download.path().read_text()
assert "privacy_unit = dp.unit_of(contributions=42)" in script
assert "contributions = 42" in script

# Notebook:
with page.expect_download() as notebook_download_info:
Expand All @@ -165,7 +165,7 @@ def expect_no_error():

notebook_download = notebook_download_info.value
notebook = notebook_download.path().read_text()
assert "privacy_unit = dp.unit_of(contributions=42)" in notebook
assert "contributions = 42" in notebook

# -- Feedback --
page.get_by_text("Feedback").click()
Expand Down

0 comments on commit ec26413

Please sign in to comment.