-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathartifacts.py
More file actions
333 lines (303 loc) · 11.6 KB
/
artifacts.py
File metadata and controls
333 lines (303 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
65
66
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
112
113
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""Artifact specifications for the Stage 1 dataset build."""
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass
from policyengine_us_data.pipeline_metadata import pipeline_node
ScriptOutput = str | list[str]
@dataclass(frozen=True, kw_only=True)
class DatasetArtifactSpec:
"""A durable or checkpointed output produced by Stage 1."""
filename: str
logical_name: str
artifact_family: str
substage_id: str
period: int | None = None
storage_path: str | None = None
script_path: str | None = None
required: bool = True
required_for_stage_2: bool = False
yearless_alias: bool = False
contract_output: bool = True
pipeline_output: bool = True
diagnostic_output: bool = False
diagnostic_kind: str | None = None
skip_when_enhanced_cps_skipped: bool = False
skip_when_stage_5_skipped: bool = False
_UPRATING_SCRIPT = "policyengine_us_data/utils/uprating.py"
_ACS_SCRIPT = "policyengine_us_data/datasets/acs/acs.py"
_IRS_PUF_SCRIPT = "policyengine_us_data/datasets/puf/irs_puf.py"
_CPS_SCRIPT = "policyengine_us_data/datasets/cps/cps.py"
_PUF_SCRIPT = "policyengine_us_data/datasets/puf/puf.py"
_EXTENDED_CPS_SCRIPT = "policyengine_us_data/datasets/cps/extended_cps.py"
_ENHANCED_CPS_SCRIPT = "policyengine_us_data/datasets/cps/enhanced_cps.py"
_STRATIFIED_CPS_SCRIPT = "policyengine_us_data/calibration/create_stratified_cps.py"
_SOURCE_IMPUTED_CPS_SCRIPT = (
"policyengine_us_data/calibration/create_source_imputed_cps.py"
)
_SMALL_ENHANCED_CPS_SCRIPT = "policyengine_us_data/datasets/cps/small_enhanced_cps.py"
STAGE_1_ARTIFACT_SPECS: tuple[DatasetArtifactSpec, ...] = (
DatasetArtifactSpec(
filename="uprating_factors.csv",
logical_name="uprating_factors",
artifact_family="uprating_table",
substage_id="1a_raw_data_download",
storage_path="policyengine_us_data/storage/uprating_factors.csv",
script_path=_UPRATING_SCRIPT,
contract_output=False,
pipeline_output=False,
),
DatasetArtifactSpec(
filename="acs_2022.h5",
logical_name="acs_2022",
artifact_family="dataset",
period=2022,
substage_id="1b_base_dataset_construction",
storage_path="policyengine_us_data/storage/acs_2022.h5",
script_path=_ACS_SCRIPT,
),
DatasetArtifactSpec(
filename="irs_puf_2015.h5",
logical_name="irs_puf_2015",
artifact_family="dataset",
period=2015,
substage_id="1b_base_dataset_construction",
storage_path="policyengine_us_data/storage/irs_puf_2015.h5",
script_path=_IRS_PUF_SCRIPT,
),
DatasetArtifactSpec(
filename="cps_2024.h5",
logical_name="cps_2024",
artifact_family="dataset",
period=2024,
substage_id="1b_base_dataset_construction",
storage_path="policyengine_us_data/storage/cps_2024.h5",
script_path=_CPS_SCRIPT,
),
DatasetArtifactSpec(
filename="puf_2024.h5",
logical_name="puf_2024",
artifact_family="dataset",
period=2024,
substage_id="1b_base_dataset_construction",
storage_path="policyengine_us_data/storage/puf_2024.h5",
script_path=_PUF_SCRIPT,
),
DatasetArtifactSpec(
filename="extended_cps_2024.h5",
logical_name="extended_cps_2024",
artifact_family="dataset",
period=2024,
substage_id="1c_extended_cps_puf_clone",
storage_path="policyengine_us_data/storage/extended_cps_2024.h5",
script_path=_EXTENDED_CPS_SCRIPT,
),
DatasetArtifactSpec(
filename="enhanced_cps_2024.h5",
logical_name="enhanced_cps_2024",
artifact_family="dataset",
period=2024,
substage_id="1d_enhanced_cps_reweighting",
storage_path="policyengine_us_data/storage/enhanced_cps_2024.h5",
script_path=_ENHANCED_CPS_SCRIPT,
skip_when_enhanced_cps_skipped=True,
),
DatasetArtifactSpec(
filename="enhanced_cps_2024.clone_diagnostics.json",
logical_name="enhanced_cps_2024_clone_diagnostics",
artifact_family="diagnostic",
period=2024,
substage_id="1d_enhanced_cps_reweighting",
storage_path=(
"policyengine_us_data/storage/enhanced_cps_2024.clone_diagnostics.json"
),
script_path=_ENHANCED_CPS_SCRIPT,
contract_output=False,
pipeline_output=False,
skip_when_enhanced_cps_skipped=True,
),
DatasetArtifactSpec(
filename="calibration_log.csv",
logical_name="enhanced_cps_calibration_log",
artifact_family="log",
substage_id="1d_enhanced_cps_reweighting",
storage_path="calibration_log.csv",
script_path=_ENHANCED_CPS_SCRIPT,
contract_output=False,
pipeline_output=False,
skip_when_enhanced_cps_skipped=True,
),
DatasetArtifactSpec(
filename="stratified_extended_cps_2024.h5",
logical_name="stratified_extended_cps_2024",
artifact_family="dataset",
period=2024,
substage_id="1e_stratified_cps",
storage_path="policyengine_us_data/storage/stratified_extended_cps_2024.h5",
script_path=_STRATIFIED_CPS_SCRIPT,
),
DatasetArtifactSpec(
filename="source_imputed_stratified_extended_cps_2024.h5",
logical_name="source_imputed_stratified_extended_cps_2024",
artifact_family="dataset",
period=2024,
substage_id="1f_source_imputation",
storage_path=(
"policyengine_us_data/storage/"
"source_imputed_stratified_extended_cps_2024.h5"
),
script_path=_SOURCE_IMPUTED_CPS_SCRIPT,
required_for_stage_2=True,
skip_when_stage_5_skipped=True,
),
DatasetArtifactSpec(
filename="small_enhanced_cps_2024.h5",
logical_name="small_enhanced_cps_2024",
artifact_family="dataset",
period=2024,
substage_id="1d_enhanced_cps_reweighting",
storage_path="policyengine_us_data/storage/small_enhanced_cps_2024.h5",
script_path=_SMALL_ENHANCED_CPS_SCRIPT,
skip_when_enhanced_cps_skipped=True,
skip_when_stage_5_skipped=True,
),
DatasetArtifactSpec(
filename="source_imputed_stratified_extended_cps.h5",
logical_name="source_imputed_stratified_extended_cps",
artifact_family="dataset",
period=2024,
substage_id="1f_source_imputation",
required_for_stage_2=True,
yearless_alias=True,
skip_when_stage_5_skipped=True,
),
DatasetArtifactSpec(
filename="policy_data.db",
logical_name="policy_data_db",
artifact_family="target_database",
substage_id="1g_stage_base_datasets",
storage_path="policyengine_us_data/storage/calibration/policy_data.db",
required_for_stage_2=True,
),
DatasetArtifactSpec(
filename="calibration_weights.npy",
logical_name="calibration_weights",
artifact_family="legacy_optional_weight",
substage_id="1g_stage_base_datasets",
storage_path="policyengine_us_data/storage/calibration_weights.npy",
required=False,
contract_output=False,
),
DatasetArtifactSpec(
filename="build_log.txt",
logical_name="build_log",
artifact_family="log",
substage_id="1g_stage_base_datasets",
storage_path="build_log.txt",
),
DatasetArtifactSpec(
filename="data_build_checkpoint_stats.json",
logical_name="data_build_checkpoint_stats",
artifact_family="execution_metadata",
substage_id="1g_stage_base_datasets",
),
DatasetArtifactSpec(
filename="dataset_inventory.json",
logical_name="dataset_inventory",
artifact_family="diagnostic",
substage_id="1g_stage_base_datasets",
required=False,
contract_output=False,
pipeline_output=False,
diagnostic_output=True,
diagnostic_kind="dataset_inventory",
),
DatasetArtifactSpec(
filename="source_dataset_schema_summary.json",
logical_name="source_dataset_schema_summary",
artifact_family="diagnostic",
substage_id="1f_source_imputation",
required=False,
contract_output=False,
pipeline_output=False,
diagnostic_output=True,
diagnostic_kind="source_dataset_schema_summary",
skip_when_stage_5_skipped=True,
),
DatasetArtifactSpec(
filename="target_database_schema_summary.json",
logical_name="target_database_schema_summary",
artifact_family="diagnostic",
substage_id="1g_stage_base_datasets",
required=False,
contract_output=False,
pipeline_output=False,
diagnostic_output=True,
diagnostic_kind="target_database_schema_summary",
),
)
@pipeline_node(
id="stage_1_dataset_artifact_specs",
label="Stage 1 Dataset Artifact Specs",
node_type="library",
description="Canonical artifact inventory for Stage 1 dataset-build outputs.",
source_file="policyengine_us_data/build_datasets/artifacts.py",
status="current",
stability="stable",
pathways=["data_build", "stage_contracts", "pipeline_docs"],
artifacts_out=[
"uprating_factors.csv",
"acs_2022.h5",
"irs_puf_2015.h5",
"cps_2024.h5",
"puf_2024.h5",
"extended_cps_2024.h5",
"enhanced_cps_2024.h5",
"enhanced_cps_2024.clone_diagnostics.json",
"calibration_log.csv",
"stratified_extended_cps_2024.h5",
"source_imputed_stratified_extended_cps_2024.h5",
"small_enhanced_cps_2024.h5",
"source_imputed_stratified_extended_cps.h5",
"policy_data.db",
"calibration_weights.npy",
"build_log.txt",
"data_build_checkpoint_stats.json",
"dataset_inventory.json",
"source_dataset_schema_summary.json",
"target_database_schema_summary.json",
],
validation_commands=["uv run pytest tests/unit/test_build_dataset_specs.py"],
)
def stage_1_artifact_specs() -> tuple[DatasetArtifactSpec, ...]:
"""Return all artifact specs known to the Stage 1 dataset build."""
return STAGE_1_ARTIFACT_SPECS
def stage_1_contract_artifact_specs() -> tuple[DatasetArtifactSpec, ...]:
"""Return artifact specs emitted in the Stage 1 handoff contract."""
return tuple(spec for spec in STAGE_1_ARTIFACT_SPECS if spec.contract_output)
def stage_1_pipeline_artifact_specs() -> tuple[DatasetArtifactSpec, ...]:
"""Return artifact specs staged into the run-scoped pipeline directory."""
return tuple(spec for spec in STAGE_1_ARTIFACT_SPECS if spec.pipeline_output)
def stage_1_diagnostic_artifact_specs() -> tuple[DatasetArtifactSpec, ...]:
"""Return diagnostic artifact specs emitted by Stage 1 writers."""
return tuple(spec for spec in STAGE_1_ARTIFACT_SPECS if spec.diagnostic_output)
def stage_1_script_outputs() -> Mapping[str, ScriptOutput]:
"""Return the checkpoint output mapping consumed by Modal data-build."""
outputs: dict[str, list[str]] = {}
for spec in STAGE_1_ARTIFACT_SPECS:
if spec.script_path is None or spec.storage_path is None:
continue
outputs.setdefault(spec.script_path, []).append(spec.storage_path)
return {
script_path: paths[0] if len(paths) == 1 else paths
for script_path, paths in outputs.items()
}
__all__ = [
"DatasetArtifactSpec",
"STAGE_1_ARTIFACT_SPECS",
"ScriptOutput",
"stage_1_artifact_specs",
"stage_1_contract_artifact_specs",
"stage_1_diagnostic_artifact_specs",
"stage_1_pipeline_artifact_specs",
"stage_1_script_outputs",
]