produce a string for the front end to ingest (#1963)

* wip

* i believe this works -- let's see the pipeline

* updated fixtures
This commit is contained in:
Emma Nechamkin 2022-10-04 10:05:00 -04:00 committed by GitHub
parent ecabe79299
commit a438b44bfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 3 deletions

View file

@ -381,6 +381,8 @@ TILES_SCORE_COLUMNS = {
field_names.PERCENT_AGE_OVER_64: "AGE_OLD",
field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT: "TA_COUNT",
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT: "TA_PERC",
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT_DISPLAY_STRING: "TA_PERC_STR",
}
# columns to round floats to 2 decimals

View file

@ -501,6 +501,7 @@ class ScoreETL(ExtractTransformLoad):
field_names.TRACT_ELIGIBLE_FOR_NONNATURAL_THRESHOLD,
field_names.AGRICULTURAL_VALUE_BOOL_FIELD,
field_names.NAMES_OF_TRIBAL_AREAS_IN_TRACT,
field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT_DISPLAY_STRING,
]
boolean_columns = [

File diff suppressed because one or more lines are too long

View file

@ -55,6 +55,7 @@ class TribalOverlapETL(ExtractTransformLoad):
field_names.COUNT_OF_TRIBAL_AREAS_IN_TRACT,
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,
]
self.output_df: pd.DataFrame
@ -68,6 +69,42 @@ class TribalOverlapETL(ExtractTransformLoad):
str_list = sorted(str_list)
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
if percentage_float < 0.01:
return "less than 1%"
# Rule 2
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 + "%"
# Rule 4
if last_digits != "00":
return rounded_percentage_str + "%"
# There is something missing!
raise Exception("Yikes! The string conversion here failed!")
def extract(self) -> None:
self.census_tract_gdf = get_tract_geojson()
self.tribal_gdf = get_tribal_geojson()
@ -209,4 +246,12 @@ class TribalOverlapETL(ExtractTransformLoad):
merged_output_df[field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT],
)
# 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
] = merged_output_df[field_names.PERCENT_OF_TRIBAL_AREA_IN_TRACT].apply(
self._adjust_percentage_to_string
)
self.output_df = merged_output_df

View file

@ -360,6 +360,9 @@ NAMES_OF_TRIBAL_AREAS_IN_TRACT = "Names of Tribal areas within Census tract"
PERCENT_OF_TRIBAL_AREA_IN_TRACT = (
"Percent of the Census tract that is within Tribal areas"
)
PERCENT_OF_TRIBAL_AREA_IN_TRACT_DISPLAY_STRING = (
"Percent of the Census tract that is within Tribal areas, for display"
)
#####
# Names for individual factors being exceeded