Skip to content

Commit

Permalink
use configure session in examples; update spark example to use apply_…
Browse files Browse the repository at this point in the history
…policy
  • Loading branch information
jvmncs committed Jun 23, 2020
1 parent 37ab55b commit a04440a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/perturb_value_field.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
label: perturb-value-field
label: perturb-ones-field
version: 1
rules:
- match:
name: value
name: ones
actions:
- transform:
type: numeric-perturbation
Expand Down
8 changes: 3 additions & 5 deletions examples/simple_transformation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import cape_privacy as cape
import pandas as pd
import numpy as np

import cape_privacy as cape

df = pd.DataFrame(np.ones(5,), columns=["value"])

policy = cape.parse_policy("perturb_value_field.yaml")
df = cape.apply_policy(policy, df)

df = pd.DataFrame(np.ones(5,), columns=["ones"])
df = cape.apply_policy(policy, df)
print(df.head())
20 changes: 6 additions & 14 deletions examples/spark_example.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import cape_privacy as cape
import numpy as np
import pandas as pd
from pyspark import sql
from pyspark.sql import functions

import cape_privacy as cape
from cape_privacy.spark import transformations as tfms
from cape_privacy.spark import dtypes

sess_builder = sql.SparkSession.builder
sess_builder = sess_builder.appName('cape.examples.rounding')
sess_builder = sess_builder.config('spark.sql.execution.arrow.enabled', 'true')
sess_builder = sess_builder.appName("cape.examples.rounding")
sess = sess_builder.getOrCreate()
sess = cape.spark.configure_session(sess)

pdf = pd.DataFrame(np.ones(5, dtype=np.float32) + .2, columns=["value"])
pdf = pd.DataFrame(np.ones(5, dtype=np.float32) + 0.2, columns=["ones"])
df = sess.createDataFrame(pdf)
df.show()

policy = cape.parse_policy("spark_round.yaml")
rounding_spec = policy.transformations[0]
type_arg = getattr(dtypes, rounding_spec.args['dtype'])
rounding_spec.args['dtype'] = type_arg
rounder = tfms.NumericRounding(**rounding_spec.args)
df = df.select(rounder(functions.col('value')))
df.show()
result = cape.apply_policy(policy, df)
result.show()
6 changes: 3 additions & 3 deletions examples/spark_round.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
label: spark-round-float
version: 1
transformations:
- name: roundToZero
type: roundNumeric
- name: roundFloat
type: numeric-rounding
dtype: Float
precision: 0
rules:
- match:
name: value
name: ones
actions:
- transform:
name: roundFloat

0 comments on commit a04440a

Please sign in to comment.