1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Component trait implementations
//!
//! Each component represents an abstract computation.
//! Example components are Materialize for loading a dataframe, Index for retrieving specific columns from the dataframe, Mean for aggregating data, LaplaceMechanism for privatizing data, etc.
//!
//! There are a set of possible behaviours each component may implement. Each behavior corresponds to a trait. A listing of traits is at the bottom of the page.
//!
//! Implementations of the traits are distributed among the module files.

use crate::errors::*;


mod transforms;
//mod bin;
mod cast;
mod clamp;
mod count;
mod covariance;
mod column_bind;
mod digitize;
mod dp_count;
mod dp_variance;
mod dp_covariance;
mod dp_gumbel_median;
mod dp_histogram;
mod dp_linear_regression;
mod dp_maximum;
mod dp_median;
mod dp_minimum;
mod dp_mean;
mod dp_quantile;
mod dp_raw_moment;
mod dp_sum;
mod filter;
mod histogram;
mod impute;
pub mod index;
mod raw_moment;
mod literal;
mod map;
mod materialize;
pub mod partition;
mod quantile;
mod reshape;
mod mean;
mod exponential_mechanism;
pub mod gaussian_mechanism;
mod laplace_mechanism;
mod simple_geometric_mechanism;
pub mod snapping_mechanism;
mod resize;
mod theil_sen;
mod to_dataframe;
mod sum;
mod union;
mod variance;

use crate::base::{IndexKey, Value, NodeProperties, SensitivitySpace, ValueProperties};
use crate::{proto, Warnable, base};
use crate::utilities::json::{JSONRelease};
use crate::utilities::set_node_id;
use indexmap::map::IndexMap;

/// Universal Component trait
///
/// To be a component, a struct must represent an abstract computation, for which properties can be derived about the resulting data.
pub trait Component {
    /// Given properties known about private arguments, and public arguments, derive properties about the resulting data.
    ///
    /// A component must fail to propagate properties if requirements on the input properties are not met.
    /// For example, if a Component represents an abstract computation that requires prior knowledge of the number of records to be safe or function properly,
    /// the propagate_property implementation is expected to return an error state if the prior knowledge is not known.
    ///
    /// For example, if a definition of privacy is used that is incompatible with the abstract computation,
    /// the propagate_property implementation is expected to return an error state.
    ///
    /// # Arguments
    /// * `self` - the protobuf object corresponding to the prost protobuf struct
    /// * `privacy_definition` - the definition of privacy under which the computation takes place
    /// * `public_arguments` - actual data values of arguments, typically either supplied literals or released values.
    /// * `properties` - derived properties of private input arguments
    /// * `node_id` - id of the node in the analysis graph (used to set dataset_id in the data loaders)
    ///
    /// # Returns
    /// Derived properties on the data resulting from the abstract computation
    fn propagate_property(
        &self,
        privacy_definition: &Option<proto::PrivacyDefinition>,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        properties: NodeProperties,
        _node_id: u32,
    ) -> Result<Warnable<ValueProperties>>;
}

/// Expandable Component trait
///
/// When a component is expandable, it represents a higher order computation that may be expressed in multiple components that are more granular.
/// Oftentimes Expandable components correspond to differentially private algorithms,
/// that are represented in terms of an aggregation and a mechanism.
pub trait Expandable {
    /// Concrete implementation for an Expandable component that returns a patch that may be applied to a computation graph.
    ///
    /// # Arguments
    /// * `self` - the protobuf object corresponding to the prost protobuf struct
    /// * `privacy_definition` - definition of privacy to use when expanding. Some expansions are not valid under some privacy definitions
    /// * `component` - contains additional metadata about the argument node ids
    /// * `properties` - properties on the data supplied as arguments
    /// * `component_id` - the id of the node to expand. The final node in the returned patch must use this id.
    /// * `maximum_id` - the starting id for which additional nodes may be added to the graph without overwriting existing nodes
    ///
    /// # Returns
    /// Sufficient information to patch the runtime with more granular steps.
    /// More documentation at [ComponentExpansion](proto::ComponentExpansion).
    fn expand_component(
        &self,
        privacy_definition: &Option<proto::PrivacyDefinition>,
        component: &proto::Component,
        public_arguments: &IndexMap<base::IndexKey, &Value>,
        properties: &NodeProperties,
        component_id: u32,
        maximum_id: u32,
    ) -> Result<base::ComponentExpansion>;
}

/// Mechanism component trait
///
/// When a component is a Mechanism, it consumes a privacy budget.
pub trait Mechanism {
    /// Extraction of privacy usage by the component.
    ///
    /// By default, this returns the upper bound of the privacy usage of the component.
    ///
    /// If the component has been evaluated, it is possible the actual usage of the component differs from the upper bound.
    /// In this case, the release_usage is returned.
    ///
    /// # Arguments
    /// * `self` - the protobuf object corresponding to the prost protobuf struct, containing an upper bound on privacy usage
    /// * `privacy_definition` - the definition of privacy under which the sensitivity is to be computed
    /// * `release_usage` - optionally, the privacy actually used by the mechanism (if it has already been released)
    /// * `sensitivity_type` - space for which the sensitivity is computed within
    ///
    /// # Returns
    /// Privacy usages after group_size, c_stability and privacy amplification have been taken into account.
    fn get_privacy_usage(
        &self,
        privacy_definition: &proto::PrivacyDefinition,
        release_usage: Option<&Vec<proto::PrivacyUsage>>,
        properties: &NodeProperties
    ) -> Result<Option<Vec<proto::PrivacyUsage>>>;
}

/// Sensitivity component trait
///
/// When a component has sensitivity, the abstract computation the component represents combines multiple rows together into a single value.
/// For example, a mean, minimum, or scoring function on a dataset. A component that aggregates data has an associated sensitivity, which captures
/// how much the input data affects the output of the aggregator.
pub trait Sensitivity {
    /// Derivation for the sensitivity of an aggregator based on available local metadata.
    ///
    /// The sensitivity is the maximum amount that a perturbation of input data may have on the resulting value.
    /// The type of perturbation is described in the privacy_definition.
    ///
    /// # Arguments
    /// * `self` - the protobuf object corresponding to the prost protobuf struct
    /// * `privacy_definition` - the definition of privacy under which the sensitivity is to be computed
    /// * `properties` - derived properties for the input data
    /// * `sensitivity_type` - space for which the sensitivity is computed within
    ///
    /// # Returns
    /// Sensitivities for each of the values in the resulting computation
    fn compute_sensitivity(
        &self,
        privacy_definition: &proto::PrivacyDefinition,
        properties: &NodeProperties,
        sensitivity_type: &SensitivitySpace,
    ) -> Result<Value>;
}

/// Accuracy component trait
///
/// Components with Accuracy implemented may convert between privacy units and accuracy estimates
pub trait Accuracy {
    fn accuracy_to_privacy_usage(
        &self,
        accuracies: &proto::Accuracies,
        public_arguments: IndexMap<base::IndexKey, &Value>
    ) -> Result<Option<Vec<proto::PrivacyUsage>>>;

    fn privacy_usage_to_accuracy(
        &self,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        alpha: f64,
    ) -> Result<Option<Vec<proto::Accuracy>>>;
}

/// Report component trait
///
/// Reportable components correspond to a computation that a researcher may want a JSON summary for
pub trait Report {
    /// Summarize the relevant metadata around a computation in a readable, JSON-serializable format.
    fn summarize(
        &self,
        node_id: u32,
        component: &proto::Component,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        properties: NodeProperties,
        release: &Value,
        variable_names: Option<&Vec<base::IndexKey>>,
    ) -> Result<Option<Vec<JSONRelease>>>;
}

/// Named component trait
///
/// Named components involve variables and keep track of the human readable names for these variables
/// and may modify these variables names.
pub trait Named {
    /// Propagate the human readable names of the variables associated with this component
    fn get_names(
        &self,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        argument_variables: IndexMap<base::IndexKey, Vec<IndexKey>>,
        release: Option<&Value>,
    ) -> Result<Vec<IndexKey>>;
}


impl Component for proto::Component {
    /// Utility implementation on the component.
    ///
    /// This utility delegates evaluation to the concrete implementation of each component variant.
    fn propagate_property(
        &self,
        privacy_definition: &Option<proto::PrivacyDefinition>,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        properties: NodeProperties,
        node_id: u32,
    ) -> Result<Warnable<ValueProperties>> {
        let variant = self.variant.as_ref()
            .ok_or_else(|| "variant: must be defined")?;

        macro_rules! propagate_property {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = variant {
                            let Warnable(mut property, warnings) = x.propagate_property(
                                privacy_definition, public_arguments, properties, node_id)
                                .chain_err(|| format!("node specification {:?}:", variant))?;
                            set_node_id(&mut property, node_id);

                            return Ok(Warnable(property, warnings));
                       }
                    )*
                }
            }
        }

        propagate_property!(
            // INSERT COMPONENT LIST
            Cast, Clamp, ColumnBind, Count, Covariance, Digitize,
            Filter, Histogram, Impute, Index, Literal, Materialize, Mean,
            Partition, Quantile, RawMoment, Reshape, Resize, Sum, ToDataframe, Union, Variance,

            ExponentialMechanism, GaussianMechanism, LaplaceMechanism,
            SimpleGeometricMechanism, SnappingMechanism,

            Abs, Add, LogicalAnd, Divide, Equal, GreaterThan, LessThan, Log, Modulo, Multiply,
            Negate, Negative, LogicalOr, Power, RowMax, RowMin, Subtract, TheilSen, DpGumbelMedian
        );

        Err(format!("proto component {:?} is missing its Component trait", variant).into())
    }
}

