Skip to content

Commit

Permalink
linter
Browse files Browse the repository at this point in the history
  • Loading branch information
lennybronner committed Sep 25, 2023
1 parent 88719c7 commit eb5de99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 47 deletions.
4 changes: 3 additions & 1 deletion src/elexmodel/handlers/data/Estimandizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ def add_turnout_factor(self, data_df):


def party_vote_share_dem(data_df, col_prefix):
data_df[f"{col_prefix}party_vote_share_dem"] = np.nan_to_num(data_df[f"{col_prefix}dem"] /data_df[f"{col_prefix}turnout"])
data_df[f"{col_prefix}party_vote_share_dem"] = np.nan_to_num(
data_df[f"{col_prefix}dem"] / data_df[f"{col_prefix}turnout"]
)
return data_df, []
70 changes: 24 additions & 46 deletions tests/handlers/test_combined_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@

from elexmodel.handlers.data.CombinedData import CombinedDataHandler
from elexmodel.handlers.data.LiveData import MockLiveDataHandler
from elexmodel.handlers.data.PreprocessedData import PreprocessedDataHandler


def test_load(va_governor_county_data):
election_id = "2017-11-07_VA_G"
office_id = "G"
geographic_unit_type = "county"
estimands = ["turnout"]
estimand_baselines = {"turnout": "turnout"}
live_data_handler = MockLiveDataHandler(
election_id, office_id, geographic_unit_type, estimands=["turnout"], data=va_governor_county_data
)
current_data = live_data_handler.data
va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, "county", handle_unreporting="drop"
Expand All @@ -32,7 +30,6 @@ def test_zero_unreporting_missing_single_estimand_value(va_governor_county_data)
election_id = "2017-11-07_VA_G"
office_id = "G"
geographic_unit_type = "county"
estimand_baselines = {"turnout": "turnout", "dem": "dem"}
estimands = ["turnout", "dem"]
live_data_handler = MockLiveDataHandler(
election_id, office_id, geographic_unit_type, estimands, data=va_governor_county_data
Expand All @@ -41,7 +38,7 @@ def test_zero_unreporting_missing_single_estimand_value(va_governor_county_data)
current_data["percent_expected_vote"] = 100
current_data.loc[0, "results_dem"] = np.nan

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, "county", handle_unreporting="zero"
Expand All @@ -62,7 +59,6 @@ def test_zero_unreporting_missing_multiple_estimands_value(va_governor_county_da
election_id = "2017-11-07_VA_G"
office_id = "G"
geographic_unit_type = "county"
estimand_baselines = {"turnout": "turnout", "dem": "dem"}
estimands = ["turnout", "dem"]
live_data_handler = MockLiveDataHandler(
election_id, office_id, geographic_unit_type, estimands, data=va_governor_county_data
Expand All @@ -72,7 +68,7 @@ def test_zero_unreporting_missing_multiple_estimands_value(va_governor_county_da
current_data.loc[0, "results_dem"] = np.nan
current_data.loc[0, "results_turnout"] = np.nan

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, "county", handle_unreporting="zero"
Expand All @@ -92,7 +88,6 @@ def test_zero_unreporting_missing_percent_expected_vote_value(va_governor_county
election_id = "2017-11-07_VA_G"
office_id = "G"
geographic_unit_type = "county"
estimand_baselines = {"turnout": "turnout", "dem": "dem"}
estimands = ["turnout", "dem"]
live_data_handler = MockLiveDataHandler(
election_id, office_id, geographic_unit_type, estimands, data=va_governor_county_data
Expand All @@ -102,7 +97,7 @@ def test_zero_unreporting_missing_percent_expected_vote_value(va_governor_county
current_data.loc[0, "percent_expected_vote"] = np.nan
current_data.loc[0, "results_dem"] = np.nan

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, "county", handle_unreporting="zero"
Expand All @@ -120,7 +115,6 @@ def test_zero_unreporting_random_percent_expected_vote_value(va_governor_county_
election_id = "2017-11-07_VA_G"
office_id = "G"
geographic_unit_type = "county"
estimand_baselines = {"turnout": "turnout", "dem": "dem"}
estimands = ["turnout", "dem"]
live_data_handler = MockLiveDataHandler(
election_id, office_id, geographic_unit_type, estimands, data=va_governor_county_data
Expand All @@ -129,7 +123,7 @@ def test_zero_unreporting_random_percent_expected_vote_value(va_governor_county_
current_data["percent_expected_vote"] = np.random.randint(1, 100, current_data.shape[0])
current_data.loc[0, "results_dem"] = np.nan

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, "county", handle_unreporting="zero"
Expand All @@ -147,7 +141,6 @@ def test_drop_unreporting_missing_single_estimand_value(va_governor_county_data)
election_id = "2017-11-07_VA_G"
office_id = "G"
geographic_unit_type = "county"
estimand_baselines = {"turnout": "turnout", "dem": "dem"}
estimands = ["turnout", "dem"]
live_data_handler = MockLiveDataHandler(
election_id, office_id, geographic_unit_type, estimands, data=va_governor_county_data
Expand All @@ -156,7 +149,7 @@ def test_drop_unreporting_missing_single_estimand_value(va_governor_county_data)
current_data["percent_expected_vote"] = 100
current_data.loc[0, "results_dem"] = np.nan

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, "county", handle_unreporting="drop"
Expand All @@ -170,20 +163,17 @@ def test_get_reporting_data(va_governor_county_data):
office = "G"
geographic_unit_type = "county"
estimands = ["turnout"]
estimand_baseline = {"turnout": "turnout"}

live_data_handler = MockLiveDataHandler(
election_id, office, geographic_unit_type, estimands, data=va_governor_county_data
)
current_data = live_data_handler.get_n_fully_reported(n=20)

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data['last_election_results_turnout'] = va_governor_county_data.baseline_turnout + 1
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout
va_governor_county_data["last_election_results_turnout"] = va_governor_county_data.baseline_turnout + 1

# no fixed effects
combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, geographic_unit_type
)
combined_data_handler = CombinedDataHandler(va_governor_county_data, current_data, estimands, geographic_unit_type)
observed_data = combined_data_handler.get_reporting_units(100)
assert observed_data.shape[0] == 20
assert observed_data.reporting.iloc[0] == 1
Expand All @@ -195,19 +185,16 @@ def test_get_reporting_data_dropping_with_turnout_factor(va_governor_county_data
office = "G"
geographic_unit_type = "county"
estimands = ["turnout"]
estimand_baseline = {"turnout": "turnout"}

live_data_handler = MockLiveDataHandler(
election_id, office, geographic_unit_type, estimands, data=va_governor_county_data
)
current_data = live_data_handler.get_n_fully_reported(n=20)

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data['last_election_results_turnout'] = va_governor_county_data.baseline_turnout + 1

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, geographic_unit_type
)
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout
va_governor_county_data["last_election_results_turnout"] = va_governor_county_data.baseline_turnout + 1

combined_data_handler = CombinedDataHandler(va_governor_county_data, current_data, estimands, geographic_unit_type)

turnout_factor_lower = 0.95
turnout_factor_upper = 1.2
Expand Down Expand Up @@ -235,20 +222,17 @@ def test_get_nonreporting_adding_with_turnout_factor(va_governor_county_data):
office = "G"
geographic_unit_type = "county"
estimands = ["turnout"]
estimand_baseline = {"turnout": "turnout"}

live_data_handler = MockLiveDataHandler(
election_id, office, geographic_unit_type, estimands, data=va_governor_county_data
)
n = 20
current_data = live_data_handler.get_n_fully_reported(n=n)

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data['last_election_results_turnout'] = va_governor_county_data.baseline_turnout + 1

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, geographic_unit_type
)
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout
va_governor_county_data["last_election_results_turnout"] = va_governor_county_data.baseline_turnout + 1

combined_data_handler = CombinedDataHandler(va_governor_county_data, current_data, estimands, geographic_unit_type)

turnout_factor_lower = 0.95
turnout_factor_upper = 1.2
Expand Down Expand Up @@ -280,7 +264,6 @@ def test_get_unexpected_units_county_district(va_assembly_county_data):
geographic_unit_type = "county-district"
estimands = ["turnout"]
unexpected_units = 5
estimand_baseline = {"turnout": "turnout"}

live_data_handler = MockLiveDataHandler(
election_id,
Expand All @@ -291,13 +274,11 @@ def test_get_unexpected_units_county_district(va_assembly_county_data):
unexpected_units=unexpected_units,
)
current_data = live_data_handler.get_n_fully_reported(n=20)

va_assembly_county_data['baseline_weights'] = va_assembly_county_data.baseline_turnout
va_assembly_county_data['last_election_results_turnout'] = va_assembly_county_data.baseline_turnout + 1

combined_data_handler = CombinedDataHandler(
va_assembly_county_data, current_data, estimands, geographic_unit_type
)
va_assembly_county_data["baseline_weights"] = va_assembly_county_data.baseline_turnout
va_assembly_county_data["last_election_results_turnout"] = va_assembly_county_data.baseline_turnout + 1

combined_data_handler = CombinedDataHandler(va_assembly_county_data, current_data, estimands, geographic_unit_type)
unexpected_data = combined_data_handler.get_unexpected_units(100, ["county_fips", "district"])
assert unexpected_data.shape[0] == unexpected_units
assert unexpected_data[unexpected_data.county_fips == ""].shape[0] == 0
Expand All @@ -312,7 +293,6 @@ def test_get_unexpected_units_county(va_governor_county_data):
geographic_unit_type = "county"
estimands = ["turnout"]
reporting_unexpected_units = 5
estimand_baseline = {"turnout": "turnout"}

live_data_handler = MockLiveDataHandler(
election_id,
Expand All @@ -329,12 +309,10 @@ def test_get_unexpected_units_county(va_governor_county_data):
extra_row["percent_expected_vote"] = 50
current_data = pd.concat([current_data, extra_row])

va_governor_county_data['baseline_weights'] = va_governor_county_data.baseline_turnout
va_governor_county_data['last_election_results_turnout'] = va_governor_county_data.baseline_turnout + 1
va_governor_county_data["baseline_weights"] = va_governor_county_data.baseline_turnout
va_governor_county_data["last_election_results_turnout"] = va_governor_county_data.baseline_turnout + 1

combined_data_handler = CombinedDataHandler(
va_governor_county_data, current_data, estimands, geographic_unit_type
)
combined_data_handler = CombinedDataHandler(va_governor_county_data, current_data, estimands, geographic_unit_type)
unexpected_data = combined_data_handler.get_unexpected_units(100, ["county_fips"])
assert unexpected_data.shape[0] == reporting_unexpected_units + 1
assert unexpected_data[unexpected_data.county_fips == ""].shape[0] == 0
Expand Down

0 comments on commit eb5de99

Please sign in to comment.