Skip to content

Commit

Permalink
add boundary filtering to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Hussein-Mahfouz committed Oct 2, 2024
1 parent c04d89a commit d899628
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
4 changes: 4 additions & 0 deletions config/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ region = "leeds"
number_of_households = 10000
zone_id = "OA21CD"
travel_times = true
boundary_geography = "OA"
boundary_filter_column = "LEP22NM1"
boundary_filter_values = ["Leeds City Region"]


[work_assignment]
use_percentages = true
Expand Down
4 changes: 4 additions & 0 deletions config/base_500.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ region = "leeds"
number_of_households = 500
zone_id = "OA21CD"
travel_times = true
boundary_geography = "OA"
boundary_filter_column = "LEP22NM1"
boundary_filter_values = ["Leeds City Region"]


[work_assignment]
use_percentages = true
Expand Down
4 changes: 4 additions & 0 deletions config/base_5000.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ region = "leeds"
number_of_households = 5000
zone_id = "OA21CD"
travel_times = true
boundary_geography = "OA"
boundary_filter_column = "LEP22NM1"
boundary_filter_values = ["Leeds City Region"]


[work_assignment]
use_percentages = true
Expand Down
4 changes: 4 additions & 0 deletions config/base_all.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ seed = 0
region = "leeds"
zone_id = "OA21CD"
travel_times = true
boundary_geography = "OA"
boundary_filter_column = "LEP22NM1"
boundary_filter_values = ["Leeds City Region"]


[work_assignment]
use_percentages = false
Expand Down
69 changes: 41 additions & 28 deletions scripts/0_preprocess_inputs.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
import geopandas as gpd

import acbm
from acbm.logger_config import preprocessing_logger_logger as logger
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, filter_boundaries

# ----- BOUNDARIES
logger.info("Preprocessing Boundary Layer")

## Read in the boundary layer for the whole of England
@acbm_cli
def main(config_file):
config = load_config(config_file)
config.init_rng()

logger.info("1. Reading in the boundary layer for the whole of England")
# ----- BOUNDARIES
logger.info("Preprocessing Boundary Layer")

## Read in the boundary layer for the whole of England

boundaries = gpd.read_file(
acbm.root_path / "data/external/boundaries/oa_england.geojson"
)
logger.info("1. Reading in the boundary layer for the whole of England")

boundaries = boundaries.to_crs(epsg=4326)
boundaries = gpd.read_file(
acbm.root_path / "data/external/boundaries/oa_england.geojson"
)

## Dissolve boundaries if resolution is MSOA
boundaries = boundaries.to_crs(epsg=4326)

boundary_geography = "OA" # can only be OA or MSOA
logger.info(f"2. Dissolving boundaries to {boundary_geography} level")
## Dissolve boundaries if resolution is MSOA

boundaries = edit_boundary_resolution(boundaries, boundary_geography)
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(boundaries, boundary_geography)

## Filter to study area
## Filter to study area

logger.info("3. Filtering boundaries to specified study area")
# TODO get from config and log
# logger.info(f"3. Filtering boundaries to {config.parameters.boundary_filter_column} = {config.parameters.study_area}")
logger.info("3. Filtering boundaries to specified study area")
# TODO get from config and log
# logger.info(f"3. Filtering boundaries to {config.parameters.boundary_filter_column} = {config.parameters.study_area}")

boundaries_filtered = filter_boundaries(
boundaries=boundaries, column="LEP22NM1", values=["Leeds City Region"]
)
boundaries_filtered = filter_boundaries(
# boundaries=boundaries, column="LEP22NM1", values=["Leeds City Region"]
boundaries=boundaries,
column=config.parameters.boundary_filter_column,
values=config.parameters.boundary_filter_values,
)

## Save the output as parquet
logger.info(
f"4. Saving the boundaries to {acbm.root_path / 'data/external/boundaries/'} path"
)
## 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",
)
boundaries_filtered.to_file(
acbm.root_path / "data/external/boundaries/study_area_zones.geojson",
driver="GeoJSON",
)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion scripts/run_pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

python scripts/0_preprocessing_inputs.py --config_file $1
python scripts/0_preprocess_inputs.py --config_file $1
python scripts/1_prep_synthpop.py --config_file $1
python scripts/2_match_households_and_individuals.py --config_file $1
python scripts/3.1_assign_primary_feasible_zones.py --config_file $1
Expand Down
3 changes: 3 additions & 0 deletions src/acbm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Parameters(BaseModel):
number_of_households: int | None = None
zone_id: str
travel_times: bool
boundary_geography: str
boundary_filter_column: str
boundary_filter_values: list[str]


@dataclass(frozen=True)
Expand Down

0 comments on commit d899628

Please sign in to comment.