OpenDP Integration¶
Utilities for integrating CSVW-EO metadata with OpenDP.
OpenDP Context¶
csvw_eo.csvw_to_opendp_context
¶
Create an OpenDP Context from CSVW-EO metadata and a dataset.
This module: - Converts CSVW-EO metadata into OpenDP margins - Builds an OpenDP Context using a provided dataset - Supports epsilon-based (Laplace) and rho-based (Gaussian) DP - Exposes both a Python API and CLI
The resulting context can be used for differentially private queries.
csvw_to_opendp_context(csvw_meta: dict[str, Any], data: pl.LazyFrame, epsilon: float | None = None, rho: float | None = None, delta: float | None = None, split_evenly_over: int | None = None, split_by_weights: list[float] | None = None, distance: str = 'contributions') -> dp.Context
¶
Create an OpenDP Context from CSVW-EO metadata and a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csvw_meta
|
Dict[str, Any]
|
CSVW-EO metadata dictionary.
Must include |
required |
data
|
LazyFrame
|
Input dataset (recommended as LazyFrame). |
required |
epsilon
|
float
|
Privacy budget epsilon (for Laplace DP). |
None
|
rho
|
float
|
Privacy budget rho (for Gaussian / zCDP). |
None
|
delta
|
float
|
Privacy budget delta (if using approximate DP). |
None
|
split_evenly_over
|
int
|
Number of queries to split privacy budget across. |
None
|
split_by_weights
|
list[float] | None
|
List of privacy budget weight by query. |
None
|
distance
|
str
|
Distance metric for privacy unit. |
'contributions'
|
Returns:
| Type | Description |
|---|---|
Context
|
OpenDP Context object ready for queries. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required metadata (max_contributions) is missing. If neither epsilon nor rho is provided. |
Source code in csvw-eo-library/src/csvw_eo/csvw_to_opendp_context.py
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 | |
get_privacy_loss(epsilon: float | None = None, rho: float | None = None, delta: float | None = None) -> tuple[Measure, Any]
¶
Create an opendp privacy loss object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epsilon
|
float
|
Privacy budget epsilon (for Laplace DP). |
None
|
rho
|
float
|
Privacy budget rho (for Gaussian / zCDP). |
None
|
delta
|
float
|
Privacy budget delta (if using approximate DP). |
None
|
Returns:
| Type | Description |
|---|---|
privacy_loss
|
opendp privacy loss object |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither epsilon nor rho is provided. |
Source code in csvw-eo-library/src/csvw_eo/csvw_to_opendp_context.py
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 | |
get_privacy_unit(csvw_meta: dict[str, Any], distance: str) -> tuple[Metric, Union[float, Sequence[Bound]]]
¶
Construct an OpenDP privacy unit from CSVW-EO metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csvw_meta
|
Dict[str, Any]
|
CSVW-EO metadata dictionary. |
required |
distance
|
str
|
Type of privacy distance metric to use (e.g. "contributions", "changes"). |
required |
Returns:
| Type | Description |
|---|---|
privacy_unit
|
OpenDP privacy unit descriptor. |
Source code in csvw-eo-library/src/csvw_eo/csvw_to_opendp_context.py
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 | |
OpenDP Margins¶
csvw_eo.csvw_to_opendp_margins
¶
Convert CSVW-EO JSON metadata into OpenDP margin descriptors.
This module provides:
- A function to translate CSVW-EO differential privacy metadata into
OpenDP dp.polars.Margin objects.
- A CLI for generating margin specifications from a JSON metadata file.
The resulting margins can be used in an OpenDP context, for example:
dp.Context.compositor(
data=...,
privacy_unit=dp.unit_of(contributions=...),
privacy_loss=dp.loss_of(epsilon=...),
margins=[...],
)
csvw_to_opendp_margins(csvw_meta: dict[str, Any]) -> list[Margin]
¶
Convert CSVW-EO metadata to a list of OpenDP Margin objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csvw_meta
|
Dict[str, Any]
|
CSVW-EO metadata dictionary. |
required |
Returns:
| Type | Description |
|---|---|
List[Margin]
|
List of OpenDP margin descriptors. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required metadata (e.g., max_contributions) is missing. |
Source code in csvw-eo-library/src/csvw_eo/csvw_to_opendp_margins.py
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 | |
get_margins(col_meta: dict[str, Any], by: list[str]) -> dict[str, Any]
¶
Build margin keyword arguments for a given column or column group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col_meta
|
Dict[str, Any]
|
Metadata describing a column or group of columns, including differential privacy constraints (e.g., max_length, max_groups). |
required |
by
|
List[str]
|
Column name(s) to group by when defining the margin. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary of keyword arguments suitable for constructing an OpenDP Margin object. |
Source code in csvw-eo-library/src/csvw_eo/csvw_to_opendp_margins.py
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 | |