Skip to content

Commit b4b9bd7

Browse files
committed
Merge branch 'nikhilwoodruff/issue2557' of https://github.com/PolicyEngine/policyengine-api into nikhilwoodruff/issue2557
2 parents 173b9fb + 5c54630 commit b4b9bd7

2 files changed

Lines changed: 13 additions & 119 deletions

File tree

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: minor
2+
changes:
3+
changed:
4+
- Simulations run off APIv2.

policyengine_api/jobs/calculate_economy_simulation_job.py

Lines changed: 9 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,19 @@
7373
datasets["us"][dataset] = f"{datasets['us'][dataset]}@{us_dataset_version}"
7474

7575

76-
check_against_api_v2 = (
77-
os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") is not None
78-
)
76+
run_api_v2 = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") is not None
7977

80-
if not check_against_api_v2:
78+
if not run_api_v2:
8179
logger.log_text(
82-
"Didn't find any GOOGLE_APPLICATION_CREDENTIALS, so will not check APIv1 results against APIv2.",
80+
"Didn't find any GOOGLE_APPLICATION_CREDENTIALS, so will not run results with APIv2.",
8381
severity="WARNING",
8482
)
8583

8684

8785
class CalculateEconomySimulationJob(BaseJob):
8886
def __init__(self):
8987
super().__init__()
90-
if check_against_api_v2:
88+
if run_api_v2:
9189
self.api_v2 = SimulationAPIv2()
9290

9391
def run(
@@ -234,7 +232,7 @@ def run(
234232
)
235233

236234
# If comparing against API v2, start job
237-
if check_against_api_v2:
235+
if run_api_v2:
238236

239237
try:
240238
# Populate v2/v1 comparison config data; we will pass this
@@ -313,53 +311,8 @@ def run(
313311
except Exception as e2:
314312
print("Something really wrong.", e2)
315313
pass
316-
317-
# Compute baseline economy
318-
logger.log_struct(
319-
{
320-
"message": "Computing baseline economy...",
321-
**job_setup_options,
322-
}
323-
)
324-
baseline_economy = self._compute_economy(
325-
country_id=country_id,
326-
region=region,
327-
dataset=dataset,
328-
time_period=time_period,
329-
options=options,
330-
policy_json=baseline_policy,
331-
)
332-
333-
# Compute reform economy
334-
logger.log_struct(
335-
{
336-
"message": "Computing reform economy...",
337-
**job_setup_options,
338-
}
339-
)
340-
reform_economy = self._compute_economy(
341-
country_id=country_id,
342-
region=region,
343-
dataset=dataset,
344-
time_period=time_period,
345-
options=options,
346-
policy_json=reform_policy,
347-
)
348-
349-
baseline_economy = baseline_economy["result"]
350-
reform_economy = reform_economy["result"]
351-
logger.log_struct(
352-
{
353-
"message": "Computed baseline and reform economies",
354-
**job_setup_options,
355-
}
356-
)
357-
impact: dict[str, Any] = compare_economic_outputs(
358-
baseline_economy, reform_economy, country_id=country_id
359-
)
360-
361-
# If comparing against API v2, wait for job to complete
362-
if check_against_api_v2:
314+
# Wait for job to complete
315+
if run_api_v2:
363316

364317
try:
365318
execution_id: str = self.api_v2.get_execution_id(
@@ -369,56 +322,14 @@ def run(
369322
api_v2_execution
370323
)
371324

372-
v2_country_package_version = api_v2_output["model_version"]
373-
374-
completion_log: V2V1Comparison = (
375-
V2V1Comparison.model_validate(
376-
{
377-
**comparison_data,
378-
"v1_impact": impact,
379-
"v2_impact": api_v2_output,
380-
"v1_v2_diff": None,
381-
"v2_country_package_version": v2_country_package_version,
382-
"message": "APIv2 job completed",
383-
}
384-
)
385-
)
386-
387-
logger.log_struct(
388-
completion_log.model_dump(mode="json"),
389-
)
390-
# Run v2/v1 comparison
391-
392-
v1_v2_diff: dict[str, Any] = compute_difference(
393-
x=impact,
394-
y=api_v2_output,
395-
)
396-
# Push relevant info into logging schema
397-
comparison_log: V2V1Comparison = (
398-
V2V1Comparison.model_validate(
399-
{
400-
**comparison_data,
401-
"v1_impact": impact,
402-
"v2_impact": api_v2_output,
403-
"v1_v2_diff": v1_v2_diff,
404-
"v2_country_package_version": v2_country_package_version,
405-
"message": "APIv2 job comparison with APIv1 completed",
406-
}
407-
)
408-
)
409-
logger.log_struct(
410-
comparison_log.model_dump(mode="json"),
411-
severity="INFO",
412-
)
413-
414325
except Exception as e:
415326
trace = traceback.format_exc()
416327
# If job fails, send error log to GCP
417328
error_log: V2V1Comparison = V2V1Comparison.model_validate(
418329
{
419330
**comparison_data,
420331
"v2_error": trace,
421-
"v1_impact": impact,
332+
"v1_impact": None,
422333
"v2_impact": None,
423334
"v1_v2_diff": None,
424335
"message": "APIv2 job failed",
@@ -437,7 +348,7 @@ def run(
437348
dataset=dataset,
438349
time_period=time_period,
439350
options_hash=options_hash,
440-
reform_impact_json=json.dumps(impact),
351+
reform_impact_json=json.dumps(api_v2_output),
441352
)
442353

443354
except Exception as e:
@@ -451,27 +362,6 @@ def run(
451362
options_hash,
452363
message=traceback.format_exc(),
453364
)
454-
if check_against_api_v2:
455-
# Show that API v1 failed and API v2 was not run
456-
error_log: V2V1Comparison = V2V1Comparison.model_validate(
457-
{
458-
**job_setup_options,
459-
"v1_country_package_version": COUNTRY_PACKAGE_VERSIONS[
460-
country_id
461-
],
462-
"v2_id": None, # Unavailable until job starts
463-
"v2_country_package_version": None, # Unavailable until job completes
464-
"v1_error": str(e),
465-
"v2_error": None,
466-
"v1_impact": None,
467-
"v2_impact": None,
468-
"v1_v2_diff": None,
469-
"message": "APIv1 job failed",
470-
}
471-
)
472-
logger.log_struct(
473-
error_log.model_dump(mode="json"), severity="ERROR"
474-
)
475365
logger.log_struct(
476366
{
477367
"message": "Error during job execution",

0 commit comments

Comments
 (0)