testutils

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2023 License: Apache-2.0, BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package testutils provides helper functions, structs, etc. for testing Privacy on Beam pipelines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApproxEquals

func ApproxEquals(x, y float64) bool

ApproxEquals returns true if x and y are approximately equal within a tolerance of 1e-10.

func ApproxEqualsKVFloat64

func ApproxEqualsKVFloat64(s beam.Scope, col1, col2 beam.PCollection, tolerance float64) error

ApproxEqualsKVFloat64 checks that two PCollections col1 and col2 of type <K,float64> are approximately equal, where "approximately equal" means "the keys are the same in both col1 and col2, and the value associated with key k in col1 is within the specified tolerance of the value associated with k in col2". Each key can only hold a single value.

func ApproxEqualsKVFloat64Slice

func ApproxEqualsKVFloat64Slice(s beam.Scope, col1, col2 beam.PCollection, tolerance float64) error

ApproxEqualsKVFloat64Slice checks that two PCollections col1 and col2 of type <K,[]float64> are approximately equal, where "approximately equal" means "the keys are the same in both col1 and col2, and each value in the slice associated with key k in col1 is within the specified tolerance of each value in the slice associated with k in col2". Each key can only hold a single slice.

func ApproxEqualsKVInt64

func ApproxEqualsKVInt64(s beam.Scope, col1, col2 beam.PCollection, tolerance float64) error

ApproxEqualsKVInt64 checks that two PCollections col1 and col2 of type <K,int64> are approximately equal, where "approximately equal" means "the keys are the same in both col1 and col2, and the value associated with key k in col1 is within the specified tolerance of the value associated with k in col2". Each key can only hold a single value.

func CheckAllValuesNegativeFloat64Fn

func CheckAllValuesNegativeFloat64Fn(v float64) error

CheckAllValuesNegativeFloat64Fn returns an error if an float64 value is non-negative.

func CheckFloat64MetricsAreNoisy

func CheckFloat64MetricsAreNoisy(s beam.Scope, col beam.PCollection, exactMetric, tolerance float64)

CheckFloat64MetricsAreNoisy checks that no values in a PCollection<testFloat64Metric> (where testFloat64Metric contains the aggregate statistic) is equal to exactMetric.

func CheckInt64MetricsAreNoisy

func CheckInt64MetricsAreNoisy(s beam.Scope, col beam.PCollection, exactMetric int, tolerance float64)

CheckInt64MetricsAreNoisy checks that no values in a PCollection<testInt64Metric> (where testInt64Metric contains the aggregate statistic) is equal to exactMetric.

func CheckNoNegativeValuesFloat64Fn

func CheckNoNegativeValuesFloat64Fn(v float64) error

CheckNoNegativeValuesFloat64Fn returns an error if an float64 value is negative.

func CheckNoNegativeValuesInt64Fn

func CheckNoNegativeValuesInt64Fn(v int64) error

CheckNoNegativeValuesInt64Fn returns an error if an int64 value is negative.

func CheckNumPartitions

func CheckNumPartitions(s beam.Scope, col beam.PCollection, expected int)

CheckNumPartitions checks that col has expected number of partitions.

func CheckSomePartitionsAreDropped

func CheckSomePartitionsAreDropped(s beam.Scope, col beam.PCollection, numPartitions int)

CheckSomePartitionsAreDropped checks that the number of values in the PCollection is smaller than numPartitions, but larger than 0.

func ComplementaryGaussianTolerance

func ComplementaryGaussianTolerance(flakinessK, l0Sensitivity, lInfSensitivity, epsilon, delta float64) float64

ComplementaryGaussianTolerance returns tolerance to be used in checkMetricsAreNoisy for tests with Gaussian Noise to pass with 10⁻ᵏ flakiness. flakinessK is the parameter used to specify this.

l0Sensitivity, lInfSensitivity, epsilon and delta are the DP parameters of the test.

To see the logic and the math behind flakiness and tolerance calculation, See https://github.com/google/differential-privacy/blob/main/privacy-on-beam/docs/Tolerance_Calculation.pdf

func ComplementaryLaplaceTolerance

func ComplementaryLaplaceTolerance(flakinessK, l1Sensitivity, epsilon float64) float64

ComplementaryLaplaceTolerance returns tolerance to be used in checkMetricsAreNoisy for tests with Laplace Noise to pass with 10⁻ᵏ flakiness. flakinessK is the parameter used to specify this.

l1Sensitivity, epsilon and delta are the DP parameters of the test.

To see the logic and the math behind flakiness and tolerance calculation, See https://github.com/google/differential-privacy/blob/main/privacy-on-beam/docs/Tolerance_Calculation.pdf

func DereferenceFloat64Slice

func DereferenceFloat64Slice(v beam.V, r []float64) (beam.V, float64, error)

DereferenceFloat64Slice returns the first and only element of the slice for each key in a PCollection<K, []float64>. Returns an error if the slice does not contain exactly 1 element.

func EqualsKVFloat64

func EqualsKVFloat64(s beam.Scope, col1, col2 beam.PCollection) error

EqualsKVFloat64 checks that two PCollections col1 and col2 of type <K,float64> are exactly equal. Each key can only hold a single value.

func EqualsKVInt

func EqualsKVInt(s beam.Scope, col1, col2 beam.PCollection) error

EqualsKVInt checks that two PCollections col1 and col2 of type <K,int> are exactly equal.

func EqualsKVInt64

func EqualsKVInt64(s beam.Scope, col1, col2 beam.PCollection) error

EqualsKVInt64 checks that two PCollections col1 and col2 of type <K,int64> are exactly equal. Each key can only hold a single value.

func Float64MetricToKV

func Float64MetricToKV(tm TestFloat64Metric) (int, float64)

Float64MetricToKV transforms a TestFloat64Metric into an (int, float64) key-value pair.

func Float64Ptr

func Float64Ptr(f float64) *float64

Float64Ptr transforms a float64 into a *float64.

func Float64SliceMetricToKV

func Float64SliceMetricToKV(tm TestFloat64SliceMetric) (int, []float64)

Float64SliceMetricToKV transforms a TestFloat64SliceMetric into an (int, []float64) key-value pair.

func GaussianTolerance

func GaussianTolerance(flakinessK, l0Sensitivity, lInfSensitivity, epsilon, delta float64) float64

GaussianTolerance returns tolerance to be used in approxEquals or in threshold computations for tests with Gaussian Noise to pass with 10⁻ᵏ flakiness. flakinessK is the parameter used to specify this.

l0Sensitivity, lInfSensitivity, epsilon and delta are the DP parameters of the test.

To see the logic and the math behind flakiness and tolerance calculation, See https://github.com/google/differential-privacy/blob/main/privacy-on-beam/docs/Tolerance_Calculation.pdf

func Int64MetricToKV

func Int64MetricToKV(tm TestInt64Metric) (int, int64)

Int64MetricToKV transforms a TestInt64Metric into an (int, int64) key-value pair.

func Int64Ptr

func Int64Ptr(i int64) *int64

Int64Ptr transforms an int64 into an *int64.

func LaplaceTolerance

func LaplaceTolerance(flakinessK, l1Sensitivity, epsilon float64) float64

LaplaceTolerance returns tolerance to be used in approxEquals or in threshold computations for tests with Laplace Noise to pass with 10⁻ᵏ flakiness. flakinessK is the parameter used to specify this.

l1Sensitivity and epsilon are the DP parameters of the test.

To see the logic and the math behind flakiness and tolerance calculation, See https://github.com/google/differential-privacy/blob/main/privacy-on-beam/docs/Tolerance_Calculation.pdf.

func LaplaceToleranceForMean

func LaplaceToleranceForMean(flakinessK, lower, upper float64, maxContributionsPerPartition, maxPartitionsContributed int64, epsilon float64, exactNormalizedSum, exactCount, exactMean float64) (float64, error)

LaplaceToleranceForMean returns tolerance to be used in approxEquals for tests for mean to pass with 10⁻ᵏ flakiness.

flakinessK is the parameter used to specify k in the flakiness. distanceFromMidPoint = upper - midPoint, where midPoint = (lower + upper)/2. exactNormalizedSum is a clamped (with boundaries -distanceFromMidPoint and distanceFromMidPoint) sum of distances of the input entities from the midPoint. exactNormalizedSum is needed for calculating tolerance because the algorithm of the mean aggregation uses noisy normalized sum in its calculations.

To see the logic and the math behind flakiness and tolerance calculation, See https://github.com/google/differential-privacy/blob/main/privacy-on-beam/docs/Tolerance_Calculation.pdf

func LessThanOrEqualToKVFloat64

func LessThanOrEqualToKVFloat64(s beam.Scope, col1, col2 beam.PCollection) error

LessThanOrEqualToKVFloat64 checks that for PCollections col1 and col2 of type <K,float64>, for each key k, value corresponding to col1 is less than or equal to the value corresponding in col2. Each key can only hold a single value.

func NotEqualsFloat64

func NotEqualsFloat64(s beam.Scope, col1, col2 beam.PCollection) error

NotEqualsFloat64 checks that two PCollections col1 and col2 of type <K,float64> are different. Each key can only hold a single value.

func OneFn

func OneFn(beam.V) int

OneFn always returns 1.

func PairICodedKVToKV

func PairICodedKVToKV(p PairICodedKV) (k int, v kv.Pair)

PairICodedKVToKV transforms a PairICodedKV into an (int, kv.Pair) key-value pair.

func PairIFToKV

func PairIFToKV(p PairIF64) (a int, b float64)

PairIFToKV transforms a PairIF64 into an (int, float64) key-value pair.

func PairII64ToKV

func PairII64ToKV(p PairII64) (a int, b int64)

PairII64ToKV transforms a PairII64 into an (int, int64) key-value pair.

func PairToKV

func PairToKV(p PairII) (a, b int)

PairToKV transforms a PairII into an (int, int) key-value pair.

func QuantilesTolerance

func QuantilesTolerance(lower, upper float64) float64

QuantilesTolerance returns tolerance to be used in approxEquals for tests for quantiles to pass with negligible flakiness.

When no noise is added, the quantiles should return a value that differs from the true quantile by no more than the size of the buckets the range is partitioned into, i.e., (upper - lower) / (branchingFactor^treeHeight - 1).

The tests don't disable noise, hence we multiply the tolerance by a reasonably small number, in this case 5, to account for the noise addition.

func RoundedLaplaceTolerance

func RoundedLaplaceTolerance(flakinessK, l1Sensitivity, epsilon float64) float64

RoundedLaplaceTolerance rounds laplace tolerance value to the nearest integer, in order to work with tests for integer-valued aggregations.

To see the logic and the math behind flakiness and tolerance calculation, See https://github.com/google/differential-privacy/blob/main/privacy-on-beam/docs/Tolerance_Calculation.pdf

func ToleranceForMean

func ToleranceForMean(lower, upper, exactNormalizedSum, exactCount, exactMean, countTolerance, normalizedSumTolerance float64) (float64, error)

ToleranceForMean returns tolerance to be used in approxEquals or checkMetricsAreNoisy for tests for mean to pass with 10⁻ᵏ flakiness.

flakinessK is the parameter used to specify k in the flakiness. distanceFromMidPoint = upper - midPoint, where midPoint = (lower + upper)/2. exactNormalizedSum is a clamped (with boundaries -distanceFromMidPoint and distanceFromMidPoint) sum of distances of the input entities from the midPoint. exactNormalizedSum is needed for calculating tolerance because the algorithm of the mean aggregation uses noisy normalized sum in its calculations.

To see the logic and the math behind flakiness and tolerance calculation, see https://github.com/google/differential-privacy/blob/main/privacy-on-beam/docs/Tolerance_Calculation.pdf.

func TripleWithFloatValueToKV

func TripleWithFloatValueToKV(t TripleWithFloatValue) (int, float32)

TripleWithFloatValueToKV extracts the partition ID and the value from a tripleWithFloatValue. It is used once the PrivatePCollection has been initialized, to transform it into a PrivatePCollection<partitionID,value>.

func TripleWithIntValueToKV

func TripleWithIntValueToKV(t TripleWithIntValue) (int, int)

TripleWithIntValueToKV extracts the partition ID and the value from a tripleWithIntValue. It is used once the PrivatePCollection has been initialized, to transform it into a PrivatePCollection<partitionID,value>.

Types

type PairICodedKV

type PairICodedKV struct {
	A int
	B kv.Pair
}

PairICodedKV holds a key-value pair of type (int, kv.Pair).

func KVToPairICodedKV

func KVToPairICodedKV(a int, b kv.Pair) PairICodedKV

KVToPairICodedKV transforms an (int, kv.Pair) key-value pair into a PairICodedKV.

type PairIF64

type PairIF64 struct {
	A int
	B float64
}

PairIF64 holds a key-value pair of type (int, float64).

type PairII

type PairII struct {
	A int
	B int
}

PairII holds a key-value pair of type (int, int).

func ConcatenatePairs

func ConcatenatePairs(slices ...[]PairII) []PairII

ConcatenatePairs concatenates pairII slices.

func KVToPair

func KVToPair(a, b int) PairII

KVToPair transforms an (int, int) key-value pair into a PairII.

func MakePairsWithFixedV

func MakePairsWithFixedV(numKeys, v int) []PairII

MakePairsWithFixedV returns sample data where the same value is associated with multiple privacy keys: it returns a slice of pairs {0, v}, {1, v}, ..., {numKeys-1, v}.

func MakePairsWithFixedVStartingFromKey

func MakePairsWithFixedVStartingFromKey(kOffset, numKeys, v int) []PairII

MakePairsWithFixedVStartingFromKey returns sample data where the same value is associated with multiple privacy keys: it returns a slice of pairs {0, v}, {1, v}, ..., {numKeys-1, v}. Privacy keys start from kOffset.

type PairII64

type PairII64 struct {
	A int
	B int64
}

PairII64 holds a key-value pair of type (int, int64).

type TestFloat64Metric

type TestFloat64Metric struct {
	Value  int
	Metric float64
}

TestFloat64Metric holds a Value and an associated float64 metric (aggregation).

func KVToFloat64Metric

func KVToFloat64Metric(v int, m float64) TestFloat64Metric

KVToFloat64Metric transforms an (int, float64) key-value pair into a TestFloat64Metric.

type TestFloat64SliceMetric

type TestFloat64SliceMetric struct {
	Value  int
	Metric []float64
}

TestFloat64SliceMetric holds a Value and an associated []float64 metric (aggregation).

func KVToFloat64SliceMetric

func KVToFloat64SliceMetric(v int, m []float64) TestFloat64SliceMetric

KVToFloat64SliceMetric transforms an (int, []float64) key-value pair into a TestFloat64SliceMetric.

type TestInt64Metric

type TestInt64Metric struct {
	Value  int
	Metric int64
}

TestInt64Metric holds a Value and an associated int64 metric (aggregation).

func Float64MetricToInt64Metric

func Float64MetricToInt64Metric(tm TestFloat64Metric) TestInt64Metric

Float64MetricToInt64Metric transforms a TestFloat64Metric into a TestInt64Metric.

func KVToInt64Metric

func KVToInt64Metric(v int, m int64) TestInt64Metric

KVToInt64Metric transforms an (int, int64) key-value pair into a TestInt64Metric.

type TripleWithFloatValue

type TripleWithFloatValue struct {
	ID        int
	Partition int
	Value     float32
}

TripleWithFloatValue contains a privacy ID, a partition ID, and a float value.

func ConcatenateTriplesWithFloatValue

func ConcatenateTriplesWithFloatValue(slices ...[]TripleWithFloatValue) []TripleWithFloatValue

ConcatenateTriplesWithFloatValue concatenates tripleWithFloatValue slices.

func ExtractIDFromTripleWithFloatValue

func ExtractIDFromTripleWithFloatValue(t TripleWithFloatValue) (int, TripleWithFloatValue)

ExtractIDFromTripleWithFloatValue extracts and returns the ID from a tripleWithFloatValue. It is used to initialize PrivatePCollections.

func MakeSampleTripleWithFloatValue

func MakeSampleTripleWithFloatValue(numKeys, p int) []TripleWithFloatValue

MakeSampleTripleWithFloatValue returns sample float data where the same partition ID is associated with multiple privacy keys, every time with the value 1.0: it returns a slice of tripleFloats {0,p,1}, {1,p,1}, ..., {numKeys-1,p,1}.

func MakeTripleWithFloatValue

func MakeTripleWithFloatValue(numKeys, p int, v float32) []TripleWithFloatValue

MakeTripleWithFloatValue returns float data where the same partition ID is associated with multiple privacy keys, to the given value v: it returns a slice of tripleInts {0,p,v}, {1,p,v}, ..., {numKeys-1,p,v}.

func MakeTripleWithFloatValueStartingFromKey

func MakeTripleWithFloatValueStartingFromKey(kOffset, numKeys, p int, v float32) []TripleWithFloatValue

MakeTripleWithFloatValueStartingFromKey returns float data where the same partition ID is associated with multiple privacy keys (starting from provided key), to the given value v: it returns a slice of tripleFloats {kOffset,p,v}, {kOffset + 1,p,v}, ..., {numKeys + kOffset - 1,p,v}. Privacy keys start from kOffset.

type TripleWithIntValue

type TripleWithIntValue struct {
	ID        int
	Partition int
	Value     int
}

TripleWithIntValue contains a privacy ID, a partition ID, and an int value.

func ConcatenateTriplesWithIntValue

func ConcatenateTriplesWithIntValue(slices ...[]TripleWithIntValue) []TripleWithIntValue

ConcatenateTriplesWithIntValue concatenates tripleWithIntValue slices.

func ExtractIDFromTripleWithIntValue

func ExtractIDFromTripleWithIntValue(t TripleWithIntValue) (int, TripleWithIntValue)

ExtractIDFromTripleWithIntValue extracts and returns the ID from a tripleWithIntValue. It is used to initialize PrivatePCollections.

func MakeSampleTripleWithIntValue

func MakeSampleTripleWithIntValue(numKeys, p int) []TripleWithIntValue

MakeSampleTripleWithIntValue returns sample int data where the same partition ID is associated with multiple privacy keys, every time with the value 1: it returns a slice of tripleInts {0,p,1}, {1,p,1}, ..., {numKeys-1,p,1}.

func MakeTripleWithIntValue

func MakeTripleWithIntValue(numKeys, p, v int) []TripleWithIntValue

MakeTripleWithIntValue returns int data where the same partition ID is associated with multiple privacy keys, to the given value v: it returns a slice of tripleInts {0,p,v}, {1,p,v}, ..., {numKeys-1,p,v}.

func MakeTripleWithIntValueStartingFromKey

func MakeTripleWithIntValueStartingFromKey(kOffset, numKeys, p, v int) []TripleWithIntValue

MakeTripleWithIntValueStartingFromKey returns int data where the same partition ID is associated with multiple privacy keys (starting from provided key), to the given value v: it returns a slice of tripleInts {kOffset,p,v}, {kOffset + 1,p,v}, ..., {numKeys + kOffset - 1,p,v}. Privacy keys start from kOffset.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL