Skip to content

Commit

Permalink
add typing stubs (#20)
Browse files Browse the repository at this point in the history
* fix

* update

* remote pybind11

* move to src

* update script

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* reset

---------

Co-authored-by: tang zhixiong <zhixiong.tang@momenta.ai>
  • Loading branch information
district10 and zhixiong-tang authored Oct 3, 2024
1 parent fdbf813 commit 4a10f88
Show file tree
Hide file tree
Showing 28 changed files with 2,520 additions and 450 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ on:
jobs:
build:
strategy:
fail-fast: false
fail-fast: true
matrix:
platform: [ubuntu-20.04, windows-2019, macos-12]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
platform: [ubuntu-20.04, windows-2019, macos-13]
python-version: ["3.9"]

runs-on: ${{ matrix.platform }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ _generate/
wheelhouse
!test.py
site
stubs
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[submodule "pybind11"]
path = pybind11
url = https://github.com/pybind/pybind11.git
branch = master
[submodule "headers"]
path = headers
url = https://github.com/cubao/headers.git
24 changes: 7 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,14 @@ repos:
- id: requirements-txt-fixer
- id: trailing-whitespace

# Black, the code formatter, natively supports pre-commit
- repo: https://github.com/psf/black
rev: 22.3.0
# Check linting and style issues
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.6.5"
hooks:
- id: black
exclude: ^(docs)

# Sort your imports in a standard form
- repo: https://github.com/PyCQA/isort
rev: 5.11.5
hooks:
- id: isort
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format
exclude: ^(docs)

# Upgrade older Python syntax
- repo: https://github.com/asottile/pyupgrade
Expand All @@ -60,12 +56,6 @@ repos:
- id: remove-tabs
exclude: ^(docs|Makefile)

- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear]

# CMake formatting
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.13
Expand Down
31 changes: 24 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
cmake_minimum_required(VERSION 3.4...3.18)
project(fast_crossing)
cmake_minimum_required(VERSION 3.15...3.26)
if(NOT DEFINED SKBUILD_PROJECT_NAME)
set(SKBUILD_PROJECT_NAME "fast_crossing")
endif()
if(NOT DEFINED PROJECT_VERSION)
set(PROJECT_VERSION "dev")
endif()
# https://scikit-build-core.readthedocs.io/en/latest/cmakelists.html#accessing-information
project(
${SKBUILD_PROJECT_NAME}
VERSION ${SKBUILD_PROJECT_VERSION}
LANGUAGES CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)

if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE
Expand All @@ -23,11 +35,16 @@ endif()
include_directories(${PROJECT_SOURCE_DIR}/headers/include
${PROJECT_SOURCE_DIR}/headers/include/cubao)

set(CMAKE_CXX_STANDARD 17)
set(PYBIND11_CPP_STANDARD -std=c++17)

add_subdirectory(pybind11)
pybind11_add_module(_pybind11_fast_crossing src/main.cpp)
# https://scikit-build-core.readthedocs.io/en/latest/getting_started.html
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)
include_directories(headers/include)

target_compile_definitions(_pybind11_fast_crossing
PRIVATE VERSION_INFO=${FAST_CROSSING_VERSION_INFO})
file(GLOB SRCS src/*.cpp)
python_add_library(_core MODULE ${SRCS} WITH_SOABI)
target_link_libraries(_core PRIVATE pybind11::headers)
target_include_directories(_core PRIVATE src)
target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})
install(TARGETS _core DESTINATION ${PROJECT_NAME})
44 changes: 23 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PROJECT_SOURCE_DIR ?= $(abspath ./)
PROJECT_NAME ?= $(shell basename $(PROJECT_SOURCE_DIR))
NUM_JOBS ?= 8

all:
@echo nothing special
Expand All @@ -14,19 +15,15 @@ lint:
pre-commit run -a
lint_install:
pre-commit install

build:
mkdir -p build && cd build && \
cmake .. && make
.PHONY: build
.PHONY: lint lint_install

docs_build:
mkdocs build
docs_serve:
mkdocs serve -a 0.0.0.0:8088

DOCKER_TAG_WINDOWS ?= ghcr.io/cubao/build-env-windows-x64:v0.0.1
DOCKER_TAG_LINUX ?= ghcr.io/cubao/build-env-manylinux2014-x64:v0.0.3
DOCKER_TAG_LINUX ?= ghcr.io/cubao/build-env-manylinux2014-x64:v0.0.5
DOCKER_TAG_MACOS ?= ghcr.io/cubao/build-env-macos-arm64:v0.0.1

test_in_win:
Expand All @@ -46,28 +43,31 @@ test_in_dev_container:
-v `pwd`:`pwd` -w `pwd` -it $(DEV_CONTAINER_IMAG) bash

PYTHON ?= python3
build:
$(PYTHON) -m pip install scikit_build_core pyproject_metadata pathspec pybind11
CMAKE_BUILD_PARALLEL_LEVEL=$(NUM_JOBS) $(PYTHON) -m pip install --no-build-isolation -Ceditable.rebuild=true -Cbuild-dir=build -ve.
python_install:
$(PYTHON) setup.py install
python_build:
$(PYTHON) setup.py bdist_wheel
$(PYTHON) -m pip install . --verbose
python_wheel:
$(PYTHON) -m pip wheel . -w build --verbose
python_sdist:
$(PYTHON) setup.py sdist
# tar -tvf dist/fast_crossing-*.tar.gz
$(PYTHON) -m pip sdist . --verbose
python_test: pytest
pytest:
pytest tests --capture=tee-sys
.PHONY: python_install python_build python_sdist python_test pytest
python3 -m pip install pytest
pytest tests/test_basic.py
.PHONY: build

restub:
pybind11-stubgen fast_crossing._core -o stubs
cp -rf stubs/fast_crossing/_core src/fast_crossing

# conda create -y -n py36 python=3.6
# conda create -y -n py37 python=3.7
# conda create -y -n py38 python=3.8
# conda create -y -n py39 python=3.9
# conda create -y -n py310 python=3.10
# conda create -y -n py311 python=3.11
# conda create -y -n py312 python=3.12
# conda env list
python_build_py36:
PYTHON=python conda run --no-capture-output -n py36 make python_build
python_build_py37:
PYTHON=python conda run --no-capture-output -n py37 make python_build
python_build_py38:
PYTHON=python conda run --no-capture-output -n py38 make python_build
python_build_py39:
Expand All @@ -76,11 +76,13 @@ python_build_py310:
PYTHON=python conda run --no-capture-output -n py310 make python_build
python_build_py311:
PYTHON=python conda run --no-capture-output -n py311 make python_build
python_build_all: python_build_py36 python_build_py37 python_build_py38 python_build_py39 python_build_py310 python_build_py311
python_build_py312:
PYTHON=python conda run --no-capture-output -n py312 make python_build
python_build_all: python_build_py38 python_build_py39 python_build_py310 python_build_py311 python_build_py312
python_build_all_in_linux:
docker run --rm -w `pwd` -v `pwd`:`pwd` -v `pwd`/build/linux:`pwd`/build -it $(DOCKER_TAG_LINUX) make python_build_all
make repair_wheels && rm -rf dist/*.whl && mv wheelhouse/*.whl dist && rm -rf wheelhouse
python_build_all_in_macos: python_build_py38 python_build_py39 python_build_py310 python_build_py311
python_build_all_in_macos: python_build_py38 python_build_py39 python_build_py310 python_build_py311 python_build_py312
python_build_all_in_windows: python_build_all

repair_wheels:
Expand Down
15 changes: 7 additions & 8 deletions benchmarks/benchmark_point_in_polygon.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import math
import os
import random
import time
from typing import List, Tuple

import numpy as np
from loguru import logger
Expand Down Expand Up @@ -44,8 +45,7 @@ def point_in_polygon_polygons(points: np.ndarray, polygon: np.ndarray) -> np.nda
num_edges_children = 4
num_nodes_children = 4
tree = polygons.build_search_tree(polygon, num_edges_children, num_nodes_children)
mask = polygons.points_are_inside(tree, points).astype(np.int32)
return mask
return polygons.points_are_inside(tree, points).astype(np.int32)


def point_in_polygon_shapely(points: np.ndarray, polygon: np.ndarray) -> np.ndarray:
Expand All @@ -68,8 +68,7 @@ def load_points(path: str):

def load_polygon(path: str):
if path.endswith((".npy", ".pcd")):
return load_points(path)
pass
load_points(path)


def write_mask(mask: np.ndarray, path: str):
Expand All @@ -90,12 +89,12 @@ def wrapped_fn(input_points: str, input_polygon: str, output_path: str):

# https://stackoverflow.com/questions/8997099/algorithm-to-generate-random-2d-polygon
def generate_polygon(
center: Tuple[float, float],
center: tuple[float, float],
avg_radius: float,
irregularity: float,
spikiness: float,
num_vertices: int,
) -> List[Tuple[float, float]]:
) -> list[tuple[float, float]]:
"""
Start with the center of the polygon at center, then creates the
polygon by sampling points on a circle around the center.
Expand Down Expand Up @@ -147,7 +146,7 @@ def generate_polygon(
return points


def random_angle_steps(steps: int, irregularity: float) -> List[float]:
def random_angle_steps(steps: int, irregularity: float) -> list[float]:
"""Generates the division of a circumference in random angles.
Args:
Expand Down
4 changes: 4 additions & 0 deletions docs/about/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ To upgrade `fast-crossing` to the latest version, use pip:
pip install -U fast-crossing
```

## Version 0.1.0 (2024-10-03)

* Add typing stubs

## Version 0.0.9 (2024-09-07)

* Update pybind11 (for python 3.11, 3.12), ditch python 3.6, 3.7
Expand Down
2 changes: 0 additions & 2 deletions fast_crossing/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion pybind11
Submodule pybind11 deleted from 8a801b
95 changes: 84 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,98 @@
[build-system]
requires = [
"setuptools>=42",
"wheel",
"ninja",
"cmake>=3.12",
requires = ["scikit-build-core>=0.3.3", "pybind11"]
build-backend = "scikit_build_core.build"


[project]
name = "fast_crossing"
version = "0.1.0"
url = "https://fast-crossing.readthedocs.io"
description="fast crossing"
readme = "README.md"
authors = [
{ name = "district10", email = "dvorak4tzx@gmail.com" },
]
build-backend = "setuptools.build_meta"
requires-python = ">=3.7"
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]

[project.optional-dependencies]
test = ["pytest", "scipy"]


[tool.scikit-build]
wheel.expand-macos-universal-tags = true

[tool.isort]
profile = "black"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
xfail_strict = true
filterwarnings = ["error"]
log_cli_level = "INFO"
filterwarnings = [
"error",
]
testpaths = ["tests"]


[tool.cibuildwheel]
test-command = "pytest {project}/tests"
test-extras = ["test"]
test-skip = ["*universal2:arm64"]
# Setuptools bug causes collision between pypy and cpython artifacts
before-build = "rm -rf {project}/build"
build-verbosity = 1


[tool.ruff]
src = ["src"]

[tool.ruff.lint]
exclude = ["*.pyi", "scripts/*.py"]
extend-select = [
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"NPY", # NumPy specific rules
"PD", # pandas-vet
]
ignore = [
"ARG002",
"EM101",
"NPY002",
"PLR09", # Too many X
"PLR2004", # Magic comparison
"PT018",
"PTH100",
"PTH103",
"PTH119",
"PTH120",
"RUF013",
]
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["T20"]
Loading

0 comments on commit 4a10f88

Please sign in to comment.