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,212 @@
|
|
|
1
|
+
"""The ``.dfpr`` relational container: a deterministic zip of per-table ``.dfp`` fingerprints.
|
|
2
|
+
|
|
3
|
+
Layout::
|
|
4
|
+
|
|
5
|
+
relational.json the relational section (schema, relationships, targets)
|
|
6
|
+
tables/<name>.dfp one complete single-table fingerprint per table
|
|
7
|
+
checksums.sha256 "<sha256-hex> <path>" for every other file
|
|
8
|
+
|
|
9
|
+
Loading fails closed, like ``.dfp``: path safety -> file allow-list -> size limits ->
|
|
10
|
+
checksums -> JSON parse -> section schema -> version -> table set matches the schema -> each
|
|
11
|
+
inner ``.dfp`` loaded with :meth:`Fingerprint.load` (its own checksums, schema and seal hash)
|
|
12
|
+
from a temporary file -> each table's content hash matches the one committed in
|
|
13
|
+
``relational.json``. Nothing is imported or executed.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import tempfile
|
|
21
|
+
import zipfile
|
|
22
|
+
from pathlib import Path, PurePosixPath
|
|
23
|
+
|
|
24
|
+
from pydantic import ValidationError
|
|
25
|
+
|
|
26
|
+
from ectype.errors import FingerprintError, IntegrityError, VersionError
|
|
27
|
+
from ectype.fingerprint.container import canonical_json, sha256_bytes
|
|
28
|
+
from ectype.fingerprint.core import Fingerprint
|
|
29
|
+
from ectype.relational.fit import (
|
|
30
|
+
RELATIONAL_FORMAT_VERSION,
|
|
31
|
+
RelationalFingerprint,
|
|
32
|
+
RelationalSection,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
RELATIONAL = "relational.json"
|
|
36
|
+
CHECKSUMS = "checksums.sha256"
|
|
37
|
+
_TABLES = "tables/"
|
|
38
|
+
_MAX_MEMBER_BYTES = 4 * 1024**3
|
|
39
|
+
_MAX_MEMBERS = 10_000
|
|
40
|
+
_ZIP_DATE = (2000, 1, 1, 0, 0, 0)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _check_member_name(name: str) -> None:
|
|
44
|
+
p = PurePosixPath(name)
|
|
45
|
+
if (name.startswith("/") or "\\" in name or ".." in p.parts or ":" in name
|
|
46
|
+
or name.endswith("/")):
|
|
47
|
+
raise IntegrityError(f"unsafe path in relational archive: {name!r}")
|
|
48
|
+
if name in (RELATIONAL, CHECKSUMS):
|
|
49
|
+
return
|
|
50
|
+
if not name.startswith(_TABLES) or len(p.parts) != 2 or not name.endswith(".dfp"):
|
|
51
|
+
raise IntegrityError(f"unexpected file in relational archive: {name!r}")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _check_version(version: str) -> None:
|
|
55
|
+
try:
|
|
56
|
+
major = int(str(version).split(".")[0])
|
|
57
|
+
except ValueError:
|
|
58
|
+
raise VersionError(f"invalid relational format version {version!r}") from None
|
|
59
|
+
if major != int(RELATIONAL_FORMAT_VERSION.split(".")[0]):
|
|
60
|
+
raise VersionError(f"relational format {version} is not supported by this engine "
|
|
61
|
+
f"(supports {RELATIONAL_FORMAT_VERSION.split('.')[0]}.x)")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def write_relational(rfp: RelationalFingerprint, path: str | Path) -> Path:
|
|
65
|
+
"""Write a deterministic ``.dfpr``. Returns the path written."""
|
|
66
|
+
files: dict[str, bytes] = {}
|
|
67
|
+
section = rfp.section.model_copy(deep=True)
|
|
68
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
69
|
+
for name in section.schema_.tables:
|
|
70
|
+
fp = rfp.fingerprints[name]
|
|
71
|
+
section.tables[name] = section.tables[name].model_copy(
|
|
72
|
+
update={"content_hash": fp.content_hash()})
|
|
73
|
+
p = fp.save(Path(tmp) / f"{name}.dfp")
|
|
74
|
+
files[f"{_TABLES}{name}.dfp"] = p.read_bytes()
|
|
75
|
+
files[RELATIONAL] = canonical_json(section.model_dump(mode="json", by_alias=True))
|
|
76
|
+
for n in files:
|
|
77
|
+
_check_member_name(n)
|
|
78
|
+
lines = [f"{sha256_bytes(data)} {n}" for n, data in sorted(files.items())]
|
|
79
|
+
files[CHECKSUMS] = ("\n".join(lines) + "\n").encode("utf-8")
|
|
80
|
+
|
|
81
|
+
out = Path(path)
|
|
82
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
83
|
+
tmp_out = out.with_suffix(out.suffix + ".tmp")
|
|
84
|
+
with zipfile.ZipFile(tmp_out, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
|
85
|
+
for n in sorted(files):
|
|
86
|
+
info = zipfile.ZipInfo(n, date_time=_ZIP_DATE)
|
|
87
|
+
info.compress_type = zipfile.ZIP_DEFLATED
|
|
88
|
+
info.external_attr = 0o644 << 16
|
|
89
|
+
zf.writestr(info, files[n])
|
|
90
|
+
tmp_out.replace(out)
|
|
91
|
+
return out
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _check_consistency(section: RelationalSection) -> None:
|
|
95
|
+
"""Cross-check the parts of ``relational.json`` that must agree with each other.
|
|
96
|
+
|
|
97
|
+
Checksums only prove the archive was not modified after writing; a section written (or
|
|
98
|
+
re-signed) with contradictory parts would otherwise load and generate tables that break
|
|
99
|
+
referential integrity. Raises :class:`IntegrityError` on any disagreement.
|
|
100
|
+
"""
|
|
101
|
+
from ectype.relational.schema import count_column
|
|
102
|
+
|
|
103
|
+
schema = section.schema_
|
|
104
|
+
rels = schema.relationships
|
|
105
|
+
for table, info in section.tables.items():
|
|
106
|
+
if info.primary_key != schema.tables[table]:
|
|
107
|
+
raise IntegrityError(f"table {table!r}: primary key {info.primary_key!r} does not "
|
|
108
|
+
f"match the schema's {schema.tables[table]!r}")
|
|
109
|
+
parents = [r for r in rels if r.child == table]
|
|
110
|
+
if len(parents) > 1:
|
|
111
|
+
raise IntegrityError(f"table {table!r} has several parents; the relational "
|
|
112
|
+
f"generator supports one parent per table")
|
|
113
|
+
expected_fks = {r.child_fk: r.parent for r in parents}
|
|
114
|
+
if dict(info.foreign_keys) != expected_fks:
|
|
115
|
+
raise IntegrityError(f"table {table!r}: foreign keys {dict(info.foreign_keys)} do "
|
|
116
|
+
f"not match the schema relationships {expected_fks}")
|
|
117
|
+
for fk in info.foreign_keys:
|
|
118
|
+
if fk not in info.columns:
|
|
119
|
+
raise IntegrityError(f"table {table!r}: foreign key {fk!r} is not one of its "
|
|
120
|
+
f"columns")
|
|
121
|
+
if info.primary_key not in info.columns:
|
|
122
|
+
raise IntegrityError(f"table {table!r}: primary key {info.primary_key!r} is not "
|
|
123
|
+
f"one of its columns")
|
|
124
|
+
expected_counts = [count_column(r.child) for r in rels if r.parent == table]
|
|
125
|
+
if sorted(info.count_columns) != sorted(expected_counts):
|
|
126
|
+
raise IntegrityError(f"table {table!r}: count columns {info.count_columns} do not "
|
|
127
|
+
f"match its child relationships")
|
|
128
|
+
fitted = [f.relationship for f in section.relationships]
|
|
129
|
+
if sorted(r.key for r in fitted) != sorted(r.key for r in rels):
|
|
130
|
+
raise IntegrityError("fitted relationships "
|
|
131
|
+
f"{sorted(r.key for r in fitted)} do not match the schema "
|
|
132
|
+
f"relationships {sorted(r.key for r in rels)}")
|
|
133
|
+
for f in section.relationships:
|
|
134
|
+
if f.count_column != count_column(f.relationship.child):
|
|
135
|
+
raise IntegrityError(f"relationship {f.relationship.key}: count column "
|
|
136
|
+
f"{f.count_column!r} is not the expected "
|
|
137
|
+
f"{count_column(f.relationship.child)!r}")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def read_relational(path: str | os.PathLike[str]) -> RelationalFingerprint:
|
|
141
|
+
"""Load and fully validate a ``.dfpr``. Raises on any integrity problem."""
|
|
142
|
+
p = Path(path)
|
|
143
|
+
if not p.is_file():
|
|
144
|
+
raise FingerprintError(f"relational fingerprint not found: {p}")
|
|
145
|
+
try:
|
|
146
|
+
zf = zipfile.ZipFile(p, "r")
|
|
147
|
+
except zipfile.BadZipFile as e:
|
|
148
|
+
raise IntegrityError(f"{p} is not a valid relational archive: {e}") from None
|
|
149
|
+
with zf:
|
|
150
|
+
infos = zf.infolist()
|
|
151
|
+
if len(infos) > _MAX_MEMBERS:
|
|
152
|
+
raise IntegrityError("relational archive has too many members")
|
|
153
|
+
names = []
|
|
154
|
+
for info in infos:
|
|
155
|
+
_check_member_name(info.filename)
|
|
156
|
+
if info.file_size > _MAX_MEMBER_BYTES:
|
|
157
|
+
raise IntegrityError(f"member too large: {info.filename}")
|
|
158
|
+
names.append(info.filename)
|
|
159
|
+
if len(set(names)) != len(names):
|
|
160
|
+
raise IntegrityError("duplicate members in relational archive")
|
|
161
|
+
if CHECKSUMS not in names or RELATIONAL not in names:
|
|
162
|
+
raise IntegrityError("relational archive lacks relational.json or checksums.sha256")
|
|
163
|
+
data = {n: zf.read(n) for n in names}
|
|
164
|
+
|
|
165
|
+
expected: dict[str, str] = {}
|
|
166
|
+
for line in data[CHECKSUMS].decode("utf-8").splitlines():
|
|
167
|
+
if not line.strip():
|
|
168
|
+
continue
|
|
169
|
+
digest, _, name = line.partition(" ")
|
|
170
|
+
if not name or len(digest) != 64:
|
|
171
|
+
raise IntegrityError(f"malformed checksum line: {line!r}")
|
|
172
|
+
expected[name] = digest
|
|
173
|
+
actual = set(data) - {CHECKSUMS}
|
|
174
|
+
if set(expected) != actual:
|
|
175
|
+
raise IntegrityError(f"checksum list mismatch (unlisted: {sorted(actual - set(expected))}"
|
|
176
|
+
f", missing: {sorted(set(expected) - actual)})")
|
|
177
|
+
for name, digest in expected.items():
|
|
178
|
+
if sha256_bytes(data[name]) != digest:
|
|
179
|
+
raise IntegrityError(f"checksum mismatch for {name}: file was modified")
|
|
180
|
+
|
|
181
|
+
try:
|
|
182
|
+
raw = json.loads(data[RELATIONAL])
|
|
183
|
+
except json.JSONDecodeError as e:
|
|
184
|
+
raise IntegrityError(f"relational.json is not valid JSON: {e}") from None
|
|
185
|
+
if not isinstance(raw, dict) or raw.get("format") != "dfpr":
|
|
186
|
+
raise FingerprintError("relational.json is not a dfpr section")
|
|
187
|
+
_check_version(raw.get("version", ""))
|
|
188
|
+
try:
|
|
189
|
+
section = RelationalSection.model_validate(raw)
|
|
190
|
+
except ValidationError as e:
|
|
191
|
+
raise FingerprintError(f"relational.json failed schema validation:\n{e}") from None
|
|
192
|
+
|
|
193
|
+
tables = set(section.schema_.tables)
|
|
194
|
+
members = {n[len(_TABLES):-len(".dfp")] for n in data if n.startswith(_TABLES)}
|
|
195
|
+
if members != tables or set(section.tables) != tables:
|
|
196
|
+
raise IntegrityError(f"archive tables {sorted(members)} do not match the schema "
|
|
197
|
+
f"tables {sorted(tables)}")
|
|
198
|
+
_check_consistency(section)
|
|
199
|
+
fps: dict[str, Fingerprint] = {}
|
|
200
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
201
|
+
for name in section.schema_.tables:
|
|
202
|
+
fpath = Path(tmp) / f"{name}.dfp"
|
|
203
|
+
fpath.write_bytes(data[f"{_TABLES}{name}.dfp"])
|
|
204
|
+
fp = Fingerprint.load(fpath)
|
|
205
|
+
if fp.content_hash() != section.tables[name].content_hash:
|
|
206
|
+
raise IntegrityError(f"table {name!r}: fingerprint content does not match the "
|
|
207
|
+
f"hash committed in relational.json")
|
|
208
|
+
fps[name] = fp
|
|
209
|
+
return RelationalFingerprint(section=section, fingerprints=fps)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
__all__ = ["read_relational", "write_relational"]
|