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

Fix zero total population bug in segregationcalc #20

Merged
merged 4 commits into from
Jun 6, 2024
Merged

Conversation

tylersimko
Copy link
Contributor

@tylersimko tylersimko commented Jun 5, 2024

Hi, great package! Big fan.

I think there's a bug in the internal segregationcalc function when there's 0 population in a district. Edge case for redistricting of course, but this comes up for one of my applications.

For example:

library(redistmetrics)

data(nh)
data(nh_m)

# hypothetical 3rd NH district with 0 population
nh$r_2020[nh$vap == 0] <- 3

seg_dissim(plans = nh$r_2020,
           shp = nh,
           group_pop = vap_hisp,
           total_pop = vap)

[1] NaN NaN NaN

Looks like a potential divide by 0 bug here, so I pushed a fix to src/segregation.cpp to add 0 to dissimilarity in those cases.

> seg_dissim(plans = nh$r_2020,
+            shp = nh,
+            group_pop = vap_hisp,
+            total_pop = vap)
[1] 0.01085658 0.01085658 0.01085658

@christopherkenny
Copy link
Member

Thanks Dr. @tylersimko, looks good to me. On to Dr. @CoryMcCartan

Copy link
Member

@CoryMcCartan CoryMcCartan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored for speed & readability.

All tests pass

@CoryMcCartan CoryMcCartan merged commit a797631 into main Jun 6, 2024
6 checks passed
@CoryMcCartan CoryMcCartan deleted the fix_zero branch June 6, 2024 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants