Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
28 changes: 26 additions & 2 deletions pydp/algorithms/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ def add_entry(self, value):
"""
return self.__algorithm.add_entry(value)

def quick_result(self, list):
def quick_result(self, data):
"""
Runs the algorithm on the input using the epsilon parameter provided in the constructor and returns output.

Consumes 100% of the privacy budget.
"""
return self.__algorithm.result(list)
return self.__algorithm.result(data)

def result(self, privacy_budget=None, noise_interval_level=None):
"""
Expand Down Expand Up @@ -159,3 +159,27 @@ def __init__(
linf_sensitivity=linf_sensitivity,
dtype=dtype,
)


class PercentileBase(MetaAlgorithm):
def __init__(
self,
epsilon=1.0,
percentile=0.0,
lower_bound=None,
upper_bound=None,
dtype="int",
):
super().__init__(
epsilon=epsilon,
percentile=percentile,
lower_bound=lower_bound,
upper_bound=upper_bound,
dtype=dtype,
)

@property
def percentile(self):
"""percentile Gets the value that was set in the constructor.
"""
return self._MetaAlgorithm__algorithm.percentile
9 changes: 8 additions & 1 deletion pydp/algorithms/laplacian/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
BoundedSum,
BoundedStandardDeviation,
BoundedVariance,
Max,
Min,
Median,
)
from .count import Count

from .percentile import Percentile

__all__ = [
"BoundedMean",
"BoundedStandardDeviation",
"BoundedSum",
"BoundedVariance",
"Count",
"Max",
"Min",
"Median",
"Percentile",
]
12 changes: 12 additions & 0 deletions pydp/algorithms/laplacian/bounded_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ class BoundedStandardDeviation(BoundedAlgorithm):

class BoundedVariance(BoundedAlgorithm):
pass


class Max(BoundedAlgorithm):
pass


class Min(BoundedAlgorithm):
pass


class Median(BoundedAlgorithm):
pass
5 changes: 5 additions & 0 deletions pydp/algorithms/laplacian/percentile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ..algorithm import BoundedAlgorithm, PercentileBase


class Percentile(PercentileBase):
pass
4 changes: 1 addition & 3 deletions src/bindings/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pybind_extension(
"PyDP/algorithms/*.cpp",
"PyDP/algorithms/*.cpp",
"PyDP/pydp_lib/*.hpp",
"PyDP/proto/*.cpp",
"c/*.cc",
"c/*.h"
"PyDP/proto/*.cpp"
]),

visibility = ["//src/python:__pkg__"],
Expand Down
129 changes: 17 additions & 112 deletions src/bindings/PyDP/algorithms/order_statistics.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Provides bindings for Bounded Functions

#include "../../c/c_api.h"

#include "../pydp_lib/casting.hpp" // our caster helper library
#include "../pydp_lib/helper_class.hpp" // Dummy helder class

// Provides bindings for Order Statistics
#include "../pydp_lib/algorithm_builder.hpp"
#include "algorithms/order-statistics.h"
#include "pybind11/complex.h"
#include "pybind11/functional.h"
#include "pybind11/pybind11.h"
Expand All @@ -13,115 +9,24 @@
using namespace std;

namespace py = pybind11;
namespace dp = differential_privacy;

class MaxDummy : public Dummy {
public:
using Dummy::Dummy;

double Result(py::list l, double privacy_budget) override {
return Result_Max(obj, l, privacy_budget);
}
};

class MinDummy : public Dummy {
public:
using Dummy::Dummy;

double Result(py::list l, double privacy_budget) override {
return Result_Min(obj, l, privacy_budget);
}
};

class MedianDummy : public Dummy {
public:
using Dummy::Dummy;

double Result(py::list l, double privacy_budget) override {
return Result_Median(obj, l, privacy_budget);
}
};

class PercentileDummy : public Dummy {
public:
using Dummy::Dummy;

void setPercentile(double percentile) {
_percentile = percentile;
}

double getPercentile() {
return _percentile;
}

double Result(py::list l, double privacy_budget) override {
return Result_Percentile(obj, l, privacy_budget, _percentile);
}

private:
double _percentile = 0.45;
};

void declareMax(py::module& m) {
py::class_<MaxDummy> bld(m, "Max");
bld.attr("__module__") = "pydp";
bld.def(py::init<double, int, int>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def(py::init<double>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def("result", &MaxDummy::Result);
bld.def_property("l0_sensitivity", &MaxDummy::get_l0_sensitivity,
&MaxDummy::set_l0_sensitivity);
bld.def_property("linf_sensitivity", &MaxDummy::get_linf_sensitivity,
&MaxDummy::set_linf_sensitivity);
template <typename T, class OrderStat>
void declareOrderStat(py::module& m) {
using builder = typename dp::python::AlgorithmBuilder<T, OrderStat>;
builder().declare(m);
}

void declareMin(py::module& m) {
py::class_<MinDummy> bld(m, "Min");
bld.attr("__module__") = "pydp";
bld.def(py::init<double, int, int>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def(py::init<double>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def("result", &MinDummy::Result);
bld.def_property("l0_sensitivity", &MinDummy::get_l0_sensitivity,
&MinDummy::set_l0_sensitivity);
bld.def_property("linf_sensitivity", &MinDummy::get_linf_sensitivity,
&MinDummy::set_linf_sensitivity);
}
void init_algorithms_order_statistics(py::module& m) {
declareOrderStat<int64_t, dp::continuous::Max<int64_t>>(m);
declareOrderStat<double, dp::continuous::Max<double>>(m);

void declareMedian(py::module& m) {
py::class_<MedianDummy> bld(m, "Median");
bld.attr("__module__") = "pydp";
bld.def(py::init<double, int, int>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def(py::init<double>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def("result", &MedianDummy::Result);
bld.def_property("l0_sensitivity", &MedianDummy::get_l0_sensitivity,
&MedianDummy::set_l0_sensitivity);
bld.def_property("linf_sensitivity", &MedianDummy::get_linf_sensitivity,
&MedianDummy::set_linf_sensitivity);
}
declareOrderStat<int64_t, dp::continuous::Min<int64_t>>(m);
declareOrderStat<double, dp::continuous::Min<double>>(m);

void declarePercentile(py::module& m) {
py::class_<PercentileDummy> bld(m, "Percentile");
bld.attr("__module__") = "pydp";
bld.def(py::init<double, int, int>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def(py::init<double>(), py::return_value_policy::reference,
py::call_guard<pybind11::gil_scoped_release>());
bld.def("result", &PercentileDummy::Result);
bld.def_property("percentile", &PercentileDummy::getPercentile,
&PercentileDummy::setPercentile);
bld.def_property("l0_sensitivity", &PercentileDummy::get_l0_sensitivity,
&PercentileDummy::set_l0_sensitivity);
bld.def_property("linf_sensitivity", &PercentileDummy::get_linf_sensitivity,
&PercentileDummy::set_linf_sensitivity);
}
declareOrderStat<int64_t, dp::continuous::Median<int64_t>>(m);
declareOrderStat<double, dp::continuous::Median<double>>(m);

void init_algorithms_order_statistics(py::module& m) {
declareMax(m);
declareMin(m);
declareMedian(m);
declarePercentile(m);
declareOrderStat<int64_t, dp::continuous::Percentile<int64_t>>(m);
declareOrderStat<double, dp::continuous::Percentile<double>>(m);
}
28 changes: 0 additions & 28 deletions src/bindings/PyDP/base/percentile.cpp

This file was deleted.

6 changes: 3 additions & 3 deletions src/bindings/PyDP/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace py = pybind11;
// base
void init_base_status(py::module &);
void init_base_logging(py::module &);
void init_base_percentile(py::module &);
// void init_base_percentile(py::module &);

// bounded functions
void init_algorithms_bounded_functions(py::module &);
Expand All @@ -34,14 +34,14 @@ PYBIND11_MODULE(_pydp, m) {
// Base
init_base_status(m);
init_base_logging(m);
init_base_percentile(m);
// init_base_percentile(m);

// Algorithms
auto malgorithms = m.def_submodule("_algorithms");
init_algorithms_bounded_functions(malgorithms);
init_algorithms_util(m);
init_algorithms_distributions(m);
init_algorithms_order_statistics(m);
init_algorithms_order_statistics(malgorithms);
init_algorithms_rand(m);
init_algorithms_count(malgorithms);

Expand Down
Loading