Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/2.1.1 🎉 #112

Merged
merged 7 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.1.1 (10/10/2024)
- fix: allow multiple `alpha` values passed in to `ModelClient.get_national_summary_votes_estimates()` and change that method to return a `pandas.DataFrame` [#111](https://github.com/washingtonpost/elex-live-model/pull/111)

## 2.1.0 (09/23/2024)
- fix: model evaluation functions and margin estimand rounding [#94](https://github.com/washingtonpost/elex-live-model/pull/94)
- chore: updated requirements to their latest versions [#95](https://github.com/washingtonpost/elex-live-model/pull/95), [#103](https://github.com/washingtonpost/elex-live-model/pull/103)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
LONG_DESCRIPTION = f.read()

# The full version, including alpha/beta/rc tags
RELEASE = "2.1.0"
RELEASE = "2.1.1"
# The short X.Y version
VERSION = ".".join(RELEASE.split(".")[:2])

Expand Down
2 changes: 1 addition & 1 deletion src/elexmodel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def cli(

if kwargs.get("national_summary", False):
# TODO: get_national_summary_votes_estimates() arguments via CLI
model_client.get_national_summary_votes_estimates(None, 0, 0.99)
model_client.get_national_summary_votes_estimates(None, 0, [0.99])

for aggregate_level, estimates in result.items():
print(aggregate_level, "\n", estimates, "\n")
12 changes: 7 additions & 5 deletions src/elexmodel/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,23 @@ def get_aggregate_list(self, office, aggregate):
raw_aggregate_list = base_aggregate + [aggregate]
return sorted(list(set(raw_aggregate_list)), key=lambda x: AGGREGATE_ORDER.index(x))

def get_national_summary_votes_estimates(self, nat_sum_data_dict=None, base_to_add=0, alpha=0.99):
def get_national_summary_votes_estimates(self, nat_sum_data_dict=None, base_to_add=0, alphas=[0.99]):
if self.model is None:
raise ModelClientException(
"Must call the get_estimands() method before get_national_summary_votes_estimates()."
)

nat_sum_estimates = self.model.get_national_summary_estimates(nat_sum_data_dict, base_to_add, alpha)
self.results_handler.add_national_summary_estimates(nat_sum_estimates)
nat_sum_estimates_dict = {}
for alpha in alphas:
nat_sum_estimates = self.model.get_national_summary_estimates(nat_sum_data_dict, base_to_add, alpha)
nat_sum_estimates_dict[alpha] = nat_sum_estimates
self.results_handler.add_national_summary_estimates(nat_sum_estimates_dict)

if APP_ENV != "local" and self.save_results:
self.results_handler.write_data(
self.election_id, self.office, self.geographic_unit_type, keys=["nat_sum_data"]
)

return nat_sum_estimates
return self.results_handler.final_results["nat_sum_data"]

def get_estimates(
self,
Expand Down
12 changes: 8 additions & 4 deletions src/elexmodel/handlers/data/ModelResults.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ def process_final_results(self):
lambda x, y: pd.merge(x, y, how="inner", on=merge_on), self.unit_data.values()
)

def add_national_summary_estimates(self, national_summary_dict):
df = pd.DataFrame.from_dict(
national_summary_dict, orient="index", columns=["agg_pred", "agg_lower", "agg_upper"]
)
def add_national_summary_estimates(self, nat_sum_estimates_dict):
df = pd.DataFrame(index=["margin"])
for alpha, data in nat_sum_estimates_dict.items():
if "agg_pred" not in df.columns:
df["agg_pred"] = [data["margin"][0]]
df[f"lower_{alpha}"] = [data["margin"][1]]
df[f"upper_{alpha}"] = [data["margin"][2]]

df.index.name = "estimand"
self.final_results["nat_sum_data"] = df.reset_index()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def test_estimandizer_input(model_client, va_governor_county_data, va_config):

def test_get_national_summary_votes_estimates(model_client, va_governor_county_data, va_config):
expected = {"margin": [1.0, 1.0, 1.0]}
expected_df = pd.DataFrame.from_dict(expected, orient="index", columns=["agg_pred", "agg_lower", "agg_upper"])
expected_df = pd.DataFrame.from_dict(expected, orient="index", columns=["agg_pred", "lower_0.99", "upper_0.99"])
expected_df.index.name = "estimand"
expected_df = expected_df.reset_index()

Expand Down Expand Up @@ -874,7 +874,7 @@ def test_get_national_summary_votes_estimates(model_client, va_governor_county_d
**kwargs,
)

current = model_client.get_national_summary_votes_estimates(None, 0, 0.99)
current = model_client.get_national_summary_votes_estimates(None, 0, [0.99])

assert expected == current
pd.testing.assert_frame_equal(current, model_client.results_handler.final_results["nat_sum_data"])
pd.testing.assert_frame_equal(expected_df, model_client.results_handler.final_results["nat_sum_data"])
Loading