Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Furthermore, Privugger is equipped with a module to perform *automatic attacker
tutorials/Tutorial.ipynb
tutorials/Open-dp-Tutorial.ipynb
tutorials/Governor.ipynb
tutorials/Duplicate.ipynb


.. toctree::
Expand Down
96 changes: 42 additions & 54 deletions docs/tutorials/Governor.ipynb

Large diffs are not rendered by default.

228 changes: 96 additions & 132 deletions docs/tutorials/Open-dp-Tutorial.ipynb

Large diffs are not rendered by default.

85 changes: 48 additions & 37 deletions docs/tutorials/Tutorial.ipynb

Large diffs are not rendered by default.

Binary file added docs/tutorials/__pycache__/C.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added docs/tutorials/__pycache__/avg.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added docs/tutorials/__pycache__/function.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added docs/tutorials/__pycache__/method.cpython-310.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/output.cpython-310.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/output.cpython-39.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/program.cpython-310.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/rand.cpython-310.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/randint.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added docs/tutorials/__pycache__/random.cpython-310.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/random.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added docs/tutorials/__pycache__/seed.cpython-310.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/seed.cpython-39.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/test.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added docs/tutorials/__pycache__/typed.cpython-310.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/typed.cpython-39.pyc
Binary file not shown.
Binary file added docs/tutorials/__pycache__/uniform.cpython-310.pyc
Binary file not shown.
7 changes: 7 additions & 0 deletions docs/tutorials/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import numpy as np
import pymc as pm
import aesara.tensor as at


def method(ages):
return ages.sum() / ages.size
12 changes: 12 additions & 0 deletions docs/tutorials/randint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import aesara
import aesara.tensor as at
import numpy as np


@aesara.compile.ops.as_op(itypes=[at.lscalar], otypes=[at.lscalar])
def method(arg0):

def randint(arg0):
y = np.random.randint(arg0)
return np.array(y)
return randint(arg0)
15 changes: 15 additions & 0 deletions docs/tutorials/randint_program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np
import pymc as pm
import aesara.tensor as at
import randint as ra


