testutils

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: Apache-2.0, BSD-3-Clause Imports: 15 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(t *testing.T, s beam.Scope, col1, col2 beam.PCollection, tolerance float64)

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(t *testing.T, s beam.Scope, col1, col2 beam.PCollection, tolerance float64)

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(t *testing.T, s beam.Scope, col1, col2 beam.PCollection, tolerance float64)

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 CheckAllValuesNegativeFloat64

func CheckAllValuesNegativeFloat64(v float64) error

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

func CheckApproxNumPartitions

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

CheckApproxNumPartitions checks that col has approximately expected number of partitions. col is allowed to have number of partitions within tolerance of expected.

func CheckAtLeastOneValueNegativeInt64

func CheckAtLeastOneValueNegativeInt64(s beam.Scope, col beam.PCollection)

CheckAtLeastOneValueNegativeInt64 operates on a PCollection<int64> and will return an error during runtime if none of the int64 values is negative.

func CheckFloat64MetricsAreNoisy

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

CheckFloat64MetricsAreNoisy checks that no values in a PCollection<pairIF64> (where pairIF64 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<pairII64> (where pairII64 contains the aggregate statistic) is equal to exactMetric.

func CheckNoNegativeValuesFloat64

func CheckNoNegativeValuesFloat64(v float64) error

CheckNoNegativeValuesFloat64 returns an error if an float64 value is negative.

func CheckNoNegativeValuesInt64

func CheckNoNegativeValuesInt64(v int64) error

CheckNoNegativeValuesInt64 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(t *testing.T, s beam.Scope, col1, col2 beam.PCollection)

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(t *testing.T, s beam.Scope, col1, col2 beam.PCollection)

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

func EqualsKVInt64

func EqualsKVInt64(t *testing.T, s beam.Scope, col1, col2 beam.PCollection)

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

func Float64Ptr

func Float64Ptr(f float64) *float64

Float64Ptr transforms a float64 into a *float64.

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 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(t *testing.T, s beam.Scope, col1, col2 beam.PCollection)

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(t *testing.T, s beam.Scope, col1, col2 beam.PCollection)

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 PairIF64SliceToKV

func PairIF64SliceToKV(tm PairIF64Slice) (int, []float64)

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

func PairIF64ToKV

func PairIF64ToKV(tm PairIF64) (int, float64)

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

func PairII64ToKV

func PairII64ToKV(tm PairII64) (int, int64)

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

func PairToKV

func PairToKV(p PairII) (k, v 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 {
	Key   int
	Value kv.Pair
}

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

func KVToPairICodedKV

func KVToPairICodedKV(k int, v kv.Pair) PairICodedKV

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

type PairIF64

type PairIF64 struct {
	Key   int
	Value float64
}

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

func KVToPairIF64

func KVToPairIF64(v int, m float64) PairIF64

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

type PairIF64Slice

type PairIF64Slice struct {
	Key   int
	Value []float64
}

PairIF64Slice holds a key-value pair of type (int, []float64).

func KVToPairIF64Slice

func KVToPairIF64Slice(v int, m []float64) PairIF64Slice

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

type PairII

type PairII struct {
	Key   int
	Value 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(k, v 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 {
	Key   int
	Value int64
}

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

func KVToPairII64

func KVToPairII64(v int, m int64) PairII64

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

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