-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from olincollege/SAN-30-Collect-income-levels
SAN-30 collect income levels
- Loading branch information
Showing
5 changed files
with
127 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from pygris.data import get_census | ||
from night_light.utils.fips import StateFIPS | ||
from pygris import tracts | ||
import geopandas as gpd | ||
|
||
|
||
def get_median_household_income( | ||
year: int = 2021, state: StateFIPS = StateFIPS.MASSACHUSETTS | ||
) -> gpd.GeoDataFrame: | ||
""" | ||
Fetch median household income data using the ACS 5-year survey. | ||
Args: | ||
year (int): The year of the ACS survey. | ||
state (StateFIPS): The state to fetch data for. | ||
Returns: | ||
gpd.GeoDataFrame: A GeoDataFrame containing the median household income data. | ||
""" | ||
df = get_census( | ||
year=year, | ||
variables=["B19013_001E"], | ||
params={ | ||
"for": "tract:*", | ||
"in": f"state:{state.value}", | ||
}, | ||
dataset="acs/acs5", | ||
return_geoid=True, | ||
guess_dtypes=True, | ||
) | ||
|
||
df["median_household_income"] = df["B19013_001E"] | ||
df.drop(columns=["B19013_001E"], inplace=True) | ||
|
||
gdf = tracts(state=state.value, year=year) | ||
gdf = gdf.merge(df, on="GEOID") | ||
|
||
return gdf |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import time | ||
|
||
import folium | ||
|
||
from night_light.socioeconomic.median_household_income import ( | ||
get_median_household_income, | ||
) | ||
from night_light.utils import create_folium_map, open_html_file, Tooltip | ||
from night_light.utils.fips import StateFIPS | ||
from night_light.utils.mapping import Choropleth | ||
|
||
|
||
def test_ma_median_household_income_choropleth(): | ||
"""Test creating MA median household income choropleth map.""" | ||
map_filename = "test_ma_median_household_income_map.html" | ||
median_income_data = get_median_household_income( | ||
year=2021, state=StateFIPS.MASSACHUSETTS | ||
) | ||
median_income_data = median_income_data.dropna(subset=["median_household_income"]) | ||
|
||
income_layer = Choropleth( | ||
geo_data=median_income_data, | ||
name="Median Household Income Choropleth", | ||
data=median_income_data, | ||
columns=["GEOID", "median_household_income"], | ||
key_on="feature.properties.GEOID", | ||
) | ||
|
||
tooltip_layer = folium.GeoJson( | ||
median_income_data, | ||
name="Median Household Income Tooltips", | ||
style_function=lambda x: {"fillColor": "transparent", "color": "transparent"}, | ||
tooltip=Tooltip( | ||
fields=["NAMELSAD", "median_household_income"], | ||
aliases=["Tract Name", "Median Household Income"], | ||
), | ||
) | ||
|
||
create_folium_map( | ||
layers=[income_layer, tooltip_layer], | ||
center=[42.4072, -71.3824], | ||
zoom_start=9, | ||
map_filename=map_filename, | ||
) | ||
|
||
assert os.path.exists(map_filename) | ||
open_html_file(map_filename) | ||
time.sleep(1) | ||
os.remove(map_filename) | ||
assert not os.path.exists(map_filename) |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
from night_light.socioeconomic.median_household_income import ( | ||
get_median_household_income, | ||
) | ||
from night_light.utils import query_geojson | ||
from night_light.utils.fips import StateFIPS | ||
|
||
|
||
def test_get_ma_median_household_income(): | ||
"""Test getting MA median household income""" | ||
data = get_median_household_income(state=StateFIPS.MASSACHUSETTS) | ||
assert data.shape[0] > 0 | ||
assert "median_household_income" in data.columns | ||
data = data.dropna(subset=["median_household_income"]) | ||
invalid_values = data[data["median_household_income"] < 0] | ||
assert invalid_values.empty | ||
|
||
|
||
def test_ma_median_household_geojson(): | ||
"""Test saving MA median household income to a GeoJSON file""" | ||
data = get_median_household_income(state=StateFIPS.MASSACHUSETTS) | ||
geojson_filename = "test_ma_median_household_income.geojson" | ||
query_geojson.save_geojson(data, geojson_filename) | ||
saved_gdf = query_geojson.gpd.read_file(geojson_filename) | ||
|
||
assert data.crs == saved_gdf.crs | ||
assert set(data.columns) == set(saved_gdf.columns) | ||
assert data.index.equals(saved_gdf.index) | ||
assert data.shape == saved_gdf.shape | ||
|
||
os.remove(geojson_filename) | ||
assert not os.path.exists(geojson_filename) |