dictionary#

Transformations and utilities for manipulating dictionaries.

Note that while most transformations in this module (CreateDictFromValue, Subset, and GetValue) support the metric AddRemoveKeys, AugmentDictTransformation does not. Because of this, none of the included derived transformations (such as create_copy_and_transform_value()) support AddRemoveKeys. Instead, use transformations in add_remove_keys.

Functions#

create_copy_and_transform_value()

Returns a transformation that transforms and re-adds a value in the input dict.

create_rename()

Returns a transformation that renames a single key.

create_apply_dict_of_transformations()

Returns a transformation that applies all given transformations to the input.

create_transform_value()

Returns a transformation that transforms a single value in the input dict.

create_transform_all_values()

Returns a transformation that transforms every value in the input dict.

create_copy_and_transform_value(input_domain, input_metric, key, new_key, transformation, hint)#

Returns a transformation that transforms and re-adds a value in the input dict.

The returned transformation has roughly the same behavior as

def copy_and_transform_value(data):
    data[new_key] = transformation(data[key])
    return data

The input is a dictionary, a single value is transformed and added to the dictionary at a new key. Note that the original value is left unchanged in the dictionary.

Parameters
  • input_domain (tmlt.core.domains.collections.DictDomain) – The domain for the input data.

  • input_metric (tmlt.core.metrics.DictMetric) – The metric for the input data.

  • key (Any) – The key containing the data to transform.

  • new_key (Any) – The key to store the transformed data.

  • transformation (Any) – The transformation to apply.

  • hint (Callable[[Any, Any], Any]) – A hint for the transformation.

Return type

tmlt.core.transformations.base.Transformation

create_rename(input_domain, input_metric, key, new_key)#

Returns a transformation that renames a single key.

The returned transformation has roughly the same behavior as

def rename(data):
    data[new_key] = data.pop(key)
    return data
Parameters
Return type

tmlt.core.transformations.base.Transformation

create_apply_dict_of_transformations(transformation_dict, hint_dict)#

Returns a transformation that applies all given transformations to the input.

The returned transformation has roughly the same behavior as

def apply_dict_of_transformations(data):
    return {
        key: transformation(data)
        for key, transformation in transformation_dict.items()
    }

The input is a single element, and the output is a dictionary where each value is the input transformed by the corresponding transformation.

Parameters
  • transformation_dict (Mapping[Any, tmlt.core.transformations.base.Transformation]) – A dictionary of transformations with matching input domains and input metrics.

  • hint_dict (Mapping[Any, Callable[[Any, Any], Any]]) – A dictionary of hints for the corresponding transformations in transformation_dict.

Return type

tmlt.core.transformations.base.Transformation

create_transform_value(input_domain, input_metric, key, transformation, hint)#

Returns a transformation that transforms a single value in the input dict.

The returned transformation has roughly the same behavior as

def transform_value(data):
    data[key] = transformation(data[key])
    return data

Notice that the input is a dictionary, a single value is transformed, while the other values are left unchanged.

Parameters
Return type

tmlt.core.transformations.base.Transformation

create_transform_all_values(transformation_dict, hint_dict)#

Returns a transformation that transforms every value in the input dict.

The returned transformation has roughly the same behavior as

def transform_all_values(data):
    return {
        key: transformation(data[key])
        for key, transformation in transformation_dict.items()
    }

Notice that the input is a dictionary, and each value in the dictionary is transformed by the corresponding transformation.

Parameters
  • transformation_dict (Mapping[Any, tmlt.core.transformations.base.Transformation]) – A dictionary of transformations with matching input domains and input metrics.

  • hint_dict (Mapping[Any, Callable[[Any, Any], Any]]) – A dictionary of hints for the corresponding transformations in transformation_dict.

Return type

tmlt.core.transformations.base.Transformation

Classes#

CreateDictFromValue

Create a dictionary from an object.

AugmentDictTransformation

Applies transformation to a dictionary and appends the output to the input.

Subset

Retrieve a subset of a dictionary by keys.

GetValue

Retrieve an object from a dictionary.

class CreateDictFromValue(input_domain, input_metric, key, use_add_remove_keys=False)#

Bases: tmlt.core.transformations.base.Transformation

Create a dictionary from an object.

Parameters
__init__(input_domain, input_metric, key, use_add_remove_keys=False)#

Constructor.

Parameters
  • input_domain (DomainDomain) – Domain of input objects.

  • input_metric (MetricMetric) – Distance metric on input objects.

  • key (AnyAny) – Key for constructing dictionary with given object.

  • use_add_remove_keys (boolbool (default: False)) – Whether to use AddRemoveKeys as the output metric instead of DictMetric.

property key#

Returns the key for the created dictionary.

Return type

Any

stability_function(d_in)#

Returns the smallest d_out satisfied by the transformation.

The returned d_out is {self.key: d_in}.

Parameters