def method(SECRET):
X = SECRET
pm.Deterministic('lign1', X)
var20 = pm.DiracDelta('var20', np.int64(100))
Y = ra.method(var20)
pm.Deterministic('lign2', Y)
x = X + Y
pm.Deterministic('lign3', x)
return x
192 changes: 192 additions & 0 deletions docs/tutorials/test_multiple_deterministic.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pymc as pm\n",
"import os, sys\n",
"import random as rd\n",
"\n",
"sys.path.append(os.path.join(\"../../\"))\n",
"import privugger as pv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def programme(SECRET):\n",
" X = SECRET\n",
" Y = np.random.randint(100)\n",
" x = X + Y\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"SECRET = pv.Constant('SECRET', np.int64(42))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"ds = pv.Dataset(input_specs = [SECRET])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"program = pv.Program('randint_program', dataset=ds, output_type=pv.Int, function=programme)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Multiprocess sampling (2 chains in 4 jobs)\n",
"CompoundStep\n",
">Metropolis: [SECRET]\n",
">Metropolis: [var20]\n"
]
},
{
"data": {
"text/html": [
"\n",
"<style>\n",
" /* Turns off some styling */\n",
" progress {\n",
" /* gets rid of default border in Firefox and Opera. */\n",
" border: none;\n",
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
" background-size: auto;\n",
" }\n",
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
" }\n",
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
" background: #F44336;\n",
" }\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" <progress value='22000' class='' max='22000' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" 100.00% [22000/22000 00:01&lt;00:00 Sampling 2 chains, 0 divergences]\n",
" </div>\n",
" "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Sampling 2 chains for 1_000 tune and 10_000 draw iterations (2_000 + 20_000 draws total) took 5 seconds.\n"
]
}
],
"source": [
"trace = pv.infer(program, cores=4, draws=10000, method='pymc_whitebox', return_model=False, observation=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<xarray.Dataset>\n",
"Dimensions: (chain: 2, draw: 10000)\n",
"Coordinates:\n",
" * chain (chain) int64 0 1\n",
" * draw (draw) int64 0 1 2 3 4 5 ... 9994 9995 9996 9997 9998 9999\n",
"Data variables:\n",
" SECRET (chain, draw) int64 42 42 42 42 42 42 ... 42 42 42 42 42 42\n",
" var20 (chain, draw) int64 100 100 100 100 100 ... 100 100 100 100\n",
" lign1 (chain, draw) int64 42 42 42 42 42 42 ... 42 42 42 42 42 42\n",
" lign2 (chain, draw) int64 58 73 94 7 83 90 ... 5 45 89 25 70 70\n",
" lign3 (chain, draw) int64 100 115 136 49 125 ... 131 67 112 112\n",
" randint_program (chain, draw) int64 100 115 136 49 125 ... 131 67 112 112\n",
"Attributes:\n",
" created_at: 2022-08-26T13:47:31.467161\n",
" arviz_version: 0.12.1\n",
" inference_library: pymc\n",
" inference_library_version: 4.1.5\n",
" sampling_time: 5.286848068237305\n",
" tuning_steps: 1000\n"
]
}
],
"source": [
"print(trace.posterior)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.5 ('privugger_environment')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "ead01f34da02679ce3852bdbdb51f16b97d4b75626f761dacfb86d0793faee05"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added privugger/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added privugger/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
48 changes: 26 additions & 22 deletions privugger/data_structures/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def __init__(self, name, dataset, output_type, function):
self.program = function
self.observation = None
self.execute_observations = lambda a,b: None
elif dataset is None:
self.dataset = None
self.output_type = output_type
self.name = name
self.program = function
self.observation = None
self.execute_observations = lambda a,b: None
else:
raise ValueError("The dataset has to be of type privugger.Dataset")

Expand Down Expand Up @@ -84,28 +91,25 @@ def add_observation(self, constraints, precision=0.01):

partial1 = lambda x: None
partial2 = lambda x: None
if name in var_names or "output" in name:
if val1 != "" and cons1 != "":
v1 = float(val1) if "." in val1 else int(val1)
partial1 = self._unwrap_constrain(v1, cons1, precision)
print(partial1)

if val2 != "" and cons2 != "":
v2 = float(val2) if "." in val2 else int(val2)
partial2 = self._unwrap_constrain(v2, cons2, precision, i=1)

def inner(prior, output):
if name in var_names:
idx = var_names.index(name)
distribution = prior[idx]
else:
distribution = output
partial1(distribution)
partial2(distribution)
self.execute_observations = inner
return None # to avoid having a return value
else:
raise ValueError("Observation was not known. Make sure that the name is part of the names in privugger.Datastructure")
if val1 != "" and cons1 != "":
v1 = float(val1) if "." in val1 else int(val1)
partial1 = self._unwrap_constrain(v1, cons1, precision)
print(partial1)

if val2 != "" and cons2 != "":
v2 = float(val2) if "." in val2 else int(val2)
partial2 = self._unwrap_constrain(v2, cons2, precision, i=1)

def inner(prior, output):
if name in var_names:
idx = var_names.index(name)
distribution = prior[idx]
else:
distribution = output
partial1(distribution)
partial2(distribution)
self.execute_observations = inner
return None # to avoid having a return value


def _unwrap_constrain(self, value, cons, precision, i=0):
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 5 additions & 2 deletions privugger/transformer/PyMC3/type_decoration.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def lift(self, program, decorators):
file.write(res)
tree = ast.parse(open("temp.py").read())

#first_deterministic = ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id='pm', ctx=ast.Load()), attr='Deterministic', ctx=ast.Load()), args=[ast.Constant(value='rd_countries'), ast.Name(id='rd_countries', ctx=ast.Load())], keywords=[]))
#tree.body[0].body.insert(6, first_deterministic)

os.remove("temp.py")

#if("lambda")
Expand Down Expand Up @@ -144,7 +147,7 @@ def translate_type(self, p_type):
return 'dscalar'

elif(p_type == 'int'):
return 'wscalar'
return 'lscalar'

elif(p_type == 'VectorI'):
return 'lvector'
Expand Down Expand Up @@ -415,7 +418,7 @@ def wrap_with_theano_import(self, program):
aesara_import = ast.Import(names=[ast.alias(name='aesara', asname=None)])
aesara_tensor_import = ast.Import(names=[ast.alias(name='aesara.tensor', asname='at')])
numpy_import = ast.Import(names=[ast.alias(name='numpy', asname='np')])
new_program = ast.Module(body=[aesara_import, aesara_tensor_import,numpy_import, program])
new_program = ast.Module(body=[aesara_import, aesara_tensor_import, numpy_import, program])

return new_program

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading