Skip to content

Commit

Permalink
travis county examples rework and update to years available
Browse files Browse the repository at this point in the history
  • Loading branch information
mdp0023 committed Apr 23, 2024
1 parent 2ea9dd7 commit 9b177a0
Show file tree
Hide file tree
Showing 103 changed files with 11,139 additions and 10,689 deletions.
Binary file modified SVInsight/__pycache__/svi.cpython-312.pyc
Binary file not shown.
36 changes: 19 additions & 17 deletions SVInsight/svi.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ def boundaries_data(self, boundary: str = 'bg', year: int = 2019, overwrite: boo
:return: The boundary data as a GeoDataFrame.
:rtype: gpd.GeoDataFrame
:raises ValueError: If the boundary type is invalid, the year is not between 2010 and 2022, or geoids not properly formatted.
:raises ValueError: If the boundary type is invalid, the year is not between 2013 and 2021, or geoids not properly formatted.
"""

# Validate Variables
self._validate_value(boundary, ['bg', 'tract'], 'boundary')
self._validate_value(year, [2010, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022], 'year')
self._validate_value(year, [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021], 'year')
self._validate_format(boundary, str, 'boundary')
self._validate_format(year, int, 'year')

Expand All @@ -191,7 +191,7 @@ def _download(year, state, boundary):
Downloads a shapefile from a remote FTP server, extracts it, and returns the boundary shapefile as a GeoDataFrame.
Args:
year (int): The year of the shapefile.
year (int): The year of the shapefile. Valid options are from 2011 to 2021.
state (str): The state abbreviation.
boundary (str): The boundary type. Must be 'bg' or 'tract'
Expand Down Expand Up @@ -219,8 +219,11 @@ def _download(year, state, boundary):
# print('opened binary')

# Specify the URL of the file you want to download
url = f"https://www2.census.gov/geo/tiger/GENZ{year}/shp/{zipped_filename}"

if year >= 2014:
url = f"https://www2.census.gov/geo/tiger/GENZ{year}/shp/{zipped_filename}"
elif year == 2013:
url = f"https://www2.census.gov/geo/tiger/GENZ{year}/{zipped_filename}"

# Send a GET request to the URL
response = requests.get(url, stream=True)

Expand All @@ -233,7 +236,6 @@ def _download(year, state, boundary):
if chunk:
file.write(chunk)


# Unzip the file
shutil.unpack_archive(zipped_dir, filename_dir)
# with zipfile.ZipFile(f"{zipped_dir}", 'r') as zip_ref:
Expand Down Expand Up @@ -301,24 +303,24 @@ def census_data(self, boundary: str = 'bg', year: int = 2019, interpolate: bool
:param boundary: The boundary type to retrieve data for. Valid options are 'bg' (block group) and 'tract' (census tract).
:type boundary: str
:param year: The year of the Census data to retrieve. Valid options are from 2010 to 2022.
:param year: The year of the Census data to retrieve. Valid options are from 2011 to 2021.
:type year: int
:param interpolate: Whether to interpolate missing data. Defaults to True.
:param interpolate: Whether to interpolate missing data. Defaults to True. If year is before 2014, ignored and not-interpolated.
:type interpolate: bool, optional
:param verbose: Whether to display verbose output. Defaults to False.
:type verbose: bool, optional
:param overwrite: Whether to overwrite existing data. Defaults to False.
:type overwrite: bool, optional
:raises ValueError: If the boundary type is invalid or the year is not between 2010 and 2022.
:raises ValueError: If the boundary type is invalid or the year is not between 2013 and 2021.
:raises FileNotFoundError: If the shapefile for the specified boundary and year does not exist.
:return: None
:rtype: None
"""
# Validate Variables
self._validate_value(boundary, ['bg', 'tract'], 'boundary')
self._validate_value(year, [2010, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022], 'year')
self._validate_value(year, [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021], 'year')
self._validate_format(boundary, str, 'boundary')
self._validate_format(year, int, 'year')

Expand Down Expand Up @@ -407,7 +409,7 @@ def add_variable(self, boundary: str, year: int, name:str, num: list, den: list
:type description: str, optional
:raises ValueError: If the variable name already exists.
:raises ValueError: If the boundary type is invalid or the year is not between 2010 and 2022.
:raises ValueError: If the boundary type is invalid or the year is not between 2013 and 2021.
:raises FileNotFoundError: If the raw data file doesn't exist. Run the census_data method first.
:return: None
Expand All @@ -416,7 +418,7 @@ def add_variable(self, boundary: str, year: int, name:str, num: list, den: list

# Validate Variables
self._validate_value(boundary, ['bg', 'tract'], 'boundary')
self._validate_value(year, [2010, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022], 'year')
self._validate_value(year, [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021], 'year')

# if name of variable already exists, raise error
if name in self.all_vars_eqs:
Expand Down Expand Up @@ -528,7 +530,7 @@ def calculate_svi(self, config_file: str, boundary: str = 'bg', year: int = 2019
:type year: int
:returns: None
:raises ValueError: If the boundary type is invalid or the year is not between 2010 and 2022,
:raises ValueError: If the boundary type is invalid or the year is not between 2013 and 2021,
This method reads a configuration file in YAML format, loads the raw data as a dataframe,
calculates the SVI using two different methods, and saves the results to output files.
Expand All @@ -554,7 +556,7 @@ def calculate_svi(self, config_file: str, boundary: str = 'bg', year: int = 2019
"""
# validate inputs
self._validate_value(boundary, ['bg', 'tract'], 'boundary')
self._validate_value(year, [2010, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022], 'year')
self._validate_value(year, [2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021], 'year')

# open the configuration file
with open(os.path.join(self.variables, f"{config_file}.yaml")) as stream:
Expand Down Expand Up @@ -771,7 +773,7 @@ def plot_svi(self, plot_option: int, geopackages: list):
:returns: matplotlib figure object
:raises ValueError: If the boundary type is invalid or the year is not between 2010 and 2022,
:raises ValueError: If the boundary type is invalid or the year is not between 2013 and 2021,
This method quickly creates an example SVI plot either by itself or in a comparative format. The plot options and their required information can be found below.
Expand Down Expand Up @@ -1291,8 +1293,6 @@ def extract_number(col_name):

# fill in values of data frame
data_df.loc[fips, var] = interpolated_value
if fips == 484530006011:
print(interpolated_value)

interpolated_output.append([f'{fips}', var, 'Interpolated'])

Expand Down Expand Up @@ -1534,6 +1534,8 @@ def _fill_holes(self, data_df, boundary, geoids, api_key, year, interpolate, ver
pandas.DataFrame: The DataFrame with missing values filled.
"""
if year <= 2014:
interpolate = False
# create output data file to save interpolated results
interpolated_output=[]

Expand Down
Binary file modified Travis_County/.DS_Store
Binary file not shown.
Binary file modified Travis_County/Boundaries/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
105 changes: 105 additions & 0 deletions Travis_County/Data/Travis_County_2013_bg_missing_values.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
484530001022,B25064_001E,Tract Filled
484530002031,B25077_001E,County Filled
484530006011,B25077_001E,County Filled
484530006012,B25077_001E,County Filled
484530006032,B25077_001E,Tract Filled
484530006034,B25077_001E,Tract Filled
484530006035,B25077_001E,Tract Filled
484530006041,B25077_001E,Tract Filled
484530006042,B25077_001E,Tract Filled
484530006043,B25077_001E,Tract Filled
484530015012,B25064_001E,Tract Filled
484530016023,B25077_001E,Tract Filled
484530016033,B25064_001E,Tract Filled
484530016034,B25064_001E,Tract Filled
484530016042,B25064_001E,Tract Filled
484530016044,B25064_001E,Tract Filled
484530017052,B25064_001E,Tract Filled
484530017053,B25064_001E,Tract Filled
484530017142,B25064_001E,Tract Filled
484530017143,B25064_001E,Tract Filled
484530017191,B25064_001E,Tract Filled
484530017192,B25064_001E,Tract Filled
484530017222,B25077_001E,Tract Filled
484530017292,B25064_001E,Tract Filled
484530017382,B25064_001E,Tract Filled
484530017383,B25064_001E,Tract Filled
484530017451,B25064_001E,Tract Filled
484530017493,B25077_001E,Tract Filled
484530017501,B25077_001E,Tract Filled
484530017521,B25077_001E,Tract Filled
484530017522,B25077_001E,Tract Filled
484530017531,B25077_001E,County Filled
484530017571,B25064_001E,Tract Filled
484530017702,B25064_001E,Tract Filled
484530017703,B25064_001E,Tract Filled
484530017712,B25064_001E,Tract Filled
484530017741,B25064_001E,Tract Filled
484530017743,B25064_001E,Tract Filled
484530017751,B25064_001E,Tract Filled
484530017752,B25064_001E,Tract Filled
484530017791,B25064_001E,Tract Filled
484530017811,B25064_001E,Tract Filled
484530017822,B25064_001E,Tract Filled
484530017852,B25077_001E,Tract Filled
484530017861,B25077_001E,Tract Filled
484530017863,B25077_001E,Tract Filled
484530018043,B25077_001E,Tract Filled
484530018064,B25077_001E,Tract Filled
484530018121,B25077_001E,Tract Filled
484530018122,B25077_001E,Tract Filled
484530018192,B25077_001E,Tract Filled
484530018202,B25077_001E,Tract Filled
484530018224,B25077_001E,Tract Filled
484530018333,B25064_001E,Tract Filled
484530018413,B25064_001E,Tract Filled
484530018491,B25077_001E,County Filled
484530018492,B25077_001E,County Filled
484530018493,B25077_001E,County Filled
484530018494,B25077_001E,County Filled
484530018503,B25077_001E,Tract Filled
484530018542,B25064_001E,Tract Filled
484530018551,B25064_001E,Tract Filled
484530018555,B25064_001E,Tract Filled
484530018582,B25064_001E,Tract Filled
484530018583,B25064_001E,Tract Filled
484530018584,B25064_001E,Tract Filled
484530018585,B25064_001E,Tract Filled
484530018592,B25064_001E,Tract Filled
484530018631,B25077_001E,Tract Filled
484530018642,B25077_001E,Tract Filled
484530019142,B25064_001E,Tract Filled
484530019161,B25064_001E,County Filled
484530019162,B25064_001E,County Filled
484530019171,B25064_001E,County Filled
484530019172,B25064_001E,County Filled
484530019173,B25064_001E,County Filled
484530019174,B25064_001E,County Filled
484530019181,B25064_001E,Tract Filled
484530019182,B25064_001E,Tract Filled
484530019183,B25064_001E,Tract Filled
484530019193,B25064_001E,Tract Filled
484530020052,B25077_001E,Tract Filled
484530021052,B25077_001E,Tract Filled
484530021063,B25064_001E,Tract Filled
484530021072,B25064_001E,Tract Filled
484530022121,B25064_001E,County Filled
484530023041,B25077_001E,Tract Filled
484530023043,B25077_001E,Tract Filled
484530023083,B25077_001E,Tract Filled
484530023084,B25077_001E,Tract Filled
484530023141,B25077_001E,Tract Filled
484530023142,B25077_001E,Tract Filled
484530023143,B25077_001E,Tract Filled
484530023144,B25077_001E,Tract Filled
484530023152,B25077_001E,Tract Filled
484530023161,B25077_001E,County Filled
484530023162,B25077_001E,County Filled
484530023163,B25077_001E,County Filled
484530023172,B25077_001E,Tract Filled
484530024021,B25064_001E,Tract Filled
484530024072,B25064_001E,Tract Filled
484530024191,B25077_001E,County Filled
484530024192,B25077_001E,County Filled
484530024301,B25064_001E,Tract Filled
484530025001,B25064_001E,Tract Filled
Loading

0 comments on commit 9b177a0

Please sign in to comment.