impl Expandable for proto::Component {
    /// Utility implementation on the component.
    ///
    /// This utility delegates evaluation to the concrete implementation of each component variant.
    fn expand_component(
        &self,
        privacy_definition: &Option<proto::PrivacyDefinition>,
        component: &proto::Component,
        public_arguments: &IndexMap<base::IndexKey, &Value>,
        properties: &NodeProperties,
        component_id: u32,
        maximum_id: u32,
    ) -> Result<base::ComponentExpansion> {
        let variant = self.variant.as_ref()
            .ok_or_else(|| "variant: must be defined")?;

        macro_rules! expand_component {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = variant {
                            let expansion = x.expand_component(
                                privacy_definition, component, public_arguments,
                                properties, component_id, maximum_id)
                                .chain_err(|| format!("node specification {:?}:", variant))?;

                            expansion.is_valid(component_id)?;
                            return Ok(expansion)
                       }
                    )*
                }
            }
        }

        // indexes and unions accept partitioned data as an argument- don't expand with map
        if let proto::component::Variant::Index(_) = variant {
            return Ok(base::ComponentExpansion::default())
        }
        if let proto::component::Variant::Union(_) = variant {
            return Ok(base::ComponentExpansion::default())
        }

        // list all components that accept partitioned data as arguments
        expand_component!(Map);

        if properties.values().any(|props| props.partitions().is_ok()) {
            let mut component_expansion = base::ComponentExpansion::default();
            component_expansion.computation_graph.insert(component_id, proto::Component {
                arguments: component.arguments.clone(),
                variant: Some(proto::component::Variant::Map(Box::new(proto::Map {
                    component: Some(Box::from(component.clone()))
                }))),
                omit: component.omit,
                submission: component.submission,
            });
            component_expansion.traversal.push(component_id);
            return Ok(component_expansion);
        }

        expand_component!(
            // INSERT COMPONENT LIST
            Clamp, Digitize, Histogram, Impute, Map, Maximum, Median, Minimum, Partition, Resize,

            DpCount, DpCovariance, DpHistogram, DpLinearRegression, DpMaximum, DpMean, DpMedian,
            DpMinimum, DpQuantile, DpRawMoment, DpSum, DpVariance,

            ExponentialMechanism, GaussianMechanism, LaplaceMechanism,
            SimpleGeometricMechanism, SnappingMechanism, DpGumbelMedian,

            ToBool, ToFloat, ToInt, ToString
        );

        // no expansion
        Ok(base::ComponentExpansion::default())
    }
}

impl Mechanism for proto::Component {

    fn get_privacy_usage(
        &self,
        privacy_definition: &proto::PrivacyDefinition,
        release_usage: Option<&Vec<proto::PrivacyUsage>>,
        properties: &NodeProperties
    ) -> Result<Option<Vec<proto::PrivacyUsage>>> {
        let variant = self.variant.as_ref()
            .ok_or_else(|| "variant: must be defined")?;

        macro_rules! get_privacy_usage {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = variant {
                            return x.get_privacy_usage(privacy_definition, release_usage, properties)
                                .chain_err(|| format!("node specification {:?}:", variant))
                       }
                    )*
                }
            }
        }

        get_privacy_usage!(
            // INSERT COMPONENT LIST
            ExponentialMechanism, GaussianMechanism, LaplaceMechanism,
            SimpleGeometricMechanism, SnappingMechanism
        );

        Ok(None)
    }
}


impl Sensitivity for proto::component::Variant {
    /// Utility implementation on the enum containing all variants of a component.
    ///
    /// This utility delegates evaluation to the concrete implementation of each component variant.
    fn compute_sensitivity(
        &self,
        privacy_definition: &proto::PrivacyDefinition,
        properties: &NodeProperties,
        sensitivity_type: &SensitivitySpace,
    ) -> Result<Value> {
        macro_rules! compute_sensitivity {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = self {
                            return x.compute_sensitivity(privacy_definition, properties, sensitivity_type)
                                .chain_err(|| format!("node specification {:?}:", self))
                       }
                    )*
                }
            }
        }

        compute_sensitivity!(
            // INSERT COMPONENT LIST
            Count, Covariance, Histogram, Mean, Quantile, RawMoment, Sum, Union, Variance
        );

        Err(format!("sensitivity is not implemented for proto component {:?}", self).into())
    }
}

