-
Notifications
You must be signed in to change notification settings - Fork 48
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
Fixup TA_COUNT and TA_PERC #1991
Merged
mattbowen-usds
merged 2 commits into
emma-nechamkin/release/score-narwhal
from
mattbowen-usds/1988-1989-tribal-perc-and-count-fixes
Oct 6, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
data/data-pipeline/data_pipeline/etl/score/tests/sample_data/score_data_initial.csv
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file modified
BIN
+259 Bytes
(100%)
data/data-pipeline/data_pipeline/etl/score/tests/snapshots/score_data_expected.pkl
Binary file not shown.
Binary file modified
BIN
+281 Bytes
(100%)
data/data-pipeline/data_pipeline/etl/score/tests/snapshots/score_transformed_expected.pkl
Binary file not shown.
Binary file modified
BIN
+39 Bytes
(100%)
data/data-pipeline/data_pipeline/etl/score/tests/snapshots/tile_data_expected.pkl
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Optional | ||
|
||
import geopandas as gpd | ||
import numpy as np | ||
import pandas as pd | ||
|
@@ -51,12 +53,14 @@ class TribalOverlapETL(ExtractTransformLoad): | |
def __init__(self): | ||
self.COLUMNS_TO_KEEP = [ | ||
self.GEOID_TRACT_FIELD_NAME, | ||
field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT, | ||
field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT_AK, | ||
field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT_CONUS, | ||
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT, | ||
field_names.NAMES_OF_TRIBAL_AREAS_IN_TRACT, | ||
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT_DISPLAY_STRING, | ||
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT_DISPLAY, | ||
] | ||
|
||
self.OVERALL_TRIBAL_COUNT = "OVERALL_TRIBAL_COUNT" | ||
self.output_df: pd.DataFrame | ||
self.census_tract_gdf: gpd.GeoDataFrame | ||
self.tribal_gdf: gpd.GeoDataFrame | ||
|
@@ -69,40 +73,18 @@ def _create_string_from_list(series: pd.Series) -> str: | |
return ", ".join(str_list) | ||
|
||
@staticmethod | ||
def _adjust_percentage_to_string(percentage_float: float) -> str: | ||
"""Helper method that converts numeric floats to strings based on what-to-show rules. | ||
|
||
What are these rules? | ||
0. If None, return none | ||
1. If the percentage is below 1%, produce 'less than 1%' | ||
2. If the percentage is above 99.95%, produce '100%' | ||
3. If the percentage is X.00 when rounded to two sig digits, display the integer of the percent | ||
4. If the percentage has unique significant digits, report two digits | ||
""" | ||
# Rule 0 | ||
if not percentage_float: | ||
# I believe we need to do this because JS will do weird things with a mix-type column? | ||
return "No tribal areas" | ||
# Rule 1 | ||
def _adjust_percentage_for_frontend( | ||
percentage_float: float, | ||
) -> Optional[float]: | ||
"""Round numbers very close to 0 to 0 and very close to 1 to 1 for display""" | ||
if percentage_float is None: | ||
return None | ||
if percentage_float < 0.01: | ||
return "less than 1%" | ||
# Rule 2 | ||
return 0.0 | ||
if percentage_float > 0.9995: | ||
return "100%" | ||
|
||
rounded_percentage_str = str(round(percentage_float, 4) * 100) | ||
first_digits, last_digits = rounded_percentage_str.split(".") | ||
|
||
# Rule 3 (this is a shorthand because round(4) will truncate repeated 0s) | ||
if last_digits[-1] == "0": | ||
return first_digits + "%" | ||
return 1.0 | ||
|
||
# Rule 4 | ||
if last_digits != "00": | ||
return rounded_percentage_str + "%" | ||
|
||
# There is something missing! | ||
raise Exception("Yikes! The string conversion here failed!") | ||
return percentage_float | ||
|
||
def extract(self) -> None: | ||
self.census_tract_gdf = get_tract_geojson() | ||
|
@@ -130,7 +112,7 @@ def transform(self) -> None: | |
|
||
tribal_overlap_with_tracts = tribal_overlap_with_tracts.rename( | ||
columns={ | ||
field_names.TRIBAL_ID: field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT, | ||
field_names.TRIBAL_ID: self.OVERALL_TRIBAL_COUNT, | ||
field_names.TRIBAL_LAND_AREA_NAME: field_names.NAMES_OF_TRIBAL_AREAS_IN_TRACT, | ||
} | ||
) | ||
|
@@ -245,12 +227,31 @@ def transform(self) -> None: | |
merged_output_df[field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT], | ||
) | ||
|
||
# Counting tribes in the lower 48 is different from counting in AK, | ||
# so per request by the design and frontend team, we remove all the | ||
# counts outside AK | ||
merged_output_df[ | ||
field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT_AK | ||
] = np.where( | ||
# In Alaska | ||
(merged_output_df_state_fips_code == "02"), | ||
# Keep the counts | ||
merged_output_df[self.OVERALL_TRIBAL_COUNT], | ||
# Otherwise, null them | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great comments! |
||
None, | ||
) | ||
|
||
# TODO: Count tribal areas in the lower 48 correctly | ||
merged_output_df[ | ||
field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT_CONUS | ||
] = None | ||
|
||
# The very final thing we want to do is produce a string for the front end to show | ||
# We do this here so that all of the logic is included | ||
merged_output_df[ | ||
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT_DISPLAY_STRING | ||
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT_DISPLAY | ||
] = merged_output_df[field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT].apply( | ||
self._adjust_percentage_to_string | ||
self._adjust_percentage_for_frontend | ||
) | ||
|
||
self.output_df = merged_output_df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're dropping the whole rule of "Convert
7.00%
to7
"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're returning floats now, so it'll be on the frontend to do that