-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 46-dp-demo-notebook
- Loading branch information
Showing
24 changed files
with
427 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,50 @@ | ||
"""DP Creator II makes it easier to get started with Differential Privacy.""" | ||
|
||
import os | ||
from pathlib import Path | ||
from argparse import ArgumentParser | ||
from argparse import ArgumentParser, ArgumentTypeError | ||
|
||
import shiny | ||
|
||
|
||
__version__ = "0.0.1" | ||
|
||
|
||
def existing_csv(arg): | ||
path = Path(arg) | ||
if not path.exists(): | ||
raise ArgumentTypeError(f"No such file: {arg}") | ||
if path.suffix != ".csv": | ||
raise ArgumentTypeError(f'Must have ".csv" extension: {arg}') | ||
return path | ||
|
||
|
||
def get_arg_parser(): | ||
parser = ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
"--csv", | ||
dest="csv_path", | ||
type=Path, | ||
type=existing_csv, | ||
help="Path to CSV containing private data", | ||
) | ||
parser.add_argument( | ||
"--unit", | ||
dest="unit_of_privacy", | ||
"--contrib", | ||
dest="contributions", | ||
metavar="CONTRIB", | ||
type=int, | ||
help="Unit of privacy: How many rows can an individual contribute?", | ||
default=1, | ||
help="How many rows can an individual contribute?", | ||
) | ||
return parser | ||
|
||
|
||
def main(): # pragma: no cover | ||
# We call parse_args() again inside the app. | ||
# We only call it here so "--help" is handled. | ||
# We only call it here so "--help" is handled, | ||
# and to validate inputs. | ||
get_arg_parser().parse_args() | ||
|
||
# run_app() depends on the CWD. | ||
os.chdir(Path(__file__).parent) | ||
|
||
run_app_kwargs = { | ||
"reload": True, | ||
} | ||
shiny.run_app(launch_browser=True, **run_app_kwargs) | ||
shiny.run_app( | ||
app="dp_creator_ii.app", | ||
launch_browser=True, | ||
reload=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
|
||
def plot_error_bars_with_cutoff( | ||
y_values, x_min_label="min", x_max_label="max", y_cutoff=0, y_error=0 | ||
): | ||
x_values = 0.5 + np.arange(len(y_values)) | ||
x_values_above = [] | ||
x_values_below = [] | ||
y_values_above = [] | ||
y_values_below = [] | ||
for x, y in zip(x_values, y_values): | ||
if y < y_cutoff: | ||
x_values_below.append(x) | ||
y_values_below.append(y) | ||
else: | ||
x_values_above.append(x) | ||
y_values_above.append(y) | ||
|
||
figure, axes = plt.subplots() | ||
color = "skyblue" | ||
shared = { | ||
"width": 0.8, | ||
"edgecolor": color, | ||
"linewidth": 1, | ||
"yerr": y_error, | ||
} | ||
axes.bar(x_values_above, y_values_above, color=color, **shared) | ||
axes.bar(x_values_below, y_values_below, color="white", **shared) | ||
axes.hlines([y_cutoff], 0, len(y_values), colors=["black"], linestyles=["dotted"]) | ||
|
||
axes.set(xlim=(0, len(y_values)), ylim=(0, max(y_values))) | ||
axes.get_xaxis().set_ticks( | ||
ticks=[x_values[0], x_values[-1]], | ||
labels=[x_min_label, x_max_label], | ||
) | ||
axes.get_yaxis().set_ticks([]) | ||
|
||
return figure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from htmltools.tags import details, summary | ||
from shiny import ui | ||
|
||
|
||
def output_code_sample(name_of_render_function): | ||
return details( | ||
summary("Code sample"), | ||
ui.output_code(name_of_render_function), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import csv | ||
|
||
|
||
def read_field_names(csv_path): | ||
with open(csv_path, newline="") as csv_handle: | ||
reader = csv.DictReader(csv_handle) | ||
return reader.fieldnames |
Oops, something went wrong.