constraints#

Defines Constraint types.

Constraints are necessary for most aggregations on tables using the AddRowsWithID ProtectedChange.

Illustrated examples using constraints can be found in the Working with privacy IDs tutorial.

Functions#

simplify_constraints(constraints)#

Remove redundant constraints from a list of constraints.

Given a list of the constraints on a table, produce a copy which simplifies it as much as possible by removing or combining constraints which provide overlapping information. The original list is not modified.

Parameters:

constraints (List[tmlt.analytics.constraints._base.Constraint])

Return type:

List[tmlt.analytics.constraints._base.Constraint]

Classes#

Constraint

A known, enforceable fact about a table.

MaxRowsPerID

A constraint limiting the number of rows associated with each ID in a table.

MaxGroupsPerID

A constraint limiting the number of distinct groups per ID.

MaxRowsPerGroupPerID

A constraint limiting rows per unique (ID, grouping column) pair in a table.

class Constraint#

Bases: abc.ABC

A known, enforceable fact about a table.

Constraints provide information about the contents of a table to help produce differentially-private results. For example, a constraint might say that each ID in a table corresponds to no more than two rows in that table (the MaxRowsPerID constraint). Constraints are applied via the QueryBuilder.enforce() method.

This class is a base class for all constraints, and cannot be used directly.

class MaxRowsPerID#

Bases: tmlt.analytics.constraints._base.Constraint

A constraint limiting the number of rows associated with each ID in a table.

This constraint limits how many times each distinct value may appear in the ID column of a table with the AddRowsWithID protected change. For example, MaxRowsPerID(5) guarantees that each ID appears in at most five rows. It cannot be applied to tables with other protected changes.

max: int#

The maximum number of times each distinct value may appear in the column.

class MaxGroupsPerID#

Bases: tmlt.analytics.constraints._base.Constraint

A constraint limiting the number of distinct groups per ID.

This constraint limits how many times a distinct value may appear in the grouping column for each distinct value in the table’s ID column. For example, MaxGroupsPerID("grouping_column", 4) guarantees that there are at most four distinct values of grouping_column for each distinct value of ID_column.

grouping_column: str#

The name of the grouping column.

max: int#

The maximum number of distinct values in the grouping column for each distinct value in the ID column.

class MaxRowsPerGroupPerID#

Bases: tmlt.analytics.constraints._base.Constraint

A constraint limiting rows per unique (ID, grouping column) pair in a table.

For example, MaxRowsPerGroupPerID("group_col", 5) guarantees that each ID appears in at most five rows for each distinct value in group_col.

grouping_column: str#

Name of column defining the groups to truncate.

max: int#

The maximum number of times each distinct value may appear in the column.