Skip to content
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

TYP: add type hints to functional._dimension #534

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Type checking

on:
push:
branches: [main]
pull_request:
branches:
- "*"
schedule:
- cron: "0 0 * * 1,4"

jobs:
mypy:
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0}

steps:
- uses: actions/checkout@v4

- name: setup micromamba
uses: mamba-org/setup-micromamba@v1
with:
environment-file: ci/envs/312-latest.yaml
create-args: >-
mypy

- name: Install momepy
run: pip install .

- name: Check momepy
run: |
mypy momepy/ --install-types --ignore-missing-imports --non-interactive

71 changes: 41 additions & 30 deletions momepy/functional/_dimension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np
import pandas as pd
import shapely
from geopandas import GeoDataFrame, GeoSeries
from libpysal.graph import Graph
from numpy.typing import NDArray
from pandas import Series

__all__ = [
"volume",
Expand All @@ -11,7 +14,10 @@
]


def volume(area, height):
def volume(
area: NDArray[np.float_] | Series,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can do with a bare NDArray? Since I don't know how to add the same to Series anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try: Series[np.float_]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

height: NDArray[np.float_] | Series,
) -> NDArray[np.float_] | Series:
"""
Calculates volume of each object in given GeoDataFrame based on its height and area.

Expand All @@ -20,19 +26,24 @@ def volume(area, height):

Parameters
----------
area : array_like
area : NDArray[np.float_] | Series
array of areas
height : array_like
height : NDArray[np.float_] | Series
array of heights

Returns
-------
array_like
NDArray[np.float_] | Series
array of a type depending on the input
"""
return area * height


def floor_area(area, height, floor_height=3):
def floor_area(
area: NDArray[np.float_] | Series,
height: NDArray[np.float_] | Series,
floor_height: float | NDArray[np.float_] | Series = 3,
) -> NDArray[np.float_] | Series:
"""Calculates floor area of each object based on height and area.

The number of
Expand All @@ -45,42 +56,43 @@ def floor_area(area, height, floor_height=3):

Parameters
----------
area : array_like
area : NDArray[np.float_] | Series
array of areas
height : array_like
height : NDArray[np.float_] | Series
array of heights
floor_height : float | array_like, optional
float denoting the uniform floor height or an array_like reflecting the building
floor_height : float | NDArray[np.float_] | Series, optional
float denoting the uniform floor height or an aarray reflecting the building
height by geometry, by default 3

Returns
-------
array_like
NDArray[np.float_] | Series
array of a type depending on the input
"""
return area * (height // floor_height)


def courtyard_area(gdf):
def courtyard_area(gdf: GeoDataFrame | GeoSeries) -> Series:
"""Calculates area of holes within geometry - area of courtyards.

Parameters
----------
gdf : GeoDataFrame
A GeoDataFrame containing objects to analyse.
gdf : GeoDataFrame | GeoSeries
A GeoDataFrame or GeoSeries containing polygons to analyse.

Returns
-------
pandas.Series
Series
"""
return pd.Series(
return Series(
shapely.area(shapely.polygons(shapely.get_exterior_ring(gdf.geometry.array)))
- gdf.area,
index=gdf.index,
name="courtyard_area",
)


def longest_axis_length(gdf):
def longest_axis_length(gdf: GeoDataFrame | GeoSeries) -> Series:
"""Calculates the length of the longest axis of object.

Axis is defined as a
Expand All @@ -92,35 +104,34 @@ def longest_axis_length(gdf):

Parameters
----------
gdf : GeoDataFrame
A GeoDataFrame containing objects to analyse.
gdf : GeoDataFrame | GeoSeries
A GeoDataFrame or GeoSeries containing polygons to analyse.

Returns
-------
pandas.Series
Series
"""
return shapely.minimum_bounding_radius(gdf.geometry) * 2


def perimeter_wall(gdf, graph=None):
def perimeter_wall(gdf: GeoDataFrame | GeoSeries, graph: Graph | None = None) -> Series:
"""
Calculate the perimeter wall length the joined structure.

Parameters
----------
gdf : GeoDataFrame
GeoDataFrame containing objects to analyse
graph : libpysal.graph.Graph, optional
Graph encoding Queen contiguity of ``gdf``
gdf : GeoDataFrame | GeoSeries
A GeoDataFrame or GeoSeries containing polygons to analyse.
graph : Graph | None, optional
Graph encoding Queen contiguity of ``gdf``. If ``None`` Queen contiguity is
built on the fly.

Returns
-------
pandas.Series
Series
"""

if graph is None:
from libpysal.graph import Graph

graph = Graph.build_contiguity(gdf)

isolates = graph.isolates
Expand All @@ -129,13 +140,13 @@ def perimeter_wall(gdf, graph=None):
blocks = gdf.drop(isolates)
component_perimeter = (
blocks[[blocks.geometry.name]]
.set_geometry(blocks.buffer(0.01))
.set_geometry(blocks.buffer(0.01)) # type: ignore
.dissolve(by=graph.component_labels.drop(isolates))
.exterior.length
)

# combine components with isolates
results = pd.Series(np.nan, index=gdf.index, name="perimeter_wall")
results = Series(np.nan, index=gdf.index, name="perimeter_wall")
results.loc[isolates] = gdf.geometry[isolates].exterior.length
results.loc[results.index.drop(isolates)] = component_perimeter.loc[
graph.component_labels.loc[results.index.drop(isolates)]
Expand Down