-
Notifications
You must be signed in to change notification settings - Fork 1
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
boundary preprocessing #52
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
eb1801f
boundary preprocessing wip
Hussein-Mahfouz c638777
print out valid values if input is invalid
Hussein-Mahfouz c04d89a
update bash script
Hussein-Mahfouz d899628
add boundary filtering to config
Hussein-Mahfouz 29ce798
update config
Hussein-Mahfouz ce94469
remove hardcoding
Hussein-Mahfouz 5739d89
remove hardcoded region
Hussein-Mahfouz b4148cf
update filtering logic to match spc zones
Hussein-Mahfouz 9dc2127
remove filter parameters
Hussein-Mahfouz bf65e49
fix column drop error
Hussein-Mahfouz 8d16275
always keep msoa21cd
Hussein-Mahfouz 9d7d5b7
add zone_id argument
Hussein-Mahfouz 7deda46
update configs
Hussein-Mahfouz 346a917
get commute_level from config
Hussein-Mahfouz 5e47d0c
add error message if boundary_geography is not OA / MSOA
Hussein-Mahfouz 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
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
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,78 @@ | ||
import geopandas as gpd | ||
import pandas as pd | ||
from uatk_spc import Reader | ||
|
||
import acbm | ||
from acbm.cli import acbm_cli | ||
from acbm.config import load_config | ||
from acbm.logger_config import preprocessing_logger as logger | ||
from acbm.preprocessing import edit_boundary_resolution | ||
|
||
|
||
@acbm_cli | ||
def main(config_file): | ||
config = load_config(config_file) | ||
config.init_rng() | ||
region = config.region | ||
# Pick a region with SPC output saved | ||
spc_path = acbm.root_path / "data/external/spc_output/raw/" | ||
|
||
# ----- BOUNDARIES | ||
logger.info("Preprocessing Boundary Layer") | ||
|
||
## Read in the boundary layer for the whole of England | ||
|
||
logger.info("1. Reading in the boundary layer for the whole of England") | ||
|
||
boundaries = gpd.read_file( | ||
acbm.root_path / "data/external/boundaries/oa_england.geojson" | ||
) | ||
|
||
boundaries = boundaries.to_crs(epsg=4326) | ||
|
||
## --- Dissolve boundaries if resolution is MSOA | ||
|
||
boundary_geography = config.parameters.boundary_geography # can only be OA or MSOA | ||
logger.info(f"2. Dissolving boundaries to {boundary_geography} level") | ||
|
||
boundaries = edit_boundary_resolution( | ||
study_area=boundaries, geography=boundary_geography, zone_id=config.zone_id | ||
) | ||
|
||
## --- Filter to study area | ||
# we filter using msoa21cd values, which exist regardless of the boundary resolution | ||
|
||
logger.info("3. Filtering boundaries to specified study area") | ||
|
||
# Step 1: Get zones from SPC (these will be 2011 MSOAs) | ||
spc = Reader(spc_path, region, backend="pandas") | ||
zones_in_region = list(spc.info_per_msoa.keys()) | ||
|
||
# Step 2: Filter boundaries to identified zones | ||
|
||
# a) get MSOA11CD to MSOA21CD lookup | ||
msoa_lookup = pd.read_csv( | ||
acbm.root_path | ||
/ "data/external/MSOA_2011_MSOA_2021_Lookup_for_England_and_Wales.csv" | ||
) | ||
# Filter msoa_lookup to include only rows where MSOA11CD is in zones_in_region | ||
msoa_lookup_filtered = msoa_lookup[msoa_lookup["MSOA11CD"].isin(zones_in_region)] | ||
# Extract the corresponding MSOA21CD values | ||
msoa21cd_values = msoa_lookup_filtered["MSOA21CD"].tolist() | ||
|
||
# b) filter boundaries to include only rows where MSOA21CD is in msoa21cd_values | ||
boundaries_filtered = boundaries[boundaries["MSOA21CD"].isin(msoa21cd_values)] | ||
|
||
## Save the output as parquet | ||
logger.info( | ||
f"4. Saving the boundaries to {acbm.root_path / 'data/external/boundaries/'} path" | ||
) | ||
|
||
boundaries_filtered.to_file( | ||
acbm.root_path / "data/external/boundaries/study_area_zones.geojson", | ||
driver="GeoJSON", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
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
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
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
Oops, something went wrong.
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.
Perhaps we should raise an exception here to handle any cases where geography provided is not "OA" or "MSOA"? E.g.:
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.
yup, done now: 5e47d0c