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

Exercise context #15

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions .github/workflows/smoke-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ jobs:
run: python -m pip install -e .

- name: (Re)install opendp
run: python -m pip install opendp${{ matrix.opendp-version }}
run: python -m pip install 'opendp${{ matrix.opendp-version || '[polars]' }}'

- name: Test
# OpenDP doesn't have pretty pretty reprs in older versions,
# so skip the doc test in that case.
run: coverage run -m pytest -v ${{ matrix.opendp-version && '-k "not README"' || '' }}
run: coverage run -m pytest -v ${{ matrix.opendp-version && '-k "not README and not context"' || '' }}

- name: Check coverage
run: coverage report
File renamed without changes.
41 changes: 41 additions & 0 deletions test/test_context_roundtrip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from opendp_logger import enable_logging, make_load_json
from random import randint
import pytest

enable_logging()

import opendp.prelude as dp

dp.enable_features("contrib")


def test_context():
privacy_unit = dp.unit_of(contributions=1)
privacy_loss = dp.loss_of(epsilon=1.)
bounds = (0.0, 100.0)
imputed_value = 50.0
data = [float(randint(0, 100)) for _ in range(100)]
context = dp.Context.compositor(
data=data,
privacy_unit=privacy_unit,
privacy_loss=privacy_loss,
split_evenly_over=3
)
count_query = (
context.query()
.count()
.laplace()
)
serialized_query = count_query.resolve().to_json()

# TODO:
# def trans_shift_outer(lhs: dp.Transformation, rhs):
# chain = trans_shift_inner(lhs, rhs)
# if isinstance(rhs, dp.PartialConstructor):
# > chain.log = {"_type": "partial_chain", "lhs": lhs.log, "rhs": rhs.log}
# E AttributeError: 'PartialConstructor' object has no attribute 'log'

count_query_again = make_load_json(serialized_query)
assert count_query_again.param() == ...
assert count_query_again.release() == ...

Loading