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,192 @@
|
|
|
1
|
+
"""Temporal integration: fit a temporal section into a fingerprint, generate from it, and
|
|
2
|
+
validate temporal behaviour.
|
|
3
|
+
|
|
4
|
+
Temporal targets (ACF, seasonal profile, spectrum, trend, interarrival, burstiness,
|
|
5
|
+
transitions) are stored inside the temporal section rather than in the cross-sectional
|
|
6
|
+
calibration/sealed target sets: they only make sense for time-ordered output produced by
|
|
7
|
+
``generate_temporal``. The calibrator never adjusts temporal models, so these targets are
|
|
8
|
+
evaluation-only evidence.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
import polars as pl
|
|
17
|
+
|
|
18
|
+
from ectype.errors import FingerprintError, ProfilingError
|
|
19
|
+
from ectype.fingerprint.core import ArrayStore, Fingerprint
|
|
20
|
+
from ectype.fingerprint.schema import ColumnSchema, DisclosurePolicySpec
|
|
21
|
+
from ectype.rng import stream
|
|
22
|
+
from ectype.temporal.contract import TemporalRequest
|
|
23
|
+
from ectype.types import LogicalType, Role
|
|
24
|
+
|
|
25
|
+
_NUMERIC = (LogicalType.CONTINUOUS, LogicalType.DISCRETE)
|
|
26
|
+
|
|
27
|
+
#: metric kinds evaluated per value column, by temporal shape
|
|
28
|
+
_SHAPE_METRICS = {
|
|
29
|
+
"series": ("temporal.acf", "temporal.seasonal_profile", "temporal.spectrum",
|
|
30
|
+
"temporal.trend"),
|
|
31
|
+
"panel": ("temporal.acf", "temporal.trend"),
|
|
32
|
+
"events": ("temporal.interarrival", "temporal.burstiness"),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _json_safe(obj: Any) -> Any:
|
|
37
|
+
if isinstance(obj, dict):
|
|
38
|
+
return {str(k): _json_safe(v) for k, v in obj.items()}
|
|
39
|
+
if isinstance(obj, (list, tuple)):
|
|
40
|
+
return [_json_safe(v) for v in obj]
|
|
41
|
+
if isinstance(obj, np.generic):
|
|
42
|
+
return obj.item()
|
|
43
|
+
if isinstance(obj, np.ndarray):
|
|
44
|
+
return _json_safe(obj.tolist())
|
|
45
|
+
if isinstance(obj, float) and not np.isfinite(obj):
|
|
46
|
+
return str(obj)
|
|
47
|
+
return obj
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _temporal_targets(df: pl.DataFrame, shape: Any, value_cols: list[str],
|
|
51
|
+
columns: dict[str, ColumnSchema], policy: DisclosurePolicySpec,
|
|
52
|
+
seed: int) -> list[dict[str, Any]]:
|
|
53
|
+
from ectype.validation.metrics.base import METRICS
|
|
54
|
+
|
|
55
|
+
targets: list[dict[str, Any]] = []
|
|
56
|
+
# Event metrics (interarrival, burstiness) measure the time column itself: it is always
|
|
57
|
+
# a target column for events, whatever the mark (value) columns are.
|
|
58
|
+
metric_cols = list(value_cols)
|
|
59
|
+
if shape.shape == "events" and shape.time_col not in metric_cols:
|
|
60
|
+
metric_cols = [shape.time_col, *metric_cols]
|
|
61
|
+
for kind in _SHAPE_METRICS.get(shape.shape, ()):
|
|
62
|
+
if kind not in METRICS:
|
|
63
|
+
continue
|
|
64
|
+
for col in metric_cols:
|
|
65
|
+
if col not in columns:
|
|
66
|
+
continue
|
|
67
|
+
m = METRICS.get(kind)(time_col=shape.time_col) # type: ignore[call-arg]
|
|
68
|
+
cs = [columns[col]]
|
|
69
|
+
if not m.applies_to(tuple(cs)):
|
|
70
|
+
continue
|
|
71
|
+
try:
|
|
72
|
+
value = m.measure(df, cs, policy)
|
|
73
|
+
floor = m.noise_floor(df, cs, policy, stream(seed, "temporal", kind, col),
|
|
74
|
+
n_boot=8)
|
|
75
|
+
except Exception as e: # a metric that cannot measure this column is skipped
|
|
76
|
+
targets.append({"id": f"{kind}:{col}", "skipped": str(e)})
|
|
77
|
+
continue
|
|
78
|
+
targets.append({
|
|
79
|
+
"id": f"{kind}:{col}", "metric": kind, "columns": [col],
|
|
80
|
+
"time_col": shape.time_col, "value": _json_safe(value),
|
|
81
|
+
"tolerance": float(max(m.default_tolerance, floor * 1.5)),
|
|
82
|
+
})
|
|
83
|
+
return targets
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def fit_temporal_section(df: pl.DataFrame, columns: list[ColumnSchema],
|
|
87
|
+
policy: DisclosurePolicySpec, seed: int, store: ArrayStore, *,
|
|
88
|
+
time_col: str | None = None, entity_col: str | None = None,
|
|
89
|
+
value_cols: list[str] | None = None,
|
|
90
|
+
**model_kwargs: Any) -> dict[str, Any] | None:
|
|
91
|
+
"""Detect the temporal shape and fit the matching model.
|
|
92
|
+
|
|
93
|
+
Returns the manifest ``temporal`` section (JSON-safe, arrays in ``store``) or ``None`` for
|
|
94
|
+
cross-sectional data.
|
|
95
|
+
"""
|
|
96
|
+
from ectype.temporal import detect_temporal_shape, fit_temporal
|
|
97
|
+
|
|
98
|
+
shape = detect_temporal_shape(df, time_col=time_col, entity_col=entity_col)
|
|
99
|
+
if shape.shape == "cross_sectional" or shape.time_col is None:
|
|
100
|
+
return None
|
|
101
|
+
by_name = {c.name: c for c in columns}
|
|
102
|
+
if value_cols is None:
|
|
103
|
+
if shape.shape == "events":
|
|
104
|
+
value_cols = [c.name for c in columns
|
|
105
|
+
if c.role == Role.FEATURE and c.name not in
|
|
106
|
+
(shape.time_col, shape.entity_col)
|
|
107
|
+
and c.logical_type in (*_NUMERIC, LogicalType.CATEGORICAL,
|
|
108
|
+
LogicalType.BOOLEAN)]
|
|
109
|
+
else:
|
|
110
|
+
value_cols = [c.name for c in columns
|
|
111
|
+
if c.role == Role.FEATURE and c.logical_type in _NUMERIC
|
|
112
|
+
and c.name not in (shape.time_col, shape.entity_col)]
|
|
113
|
+
if shape.shape in ("series", "panel") and not value_cols:
|
|
114
|
+
raise ProfilingError(f"temporal shape {shape.shape!r} detected but no numeric value "
|
|
115
|
+
f"columns to model; pass value_cols=")
|
|
116
|
+
model = fit_temporal(df, shape, value_cols, policy, seed, **model_kwargs)
|
|
117
|
+
section = dict(model.to_section(store))
|
|
118
|
+
section["enabled"] = True
|
|
119
|
+
section["shape"] = {
|
|
120
|
+
"shape": shape.shape, "time_col": shape.time_col, "entity_col": shape.entity_col,
|
|
121
|
+
"cadence": getattr(shape, "cadence", None),
|
|
122
|
+
"confidence": float(getattr(shape, "confidence", 1.0)),
|
|
123
|
+
}
|
|
124
|
+
section["value_cols"] = list(value_cols)
|
|
125
|
+
section["targets"] = _temporal_targets(df, shape, list(value_cols), by_name, policy, seed)
|
|
126
|
+
return _json_safe(section)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _model_from_fp(fp: Fingerprint) -> Any:
|
|
130
|
+
from ectype.temporal import temporal_model_from_section
|
|
131
|
+
|
|
132
|
+
sec = fp.manifest.temporal
|
|
133
|
+
if sec is None or not sec.enabled:
|
|
134
|
+
raise FingerprintError("this fingerprint has no temporal model; fit with "
|
|
135
|
+
"Profiler(temporal='auto') on time-ordered data")
|
|
136
|
+
return temporal_model_from_section(sec.model_dump(mode="json"), fp.arrays)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def generate_temporal(fp: Fingerprint, request: TemporalRequest | dict[str, Any] | None = None,
|
|
140
|
+
seed: int | None = None, **request_kwargs: Any) -> pl.DataFrame:
|
|
141
|
+
"""Generate time-ordered output from a fingerprint's temporal section.
|
|
142
|
+
|
|
143
|
+
The output contains the time column, the entity column (panels/events with entities,
|
|
144
|
+
regenerated identifiers) and the modeled value/mark columns. Trend extrapolation into a new
|
|
145
|
+
period is a modeling assumption, recorded in the section's ``assumptions``.
|
|
146
|
+
"""
|
|
147
|
+
model = _model_from_fp(fp)
|
|
148
|
+
if request is None:
|
|
149
|
+
request = TemporalRequest(**request_kwargs)
|
|
150
|
+
elif isinstance(request, dict):
|
|
151
|
+
request = TemporalRequest(**request)
|
|
152
|
+
s = fp.manifest.generation.default_seed if seed is None else int(seed)
|
|
153
|
+
return model.generate(request, s)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def validate_temporal(fp: Fingerprint, synthetic: pl.DataFrame) -> dict[str, Any]:
|
|
157
|
+
"""Evaluate the stored temporal targets on time-ordered synthetic output."""
|
|
158
|
+
from ectype.validation.metrics.base import METRICS
|
|
159
|
+
|
|
160
|
+
sec = fp.manifest.temporal
|
|
161
|
+
if sec is None or not sec.enabled:
|
|
162
|
+
raise FingerprintError("this fingerprint has no temporal section")
|
|
163
|
+
data = sec.model_dump(mode="json")
|
|
164
|
+
by_name = {c.name: c for c in fp.manifest.columns}
|
|
165
|
+
policy = fp.manifest.disclosure_policy
|
|
166
|
+
results = []
|
|
167
|
+
for t in data.get("targets", []):
|
|
168
|
+
if "metric" not in t:
|
|
169
|
+
continue
|
|
170
|
+
col = t["columns"][0]
|
|
171
|
+
if col not in synthetic.columns or t["time_col"] not in synthetic.columns:
|
|
172
|
+
results.append({**t, "distance": None, "passed": False,
|
|
173
|
+
"reason": "column missing from synthetic output"})
|
|
174
|
+
continue
|
|
175
|
+
m = METRICS.get(t["metric"])(time_col=t["time_col"]) # type: ignore[call-arg]
|
|
176
|
+
try:
|
|
177
|
+
cand = m.measure(synthetic, [by_name[col]], policy)
|
|
178
|
+
d = float(m.distance(t["value"], cand))
|
|
179
|
+
except Exception as e:
|
|
180
|
+
results.append({**t, "distance": None, "passed": False, "reason": str(e)})
|
|
181
|
+
continue
|
|
182
|
+
results.append({"id": t["id"], "metric": t["metric"], "columns": t["columns"],
|
|
183
|
+
"distance": d, "tolerance": t["tolerance"],
|
|
184
|
+
"passed": d <= t["tolerance"]})
|
|
185
|
+
n_pass = sum(1 for r in results if r["passed"])
|
|
186
|
+
out = {"section": "temporal", "n_targets": len(results), "n_passed": n_pass,
|
|
187
|
+
"passed": bool(results) and n_pass == len(results), "results": results,
|
|
188
|
+
"assumptions": data.get("assumptions", [])}
|
|
189
|
+
if not results:
|
|
190
|
+
# zero checks is not evidence of temporal fidelity
|
|
191
|
+
out["reason"] = "no temporal targets"
|
|
192
|
+
return out
|