-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use configure session in examples; update spark example to use apply_…
…policy
- Loading branch information
Showing
4 changed files
with
14 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |