From 633c205c4549b041da6a1e194a360326401b2557 Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Fri, 22 Nov 2024 17:10:00 +0900 Subject: [PATCH 1/6] Migrate to pyproject.toml --- generate_readme.py | 27 ++++----- pyproject.toml | 105 ++++++++++++++++++++++++++++++++ requirements-dev.txt | 5 -- requirements.txt | 6 -- ruff.toml | 33 ---------- setup.cfg | 10 --- setup.py | 141 ------------------------------------------- 7 files changed, 118 insertions(+), 209 deletions(-) create mode 100644 pyproject.toml delete mode 100644 requirements-dev.txt delete mode 100644 requirements.txt delete mode 100644 ruff.toml delete mode 100644 setup.cfg delete mode 100644 setup.py diff --git a/generate_readme.py b/generate_readme.py index 18c495b..ff7ef12 100755 --- a/generate_readme.py +++ b/generate_readme.py @@ -17,6 +17,10 @@ def tabulate(rows): return html +def _get_github_image_url(relpath: str) -> str: + return f"https://github.com/wkentaro/imgviz/raw/main/{relpath}" + + def main(): examples = [] for py_file in sorted(glob.glob("examples/*.py")): @@ -28,24 +32,19 @@ def main(): width = 20.0 / img.height * img.width examples.append( ( - '
{}
'.format(py_file, py_file), - ''.format(img_file, width), + f'
{py_file}
', + f'', # NOQA: E501 ) ) examples = tabulate(examples) + # TODO: read from pyproject.toml dependencies = [] - with open("requirements.txt") as f: - for req in f: - if req.startswith("#"): - continue - req = req.strip() - pkg = req - for sep in "<=>": - pkg = pkg.split(sep)[0] - dependencies.append( - "- [{0}](https://pypi.org/project/{1})".format(req, pkg) - ) + for req in ["matplotlib", "numpy", "Pillow>=5.3.0", "PyYAML"]: + pkg = req + for sep in "<=>": + pkg = pkg.split(sep)[0] + dependencies.append("- [{0}](https://pypi.org/project/{1})".format(req, pkg)) dependencies = "\n".join(dependencies) py_file = "getting_started.py" @@ -90,7 +89,7 @@ def main():
- +
## Installation diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..fc2555c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,105 @@ +[build-system] +requires = ["hatchling>=1.20.0", "hatch-vcs", "hatch-fancy-pypi-readme"] +build-backend = "hatchling.build" + +[project.urls] +Homepage = "https://github.com/wkentaro/imgviz" + +[project] +name = "imgviz" +description = "Image Visualization Tools" +license = { text = "MIT" } +requires-python = ">=3.9" +authors = [ + { name = "Kentaro Wada", email = "www.kentaro.wada@gmail.com" }, +] +keywords = [] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Console", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Topic :: Software Development :: Libraries :: Python Modules", +] +dependencies = [ + "matplotlib", + "numpy", + "Pillow>=5.3.0", + "PyYAML", +] +dynamic = ["readme", "version"] + +[project.optional-dependencies] +all = [ + "scikit-image", + "scikit-learn", + "opencv-python", + "pyglet", +] +test = [ + "build", + "mypy", + "types-Pillow", + "types-PyYAML", + "pytest", + "ruff", + "twine", +] + +[tool.hatch.metadata] +allow-direct-references = true + +[tool.hatch.metadata.hooks.fancy-pypi-readme] +content-type = "text/markdown" +fragments = [ + { path = "README.md" }, +] + +[tool.hatch.version] +source = "vcs" + +[tool.mypy] +ignore_missing_imports = true + +[tool.ruff] +exclude = [ + ".conda", + ".git", + "src", +] + +line-length = 88 +indent-width = 4 + +[tool.ruff.lint] +# Enable Pyflakes (`F`), pycodestyle (`E`), isort (`I`). +select = ["E", "F", "I"] +ignore = [] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +[tool.ruff.format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" + +[tool.ruff.lint.isort] +force-single-line = true diff --git a/requirements-dev.txt b/requirements-dev.txt deleted file mode 100644 index 5f6ccbe..0000000 --- a/requirements-dev.txt +++ /dev/null @@ -1,5 +0,0 @@ --r requirements.txt -github2pypi==1.0.0 -mypy -pytest -ruff==0.1.9 diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index f6c3f8c..0000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -# NOTE: matplotlib<3.1.3 and Pillow==7 is not compatible -# https://github.com/matplotlib/matplotlib/issues/16083 -matplotlib -numpy -Pillow>=5.3.0 -PyYAML diff --git a/ruff.toml b/ruff.toml deleted file mode 100644 index 1e3f95b..0000000 --- a/ruff.toml +++ /dev/null @@ -1,33 +0,0 @@ -exclude = [ - ".conda", - ".git", - "src", -] - -line-length = 88 -indent-width = 4 - -[lint] -# Enable Pyflakes (`F`), pycodestyle (`E`), isort (`I`). -select = ["E", "F", "I"] -ignore = [] - -# Allow fix for all enabled rules (when `--fix`) is provided. -fixable = ["ALL"] -unfixable = [] - -[format] -# Like Black, use double quotes for strings. -quote-style = "double" - -# Like Black, indent with spaces, rather than tabs. -indent-style = "space" - -# Like Black, respect magic trailing commas. -skip-magic-trailing-comma = false - -# Like Black, automatically detect the appropriate line ending. -line-ending = "auto" - -[isort] -force-single-line = true diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 2ef8998..0000000 --- a/setup.cfg +++ /dev/null @@ -1,10 +0,0 @@ -[metadata] -license_file = LICENSE - -[flake8] -exclude = .eggs, *.egg, build/ , .*/, src/, docs/, imgviz/external/ -ignore = E203, E741, W503, W504 - -[isort] -skip_glob = .* -force_single_line = true diff --git a/setup.py b/setup.py deleted file mode 100644 index 08a7dfa..0000000 --- a/setup.py +++ /dev/null @@ -1,141 +0,0 @@ -import os -import os.path as osp -import re -import shlex -import subprocess -import sys - -from setuptools import find_packages -from setuptools import setup - - -def get_version(): - filename = "imgviz/__init__.py" - with open(filename) as f: - match = re.search(r"""^__version__ = ['"]([^'"]*)['"]""", f.read(), re.M) - if not match: - raise RuntimeError("{} doesn't contain __version__".format(filename)) - version = match.groups()[0] - return version - - -def get_install_requires(): - install_requires = [] - with open("requirements.txt") as f: - for req in f: - install_requires.append(req.strip()) - return install_requires - - -def get_extras_require(): - extras_require = { - "all": [ - "scikit-image", - "scikit-learn", - ] - } - if sys.version_info.major == 2: - extras_require["all"].append("opencv-python<3.4.10.37") - extras_require["all"].append("pyglet<1.5") - elif sys.version_info.major == 3 and sys.version_info.minor < 8: - extras_require["all"].append("opencv-python") - extras_require["all"].append("pyglet<2") - else: - extras_require["all"].append("opencv-python") - extras_require["all"].append("pyglet") - return extras_require - - -def get_long_description(): - with open("README.md") as f: - long_description = f.read() - try: - # when this package is being released - import github2pypi - - return github2pypi.replace_url( - slug="wkentaro/imgviz", content=long_description, branch="main" - ) - except ImportError: - # when this package is being installed - return long_description - - -def get_package_data(): - package_data = [] - for dirpath, dirnames, filenames in os.walk("data"): - for filename in filenames: - data_file = osp.join(dirpath, filename) - data_file = osp.join(osp.split(data_file)[1:]) - package_data.append(data_file) - return {"imgviz": package_data} - - -def main(): - version = get_version() - - if sys.argv[1] == "release": - try: - import github2pypi # NOQA - except ImportError: - print( - "Please install github2pypi\n\n\tpip install github2pypi\n", - file=sys.stderr, - ) - sys.exit(1) - - try: - import twine # NOQA - except ImportError: - print( - "Please install twine:\n\n\tpip install twine\n", - file=sys.stderr, - ) - sys.exit(1) - - commands = [ - "git pull origin main", - "git tag v{:s}".format(version), - "git push origin main --tags", - "python setup.py sdist", - "twine upload dist/imgviz-{:s}.tar.gz".format(version), - ] - for cmd in commands: - print("+ {}".format(cmd)) - subprocess.check_call(shlex.split(cmd)) - sys.exit(0) - - setup( - name="imgviz", - version=version, - packages=find_packages(), - python_requires=">=3.5", - install_requires=get_install_requires(), - extras_require=get_extras_require(), - description="Image Visualization Tools", - long_description=get_long_description(), - long_description_content_type="text/markdown", - package_data=get_package_data(), - include_package_data=True, - author="Kentaro Wada", - author_email="www.kentaro.wada@gmail.com", - url="http://github.com/wkentaro/imgviz", - license="MIT", - classifiers=[ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - "Programming Language :: Python", - "Programming Language :: Python :: 3.5", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - ], - ) - - -if __name__ == "__main__": - main() From 6a35a905fc9ab98d1b3a630934cc9ba8dbe48a7e Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Fri, 22 Nov 2024 17:13:38 +0900 Subject: [PATCH 2/6] Use python -m build to build dist --- .github/workflows/ci.yml | 3 ++- pyproject.toml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 41ad623..d137b28 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,5 +41,6 @@ jobs: done - name: Install from dist run: | - python setup.py sdist + python -m build pip install dist/*.tar.gz + pip install dist/*.whl diff --git a/pyproject.toml b/pyproject.toml index fc2555c..4d60151 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,6 +50,7 @@ test = [ "types-Pillow", "types-PyYAML", "pytest", + "pytest-xdist", "ruff", "twine", ] From a10f3a6e2c170454721f43acf483e411a3fd46d3 Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Fri, 22 Nov 2024 17:15:42 +0900 Subject: [PATCH 3/6] Update Makefile --- Makefile | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 2068c7b..fb79313 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,29 @@ all: @echo @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs +PACKAGE_DIR=imgviz + +mypy: + mypy --package $(PACKAGE_DIR) + lint: - ruff format --check - ruff check + ruff format --check || ruff format --check --diff + ruff check || ruff check --diff format: ruff format ruff check --fix test: - pytest tests + python -m pytest -n auto -v tests + +clean: + rm -rf build dist *.egg-info + +build: clean + python -m build --sdist --wheel + +upload: build + python -m twine upload dist/$(PACKAGE_DIR)-* + +publish: build upload From 6e6490c756b5f49588383ce69f6e9ff871d7e45f Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Fri, 22 Nov 2024 17:16:47 +0900 Subject: [PATCH 4/6] Apply ruff --- imgviz/depth.py | 1 - imgviz/nchannel.py | 1 - 2 files changed, 2 deletions(-) diff --git a/imgviz/depth.py b/imgviz/depth.py index 11a001b..8d9ee63 100644 --- a/imgviz/depth.py +++ b/imgviz/depth.py @@ -7,7 +7,6 @@ class Depth2RGB(object): - """Convert depth array to rgb. Parameters diff --git a/imgviz/nchannel.py b/imgviz/nchannel.py index 0f3fef5..9c9b4c8 100644 --- a/imgviz/nchannel.py +++ b/imgviz/nchannel.py @@ -4,7 +4,6 @@ class Nchannel2RGB(object): - """Convert nchannel array to rgb by PCA. Parameters From 6c8d7ba621d7153d4d1cde1e9e1b1bdc83376f0d Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Fri, 22 Nov 2024 17:17:57 +0900 Subject: [PATCH 5/6] Ignore mypy check on cv2 import --- imgviz/_io/opencv.py | 2 +- imgviz/resize.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/imgviz/_io/opencv.py b/imgviz/_io/opencv.py index a37c939..e69ff28 100644 --- a/imgviz/_io/opencv.py +++ b/imgviz/_io/opencv.py @@ -1,7 +1,7 @@ try: import cv2 except ImportError: - cv2 = None + cv2 = None # type: ignore import numpy as np # NOQA diff --git a/imgviz/resize.py b/imgviz/resize.py index 487241b..2d2da53 100644 --- a/imgviz/resize.py +++ b/imgviz/resize.py @@ -6,7 +6,7 @@ try: import cv2 except ImportError: - cv2 = None + cv2 = None # type: ignore def _resize_pillow(src, height, width, interpolation): From bd100b8c6791f6279879ca7e6db25e7c2f21434a Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Fri, 22 Nov 2024 17:22:16 +0900 Subject: [PATCH 6/6] Cleanup github ci --- .github/workflows/ci.yml | 29 ++++++++++++++++++++++++++--- .github/workflows/lint.yml | 34 ---------------------------------- 2 files changed, 26 insertions(+), 37 deletions(-) delete mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d137b28..43ad02f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,29 @@ on: pull_request: jobs: + lint: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.9] + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + submodules: true + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + pip install -e .[test] + - name: Lint + run: | + make lint + - name: Mypy + run: | + make mypy build: runs-on: ubuntu-latest strategy: @@ -27,10 +50,10 @@ jobs: run: | pip install setuptools pip install .[all] + pip install .[test] - name: Test run: | - pip install pytest - pytest -vsx tests + make test - name: Run examples run: | python getting_started.py @@ -41,6 +64,6 @@ jobs: done - name: Install from dist run: | - python -m build + make build pip install dist/*.tar.gz pip install dist/*.whl diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml deleted file mode 100644 index fe63daa..0000000 --- a/.github/workflows/lint.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: lint - -on: - push: - branches: - - main - pull_request: - -jobs: - lint: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: [3.9] - steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 0 - submodules: true - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - pip install -r requirements-dev.txt - - name: Lint - run: | - ruff format --check - ruff check - - name: Mypy - run: | - pip install mypy types-PyYAML - mypy -p imgviz --ignore-missing-imports