ectype-data 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ectype/__init__.py +134 -0
- ectype/_version.py +5 -0
- ectype/authoring/__init__.py +13 -0
- ectype/authoring/builder.py +134 -0
- ectype/authoring/spec.py +555 -0
- ectype/calibration/__init__.py +26 -0
- ectype/calibration/adjustments.py +539 -0
- ectype/calibration/calibrator.py +781 -0
- ectype/cli.py +1181 -0
- ectype/compiler/__init__.py +1 -0
- ectype/compiler/plan.py +678 -0
- ectype/errors.py +43 -0
- ectype/fingerprint/__init__.py +1 -0
- ectype/fingerprint/comparison.py +746 -0
- ectype/fingerprint/container.py +408 -0
- ectype/fingerprint/core.py +263 -0
- ectype/fingerprint/disclosure.py +207 -0
- ectype/fingerprint/expressions.py +1317 -0
- ectype/fingerprint/schema.py +645 -0
- ectype/fingerprint/targets.py +352 -0
- ectype/generation/__init__.py +1 -0
- ectype/generation/enforce.py +163 -0
- ectype/generation/generator.py +525 -0
- ectype/generation/identifiers.py +235 -0
- ectype/generation/manifest.py +71 -0
- ectype/generation/parallel.py +229 -0
- ectype/io/__init__.py +16 -0
- ectype/io/adapters.py +224 -0
- ectype/io/sql.py +289 -0
- ectype/io/values.py +269 -0
- ectype/llm/__init__.py +27 -0
- ectype/llm/authoring.py +766 -0
- ectype/llm/providers.py +269 -0
- ectype/models/__init__.py +8 -0
- ectype/models/base.py +120 -0
- ectype/models/chain.py +2426 -0
- ectype/models/copulas.py +254 -0
- ectype/models/encoding.py +1117 -0
- ectype/models/forest.py +1087 -0
- ectype/models/marginals.py +2099 -0
- ectype/models/mixtures.py +336 -0
- ectype/models/neural/__init__.py +21 -0
- ectype/models/neural/adapter.py +638 -0
- ectype/models/neural/vae.py +432 -0
- ectype/models/vines.py +608 -0
- ectype/plugins.py +40 -0
- ectype/privacy/__init__.py +49 -0
- ectype/privacy/attacks.py +599 -0
- ectype/privacy/dp.py +313 -0
- ectype/privacy/report.py +364 -0
- ectype/profiler.py +783 -0
- ectype/profiling/__init__.py +1 -0
- ectype/profiling/bivariate.py +130 -0
- ectype/profiling/calendar.py +743 -0
- ectype/profiling/conditional.py +263 -0
- ectype/profiling/constraints.py +661 -0
- ectype/profiling/entity.py +1143 -0
- ectype/profiling/functional.py +651 -0
- ectype/profiling/multivariate.py +492 -0
- ectype/profiling/personal.py +277 -0
- ectype/profiling/pseudonym.py +370 -0
- ectype/profiling/reference.py +334 -0
- ectype/profiling/sketches.py +653 -0
- ectype/profiling/structural.py +1087 -0
- ectype/profiling/structure.py +1733 -0
- ectype/profiling/sums.py +1710 -0
- ectype/profiling/tolerance.py +435 -0
- ectype/profiling/univariate.py +555 -0
- ectype/py.typed +0 -0
- ectype/registry.py +48 -0
- ectype/relational/__init__.py +61 -0
- ectype/relational/container.py +212 -0
- ectype/relational/fit.py +633 -0
- ectype/relational/generate.py +418 -0
- ectype/relational/schema.py +280 -0
- ectype/relational/validate.py +288 -0
- ectype/reporting/__init__.py +439 -0
- ectype/reporting/adapter.py +2003 -0
- ectype/reporting/charts.py +964 -0
- ectype/reporting/drift.py +902 -0
- ectype/reporting/findings.py +622 -0
- ectype/reporting/fmt.py +429 -0
- ectype/reporting/render.py +1705 -0
- ectype/reporting/select.py +84 -0
- ectype/reporting/style.py +272 -0
- ectype/reporting/view.py +593 -0
- ectype/rng.py +33 -0
- ectype/scenarios/__init__.py +30 -0
- ectype/scenarios/feasibility.py +411 -0
- ectype/scenarios/merge.py +1392 -0
- ectype/scenarios/mutate.py +1613 -0
- ectype/temporal/__init__.py +80 -0
- ectype/temporal/contract.py +221 -0
- ectype/temporal/detect.py +431 -0
- ectype/temporal/events.py +946 -0
- ectype/temporal/panel.py +937 -0
- ectype/temporal/pipeline.py +192 -0
- ectype/temporal/regular.py +674 -0
- ectype/types.py +46 -0
- ectype/validation/__init__.py +1 -0
- ectype/validation/metrics/__init__.py +7 -0
- ectype/validation/metrics/base.py +68 -0
- ectype/validation/metrics/bivariate.py +353 -0
- ectype/validation/metrics/missingness.py +54 -0
- ectype/validation/metrics/multivariate.py +265 -0
- ectype/validation/metrics/temporal.py +596 -0
- ectype/validation/metrics/univariate.py +514 -0
- ectype/validation/report.py +627 -0
- ectype/validation/validator.py +707 -0
- ectype_data-0.1.0.dist-info/METADATA +376 -0
- ectype_data-0.1.0.dist-info/RECORD +115 -0
- ectype_data-0.1.0.dist-info/WHEEL +4 -0
- ectype_data-0.1.0.dist-info/entry_points.txt +2 -0
- ectype_data-0.1.0.dist-info/licenses/LICENSE +192 -0
- ectype_data-0.1.0.dist-info/licenses/NOTICE +5 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"""Disclosure policies: what a fingerprint is allowed to store.
|
|
2
|
+
|
|
3
|
+
These primitives are used by every component that stores data-derived values (marginals,
|
|
4
|
+
metrics/targets, structural stats). They reduce disclosure risk; they are NOT differential
|
|
5
|
+
privacy and nothing in the artifact claims otherwise.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import math
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
from ectype.errors import PolicyError
|
|
15
|
+
from ectype.fingerprint.schema import DisclosurePolicySpec
|
|
16
|
+
|
|
17
|
+
OTHER = "__other__"
|
|
18
|
+
|
|
19
|
+
POLICIES: dict[str, DisclosurePolicySpec] = {
|
|
20
|
+
"strict": DisclosurePolicySpec(profile="strict", min_category_support=20,
|
|
21
|
+
quantile_resolution=50, tail_clip=(0.01, 0.99),
|
|
22
|
+
rare_cell_min=20, significant_digits=4),
|
|
23
|
+
"standard": DisclosurePolicySpec(profile="standard", min_category_support=10,
|
|
24
|
+
quantile_resolution=100, tail_clip=(0.005, 0.995),
|
|
25
|
+
rare_cell_min=10, significant_digits=6),
|
|
26
|
+
"research": DisclosurePolicySpec(profile="research", min_category_support=1,
|
|
27
|
+
quantile_resolution=1000, tail_clip=(0.0, 1.0),
|
|
28
|
+
rare_cell_min=1, significant_digits=10, sensitive=True),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def resolve_policy(policy: str | DisclosurePolicySpec | dict | None) -> DisclosurePolicySpec:
|
|
33
|
+
if policy is None:
|
|
34
|
+
return POLICIES["standard"].model_copy()
|
|
35
|
+
if isinstance(policy, DisclosurePolicySpec):
|
|
36
|
+
return policy.model_copy()
|
|
37
|
+
if isinstance(policy, dict):
|
|
38
|
+
base = POLICIES.get(str(policy.get("profile", "standard")), POLICIES["standard"])
|
|
39
|
+
merged = {**base.model_dump(), **policy}
|
|
40
|
+
if merged.get("profile") not in POLICIES:
|
|
41
|
+
merged["profile"] = "custom"
|
|
42
|
+
try:
|
|
43
|
+
return DisclosurePolicySpec.model_validate(merged)
|
|
44
|
+
except Exception as e: # pydantic ValidationError
|
|
45
|
+
raise PolicyError(f"invalid disclosure policy: {e}") from None
|
|
46
|
+
if not isinstance(policy, str):
|
|
47
|
+
raise PolicyError(f"unknown disclosure policy {policy!r}: must be a name, dict or "
|
|
48
|
+
f"DisclosurePolicySpec, not {type(policy).__name__}")
|
|
49
|
+
try:
|
|
50
|
+
return POLICIES[policy].model_copy()
|
|
51
|
+
except KeyError:
|
|
52
|
+
raise PolicyError(f"unknown disclosure policy {policy!r}; "
|
|
53
|
+
f"choose from {sorted(POLICIES)} or pass a DisclosurePolicySpec") from None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def quantile_levels(n_rows: int, policy: DisclosurePolicySpec) -> np.ndarray:
|
|
57
|
+
"""Probability levels at which a marginal may store quantiles.
|
|
58
|
+
|
|
59
|
+
The count is capped by the policy resolution and by ``n_rows / min_category_support`` so each
|
|
60
|
+
stored quantile summarises at least ``min_category_support`` rows. Levels span the policy
|
|
61
|
+
tail-clip interval; tails beyond it must be modeled parametrically.
|
|
62
|
+
"""
|
|
63
|
+
lo, hi = support_clip_levels(n_rows, policy)
|
|
64
|
+
by_support = n_rows // max(1, policy.min_category_support)
|
|
65
|
+
k = int(max(1, min(policy.quantile_resolution, by_support))) # k intervals, k+1 levels
|
|
66
|
+
return np.linspace(lo, hi, k + 1)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
#: smallest gap between the lower and upper clip levels on tiny tables
|
|
70
|
+
_MIN_LEVEL_GAP = 1e-6
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def support_clip_levels(n_rows: int, policy: DisclosurePolicySpec) -> tuple[float, float]:
|
|
74
|
+
"""Tail-clip levels widened on small tables so that at least ``min_category_support`` rows lie
|
|
75
|
+
beyond each clipped end: ``max(tail_clip[0], m / n)`` and ``min(tail_clip[1], 1 - m / n)``.
|
|
76
|
+
|
|
77
|
+
On large tables (``n * tail_clip[0] >= m``) these are exactly the policy tail-clip levels. A
|
|
78
|
+
side the policy does not clip (level 0 or 1, the research profile) is left unclipped. When
|
|
79
|
+
the widened levels would cross (fewer than ``2 m`` rows) both collapse towards the median.
|
|
80
|
+
|
|
81
|
+
Such a collapsed (or nearly collapsed) grid carries no usable spread, so marginal fitters do
|
|
82
|
+
not store one: when ``needs_coarse_summary`` is true they store a coarse location/scale
|
|
83
|
+
summary instead (``models.marginals.SummaryMarginal``).
|
|
84
|
+
"""
|
|
85
|
+
lo, hi = policy.tail_clip
|
|
86
|
+
n = max(int(n_rows), 1)
|
|
87
|
+
frac = max(1, policy.min_category_support) / n
|
|
88
|
+
if lo > 0.0:
|
|
89
|
+
lo = max(lo, frac)
|
|
90
|
+
if hi < 1.0:
|
|
91
|
+
hi = min(hi, 1.0 - frac)
|
|
92
|
+
if lo >= hi:
|
|
93
|
+
lo, hi = min(lo, 0.5 - _MIN_LEVEL_GAP), max(hi, 0.5 + _MIN_LEVEL_GAP)
|
|
94
|
+
return float(lo), float(hi)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
#: a quantile grid is replaced by a coarse location/scale summary when its support-widened clip
|
|
98
|
+
#: level would exceed this tail probability (``n < m / SUMMARY_CLIP_LEVEL`` rows): the grid then
|
|
99
|
+
#: has at most ``1 / SUMMARY_CLIP_LEVEL`` intervals inside a clip interval so narrow that its
|
|
100
|
+
#: extrapolated exponential tails overstate (or, below ``2 m`` rows, erase) the spread
|
|
101
|
+
SUMMARY_CLIP_LEVEL = 0.1
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def needs_coarse_summary(n_rows: int, policy: DisclosurePolicySpec) -> bool:
|
|
105
|
+
"""Whether a column with ``n_rows`` values has too few rows to store a quantile grid safely:
|
|
106
|
+
every stored quantile must have ``min_category_support`` rows beyond it, which on small
|
|
107
|
+
tables pushes the clip levels deep into the body (see ``support_clip_levels``). A policy
|
|
108
|
+
that does not clip its tails (research) never needs the summary."""
|
|
109
|
+
lo, hi = policy.tail_clip
|
|
110
|
+
if lo <= 0.0 and hi >= 1.0:
|
|
111
|
+
return False
|
|
112
|
+
frac = max(1, policy.min_category_support) / max(int(n_rows), 1)
|
|
113
|
+
return frac > SUMMARY_CLIP_LEVEL
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def coarse_summary(values: np.ndarray, policy: DisclosurePolicySpec, *,
|
|
117
|
+
log_scale: bool = False) -> tuple[float, float]:
|
|
118
|
+
"""Location and scale (mean and standard deviation, of ``log(values)`` when ``log_scale``)
|
|
119
|
+
stored for a marginal with too few rows for a quantile grid. Both are aggregates over every
|
|
120
|
+
row and are rounded to at most 3 significant digits (fewer if the policy says so), with the
|
|
121
|
+
location rounded no finer than a hundredth of the scale; no order statistic is stored."""
|
|
122
|
+
v = np.asarray(values, dtype=float)
|
|
123
|
+
v = v[np.isfinite(v)]
|
|
124
|
+
if log_scale:
|
|
125
|
+
v = np.log(v)
|
|
126
|
+
if v.size == 0:
|
|
127
|
+
return float("nan"), float("nan")
|
|
128
|
+
loc = float(v.mean())
|
|
129
|
+
scale = float(v.std(ddof=1)) if v.size > 1 else 0.0
|
|
130
|
+
pol = policy.model_copy(update={"significant_digits": min(policy.significant_digits, 3)})
|
|
131
|
+
scale = coarsen(scale, pol)
|
|
132
|
+
if scale > 0:
|
|
133
|
+
step = 10.0 ** (math.floor(math.log10(scale)) - 2)
|
|
134
|
+
loc = float(round(loc / step) * step)
|
|
135
|
+
return coarsen(loc, policy), scale
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def pool_categories(labels: np.ndarray, counts: np.ndarray,
|
|
139
|
+
policy: DisclosurePolicySpec) -> tuple[np.ndarray, np.ndarray, int]:
|
|
140
|
+
"""Keep categories with support >= min_category_support; pool the rest into ``__other__``
|
|
141
|
+
(appended last). Returns (labels, counts, n_pooled). Order: by descending count, then label,
|
|
142
|
+
for determinism."""
|
|
143
|
+
labels = np.asarray(labels).astype(str)
|
|
144
|
+
counts = np.asarray(counts, dtype=np.int64)
|
|
145
|
+
# A literal "__other__" label in the source is merged into the pool, never kept twice.
|
|
146
|
+
src_other = int(counts[labels == OTHER].sum())
|
|
147
|
+
labels, counts = labels[labels != OTHER], counts[labels != OTHER]
|
|
148
|
+
order = sorted(range(len(labels)), key=lambda i: (-counts[i], labels[i]))
|
|
149
|
+
labels, counts = labels[order], counts[order]
|
|
150
|
+
keep = counts >= policy.min_category_support
|
|
151
|
+
n_pooled = int((~keep).sum())
|
|
152
|
+
if n_pooled == 0 and src_other == 0:
|
|
153
|
+
return labels, counts, 0
|
|
154
|
+
pooled_mass = int(counts[~keep].sum()) + src_other
|
|
155
|
+
# Never store a below-threshold exact count for the pool (it would reveal a rare
|
|
156
|
+
# category's size when only one is pooled): round it up to the support minimum.
|
|
157
|
+
if 0 < pooled_mass < policy.min_category_support:
|
|
158
|
+
pooled_mass = policy.min_category_support
|
|
159
|
+
kept_l, kept_c = labels[keep], counts[keep]
|
|
160
|
+
return (np.append(kept_l, OTHER).astype(str), np.append(kept_c, pooled_mass), n_pooled)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def coarsen(x: float, policy: DisclosurePolicySpec) -> float:
|
|
164
|
+
"""Round to the policy's significant digits."""
|
|
165
|
+
if x is None or not math.isfinite(x) or x == 0:
|
|
166
|
+
return float(x) if x is not None else x
|
|
167
|
+
digits = policy.significant_digits - int(math.floor(math.log10(abs(x)))) - 1
|
|
168
|
+
return float(round(x, digits))
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def coarsen_array(a: np.ndarray, policy: DisclosurePolicySpec) -> np.ndarray:
|
|
172
|
+
return np.array([coarsen(float(v), policy) for v in np.asarray(a, dtype=float).ravel()]
|
|
173
|
+
).reshape(np.shape(a))
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def clip_bounds(values: np.ndarray, policy: DisclosurePolicySpec) -> tuple[float, float]:
|
|
177
|
+
"""Support bounds that do not reveal exact extremes: the tail-clip quantiles, with at least
|
|
178
|
+
``min_category_support`` rows beyond each bound (see ``_clip_stat``)."""
|
|
179
|
+
v = np.asarray(values, dtype=float)
|
|
180
|
+
v = v[np.isfinite(v)]
|
|
181
|
+
if v.size == 0:
|
|
182
|
+
return (float("nan"), float("nan"))
|
|
183
|
+
return (coarsen(_clip_stat(v, policy, upper=False), policy),
|
|
184
|
+
coarsen(_clip_stat(v, policy, upper=True), policy))
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _clip_stat(v: np.ndarray, policy: DisclosurePolicySpec, *, upper: bool) -> float:
|
|
188
|
+
"""One clip bound. When the policy tail-clip level already leaves ``min_category_support``
|
|
189
|
+
rows beyond it (large tables) this is the interpolated tail-clip quantile, as always. On
|
|
190
|
+
smaller tables it is the non-interpolating order statistic with ``min_category_support`` rows
|
|
191
|
+
beyond it (an interpolated quantile would blend in, and so reveal, the extreme rows)."""
|
|
192
|
+
n = v.size
|
|
193
|
+
m = max(1, policy.min_category_support)
|
|
194
|
+
level = policy.tail_clip[1] if upper else policy.tail_clip[0]
|
|
195
|
+
tail = (1.0 - level) if upper else level
|
|
196
|
+
if tail <= 0.0 or tail * n >= m:
|
|
197
|
+
return float(np.quantile(v, level))
|
|
198
|
+
s = np.sort(v)
|
|
199
|
+
i = min(m, (n - 1) // 2) # i rows strictly before s[i] (or after, mirrored)
|
|
200
|
+
return float(s[n - 1 - i] if upper else s[i])
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def suppress_counts(counts: np.ndarray, policy: DisclosurePolicySpec) -> np.ndarray:
|
|
204
|
+
"""Zero out cells below rare_cell_min (used for contingency tables)."""
|
|
205
|
+
c = np.asarray(counts, dtype=np.int64).copy()
|
|
206
|
+
c[c < policy.rare_cell_min] = 0
|
|
207
|
+
return c
|