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#
Returns a transformation that transforms and re-adds a value in the input dict. |
|
Returns a transformation that renames a single key. |
|
Returns a transformation that applies all given transformations to the input. |
|
Returns a transformation that transforms a single value in the input dict. |
|
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
- 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
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 original key.
new_key (Any) – The new key.
- Return type
- 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
- 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
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 to transform.
transformation (Any) – The transformation to apply.
hint (Callable[[Any, Any], Any]) – A hint for the transformation.
- Return type
- 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
Classes#
Create a dictionary from an object. |
|
Applies transformation to a dictionary and appends the output to the input. |
|
Retrieve a subset of a dictionary by keys. |
|
Retrieve an object from a dictionary. |
- class CreateDictFromValue(input_domain, input_metric, key, use_add_remove_keys=False)#
Bases:
tmlt.core.transformations.base.TransformationCreate a dictionary from an object.
- Parameters
input_domain (tmlt.core.domains.base.Domain) –
input_metric (tmlt.core.metrics.Metric) –
key (Any) –
use_add_remove_keys (bool) –
- __init__(input_domain, input_metric, key, use_add_remove_keys=False)#
Constructor.
- Parameters
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 useAddRemoveKeysas the output metric instead ofDictMetric.
- 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
- property input_metric#
Distance metric on input domain.
- Return type
- property output_domain#
Return input domain for the measurement.
- Return type
- property output_metric#
Distance metric on input domain.
- Return type
- 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
- __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.TransformationApplies 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
- 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
- __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
- property input_metric#
Distance metric on input domain.
- Return type
- property output_domain#
Return input domain for the measurement.
- Return type
- property output_metric#
Distance metric on input domain.
- Return type
- __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.TransformationRetrieve a subset of a dictionary by keys.
- Parameters
input_domain (tmlt.core.domains.collections.DictDomain) –
input_metric (Union[tmlt.core.metrics.DictMetric, tmlt.core.metrics.AddRemoveKeys]) –
keys (List[Any]) –
- __init__(input_domain, input_metric, keys)#
Constructor.
- Parameters
input_domain (
DictDomainDictDomain) – Domain of input dictionaries.input_metric (
DictMetric|AddRemoveKeysUnion[DictMetric,AddRemoveKeys]) – Distance metric over input dictionaries.keys (
List[Any]List[Any]) – Keys to be used for extracting subset.
- 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
- property input_metric#
Distance metric on input domain.
- Return type
- property output_domain#
Return input domain for the measurement.
- Return type
- property output_metric#
Distance metric on input domain.
- Return type
- 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
- __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.TransformationRetrieve an object from a dictionary.
- Parameters
input_domain (tmlt.core.domains.collections.DictDomain) –
input_metric (Union[tmlt.core.metrics.DictMetric, tmlt.core.metrics.AddRemoveKeys]) –
key (Any) –
- __init__(input_domain, input_metric, key)#
Constructor.
- Parameters
input_domain (
DictDomainDictDomain) – Domain of input dictionaries.input_metric (
DictMetric|AddRemoveKeysUnion[DictMetric,AddRemoveKeys]) – Distance metric for input dictionaries.
- 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
- property input_metric#
Distance metric on input domain.
- Return type
- property output_domain#
Return input domain for the measurement.
- Return type
- property output_metric#
Distance metric on input domain.
- Return type
- 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
- __or__(other: Transformation) Transformation#
- __or__(other: tmlt.core.measurements.base.Measurement) tmlt.core.measurements.base.Measurement
Return this transformation chained with another component.