Skip to content

Commit

Permalink
Merge branch 'gtfs-html-new' of https://github.com/datasciencecampus/…
Browse files Browse the repository at this point in the history
  • Loading branch information
CBROWN-ONS committed Sep 12, 2023
2 parents cbad501 + 5743d11 commit b649429
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 172 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/all-os-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will install OS dependencies and run a 'base' set of unit tests with Python 3.9
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Unit tests on macOS/Linux/Windows

on:
push:
pull_request:
branches: [ "dev", "main" ]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ["3.9"]
os: ["macos-latest", "ubuntu-latest", "windows-latest"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Integration Tests # run only those tests marked runinteg & with no osmosis deps
run: |
pytest -m runinteg --runinteg --deselect tests/osm/
- name: Test with pytest # run only tests with no osmosis deps
run: |
pytest --deselect tests/osm/
28 changes: 16 additions & 12 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# This workflow will install Python dependencies, run tests and lint with Python 3.9
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python package
Expand All @@ -10,7 +10,6 @@ on:

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -23,11 +22,6 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install geos # required for cartopy installation
uses: ConorMacBride/install-package@v1
with:
brew: geos
apt: libgeos-dev
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -40,11 +34,19 @@ jobs:
- name: Check Java Install
run: |
java --version
- name: Install Osmosis
uses: ConorMacBride/install-package@v1
with:
brew: osmosis
apt: osmosis
- name: Install mac depencies with brew
if: runner.os == 'macOS' # not updating brew version, issue with aom
run: |
brew install geos
brew install osmosis
shell: sh
- name: Install linux depencies with apt
if: runner.os == 'Linux'
run: |
sudo apt update
sudo apt install -y libgeos-dev
sudo apt install -y osmosis
shell: sh
- name: Run Integration Tests
run: |
pytest -m runinteg --runinteg # run only those tests marked runinteg
Expand All @@ -56,10 +58,12 @@ jobs:
run: |
pytest
- name: Generate Report
if: runner.os == 'macOS' # run coverage report only on macOS
run: |
coverage run -m pytest
coverage xml
- name: Upload coverage reports to Codecov
if: runner.os == 'macOS' # run coverage report only on macOS
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
Expand Down
24 changes: 20 additions & 4 deletions notebooks/urban_centres/poc_urban_centres.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@
AREA_OF_INTEREST = "newport"
BBOX = BBOX_DICT[AREA_OF_INTEREST]

# bbox
bbox_npt = gpd.GeoDataFrame(index=[0], crs="epsg:4326", geometry=[box(*BBOX)])
bbox_npt_r = bbox_npt.to_crs("esri:54009")
coords = (float(bbox_npt.centroid.y), float(bbox_npt.centroid.x))

# bbox centroid
bbox_npt_centroid = bbox_npt.to_crs("epsg:27700").centroid.to_crs("epsg:4326")
coords = (bbox_npt_centroid.y[0], bbox_npt_centroid.x[0])

# pop only criteria
npt = ucc.UrbanCentre(file=(MERGED_DIR))
Expand All @@ -105,9 +109,13 @@
AREA_OF_INTEREST = "leeds"
BBOX = BBOX_DICT[AREA_OF_INTEREST]

# bbox
bbox_lds = gpd.GeoDataFrame(index=[0], crs="epsg:4326", geometry=[box(*BBOX)])
bbox_lds_r = bbox_lds.to_crs("esri:54009")
coords = (float(bbox_lds.centroid.y), float(bbox_lds.centroid.x))

# bbox centroid
bbox_lds_centroid = bbox_lds.to_crs("epsg:27700").centroid.to_crs("epsg:4326")
coords = (bbox_lds_centroid.y[0], bbox_lds_centroid.x[0])

# pop only criteria
lds = ucc.UrbanCentre(file=(MERGED_DIR))
Expand All @@ -126,9 +134,13 @@
AREA_OF_INTEREST = "london"
BBOX = BBOX_DICT[AREA_OF_INTEREST]

# bbox
bbox_lnd = gpd.GeoDataFrame(index=[0], crs="epsg:4326", geometry=[box(*BBOX)])
bbox_lnd_r = bbox_lnd.to_crs("esri:54009")
coords = (float(bbox_lnd.centroid.y), float(bbox_lnd.centroid.x))

# bbox centroid
bbox_lnd_centroid = bbox_lnd.to_crs("epsg:27700").centroid.to_crs("epsg:4326")
coords = (bbox_lnd_centroid.y[0], bbox_lnd_centroid.x[0])

# pop only criteria
lnd = ucc.UrbanCentre(file=(MERGED_DIR))
Expand All @@ -146,9 +158,13 @@
AREA_OF_INTEREST = "marseille"
BBOX = BBOX_DICT[AREA_OF_INTEREST]

# bbox
bbox_mrs = gpd.GeoDataFrame(index=[0], crs="epsg:4326", geometry=[box(*BBOX)])
bbox_mrs_r = bbox_mrs.to_crs("esri:54009")
coords = (float(bbox_mrs.centroid.y), float(bbox_mrs.centroid.x))

# bbox centroid
bbox_mrs_centroid = bbox_mrs_r.centroid.to_crs("epsg:4326")
coords = (bbox_mrs_centroid.y[0], bbox_mrs_centroid.x[0])

# pop only criteria
mrs = ucc.UrbanCentre(file=(MERGED_DIR))
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pre-commit
r5py>=0.0.4
gtfs_kit==5.2.7
rasterio
matplotlib
matplotlib>=3.7.0
scipy
rioxarray
geopandas
Expand All @@ -13,7 +13,7 @@ coverage
pyprojroot
pytest-lazy-fixture
ipykernel==6.23.1
pandas
pandas<2.1.0
beautifulsoup4
requests
pytest-mock
Expand Down
7 changes: 3 additions & 4 deletions src/transport_performance/gtfs/gtfs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from transport_performance.utils.defence import (
_is_expected_filetype,
_check_list,
_dataframe_defence,
_bool_defence,
_type_defence,
)


Expand Down Expand Up @@ -109,8 +108,8 @@ def convert_pandas_to_plotly(
}
}
# defences
_dataframe_defence(df, "df")
_bool_defence(return_html, "return_html")
_type_defence(df, "df", pd.DataFrame)
_type_defence(return_html, "return_html", bool)
if scheme not in list(schemes.keys()):
raise LookupError(
f"{scheme} is not a valid colour scheme."
Expand Down
13 changes: 6 additions & 7 deletions src/transport_performance/gtfs/report/report_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import os

from transport_performance.utils.defence import (
_bool_defence,
_string_defence,
_is_path_like,
_type_defence,
_handle_path_like,
_check_parent_dir_exists,
)

Expand Down Expand Up @@ -76,7 +75,7 @@ def __init__(self, path: Union[str, pathlib.Path]) -> None:
None
"""
_is_path_like(path, "path")
_handle_path_like(path, "path")
with open(path, "r", encoding="utf8") as f:
self.template = f.read()
return None
Expand Down Expand Up @@ -109,9 +108,9 @@ def insert(
place-holder but 'replace_multiple' is not True
"""
_string_defence(placeholder, "placeholder")
_string_defence(value, "value")
_bool_defence(replace_multiple, "replace_multiple")
_type_defence(placeholder, "placeholder", str)
_type_defence(value, "value", str)
_type_defence(replace_multiple, "replace_multiple", bool)
occurences = len(self.template.split(f"[{placeholder}]")) - 1
if occurences > 1 and not replace_multiple:
raise ValueError(
Expand Down
4 changes: 2 additions & 2 deletions src/transport_performance/gtfs/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import requests
import warnings

from transport_performance.utils.defence import _url_defence, _bool_defence
from transport_performance.utils.defence import _url_defence, _type_defence

warnings.filterwarnings(
action="ignore", category=DeprecationWarning, module=".*pkg_resources"
Expand Down Expand Up @@ -98,7 +98,7 @@ def scrape_route_type_lookup(
for url in [gtfs_url, ext_spec_url]:
_url_defence(url)

_bool_defence(extended_schema, "extended_schema")
_type_defence(extended_schema, "extended_schema", bool)
# Get the basic scheme lookup
resp_txt = _get_response_text(gtfs_url)
soup = BeautifulSoup(resp_txt, "html.parser")
Expand Down
33 changes: 14 additions & 19 deletions src/transport_performance/gtfs/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
_check_namespace_export,
_check_parent_dir_exists,
_check_column_in_df,
_bool_defence,
_integer_defence,
_string_defence,
_dataframe_defence,
_dict_defence,
_string_and_nonetype_defence,
_type_defence,
)

from transport_performance.gtfs.report.report_utils import (
Expand Down Expand Up @@ -738,17 +733,17 @@ def _plot_summary(
"""
# parameter type defences
_dataframe_defence(summary_df, "summary_df")
_string_defence(day_column, "day_column")
_string_defence(target_column, "target_column")
_dict_defence(plotly_kwargs, "plotly_kwargs")
_bool_defence(return_html, "return_html")
_integer_defence(width, "width")
_integer_defence(height, "height")
_string_and_nonetype_defence(xlabel, "xlabel")
_string_and_nonetype_defence(ylabel, "ylabel")
_bool_defence(save_html, "save_html")
_bool_defence(save_image, "save_iamge")
_type_defence(summary_df, "summary_df", pd.DataFrame)
_type_defence(day_column, "day_column", str)
_type_defence(target_column, "target_column", str)
_type_defence(plotly_kwargs, "plotly_kwargs", dict)
_type_defence(return_html, "return_html", bool)
_type_defence(width, "width", int)
_type_defence(height, "height", int)
_type_defence(xlabel, "xlabel", (str, type(None)))
_type_defence(ylabel, "ylabel", (str, type(None)))
_type_defence(save_html, "save_html", bool)
_type_defence(save_image, "save_iamge", bool)
_check_parent_dir_exists(save_pth, "save_pth", create=True)

# orientation input defences
Expand Down Expand Up @@ -1269,8 +1264,8 @@ def html_report(
An error raised if the type of summary passed is invalid
"""
_bool_defence(overwrite, "overwrite")
_string_defence(summary_type, "summary_type")
_type_defence(overwrite, "overwrite", bool)
_type_defence(summary_type, "summary_type", str)
set_up_report_dir(path=report_dir, overwrite=overwrite)
summary_type = summary_type.lower()
if summary_type not in ["mean", "min", "max", "median"]:
Expand Down
4 changes: 2 additions & 2 deletions src/transport_performance/osm/osm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pyprojroot import here

from transport_performance.utils.defence import (
_bool_defence,
_type_defence,
_check_list,
_check_parent_dir_exists,
_is_expected_filetype,
Expand Down Expand Up @@ -54,7 +54,7 @@ def filter_osm(
"tag_filter": tag_filter,
"install_osmosis": install_osmosis,
}.items():
_bool_defence(val, param_nm=nm)
_type_defence(val, nm, bool)
# check bbox values makes sense, else osmosis will error
if not bbox[0] < bbox[2]:
raise ValueError(
Expand Down
37 changes: 25 additions & 12 deletions src/transport_performance/population/rasterpop.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RasterPop:
pop_gdf : gpd.GeoDataFrame
A geopandas dataframe of the input data, with gridded geometry. This is
in the same CRS as the input raster data.
centroid_gdf
centroid_gdf : gpd.GeoDataFrame
A geopandas dataframe of grid centroids, converted to EPSG:4326 for
transport analysis.
Expand Down Expand Up @@ -153,7 +153,7 @@ def plot(
save : str, optional
Filepath to save file, with the file extension, by default None
meaning a file will not be saved.
**kwargs
kwargs : dict, optional
Extra arguments passed to plotting functions to configure the plot
styling. See Notes for more support.
Expand All @@ -177,9 +177,10 @@ def plot(
-----
Calling `help` as follows will provide more insights on possible kwarg
arguments for the valid plotting backends:
- Folium backend: `help(RasterPop._plot_folium)
- Matplotlib backend: `help(RasterPop._plot_matplotlib)
- Cartopy backend: `help(RasterPop._plot_cartopy)
- Folium backend: `help(RasterPop._plot_folium)`
- Matplotlib backend: `help(RasterPop._plot_matplotlib)`
- Cartopy backend: `help(RasterPop._plot_cartopy)`
"""
# record of valid which values
Expand Down Expand Up @@ -463,18 +464,30 @@ def _plot_folium(
)

# add the centroids to a separate layer
self.centroid_gdf.explore(
self.__UC_COL_NAME,
name="Centroids",
m=m,
show=False,
style_kwds={
# conditionally style plot based on whether UC is provided
if self._uc_gdf is not None:
centroid_plot_col = self.__UC_COL_NAME
# this dict will change the centriod color in/out the UC.
centroid_style_dict = {
"style_function": lambda x: {
"color": "#BC544B"
if x["properties"][self.__UC_COL_NAME] is False
else "#8B0000"
}
},
}
else:
centroid_plot_col = None
centroid_style_dict = {
"style_function": lambda x: {"color": "#BC544B"}
}

# add in the centroid layer with the conditional styling
self.centroid_gdf.explore(
centroid_plot_col,
name="Centroids",
m=m,
show=False,
style_kwds=centroid_style_dict,
legend=False,
)

Expand Down
Loading

0 comments on commit b649429

Please sign in to comment.