d_in (Any) – Distance between inputs under input_metric.

Return type

Any

__call__(val)#

Returns dictionary with value associated with specified key.

Parameters

val (Any) –

Return type

Dict[Any, Any]

property input_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property input_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

property output_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property output_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

stability_relation(d_in, d_out)#

Returns True only if close inputs produce close outputs.

See the privacy and stability tutorial (add link?) for more information.

Parameters
  • d_in (Any) – Distance between inputs under input_metric.

  • d_out (Any) – Distance between outputs under output_metric.

Return type

bool

__or__(other: Transformation) Transformation#
__or__(other: tmlt.core.measurements.base.Measurement) tmlt.core.measurements.base.Measurement

Return this transformation chained with another component.

class AugmentDictTransformation(transformation)#

Bases: tmlt.core.transformations.base.Transformation

Applies transformation to a dictionary and appends the output to the input.

Parameters

transformation (tmlt.core.transformations.base.Transformation) –

__init__(transformation)#

Constructor.

Parameters

transformation (TransformationTransformation) – Transformation to be applied to input dictionary.

property inner_transformation#

Returns the inner transformation.

Return type

tmlt.core.transformations.base.Transformation

stability_function(d_in)#

Returns the smallest d_out satisfied.

Returns {**d_in, **self.transformation.stability_function(d_in)}.

Parameters

d_in (Dict[Any, Any]) – Distance between inputs under input_metric.

Raises

NotImplementedError – If self.inner_transformation.stability_function(d_in) raises NotImplementedError.

Return type

Dict[Any, Any]

stability_relation(d_in, d_out)#

Returns True if close inputs produce close outputs.

Returns True if both of the following are true:

  • d_in[key] <= d_out[key] for all augmented keys.

  • self.inner_transformation.stability_relation(d_in, original_d_out)

where original_d_out is the subset of d_out excluding the augmented keys.

Parameters
  • d_in (Any) – Distance between inputs under input_metric.

  • d_out (Any) – Distance between outputs under output_metric.

Return type

bool

__call__(input_dict)#

Applies transformation on given key to produce augmented dictionary.

Parameters

input_dict (Dict[Any, Any]) –

Return type

Dict[Any, Any]

property input_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property input_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

property output_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property output_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

__or__(other: Transformation) Transformation#
__or__(other: tmlt.core.measurements.base.Measurement) tmlt.core.measurements.base.Measurement

Return this transformation chained with another component.

class Subset(input_domain, input_metric, keys)#

Bases: tmlt.core.transformations.base.Transformation

Retrieve a subset of a dictionary by keys.

Parameters
__init__(input_domain, input_metric, keys)#

Constructor.

Parameters
property keys#

Returns the keys to keep.

Return type

List[Any]

stability_function(d_in)#

Returns the smallest d_out satisfied by the transformation.

The returned d_out is {key: d_in[key] for key in self.keys}.

Parameters

d_in (Any) – Distance between inputs under input_metric.

Return type

Any

__call__(input_dict)#

Returns subset of dictionary specified by keys.

Parameters

input_dict (Any) –

Return type

Any

property input_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property input_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

property output_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property output_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

stability_relation(d_in, d_out)#

Returns True only if close inputs produce close outputs.

See the privacy and stability tutorial (add link?) for more information.

Parameters
  • d_in (Any) – Distance between inputs under input_metric.

  • d_out (Any) – Distance between outputs under output_metric.

Return type

bool

__or__(other: Transformation) Transformation#
__or__(other: tmlt.core.measurements.base.Measurement) tmlt.core.measurements.base.Measurement

Return this transformation chained with another component.

class GetValue(input_domain, input_metric, key)#

Bases: tmlt.core.transformations.base.Transformation

Retrieve an object from a dictionary.

Parameters
__init__(input_domain, input_metric, key)#

Constructor.

Parameters
property key#

Returns the key to keep.

Return type

List[Any]

stability_function(d_in)#

Returns the smallest d_out satisfied by the transformation.

The returned d_out is d_in[self.key].

Parameters

d_in (Any) – Distance between inputs under input_metric.

Return type

Any

__call__(input_dict)#

Returns value for specified key.

Parameters

input_dict (Any) –

Return type

Any

property input_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property input_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

property output_domain#

Return input domain for the measurement.

Return type

tmlt.core.domains.base.Domain

property output_metric#

Distance metric on input domain.

Return type

tmlt.core.metrics.Metric

stability_relation(d_in, d_out)#

Returns True only if close inputs produce close outputs.

See the privacy and stability tutorial (add link?) for more information.

Parameters
  • d_in (Any) – Distance between inputs under input_metric.

  • d_out (Any) – Distance between outputs under output_metric.

Return type

bool

__or__(other: Transformation) Transformation#
__or__(other: tmlt.core.measurements.base.Measurement) tmlt.core.measurements.base.Measurement

Return this transformation chained with another component.