Skip to content

Commit 43ca6f2

Browse files
Optimize test_bounded_mean test execution (#333)
* added installation for clang-format in docker file * moved pydp folver under src * moved pydp folver under src * moved pydp folver under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * moved pydp folder under src * Explicitly tell pybind which python to use * Fix CI tests * Enable protobuf serde for testing * Fix style errors * removed bin files
1 parent b423675 commit 43ca6f2

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
make build
5050
- name: Build PyDP with coverage (Windows)
5151
if: runner.os == 'Windows'
52+
# There seems to be an issue with poetry and windows in this case. We need to
53+
# install again to make pydp importable.
54+
# Related ref: https://github.com/python-poetry/poetry/issues/3075
5255
run: |
5356
bazel clean --expunge
5457

src/bindings/PyDP/proto/proto.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <fstream>
12
#include <string>
23

34
#include "../pydp_lib/casting.hpp" // our caster helper library
@@ -15,7 +16,18 @@ namespace dp = differential_privacy;
1516

1617
void init_proto(py::module &m) {
1718
py::class_<dp::Output> output(m, "Output");
18-
py::class_<dp::Summary>(m, "Summary");
19-
2019
output.def(py::init());
20+
21+
// Add save and load functions to use protobuf serde in tests
22+
py::class_<dp::Summary>(m, "Summary")
23+
.def(py::init())
24+
.def("save",
25+
[](dp::Summary &pythis, std::string &filename) {
26+
fstream output(filename, ios::out | ios::trunc | ios::binary);
27+
pythis.SerializeToOstream(&output);
28+
})
29+
.def("load", [](dp::Summary &pythis, std::string &filename) {
30+
fstream input(filename, ios::in | ios::binary);
31+
pythis.ParseFromIstream(&input);
32+
});
2133
}

tests/algorithms/test_bounded_mean.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
12
import pytest
23
from pydp.algorithms.laplacian import BoundedMean
4+
from pydp._pydp import Summary
5+
36

47
expect_near = lambda expected, actual, tol: (
58
expected + tol >= actual and expected - tol <= actual
@@ -25,12 +28,42 @@ def test_bounded_mean():
2528
# assert isinstance(bm2.quick_result([1.5, 2, 2.5]), float)
2629

2730

28-
def test_bounded_mean_int64():
29-
example_list = [5] * 100000000
30-
x = BoundedMean(1.0, 0, 10, dtype="int64")
31-
for _ in range(5):
32-
x.add_entries(example_list)
31+
@pytest.fixture(scope="function")
32+
def make_loaded_object(request):
33+
dir_path = os.path.dirname(os.path.realpath(__file__))
34+
35+
def _make_loaded_object(value, size, iter):
36+
dump_filepath = os.path.join(
37+
dir_path,
38+
request.module.__name__,
39+
"{}_data.proto".format(request.function.__name__),
40+
)
41+
42+
# Algorithm to initialize
43+
x = BoundedMean(1.0, 0, 10, dtype="int64")
44+
45+
if os.path.exists(dump_filepath):
46+
# Search for data dump to import
47+
data = Summary()
48+
data.load(dump_filepath)
49+
x.merge(data)
50+
else:
51+
# No data dump found, we have to init the alforithm from scratch
52+
# Add entries into algorithm
53+
example_list = [value] * size
54+
for _ in range(iter):
55+
x.add_entries(example_list)
56+
57+
# Dump the initialized algorithm data to retrieve in future tests
58+
x.serialize().save(dump_filepath)
59+
60+
return x
61+
62+
return _make_loaded_object
63+
3364

65+
def test_bounded_mean_int64(make_loaded_object):
66+
x = make_loaded_object(5, 100000000, 5)
3467
assert expect_near(5.0, x.result(), 0.1)
3568

3669

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
M
2+
;type.googleapis.com/differential_privacy.BoundedMeanSummary€Êµî€ò‹¨

0 commit comments

Comments
 (0)