Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:washingtonpost/elex-live-model i…
Browse files Browse the repository at this point in the history
…nto fix-warnings
  • Loading branch information
dmnapolitano committed Dec 13, 2024
2 parents d0935dc + ed6e9fa commit 087b974
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from setuptools import find_packages, setup

INSTALL_REQUIRES = (
"boto3>=1.34",
"botocore>=1.34.162",
"click>=8.1",
"cvxpy~=1.5",
"elex-solver>=2.1.1",
"pandas>=2.2",
"boto3>=1.34",
"python-dotenv>=1.0",
"scipy>=1.14",
"s3transfer>=0.9.0",
"botocore>=1.34.162",
)

THIS_FILE_DIR = os.path.dirname(__file__)
Expand Down
2 changes: 1 addition & 1 deletion src/elexmodel/models/BaseElectionModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ def get_coefficients(self) -> dict:
"""
return self.features_to_coefficients

def get_national_summary_estimates(self, nat_sum_data_dict, called_states, base_to_add, alpha):
def get_national_summary_estimates(self, nat_sum_data_dict: dict, base_to_add: int | float, alpha: float):
raise NotImplementedError()
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,21 @@ def az_assembly_precinct_data(get_fixture):
@pytest.fixture(scope="session")
def test_path():
return _TEST_FOLDER


@pytest.fixture(scope="function")
def versioned_data_no_errors(get_fixture):
path = os.path.join("data", "2024-11-05_USA_G", "S", "versioned_no_errors.csv")
return get_fixture(path, load=False, pandas=True)


@pytest.fixture(scope="function")
def versioned_data_non_monotone(get_fixture):
path = os.path.join("data", "2024-11-05_USA_G", "S", "versioned_non_monotone.csv")
return get_fixture(path, load=False, pandas=True)


@pytest.fixture(scope="function")
def versioned_data_batch_margin(get_fixture):
path = os.path.join("data", "2024-11-05_USA_G", "S", "versioned_batch_margin.csv")
return get_fixture(path, load=False, pandas=True)
12 changes: 12 additions & 0 deletions tests/fixtures/data/2024-11-05_USA_G/S/versioned_batch_margin.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
postal_code,geographic_unit_fips,results_turnout,results_dem,results_gop,percent_expected_vote,results_weights,results_margin,results_normalized_margin,last_modified
TN,47085,6006,1379,4508,66.46,5887,-3129,-0.5315101070154578,2024-11-05 21:02:13-05:00
TN,47085,6006,1379,4508,66.46,5887,-3129,-0.5315101070154578,2024-11-05 21:04:56-05:00
TN,47085,6006,1379,4508,66.46,5887,-3129,-0.5315101070154578,2024-11-05 21:07:53-05:00
TN,47085,6006,1379,4508,66.46,5887,-3129,-0.5315101070154578,2024-11-05 21:10:33-05:00
TN,47085,6350,1446,4772,70.27,6218,-3326,-0.5348986812479897,2024-11-05 21:13:08-05:00
TN,47085,6350,1446,4772,70.27,6218,-3326,-0.5348986812479897,2024-11-05 21:15:54-05:00
TN,47085,6350,1446,4772,70.27,6218,-3326,-0.5348986812479897,2024-11-05 21:18:13-05:00
TN,47085,6350,1446,4772,70.27,6218,-3326,-0.5348986812479897,2024-11-05 21:21:06-05:00
TN,47085,6350,1446,4772,70.27,6218,-3326,-0.5348986812479897,2024-11-05 21:23:38-05:00
TN,47085,6350,1446,4772,70.27,6218,-3326,-0.5348986812479897,2024-11-05 21:26:29-05:00
TN,47085,6527,1545,4671,72.23,6216,-3126,-0.502895752895753,2024-11-05 21:29:06-05:00
12 changes: 12 additions & 0 deletions tests/fixtures/data/2024-11-05_USA_G/S/versioned_no_errors.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
postal_code,geographic_unit_fips,results_turnout,results_dem,results_gop,percent_expected_vote,results_weights,results_margin,results_normalized_margin,last_modified
MA,2501147835,0,0,0,0.0,0,0,0.0,2024-11-05 21:02:13-05:00
MA,2501147835,0,0,0,0.0,0,0,0.0,2024-11-05 21:04:56-05:00
MA,2501147835,0,0,0,0.0,0,0,0.0,2024-11-05 21:07:53-05:00
MA,2501147835,0,0,0,0.0,0,0,0.0,2024-11-05 21:10:33-05:00
MA,2501147835,0,0,0,0.0,0,0,0.0,2024-11-05 21:13:08-05:00
MA,2501147835,0,0,0,0.0,0,0,0.0,2024-11-05 21:15:54-05:00
MA,2501147835,1883,1117,766,97.51,1883,351,0.186404673393521,2024-11-05 21:18:13-05:00
MA,2501147835,1883,1117,766,97.51,1883,351,0.186404673393521,2024-11-05 21:21:06-05:00
MA,2501147835,1883,1117,766,99.0,1883,351,0.186404673393521,2024-11-05 21:23:38-05:00
MA,2501147835,1883,1117,766,99.0,1883,351,0.186404673393521,2024-11-05 21:26:29-05:00
MA,2501147835,1883,1117,766,99.0,1883,351,0.186404673393521,2024-11-05 21:29:06-05:00
12 changes: 12 additions & 0 deletions tests/fixtures/data/2024-11-05_USA_G/S/versioned_non_monotone.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
postal_code,geographic_unit_fips,results_turnout,results_dem,results_gop,percent_expected_vote,results_weights,results_margin,results_normalized_margin,last_modified
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:02:13-05:00
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:04:56-05:00
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:07:53-05:00
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:10:33-05:00
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:13:08-05:00
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:15:54-05:00
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:18:13-05:00
WV,54083,9207,2528,6343,77.83,8871,-3815,-0.4300529816255213,2024-11-05 21:21:06-05:00
WV,54083,10862,2772,7680,91.82,10452,-4908,-0.4695752009184845,2024-11-05 21:23:38-05:00
WV,54083,4865,1230,3554,41.12,4784,-2324,-0.4857859531772575,2024-11-05 21:26:29-05:00
WV,54083,4865,1230,3554,41.12,4784,-2324,-0.4857859531772575,2024-11-05 21:29:06-05:00
29 changes: 29 additions & 0 deletions tests/handlers/test_versioned_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from elexmodel.handlers.data.VersionedData import VersionedDataHandler


def test_versioned_data_without_errors(versioned_data_no_errors):
vdh = VersionedDataHandler("2024-11-05_USA_G", "S", "county")
results = vdh.compute_versioned_margin_estimate(data=versioned_data_no_errors)
assert len(results) == 100
assert (results["nearest_observed_vote"] == 99.0).all()
assert results["est_margin"].max().round(6) == 0.186405
assert results["est_correction"].min() == -2.7755575615628914e-17
assert (results["error_type"] == "none").all()


def test_versioned_data_with_non_monotone_ev(versioned_data_non_monotone):
vdh = VersionedDataHandler("2024-11-05_USA_G", "S", "county")
results = vdh.compute_versioned_margin_estimate(data=versioned_data_non_monotone)
assert len(results) == 101
assert len(results[results["est_margin"].isnull()]) == 101
assert len(results[results["est_correction"].isnull()]) == 101
assert (results["error_type"] == "non-monotone percent expected vote").all()


def test_versioned_data_with_batch_margin_error(versioned_data_batch_margin):
vdh = VersionedDataHandler("2024-11-05_USA_G", "S", "county")
results = vdh.compute_versioned_margin_estimate(data=versioned_data_batch_margin)
assert len(results) == 101
assert len(results[results["est_margin"].isnull()]) == 101
assert len(results[results["est_correction"].isnull()]) == 101
assert (results["error_type"] == "batch_margin").all()

0 comments on commit 087b974

Please sign in to comment.