diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 70a7895f..4e1b5285 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,6 +32,15 @@ repos: - --extra-keys - "metadata.colab metadata.kernelspec cell.metadata.colab cell.metadata.executionInfo cell.metadata.id cell.metadata.outputId" +# ----- isort - imports sorting +- repo: https://github.com/pycqa/isort + rev: 5.12.0 + hooks: + - id: isort + name: isort (python) + types: [python] + args: [ "--profile", "black", "--filter-files" ] + # ----- black - code formatting - repo: https://github.com/psf/black rev: 22.3.0 # Replace by any tag/version: https://github.com/psf/black/tags @@ -49,15 +58,6 @@ repos: - id: flake8 name: flake8 - Python linting -# ----- isort - imports sorting -- repo: https://github.com/pycqa/isort - rev: 5.12.0 - hooks: - - id: isort - name: isort - Sort Python imports (auto-fixes) - types: [ cython, pyi, python ] - args: [ "--profile", "black", "--filter-files" ] - # ----- nbQA - notebook quality assuring - repo: https://github.com/nbQA-dev/nbQA rev: 0.12.0 diff --git a/notebooks/e2e/e2e_leeds.py b/notebooks/e2e/e2e_leeds.py index f77ab464..cfccb0d0 100644 --- a/notebooks/e2e/e2e_leeds.py +++ b/notebooks/e2e/e2e_leeds.py @@ -10,29 +10,27 @@ Call in script wide imports and the configuration information. """ -# %% -import toml -import os import datetime +import os + +import folium import geopandas as gpd -import pandas as pd import gtfs_kit as gk -import folium +import pandas as pd import seaborn as sns +# %% +import toml +from folium.map import Icon from pyprojroot import here +from r5py import TransportMode, TransportNetwork, TravelTimeMatrixComputer from shapely.geometry import box -from folium.map import Icon -from r5py import ( - TransportNetwork, - TravelTimeMatrixComputer, - TransportMode, -) -from transport_performance.urban_centres.raster_uc import UrbanCentre -from transport_performance.population.rasterpop import RasterPop + from transport_performance.gtfs.gtfs_utils import bbox_filter_gtfs from transport_performance.gtfs.validation import GtfsInstance from transport_performance.osm.osm_utils import filter_osm +from transport_performance.population.rasterpop import RasterPop +from transport_performance.urban_centres.raster_uc import UrbanCentre from transport_performance.utils.raster import ( merge_raster_files, sum_resample_file, diff --git a/notebooks/e2e/e2e_london.py b/notebooks/e2e/e2e_london.py index 4e4ee156..7c559a6e 100644 --- a/notebooks/e2e/e2e_london.py +++ b/notebooks/e2e/e2e_london.py @@ -10,8 +10,6 @@ Call in script wide imports and the configuration information. """ -# %% -import toml import os # import datetime @@ -20,11 +18,16 @@ # import pandas as pd import gtfs_kit as gk -# import folium - +# %% +import toml from pyprojroot import here from shapely.geometry import box +from transport_performance.gtfs.gtfs_utils import bbox_filter_gtfs +from transport_performance.gtfs.validation import GtfsInstance +from transport_performance.osm.osm_utils import filter_osm +from transport_performance.population.rasterpop import RasterPop + # from folium.map import Icon # from r5py import ( # TransportNetwork, @@ -32,15 +35,12 @@ # TransportMode, # ) from transport_performance.urban_centres.raster_uc import UrbanCentre -from transport_performance.population.rasterpop import RasterPop -from transport_performance.gtfs.gtfs_utils import bbox_filter_gtfs -from transport_performance.gtfs.validation import GtfsInstance -from transport_performance.osm.osm_utils import filter_osm - from transport_performance.utils.raster import ( merge_raster_files, - # sum_resample_file, -) +) # sum_resample_file, + +# import folium + # %% # config filepath, and loading diff --git a/notebooks/e2e/e2e_marseille.py b/notebooks/e2e/e2e_marseille.py index 322e18a9..288fe752 100644 --- a/notebooks/e2e/e2e_marseille.py +++ b/notebooks/e2e/e2e_marseille.py @@ -10,30 +10,28 @@ Call in script wide imports and the configuration information. """ -# %% -import toml +import datetime import glob import os -import datetime + +import folium import geopandas as gpd -import pandas as pd import gtfs_kit as gk -import folium +import pandas as pd import seaborn as sns +# %% +import toml +from folium.map import Icon from pyprojroot import here +from r5py import TransportMode, TransportNetwork, TravelTimeMatrixComputer from shapely.geometry import box -from folium.map import Icon -from r5py import ( - TransportNetwork, - TravelTimeMatrixComputer, - TransportMode, -) -from transport_performance.urban_centres.raster_uc import UrbanCentre -from transport_performance.population.rasterpop import RasterPop + from transport_performance.gtfs.gtfs_utils import bbox_filter_gtfs from transport_performance.gtfs.validation import GtfsInstance from transport_performance.osm.osm_utils import filter_osm +from transport_performance.population.rasterpop import RasterPop +from transport_performance.urban_centres.raster_uc import UrbanCentre from transport_performance.utils.raster import ( merge_raster_files, sum_resample_file, diff --git a/notebooks/e2e/e2e_newport.py b/notebooks/e2e/e2e_newport.py index a9a92cd4..4266ffc1 100644 --- a/notebooks/e2e/e2e_newport.py +++ b/notebooks/e2e/e2e_newport.py @@ -10,29 +10,27 @@ Call in script wide imports and the configuration information. """ -# %% -import toml -import os import datetime +import os + +import folium import geopandas as gpd -import pandas as pd import gtfs_kit as gk -import folium +import pandas as pd import seaborn as sns +# %% +import toml +from folium.map import Icon from pyprojroot import here +from r5py import TransportMode, TransportNetwork, TravelTimeMatrixComputer from shapely.geometry import box -from folium.map import Icon -from r5py import ( - TransportNetwork, - TravelTimeMatrixComputer, - TransportMode, -) -from transport_performance.urban_centres.raster_uc import UrbanCentre -from transport_performance.population.rasterpop import RasterPop + from transport_performance.gtfs.gtfs_utils import bbox_filter_gtfs from transport_performance.gtfs.validation import GtfsInstance from transport_performance.osm.osm_utils import filter_osm +from transport_performance.population.rasterpop import RasterPop +from transport_performance.urban_centres.raster_uc import UrbanCentre from transport_performance.utils.raster import ( merge_raster_files, sum_resample_file, diff --git a/notebooks/gtfs/check_unmatched_id_warnings.py b/notebooks/gtfs/check_unmatched_id_warnings.py index 6e0c4f1d..2777123c 100644 --- a/notebooks/gtfs/check_unmatched_id_warnings.py +++ b/notebooks/gtfs/check_unmatched_id_warnings.py @@ -3,9 +3,9 @@ # %% # imports import gtfs_kit as gk -from pyprojroot import here -import pandas as pd import numpy as np +import pandas as pd +from pyprojroot import here # %% # initialise my feed from GTFS test data diff --git a/notebooks/gtfs/stop_id_check.py b/notebooks/gtfs/stop_id_check.py index a49d846e..79826a81 100644 --- a/notebooks/gtfs/stop_id_check.py +++ b/notebooks/gtfs/stop_id_check.py @@ -1,8 +1,8 @@ """A notebook containing viable code for additional stop_id validation.""" # %% # IMPORTS -from transport_performance.gtfs.validation import GtfsInstance from transport_performance.gtfs.gtfs_utils import _add_validation_row +from transport_performance.gtfs.validation import GtfsInstance # %% diff --git a/notebooks/metrics/metrics_experiments.py b/notebooks/metrics/metrics_experiments.py index 832cd68a..231611e8 100644 --- a/notebooks/metrics/metrics_experiments.py +++ b/notebooks/metrics/metrics_experiments.py @@ -9,23 +9,21 @@ Call in script wide imports and the configuration information. """ -# %% -import folium -import geopandas as gpd import os -import pandas as pd import pathlib import pickle +from typing import Union +# %% +import folium +import geopandas as gpd +import pandas as pd from folium.map import Icon from haversine import haversine_vector from pyprojroot import here from tqdm import tqdm -from typing import Union -from transport_performance.utils.defence import ( - _check_parent_dir_exists, -) +from transport_performance.utils.defence import _check_parent_dir_exists # %% # name of area and source of metrics inputs diff --git a/notebooks/osm/01-visualise-osm.py b/notebooks/osm/01-visualise-osm.py index 366d74fb..9df222c8 100644 --- a/notebooks/osm/01-visualise-osm.py +++ b/notebooks/osm/01-visualise-osm.py @@ -1,6 +1,7 @@ """Visualise OMS spatial features.""" # %% from pyprojroot import here + from transport_performance.osm import validate_osm as osmval # %% diff --git a/notebooks/population/grid_pop_experiment.py b/notebooks/population/grid_pop_experiment.py index f680fa12..1a14996f 100644 --- a/notebooks/population/grid_pop_experiment.py +++ b/notebooks/population/grid_pop_experiment.py @@ -49,34 +49,35 @@ """ -# %% -import rasterio as rio +import logging import os import sys -import logging -import numpy as np -import pandas as pd -import geopandas as gpd -import matplotlib.pyplot as plt -import seaborn as sns +import textwrap +from datetime import datetime + import cartopy.crs as ccrs import cartopy.io.img_tiles as cimgt +import geopandas as gpd +import matplotlib.pyplot as plt +import numpy as np +import pandas as pd + +# %% +import rasterio as rio +import requests import rioxarray +import seaborn as sns import xarray as xr -import requests -import textwrap - -from datetime import datetime -from pyproj import Transformer -from pyprojroot import here +from geocube.vector import vectorize from matplotlib import colormaps from matplotlib.colors import LogNorm from matplotlib.ticker import AutoMinorLocator +from pyproj import Transformer +from pyprojroot import here from rasterio.warp import Resampling -from geocube.vector import vectorize -from rioxarray.merge import merge_arrays -from rioxarray.exceptions import NoDataInBounds from requests.exceptions import HTTPError +from rioxarray.exceptions import NoDataInBounds +from rioxarray.merge import merge_arrays from shapely.geometry.polygon import Polygon from xarray import DataArray diff --git a/notebooks/urban_centres/poc_urban_centres.py b/notebooks/urban_centres/poc_urban_centres.py index 4251cd39..8c3468e6 100644 --- a/notebooks/urban_centres/poc_urban_centres.py +++ b/notebooks/urban_centres/poc_urban_centres.py @@ -27,13 +27,13 @@ # %% import os +import folium import geopandas as gpd -from shapely.geometry import box -from pyprojroot import here +import matplotlib.pyplot as plt import rioxarray as rxr +from pyprojroot import here from rioxarray.merge import merge_arrays -import matplotlib.pyplot as plt -import folium +from shapely.geometry import box import transport_performance.urban_centres.raster_uc as ucc diff --git a/pipeline/gtfs/01-validate-gtfs.py b/pipeline/gtfs/01-validate-gtfs.py index b46d8b15..89c63adc 100644 --- a/pipeline/gtfs/01-validate-gtfs.py +++ b/pipeline/gtfs/01-validate-gtfs.py @@ -10,10 +10,11 @@ 8. new - modalities available (including extended spec) 9. new - feed stats by is-weekend """ +import subprocess +import time + import toml from pyprojroot import here -import time -import subprocess from transport_performance.gtfs.validation import GtfsInstance from transport_performance.utils.defence import _is_expected_filetype diff --git a/pipeline/osm/01-validate-osm.py b/pipeline/osm/01-validate-osm.py index 7e218e53..6fa66539 100644 --- a/pipeline/osm/01-validate-osm.py +++ b/pipeline/osm/01-validate-osm.py @@ -6,14 +6,16 @@ 4. Inspect tags by feature type. """ +import time + import toml from pyprojroot import here + from transport_performance.osm.validate_osm import ( FindIds, - FindTags, FindLocations, + FindTags, ) -import time CONFIG = toml.load(here("pipeline/osm/config/01-validate-osm.toml")) OSM_PTH = CONFIG["OSM"]["PATH"] diff --git a/pyproject.toml b/pyproject.toml index 1bba9635..d11f27ae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,8 @@ exclude_lines = [ # `isort` configurations [tool.isort] profile = "black" +skip_glob = ["notebooks/*"] +line_length = 79 # `pytest` configurations [tool.pytest.ini_options] diff --git a/scripts/prep-fixtures.py b/scripts/prep-fixtures.py index eedf92cc..3d54dcc8 100644 --- a/scripts/prep-fixtures.py +++ b/scripts/prep-fixtures.py @@ -12,14 +12,14 @@ station, filtered to a date of 20230613. """ import os -import gtfs_kit as gk + import geopandas as gpd -from shapely.geometry import box +import gtfs_kit as gk from pyprojroot import here +from shapely.geometry import box from transport_performance.osm.osm_utils import filter_osm - fix_dat = os.path.join("tests", "data", "gtfs") gtfs_zip = [ os.path.join(fix_dat, x) for x in os.listdir(fix_dat) if x.endswith(".zip") diff --git a/scripts/prep-metrics-fixtures.py b/scripts/prep-metrics-fixtures.py index c72423c1..d9c9086a 100644 --- a/scripts/prep-metrics-fixtures.py +++ b/scripts/prep-metrics-fixtures.py @@ -25,7 +25,6 @@ import geopandas as gpd import numpy as np import pandas as pd - from pyprojroot import here from shapely.geometry import Polygon diff --git a/src/transport_performance/_metrics/tp_utils.py b/src/transport_performance/_metrics/tp_utils.py index 574f3677..097c6d5e 100644 --- a/src/transport_performance/_metrics/tp_utils.py +++ b/src/transport_performance/_metrics/tp_utils.py @@ -1,13 +1,13 @@ """Transport performance helper functions.""" -import geopandas as gpd -import numpy as np -import pandas as pd import pathlib import warnings +from typing import Union +import geopandas as gpd +import numpy as np +import pandas as pd from haversine import haversine_vector -from typing import Union def _transport_performance_pandas( diff --git a/src/transport_performance/analyse_network.py b/src/transport_performance/analyse_network.py index a4acd68c..d74e38f9 100644 --- a/src/transport_performance/analyse_network.py +++ b/src/transport_performance/analyse_network.py @@ -2,17 +2,16 @@ import glob import pathlib import warnings +from math import ceil +from typing import Union import dask.dataframe as dd import geopandas as gpd import numpy as np import pandas as pd - from haversine import Unit, haversine_vector -from math import ceil from r5py import TransportNetwork, TravelTimeMatrixComputer from tqdm import tqdm -from typing import Union import transport_performance.utils.defence as d diff --git a/src/transport_performance/gtfs/calendar.py b/src/transport_performance/gtfs/calendar.py index e857b64f..8be5e862 100644 --- a/src/transport_performance/gtfs/calendar.py +++ b/src/transport_performance/gtfs/calendar.py @@ -5,8 +5,8 @@ import pandas as pd from transport_performance.utils.defence import ( - _type_defence, _check_column_in_df, + _type_defence, ) diff --git a/src/transport_performance/gtfs/cleaners.py b/src/transport_performance/gtfs/cleaners.py index 45baab0e..b0802690 100644 --- a/src/transport_performance/gtfs/cleaners.py +++ b/src/transport_performance/gtfs/cleaners.py @@ -1,10 +1,10 @@ """A set of functions that clean the gtfs data.""" -from typing import Union import warnings +from typing import Union import numpy as np -from transport_performance.utils.defence import _gtfs_defence, _check_iterable +from transport_performance.utils.defence import _check_iterable, _gtfs_defence def drop_trips(gtfs, trip_id: Union[str, list, np.ndarray]) -> None: diff --git a/src/transport_performance/gtfs/gtfs_utils.py b/src/transport_performance/gtfs/gtfs_utils.py index 5f98d5d2..e2d3f819 100644 --- a/src/transport_performance/gtfs/gtfs_utils.py +++ b/src/transport_performance/gtfs/gtfs_utils.py @@ -1,31 +1,32 @@ """Utility functions for GTFS archives.""" -import gtfs_kit as gk +import os +import pathlib +import warnings +from typing import TYPE_CHECKING, Union + import geopandas as gpd -from shapely.geometry import box -from pyprojroot import here +import gtfs_kit as gk import pandas as pd -import os import plotly.graph_objects as go -from typing import Union, TYPE_CHECKING -import pathlib from geopandas import GeoDataFrame -import warnings +from pyprojroot import here +from shapely.geometry import box if TYPE_CHECKING: from transport_performance.gtfs.validation import GtfsInstance +from transport_performance.utils.constants import PKG_PATH from transport_performance.utils.defence import ( - _is_expected_filetype, - _check_iterable, - _type_defence, _check_attribute, + _check_item_in_iter, + _check_iterable, + _check_parent_dir_exists, + _enforce_file_extension, _gtfs_defence, + _is_expected_filetype, + _type_defence, _validate_datestring, - _enforce_file_extension, - _check_parent_dir_exists, - _check_item_in_iter, ) -from transport_performance.utils.constants import PKG_PATH def _validate_bbox_list(bbox: list) -> None: diff --git a/src/transport_performance/gtfs/multi_validation.py b/src/transport_performance/gtfs/multi_validation.py index 9f53579f..2be6f3a5 100644 --- a/src/transport_performance/gtfs/multi_validation.py +++ b/src/transport_performance/gtfs/multi_validation.py @@ -1,26 +1,26 @@ """Validating multiple GTFS at once.""" -from typing import Union -from tqdm import tqdm -import pathlib import glob import os +import pathlib import warnings from copy import deepcopy +from typing import Union -from geopandas import GeoDataFrame +import folium import numpy as np import pandas as pd -import folium -from folium.plugins import FastMarkerCluster import plotly.express as px import plotly.graph_objs as go +from folium.plugins import FastMarkerCluster +from geopandas import GeoDataFrame +from tqdm import tqdm from transport_performance.gtfs.validation import GtfsInstance from transport_performance.utils.defence import ( - _type_defence, - _is_expected_filetype, _check_parent_dir_exists, _enforce_file_extension, + _is_expected_filetype, + _type_defence, ) diff --git a/src/transport_performance/gtfs/report/report_utils.py b/src/transport_performance/gtfs/report/report_utils.py index 22d91bcf..a1cd0920 100644 --- a/src/transport_performance/gtfs/report/report_utils.py +++ b/src/transport_performance/gtfs/report/report_utils.py @@ -1,15 +1,15 @@ """Utils to assist in the creation of a HTML report for GTFS.""" -from typing import Union +import os import pathlib import shutil -import os +from typing import Union +from transport_performance.utils.constants import PKG_PATH from transport_performance.utils.defence import ( - _type_defence, - _handle_path_like, _check_parent_dir_exists, + _handle_path_like, + _type_defence, ) -from transport_performance.utils.constants import PKG_PATH class TemplateHTML: diff --git a/src/transport_performance/gtfs/routes.py b/src/transport_performance/gtfs/routes.py index 4fb1e082..b624200d 100644 --- a/src/transport_performance/gtfs/routes.py +++ b/src/transport_performance/gtfs/routes.py @@ -1,18 +1,19 @@ """Helpers for working with routes.txt.""" -import pandas as pd -from bs4 import BeautifulSoup -import requests +import os +import pathlib import warnings from typing import Union -import pathlib -import os +import pandas as pd +import requests +from bs4 import BeautifulSoup + +from transport_performance.utils.constants import PKG_PATH from transport_performance.utils.defence import ( - _url_defence, - _type_defence, _is_expected_filetype, + _type_defence, + _url_defence, ) -from transport_performance.utils.constants import PKG_PATH warnings.filterwarnings( action="ignore", category=DeprecationWarning, module=".*pkg_resources" diff --git a/src/transport_performance/gtfs/validation.py b/src/transport_performance/gtfs/validation.py index 52ce3562..203531c4 100644 --- a/src/transport_performance/gtfs/validation.py +++ b/src/transport_performance/gtfs/validation.py @@ -1,55 +1,53 @@ """Validating GTFS data.""" -import gtfs_kit as gk -import pandas as pd -import geopandas as gpd -import folium import datetime -import numpy as np -import os import inspect +import os +import pathlib +import warnings +import zipfile +from typing import Callable, Union + +import folium +import geopandas as gpd +import gtfs_kit as gk +import numpy as np +import pandas as pd import plotly.express as px import plotly.io as plotly_io -from pretty_html_table import build_table -import zipfile -import warnings -import pathlib -from typing import Union, Callable -from plotly.graph_objects import Figure as PlotlyFigure from geopandas import GeoDataFrame +from plotly.graph_objects import Figure as PlotlyFigure +from pretty_html_table import build_table -from transport_performance.gtfs.validators import ( - validate_travel_over_multiple_stops, - validate_travel_between_consecutive_stops, -) from transport_performance.gtfs.calendar import create_calendar_from_dates from transport_performance.gtfs.cleaners import ( clean_consecutive_stop_fast_travel_warnings, clean_multiple_stop_fast_travel_warnings, ) +from transport_performance.gtfs.gtfs_utils import filter_gtfs +from transport_performance.gtfs.report.report_utils import ( + TemplateHTML, + _set_up_report_dir, +) from transport_performance.gtfs.routes import ( - scrape_route_type_lookup, get_saved_route_type_lookup, + scrape_route_type_lookup, ) +from transport_performance.gtfs.validators import ( + validate_travel_between_consecutive_stops, + validate_travel_over_multiple_stops, +) +from transport_performance.utils.constants import PKG_PATH from transport_performance.utils.defence import ( - _is_expected_filetype, - _check_namespace_export, - _check_parent_dir_exists, + _check_attribute, _check_column_in_df, - _type_defence, _check_item_in_iter, - _check_attribute, + _check_namespace_export, + _check_parent_dir_exists, _enforce_file_extension, + _is_expected_filetype, + _type_defence, ) -from transport_performance.gtfs.report.report_utils import ( - TemplateHTML, - _set_up_report_dir, -) - -from transport_performance.gtfs.gtfs_utils import filter_gtfs - -from transport_performance.utils.constants import PKG_PATH - def _get_intermediate_dates( start: pd.Timestamp, end: pd.Timestamp diff --git a/src/transport_performance/metrics.py b/src/transport_performance/metrics.py index 654bc832..11a1ea43 100644 --- a/src/transport_performance/metrics.py +++ b/src/transport_performance/metrics.py @@ -1,20 +1,20 @@ """Metrics to assess the performance of transport networks.""" import os -import pandas as pd -import geopandas as gpd import pathlib - from glob import glob from typing import Optional, Tuple, Union +import geopandas as gpd +import pandas as pd + from transport_performance._metrics.tp_utils import ( _transport_performance_pandas, _transport_performance_stats, ) from transport_performance.utils.defence import ( - _type_defence, _is_expected_filetype, + _type_defence, ) diff --git a/src/transport_performance/osm/osm_utils.py b/src/transport_performance/osm/osm_utils.py index d1347af3..a0bd4ce2 100644 --- a/src/transport_performance/osm/osm_utils.py +++ b/src/transport_performance/osm/osm_utils.py @@ -1,15 +1,16 @@ """Utility functions for OSM files.""" +import pathlib import subprocess -from pyprojroot import here from typing import Union -import pathlib + +from pyprojroot import here from transport_performance.utils.defence import ( - _type_defence, _check_iterable, _check_parent_dir_exists, - _is_expected_filetype, _enforce_file_extension, + _is_expected_filetype, + _type_defence, ) diff --git a/src/transport_performance/osm/validate_osm.py b/src/transport_performance/osm/validate_osm.py index 0115016e..b3c0e392 100644 --- a/src/transport_performance/osm/validate_osm.py +++ b/src/transport_performance/osm/validate_osm.py @@ -21,17 +21,17 @@ from pathlib import Path from typing import Union +import folium +import geopandas as gpd import osmium import pandas as pd -import geopandas as gpd from shapely import Point -import folium from transport_performance.utils.defence import ( _check_item_in_iter, _check_iterable, - _type_defence, _is_expected_filetype, + _type_defence, ) # ---------utilities----------- diff --git a/src/transport_performance/population/rasterpop.py b/src/transport_performance/population/rasterpop.py index 8685748b..c23e7336 100644 --- a/src/transport_performance/population/rasterpop.py +++ b/src/transport_performance/population/rasterpop.py @@ -1,28 +1,29 @@ """Class to handle raster population data.""" -import geopandas as gpd import os -import numpy as np -import rasterio as rio -import rioxarray -import folium +from datetime import datetime +from typing import Tuple, Type, Union + import cartopy.crs as ccrs import cartopy.io.img_tiles as cimgt +import folium +import geopandas as gpd import matplotlib.pyplot as plt - -from datetime import datetime +import numpy as np +import rasterio as rio +import rioxarray +from cartopy.mpl.geoaxes import GeoAxes from geocube.vector import vectorize -from typing import Union, Type, Tuple -from shapely.geometry.polygon import Polygon from matplotlib import colormaps -from cartopy.mpl.geoaxes import GeoAxes +from shapely.geometry.polygon import Polygon + from transport_performance.utils.defence import ( - _is_expected_filetype, - _type_defence, - _handle_path_like, + _check_iter_length, _check_parent_dir_exists, _enforce_file_extension, - _check_iter_length, + _handle_path_like, + _is_expected_filetype, + _type_defence, ) diff --git a/src/transport_performance/urban_centres/raster_uc.py b/src/transport_performance/urban_centres/raster_uc.py index 615f508a..3f082a20 100644 --- a/src/transport_performance/urban_centres/raster_uc.py +++ b/src/transport_performance/urban_centres/raster_uc.py @@ -1,8 +1,7 @@ """Functions to calculate urban centres following Eurostat definition.""" +import pathlib from collections import Counter - from typing import Union -import pathlib import affine import geopandas as gpd diff --git a/src/transport_performance/utils/constants.py b/src/transport_performance/utils/constants.py index 97b98598..cbd73b86 100644 --- a/src/transport_performance/utils/constants.py +++ b/src/transport_performance/utils/constants.py @@ -1,5 +1,6 @@ """Constants to be used throughout the transport-performance package.""" -import transport_performance from importlib import resources as pkg_resources +import transport_performance + PKG_PATH = pkg_resources.files(transport_performance) diff --git a/src/transport_performance/utils/defence.py b/src/transport_performance/utils/defence.py index 2d7a9382..3d8ee064 100644 --- a/src/transport_performance/utils/defence.py +++ b/src/transport_performance/utils/defence.py @@ -1,13 +1,13 @@ """Defensive check utility funcs. Internals only.""" -from typing import Union +import os +import pathlib +import warnings from collections.abc import Iterable +from datetime import datetime +from typing import Union -import pathlib import numpy as np -import os import pandas as pd -import warnings -from datetime import datetime def _handle_path_like( diff --git a/src/transport_performance/utils/io.py b/src/transport_performance/utils/io.py index 2a4b7113..11ccffda 100644 --- a/src/transport_performance/utils/io.py +++ b/src/transport_performance/utils/io.py @@ -2,7 +2,6 @@ import pathlib import pickle - from typing import Union from transport_performance.utils.defence import ( diff --git a/src/transport_performance/utils/raster.py b/src/transport_performance/utils/raster.py index c4e1c6e2..54b358c8 100644 --- a/src/transport_performance/utils/raster.py +++ b/src/transport_performance/utils/raster.py @@ -6,21 +6,22 @@ gridded population data pre-processing. """ -import os import glob -import re -import rioxarray +import os import pathlib - +import re from typing import Union -from rioxarray.merge import merge_arrays + +import rioxarray from rasterio.warp import Resampling +from rioxarray.merge import merge_arrays + from transport_performance.utils.defence import ( - _handle_path_like, _check_parent_dir_exists, + _enforce_file_extension, + _handle_path_like, _is_expected_filetype, _type_defence, - _enforce_file_extension, ) diff --git a/tests/_metrics/metrics_fixtures.py b/tests/_metrics/metrics_fixtures.py index 22806c79..f3abc2ec 100644 --- a/tests/_metrics/metrics_fixtures.py +++ b/tests/_metrics/metrics_fixtures.py @@ -9,14 +9,14 @@ """ -import geopandas as gpd import os import pathlib +from typing import Union + +import geopandas as gpd import pandas as pd import pytest - from pyprojroot import here -from typing import Union from transport_performance.utils.io import from_pickle diff --git a/tests/_metrics/test_tp_utils.py b/tests/_metrics/test_tp_utils.py index e08a3e2f..10f3eca0 100644 --- a/tests/_metrics/test_tp_utils.py +++ b/tests/_metrics/test_tp_utils.py @@ -1,5 +1,4 @@ import pytest - from pandas.testing import assert_frame_equal from pytest_lazyfixture import lazy_fixture diff --git a/tests/analyse_network/analyse_network_fixtures.py b/tests/analyse_network/analyse_network_fixtures.py index b71ef677..982b61ab 100644 --- a/tests/analyse_network/analyse_network_fixtures.py +++ b/tests/analyse_network/analyse_network_fixtures.py @@ -1,20 +1,20 @@ """Fixtures for transport_performance/analyse_network/analyse network.""" +import os + import geopandas as gpd import numpy as np -import os import pandas as pd import pytest - from pyprojroot import here from shapely.geometry import Point import transport_performance.analyse_network as an - ############ # fixtures # ############ + # gtfs file @pytest.fixture(scope="module") def dummy_gtfs(): diff --git a/tests/gtfs/conftest.py b/tests/gtfs/conftest.py index 96558eae..6705667a 100644 --- a/tests/gtfs/conftest.py +++ b/tests/gtfs/conftest.py @@ -1,7 +1,6 @@ """Test fixtures used throughout GTFS tests.""" -import pytest - import pandas as pd +import pytest @pytest.fixture(scope="session") diff --git a/tests/gtfs/test_calendar.py b/tests/gtfs/test_calendar.py index a79d646f..f5383e09 100644 --- a/tests/gtfs/test_calendar.py +++ b/tests/gtfs/test_calendar.py @@ -1,8 +1,8 @@ """Test calendar utilities.""" import calendar -import pytest import pandas as pd +import pytest from transport_performance.gtfs.calendar import create_calendar_from_dates diff --git a/tests/gtfs/test_cleaners.py b/tests/gtfs/test_cleaners.py index 0163db48..6d98b668 100644 --- a/tests/gtfs/test_cleaners.py +++ b/tests/gtfs/test_cleaners.py @@ -1,17 +1,17 @@ """Tests for the transport_performance.gtfs.cleaners.py module.""" import os import re -import pytest import numpy as np import pandas as pd +import pytest -from transport_performance.gtfs.validation import GtfsInstance from transport_performance.gtfs.cleaners import ( - drop_trips, clean_consecutive_stop_fast_travel_warnings, clean_multiple_stop_fast_travel_warnings, + drop_trips, ) +from transport_performance.gtfs.validation import GtfsInstance @pytest.fixture(scope="function") diff --git a/tests/gtfs/test_gtfs_utils.py b/tests/gtfs/test_gtfs_utils.py index ca5b40a8..6076d145 100644 --- a/tests/gtfs/test_gtfs_utils.py +++ b/tests/gtfs/test_gtfs_utils.py @@ -1,24 +1,24 @@ """Test GTFS utility functions.""" import os -import pytest -import re import pathlib +import re -import pandas as pd import geopandas as gpd -from shapely.geometry import box +import pandas as pd +import pytest from plotly.graph_objects import Figure as PlotlyFigure +from shapely.geometry import box -from transport_performance.gtfs.validation import GtfsInstance from transport_performance.gtfs.gtfs_utils import ( + _add_validation_row, + _validate_datestring, bbox_filter_gtfs, + convert_pandas_to_plotly, filter_gtfs, - _add_validation_row, filter_gtfs_around_trip, - convert_pandas_to_plotly, - _validate_datestring, ) +from transport_performance.gtfs.validation import GtfsInstance # location of GTFS test fixture GTFS_FIX_PTH = os.path.join( diff --git a/tests/gtfs/test_multi_validation.py b/tests/gtfs/test_multi_validation.py index 96478a1b..233ed000 100644 --- a/tests/gtfs/test_multi_validation.py +++ b/tests/gtfs/test_multi_validation.py @@ -1,23 +1,21 @@ """Tests for multi_validation.py.""" import calendar -import pytest -import os import glob +import os import pathlib import shutil import subprocess import zipfile from copy import deepcopy +import folium import numpy as np import pandas as pd -import folium -from pyprojroot import here import plotly.graph_objs as go +import pytest +from pyprojroot import here -from transport_performance.gtfs.multi_validation import ( - MultiGtfsInstance, -) +from transport_performance.gtfs.multi_validation import MultiGtfsInstance from transport_performance.gtfs.validation import GtfsInstance diff --git a/tests/gtfs/test_routes.py b/tests/gtfs/test_routes.py index 109437a4..28442ed3 100644 --- a/tests/gtfs/test_routes.py +++ b/tests/gtfs/test_routes.py @@ -1,19 +1,19 @@ """Testing routes module.""" -import pytest -from pyprojroot import here -from unittest.mock import call -from typing import Union, Type +import os import pathlib -from _pytest.python_api import RaisesContext -import re import pickle -import os +import re +from typing import Type, Union +from unittest.mock import call import pandas as pd +import pytest +from _pytest.python_api import RaisesContext +from pyprojroot import here from transport_performance.gtfs.routes import ( - scrape_route_type_lookup, get_saved_route_type_lookup, + scrape_route_type_lookup, ) from transport_performance.utils.constants import PKG_PATH diff --git a/tests/gtfs/test_validation.py b/tests/gtfs/test_validation.py index 753be062..c615795f 100644 --- a/tests/gtfs/test_validation.py +++ b/tests/gtfs/test_validation.py @@ -1,23 +1,23 @@ """Tests for validation module.""" -import re import os -import pytest import pathlib -from pyprojroot import here +import re from contextlib import nullcontext as does_not_raise +from unittest.mock import call, patch import gtfs_kit as gk +import numpy as np import pandas as pd -from unittest.mock import patch, call +import pytest from geopandas import GeoDataFrame -import numpy as np from plotly.graph_objects import Figure as PlotlyFigure +from pyprojroot import here from transport_performance.gtfs.validation import ( GtfsInstance, - _get_intermediate_dates, - _create_map_title_text, _convert_multi_index_to_single, + _create_map_title_text, + _get_intermediate_dates, ) from transport_performance.utils.constants import PKG_PATH diff --git a/tests/gtfs/test_validators.py b/tests/gtfs/test_validators.py index 437a3dea..3153163d 100644 --- a/tests/gtfs/test_validators.py +++ b/tests/gtfs/test_validators.py @@ -1,9 +1,9 @@ """Tests for validation module.""" import re -import pytest -from pyprojroot import here import pandas as pd +import pytest +from pyprojroot import here from transport_performance.gtfs.validation import GtfsInstance from transport_performance.gtfs.validators import ( diff --git a/tests/osm/test_osm_utils.py b/tests/osm/test_osm_utils.py index a133dddd..1ddae6b2 100644 --- a/tests/osm/test_osm_utils.py +++ b/tests/osm/test_osm_utils.py @@ -1,10 +1,10 @@ """Test osm_utils module.""" -import pytest +import glob import os -from unittest.mock import patch, call import re -import glob +from unittest.mock import call, patch +import pytest from pyprojroot import here from transport_performance.osm.osm_utils import filter_osm diff --git a/tests/osm/test_validate_osm.py b/tests/osm/test_validate_osm.py index a5f6f8d6..9cf97bea 100644 --- a/tests/osm/test_validate_osm.py +++ b/tests/osm/test_validate_osm.py @@ -1,18 +1,18 @@ """Test validate_osm.""" -import pytest import re -from pyprojroot import here -import geopandas as gpd import folium +import geopandas as gpd import pandas as pd +import pytest +from pyprojroot import here from transport_performance.osm.validate_osm import ( - _filter_target_dict_with_list, FindIds, FindLocations, FindTags, _convert_osm_dict_to_gdf, + _filter_target_dict_with_list, ) osm_pth = here("tests/data/small-osm.pbf") diff --git a/tests/population/test_rasterpop.py b/tests/population/test_rasterpop.py index ab4f1ad6..053bf6bb 100644 --- a/tests/population/test_rasterpop.py +++ b/tests/population/test_rasterpop.py @@ -7,23 +7,22 @@ """ import os -import pytest +from contextlib import nullcontext as does_not_raise +from typing import Tuple, Type + +import geopandas as gpd import numpy as np +import pytest import rasterio as rio import xarray as xr -import geopandas as gpd - -from typing import Type, Tuple -from shapely.geometry import Polygon, Point +from _pytest.python_api import RaisesContext from numpy.dtypes import Float64DType from pytest_lazyfixture import lazy_fixture -from _pytest.python_api import RaisesContext -from contextlib import nullcontext as does_not_raise +from shapely.geometry import Point, Polygon from transport_performance.population.rasterpop import RasterPop from transport_performance.utils.test_utils import _np_to_rioxarray - # value to test thresholding (removal of populations below this value) THRESHOLD_TEST = 5 diff --git a/tests/test_analyse_network.py b/tests/test_analyse_network.py index 07480ad1..eb3190a2 100644 --- a/tests/test_analyse_network.py +++ b/tests/test_analyse_network.py @@ -2,19 +2,18 @@ import datetime import glob import os +import pathlib +from contextlib import nullcontext as does_not_raise +from typing import Any, Type import geopandas as gpd import numpy as np import pandas as pd -import pathlib import pytest import r5py - -from contextlib import nullcontext as does_not_raise +from _pytest.python_api import RaisesContext from haversine import Unit from r5py import TransportMode -from typing import Type, Any -from _pytest.python_api import RaisesContext import transport_performance.analyse_network as an diff --git a/tests/test_metrics.py b/tests/test_metrics.py index f6be5ded..4ba11a46 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -1,7 +1,6 @@ """Tests for transport_performance/metrics.py.""" import pytest - from pandas.testing import assert_frame_equal from pyprojroot import here from pytest_lazyfixture import lazy_fixture diff --git a/tests/test_setup.py b/tests/test_setup.py index 9da39643..70e2cd76 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -6,9 +6,10 @@ This test module can be run with the pytest flag --runsetup. """ -import pytest import os +import pytest + class TestSetup: """A class of tests to check correct set-up. diff --git a/tests/urban_centres/test_urban_centres.py b/tests/urban_centres/test_urban_centres.py index 0948d271..22993ebb 100644 --- a/tests/urban_centres/test_urban_centres.py +++ b/tests/urban_centres/test_urban_centres.py @@ -13,21 +13,20 @@ """ import os +import pathlib from contextlib import nullcontext as does_not_raise from pathlib import Path +from typing import Type, Union import affine import geopandas as gpd import numpy as np import pandas as pd -import pathlib import pytest import rasterio as rio - +from _pytest.python_api import RaisesContext from pytest_lazyfixture import lazy_fixture from shapely.geometry import Polygon -from typing import Union, Type -from _pytest.python_api import RaisesContext import transport_performance.urban_centres.raster_uc as ucc diff --git a/tests/utils/test_defence.py b/tests/utils/test_defence.py index a6c4fd22..ef62f92b 100644 --- a/tests/utils/test_defence.py +++ b/tests/utils/test_defence.py @@ -1,33 +1,33 @@ """Tests for defence.py. These internals may be covered elsewhere.""" -import re import os import pathlib -from typing import Union, Type +import re +from contextlib import nullcontext as does_not_raise +from typing import Type, Union +import pandas as pd import pytest from _pytest.python_api import RaisesContext -import pandas as pd from pyprojroot import here -from contextlib import nullcontext as does_not_raise - -# INFO on the use of 'does_not_raise' -# https://docs.pytest.org/en/6.2.x/example/parametrize.html...#parametrizing... -# -conditional-raising -# +from transport_performance.gtfs.validation import GtfsInstance from transport_performance.utils.defence import ( + _check_attribute, + _check_column_in_df, + _check_item_in_iter, _check_iterable, _check_parent_dir_exists, + _enforce_file_extension, _gtfs_defence, - _type_defence, - _check_column_in_df, - _check_item_in_iter, - _check_attribute, _handle_path_like, _is_expected_filetype, - _enforce_file_extension, + _type_defence, ) -from transport_performance.gtfs.validation import GtfsInstance + +# INFO on the use of 'does_not_raise' +# https://docs.pytest.org/en/6.2.x/example/parametrize.html...#parametrizing... +# -conditional-raising +# class Test_CheckIter(object): diff --git a/tests/utils/test_io.py b/tests/utils/test_io.py index 807dd560..b7df6537 100644 --- a/tests/utils/test_io.py +++ b/tests/utils/test_io.py @@ -1,17 +1,17 @@ """Tests for utils/io.py.""" -import geopandas as gpd import os -import pytest import re +from typing import Type +import geopandas as gpd +import pytest +from _pytest.python_api import RaisesContext from geopandas.testing import assert_geodataframe_equal from pyprojroot import here -from _pytest.python_api import RaisesContext from shapely.geometry import Point -from typing import Type -from transport_performance.utils.io import to_pickle, from_pickle +from transport_performance.utils.io import from_pickle, to_pickle @pytest.fixture diff --git a/tests/utils/test_raster.py b/tests/utils/test_raster.py index fc879400..cd825be3 100644 --- a/tests/utils/test_raster.py +++ b/tests/utils/test_raster.py @@ -9,17 +9,18 @@ """ import os -import re -import pytest import pathlib +import re +from typing import Type + import numpy as np +import pytest import rasterio as rio -import xarray as xr import rioxarray # noqa: F401 - import required for xarray but not needed here - -from typing import Type -from pytest_lazyfixture import lazy_fixture +import xarray as xr from _pytest.python_api import RaisesContext +from pytest_lazyfixture import lazy_fixture + from transport_performance.utils.raster import ( merge_raster_files, sum_resample_file,