Skip to content

Commit

Permalink
add flake8 and isort to CI (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi committed Sep 17, 2022
1 parent b70b216 commit e836a50
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 67 deletions.
20 changes: 15 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
repos:
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8
language_version: python3
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
language_version: python3
3 changes: 2 additions & 1 deletion benchmarks/bench_dimension.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import geopandas as gpd
import momepy as mm
import numpy as np

import momepy as mm


class TimeDimension:
def setup(self):
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/bench_distribution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import geopandas as gpd
import momepy as mm
import numpy as np
from libpysal.weights import Queen

import momepy as mm


class TimeDistribution:
def setup(self):
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/bench_diversity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import geopandas as gpd
import momepy as mm
import numpy as np

import momepy as mm


class TimeDiversity:
def setup(self):
Expand Down
1 change: 1 addition & 0 deletions benchmarks/bench_elements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import geopandas as gpd

import momepy as mm


Expand Down
1 change: 1 addition & 0 deletions benchmarks/bench_graph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import geopandas as gpd

import momepy as mm


Expand Down
3 changes: 2 additions & 1 deletion benchmarks/bench_intensity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import geopandas as gpd
import momepy as mm
import numpy as np
from libpysal.weights import Queen

import momepy as mm


class TimeIntensity:
def setup(self):
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/bench_shape.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import geopandas as gpd
import momepy as mm
import numpy as np

import momepy as mm


class TimeShape:
def setup(self):
Expand Down
1 change: 1 addition & 0 deletions benchmarks/bench_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import geopandas as gpd
import libpysal

import momepy as mm


Expand Down
1 change: 0 additions & 1 deletion momepy/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# definitions of spatial distribution characters

import math
import warnings

import networkx as nx
import numpy as np
Expand Down
13 changes: 8 additions & 5 deletions momepy/diversity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
# diversity.py
# definitions of diversity characters

import warnings

import numpy as np
from numpy.lib import NumpyVersion
import pandas as pd
import scipy as sp
from numpy.lib import NumpyVersion
from tqdm.auto import tqdm # progress bar
import warnings


__all__ = [
"Range",
Expand Down Expand Up @@ -130,8 +130,11 @@ class Theil:
.. math::
T = \\sum_{i=1}^n \\left( \\frac{y_i}{\\sum_{i=1}^n y_i} \\ln \\left[ N \\frac{y_i}
{\\sum_{i=1}^n y_i}\\right] \\right)
T = \\sum_{i=1}^n \\left(
\\frac{y_i}{\\sum_{i=1}^n y_i} \\ln \\left[
N \\frac{y_i} {\\sum_{i=1}^n y_i}
\\right]
\\right)
Parameters
----------
Expand Down
1 change: 0 additions & 1 deletion momepy/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import numpy as np
import pandas as pd
import pygeos

from packaging.version import Version
from scipy.spatial import Voronoi
from shapely.geometry.base import BaseGeometry
Expand Down
1 change: 0 additions & 1 deletion momepy/intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# definitions of intensity characters

import collections
import warnings

import numpy as np
import pandas as pd
Expand Down
27 changes: 16 additions & 11 deletions momepy/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
import pygeos
import shapely
from packaging.version import Version
from tqdm.auto import tqdm

from shapely.geometry import LineString, Point
from shapely.ops import linemerge, polygonize
from shapely.geometry import Point, LineString
from tqdm.auto import tqdm

from .shape import CircularCompactness
from .coins import COINS

from .shape import CircularCompactness

__all__ = [
"preprocess",
Expand Down Expand Up @@ -1048,9 +1046,9 @@ def roundabout_simplification(
include_adjacent : boolean (default True)
Adjacent polygons to be considered also as part of the simplification.
diameter_factor : float (default 1.5)
The factor to be applied to the diameter of each roundabout that determines how far
an adjacent polygon can stretch until it is no longer considered part of the overall
roundabout group. Only applyies when include_adjacent = True.
The factor to be applied to the diameter of each roundabout that determines
how far an adjacent polygon can stretch until it is no longer considered part
of the overall roundabout group. Only applyies when include_adjacent = True.
center_type : string (default 'centroid')
Method to use for converging the incoming LineStrings.
Current list of options available : 'centroid', 'mean'.
Expand All @@ -1073,16 +1071,23 @@ def roundabout_simplification(
Returns
-------
GeoDataFrame
GeoDataFrame with an updated geometry and an additional column labeling modified edges.
GeoDataFrame with an updated geometry and an additional
column labeling modified edges.
"""
if not GPD_09:
raise ImportError(
f"`roundabout_simplification` requires geopandas 0.9.0 or newer. Your current version is {gpd.__version__}."
(
"`roundabout_simplification` requires geopandas 0.9.0 or newer. "
f"Your current version is {gpd.__version__}."
)
)

if len(edges[edges.geom_type != "LineString"]) > 0:
raise TypeError(
"Only LineString geometries are allowed. Try using the `explode()` method to explode MultiLineStrings."
(
"Only LineString geometries are allowed. "
"Try using the `explode()` method to explode MultiLineStrings."
)
)

polys = _polygonize_ifnone(edges, polys)
Expand Down
51 changes: 18 additions & 33 deletions momepy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import networkx as nx
import numpy as np
from numpy.lib import NumpyVersion
from shapely.geometry import Point, LineString

from shapely.geometry import Point

__all__ = [
"unique_id",
Expand Down Expand Up @@ -51,15 +50,16 @@ def _angle(a, b, c):


def _generate_primal(G, gdf_network, fields, multigraph, oneway_column=None):
"""
Generate primal graph.
Helper for gdf_to_nx.
"""
"""Generate primal graph. Helper for gdf_to_nx."""
G.graph["approach"] = "primal"

msg = "%s. This can lead to unexpected behaviour. The intended usage of the conversion function is with networks made of LineStrings only."
msg = (
"%s. This can lead to unexpected behaviour. "
"The intended usage of the conversion function "
"is with networks made of LineStrings only."
)

if not "LineString" in gdf_network.geom_type.unique():
if "LineString" not in gdf_network.geom_type.unique():
warnings.warn(
message=msg % "The given network does not contain any LineString.",
category=RuntimeWarning,
Expand Down Expand Up @@ -93,10 +93,7 @@ def _generate_primal(G, gdf_network, fields, multigraph, oneway_column=None):


def _generate_dual(G, gdf_network, fields, angles, multigraph, angle):
"""
Generate dual graph
Helper for gdf_to_nx.
"""
"""Generate dual graph Helper for gdf_to_nx."""
G.graph["approach"] = "dual"
key = 0

Expand Down Expand Up @@ -187,10 +184,10 @@ def gdf_to_nx(
name of attribute of angle between LineStrings which will be saved to graph.
Ignored if ``approach="primal"``.
oneway_column : str, default None
create an additional edge for each LineString which allows bidirectional path traversal by
specifying the boolean column in the GeoDataFrame. Note, that the reverse conversion
``nx_to_gdf(gdf_to_nx(gdf, directed=True, oneway_column="oneway"))`` will contain
additional duplicated geometries.
create an additional edge for each LineString which allows bidirectional
path traversal by specifying the boolean column in the GeoDataFrame. Note,
that the reverse conversion ``nx_to_gdf(gdf_to_nx(gdf, directed=True,
oneway_column="oneway"))`` will contain additional duplicated geometries.
Returns
-------
Expand Down Expand Up @@ -284,10 +281,7 @@ def gdf_to_nx(


def _points_to_gdf(net):
"""
Generate point gdf from nodes.
Helper for nx_to_gdf.
"""
"""Generate point gdf from nodes. Helper for nx_to_gdf."""
node_xy, node_data = zip(*net.nodes(data=True))
if isinstance(node_xy[0], int) and "x" in node_data[0].keys():
geometry = [Point(data["x"], data["y"]) for data in node_data] # osmnx graph
Expand All @@ -300,10 +294,7 @@ def _points_to_gdf(net):


def _lines_to_gdf(net, points, nodeID):
"""
Generate linestring gdf from edges.
Helper for nx_to_gdf.
"""
"""Generate linestring gdf from edges. Helper for nx_to_gdf."""
starts, ends, edge_data = zip(*net.edges(data=True))
gdf_edges = gpd.GeoDataFrame(list(edge_data))

Expand All @@ -324,10 +315,7 @@ def _lines_to_gdf(net, points, nodeID):


def _primal_to_gdf(net, points, lines, spatial_weights, nodeID):
"""
Generate gdf(s) from primal network.
Helper for nx_to_gdf.
"""
"""Generate gdf(s) from primal network. Helper for nx_to_gdf."""
if points is True:
gdf_nodes = _points_to_gdf(net)

Expand All @@ -350,10 +338,7 @@ def _primal_to_gdf(net, points, lines, spatial_weights, nodeID):


def _dual_to_gdf(net):
"""
Generate linestring gdf from dual network.
Helper for nx_to_gdf.
"""
"""Generate linestring gdf from dual network. Helper for nx_to_gdf."""
starts, edge_data = zip(*net.nodes(data=True))
gdf_edges = gpd.GeoDataFrame(list(edge_data))
gdf_edges.crs = net.graph["crs"]
Expand Down Expand Up @@ -492,6 +477,6 @@ def limit_range(vals, rng):


def _azimuth(point1, point2):
"""azimuth between 2 shapely points (interval 0 - 180)"""
"""Return the azimuth between 2 shapely points (interval 0 - 180)."""
angle = np.arctan2(point2[0] - point1[0], point2[1] - point1[1])
return np.degrees(angle) if angle > 0 else np.degrees(angle) + 180
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ versionfile_build = momepy/_version.py
tag_prefix = v
parentdir_prefix = momepy-

[isort]
profile = black
src_paths = momepy, tests, benchmarks

[flake8]
# Black enforces 88 characters line length
max_line_length = 88
exclude = *__init__.py, docs/conf.py, versioneer.py, momepy/_version.py
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env/python
import os

import versioneer

try:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from random import shuffle
import uuid
from random import shuffle

import geopandas as gpd
import numpy as np
import pytest
from geopandas.testing import assert_geodataframe_equal
from packaging.version import Version
from pandas.testing import assert_index_equal
from shapely import affinity
from shapely.geometry import LineString, MultiPoint, Polygon
from packaging.version import Version

import momepy as mm

Expand Down
2 changes: 1 addition & 1 deletion tests/test_preprocess.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import geopandas as gpd
import numpy as np
import pytest
from geopandas.testing import assert_geodataframe_equal
from packaging.version import Version
from shapely import affinity
from shapely.geometry import LineString, MultiPoint, Polygon
from shapely.ops import polygonize
from geopandas.testing import assert_geodataframe_equal

import momepy as mm

Expand Down
3 changes: 1 addition & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import networkx
import numpy as np
import osmnx as ox
import warnings
import pytest
from shapely.geometry import LineString, Point
from packaging.version import Version
from shapely.geometry import LineString, Point

import momepy as mm

Expand Down

0 comments on commit e836a50

Please sign in to comment.