impl Accuracy for proto::Component {
    /// Utility implementation on the component.
    ///
    /// This utility delegates evaluation to the concrete implementation of each component variant.
    fn accuracy_to_privacy_usage(
        &self,
        accuracy: &proto::Accuracies,
        public_arguments: IndexMap<base::IndexKey, &Value>
    ) -> Result<Option<Vec<proto::PrivacyUsage>>> {
        let variant = self.variant.as_ref()
            .ok_or_else(|| "variant: must be defined")?;

        macro_rules! accuracy_to_privacy_usage {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = variant {
                            return x.accuracy_to_privacy_usage(accuracy, public_arguments)
                                .chain_err(|| format!("node specification {:?}:", variant))
                       }
                    )*
                }
            }
        }

        accuracy_to_privacy_usage!(
             LaplaceMechanism,
             GaussianMechanism,
             SimpleGeometricMechanism,
             SnappingMechanism
        );

        Ok(None)
    }

    /// Utility implementation on the component.
    ///
    /// This utility delegates evaluation to the concrete implementation of each component variant.
    fn privacy_usage_to_accuracy(
        &self,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        alpha: f64,
    ) -> Result<Option<Vec<proto::Accuracy>>> {
        let variant = self.variant.as_ref()
            .ok_or_else(|| "variant: must be defined")?;

        macro_rules! privacy_usage_to_accuracy {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = variant {
                            return x.privacy_usage_to_accuracy(public_arguments, alpha)
                                .chain_err(|| format!("node specification {:?}:", variant))
                       }
                    )*
                }
            }
        }

        privacy_usage_to_accuracy!(
            LaplaceMechanism,
            GaussianMechanism,
            SimpleGeometricMechanism,
            SnappingMechanism
        );

        Ok(None)
    }
}

impl Report for proto::Component {
    /// Utility implementation on the component.
    ///
    /// This utility delegates evaluation to the concrete implementation of each component variant.
    fn summarize(
        &self,
        node_id: u32,
        component: &proto::Component,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        properties: NodeProperties,
        release: &Value,
        variable_names: Option<&Vec<base::IndexKey>>,
    ) -> Result<Option<Vec<JSONRelease>>> {
        let variant = self.variant.as_ref()
            .ok_or_else(|| "variant: must be defined")?;

        macro_rules! summarize {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = variant {
                            return x.summarize(node_id, component, public_arguments,
                                 properties, release, variable_names)
                                .chain_err(|| format!("node specification: {:?}:", variant))
                       }
                    )*
                }
            }
        }

        summarize!(
            // INSERT COMPONENT LIST
            DpCount, DpCovariance, DpHistogram, DpMaximum, DpMean, DpMinimum, DpQuantile,
            DpRawMoment, DpSum, DpVariance
        );

        Ok(None)
    }
}

impl Named for proto::Component {
    /// Utility implementation on the component.
    ///
    /// This utility delegates evaluation to the concrete implementation of each component variant.
    fn get_names(
        &self,
        public_arguments: IndexMap<base::IndexKey, &Value>,
        argument_variables: IndexMap<base::IndexKey, Vec<IndexKey>>,
        release: Option<&Value>,
    ) -> Result<Vec<IndexKey>> {
        let variant = self.variant.as_ref()
            .ok_or_else(|| "variant: must be defined")?;

        macro_rules! get_names {
            ($( $variant:ident ),*) => {
                {
                    $(
                       if let proto::component::Variant::$variant(x) = variant {
                            return x.get_names(public_arguments, argument_variables, release)
                                .chain_err(|| format!("node specification {:?}:", variant))
                       }
                    )*
                }
            }
        }

        // TODO: transforms, covariance/cross-covariance, extended indexing, columnbind
        get_names!(
            // INSERT COMPONENT LIST
            ToDataframe, Index, Literal, Materialize
        );

        // default implementation
        match argument_variables.get(&IndexKey::from("data")) {
            // by convention, names pass through the "data" argument unchanged
            Some(variable_names) => Ok(variable_names.clone()),
            // otherwise if the component is non-standard, throw an error
            None => Err(format!("names are not implemented for proto component {:?}", variant).into())
        }
    }
}