Skip to content

Commit

Permalink
Merge pull request #231 from glotzerlab/admin/ruff
Browse files Browse the repository at this point in the history
Swap python linter and formatter to ruff
  • Loading branch information
joaander committed May 29, 2024
2 parents 2b5c354 + 978405e commit 7eac406
Show file tree
Hide file tree
Showing 34 changed files with 1,289 additions and 1,198 deletions.
18 changes: 7 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@ repos:
- --add=Part of fresnel, released under the BSD 3-Clause License.
- --keep-after=.. include
- --comment-prefix=..
- repo: https://github.com/google/yapf
rev: 'v0.40.2'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.5
hooks:
- id: yapf
- repo: https://github.com/PyCQA/flake8
rev: '7.0.0'
hooks:
- id: flake8
additional_dependencies:
- pep8-naming
- flake8-docstrings
- flake8-rst-docstrings
- id: ruff
types_or: [ python ]
args: [ --fix ]
- id: ruff-format
types_or: [ python ]
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.2
hooks:
Expand Down
5 changes: 2 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"python.linting.flake8Enabled": true,
"python.linting.pycodestyleEnabled": false,
"python.linting.pylintEnabled": false,
"python.formatting.provider": "yapf",
"cSpell.allowCompoundWords": true,
"files.exclude": {
".cache": true,
Expand Down Expand Up @@ -75,6 +74,7 @@
"pylint",
"pyside",
"pytest",
"ruff",
"spherocylinder",
"spherocylinders",
"spheropolygons",
Expand All @@ -84,7 +84,6 @@
"twxs",
"uint",
"unmap",
"verts",
"yapf"
"verts"
]
}
1 change: 1 addition & 0 deletions doc/credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Credits
The following people contributed to **fresnel**.

* Joshua A. Anderson, University of Michigan
* Jen Bradley, University of Michigan
* Bradley Dice, University of Michigan
* Jens Glaser, University of Michigan
* Tim Moore, University of Michigan
Expand Down
75 changes: 37 additions & 38 deletions doc/gallery/cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,48 @@
import sys
import os

data = numpy.load('cuboids.npz')
data = numpy.load("cuboids.npz")

scene = fresnel.Scene()
scene.lights = fresnel.light.lightbox()
W, H, D = data['width']
poly_info = fresnel.util.convex_polyhedron_from_vertices([
[-W, -H, -D],
[-W, -H, D],
[-W, H, -D],
[-W, H, D],
[W, -H, -D],
[W, -H, D],
[W, H, -D],
[W, H, D],
])

geometry = fresnel.geometry.ConvexPolyhedron(scene,
poly_info,
position=data['position'],
orientation=data['orientation'],
outline_width=0.015)
geometry.material = fresnel.material.Material(color=fresnel.color.linear(
[0.1, 0.1, 0.6]),
roughness=0.1,
specular=1)
geometry.outline_material = fresnel.material.Material(color=(0.95, 0.93, 0.88),
roughness=0.1,
metal=1.0)

scene.camera = fresnel.camera.Orthographic.fit(scene, view='front')

if 'CI' in os.environ:
W, H, D = data["width"]
poly_info = fresnel.util.convex_polyhedron_from_vertices(
[
[-W, -H, -D],
[-W, -H, D],
[-W, H, -D],
[-W, H, D],
[W, -H, -D],
[W, -H, D],
[W, H, -D],
[W, H, D],
]
)

geometry = fresnel.geometry.ConvexPolyhedron(
scene,
poly_info,
position=data["position"],
orientation=data["orientation"],
outline_width=0.015,
)
geometry.material = fresnel.material.Material(
color=fresnel.color.linear([0.1, 0.1, 0.6]), roughness=0.1, specular=1
)
geometry.outline_material = fresnel.material.Material(
color=(0.95, 0.93, 0.88), roughness=0.1, metal=1.0
)

scene.camera = fresnel.camera.Orthographic.fit(scene, view="front")

if "CI" in os.environ:
samples = 1
else:
samples = 64

out = fresnel.pathtrace(scene, samples=samples, light_samples=32, w=580, h=580)
PIL.Image.fromarray(out[:], mode='RGBA').save('cuboid.png')

if len(sys.argv) > 1 and sys.argv[1] == 'hires':
out = fresnel.pathtrace(scene,
samples=256,
light_samples=16,
w=1380,
h=1380)
PIL.Image.fromarray(out[:], mode='RGBA').save('cuboid-hires.png')
PIL.Image.fromarray(out[:], mode="RGBA").save("cuboid.png")

if len(sys.argv) > 1 and sys.argv[1] == "hires":
out = fresnel.pathtrace(scene, samples=256, light_samples=16, w=1380, h=1380)
PIL.Image.fromarray(out[:], mode="RGBA").save("cuboid-hires.png")
73 changes: 36 additions & 37 deletions doc/gallery/gumballs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@

"""Gumballs example scene."""

import fresnel
import os
import sys

import numpy as np
from matplotlib.colors import LinearSegmentedColormap
import PIL
import sys
import os
from matplotlib.colors import LinearSegmentedColormap

import fresnel

# First, we create a color map for gumballs.
colors = [
'#e56d60',
'#ee9944',
'#716e80',
'#eadecd',
'#cec746',
'#c0443f',
'#734d56',
'#5d5f7b',
'#ecb642',
'#8a9441',
"#e56d60",
"#ee9944",
"#716e80",
"#eadecd",
"#cec746",
"#c0443f",
"#734d56",
"#5d5f7b",
"#ecb642",
"#8a9441",
]
cmap = LinearSegmentedColormap.from_list(name='gumball',
colors=colors,
N=len(colors))
cmap = LinearSegmentedColormap.from_list(name="gumball", colors=colors, N=len(colors))

rng = np.random.default_rng(123)


# Next, we gather information needed for the geometry.
position = np.load('gumballs.npz')['position']
np.random.seed(123)
color = fresnel.color.linear(cmap(np.random.rand(len(position))))
position = np.load("gumballs.npz")["position"]
color = fresnel.color.linear(cmap(rng.random(len(position))))
material = fresnel.material.Material(
primitive_color_mix=1.0,
roughness=0.2,
Expand All @@ -49,31 +51,28 @@
)

# Configure camera and lighting.
scene.camera = fresnel.camera.Perspective(position=(0, 0, 25),
look_at=(0, 0, 0),
up=(0, 1, 0),
focal_length=0.5,
f_stop=0.25)
scene.camera = fresnel.camera.Perspective(
position=(0, 0, 25),
look_at=(0, 0, 0),
up=(0, 1, 0),
focal_length=0.5,
f_stop=0.25,
)
scene.camera.focus_on = (0, 0, 5.6)
scene.lights = fresnel.light.lightbox()
scene.lights.append(
fresnel.light.Light(direction=(0.3, -0.3, 1),
color=(0.5, 0.5, 0.5),
theta=np.pi))
fresnel.light.Light(direction=(0.3, -0.3, 1), color=(0.5, 0.5, 0.5), theta=np.pi)
)

if 'CI' in os.environ:
if "CI" in os.environ:
samples = 1
else:
samples = 128

# Execute rendering.
out = fresnel.pathtrace(scene, w=600, h=600, samples=samples, light_samples=64)
PIL.Image.fromarray(out[:], mode='RGBA').save('gumballs.png')
PIL.Image.fromarray(out[:], mode="RGBA").save("gumballs.png")

if len(sys.argv) > 1 and sys.argv[1] == 'hires':
out = fresnel.pathtrace(scene,
w=1500,
h=1500,
samples=256,
light_samples=64)
PIL.Image.fromarray(out[:], mode='RGBA').save('gumballs-hires.png')
if len(sys.argv) > 1 and sys.argv[1] == "hires":
out = fresnel.pathtrace(scene, w=1500, h=1500, samples=256, light_samples=64)
PIL.Image.fromarray(out[:], mode="RGBA").save("gumballs-hires.png")
32 changes: 13 additions & 19 deletions doc/gallery/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,29 @@
import sys
import os

data = numpy.load('spheres.npz')
data = numpy.load("spheres.npz")

scene = fresnel.Scene()
scene.lights = fresnel.light.cloudy()

geometry = fresnel.geometry.Sphere(scene,
position=data['position'],
radius=0.5,
outline_width=0.1)
geometry = fresnel.geometry.Sphere(
scene, position=data["position"], radius=0.5, outline_width=0.1
)

geometry.material = fresnel.material.Material(color=fresnel.color.linear(
[0.1, 0.8, 0.1]),
roughness=0.8,
specular=0.2)
geometry.material = fresnel.material.Material(
color=fresnel.color.linear([0.1, 0.8, 0.1]), roughness=0.8, specular=0.2
)

scene.camera = fresnel.camera.Orthographic.fit(scene)

if 'CI' in os.environ:
if "CI" in os.environ:
samples = 1
else:
samples = 64

out = fresnel.pathtrace(scene, samples=samples, light_samples=32, w=580, h=580)
PIL.Image.fromarray(out[:], mode='RGBA').save('sphere.png')

if len(sys.argv) > 1 and sys.argv[1] == 'hires':
out = fresnel.pathtrace(scene,
samples=256,
light_samples=16,
w=1380,
h=1380)
PIL.Image.fromarray(out[:], mode='RGBA').save('sphere-hires.png')
PIL.Image.fromarray(out[:], mode="RGBA").save("sphere.png")

if len(sys.argv) > 1 and sys.argv[1] == "hires":
out = fresnel.pathtrace(scene, samples=256, light_samples=16, w=1380, h=1380)
PIL.Image.fromarray(out[:], mode="RGBA").save("sphere-hires.png")
38 changes: 20 additions & 18 deletions doc/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ Python
------

Python code in GSD should follow `PEP8`_ with the formatting performed by
`yapf`_ (configuration in ``setup.cfg``). Code should pass all **flake8** tests
and formatted by **yapf**.
`ruff`_ (configuration in ``ruff.toml``). Code should pass all **ruff** linter checks.

.. _PEP8: https://www.python.org/dev/peps/pep-0008
.. _yapf: https://github.com/google/yapf
.. _ruff: https://github.com/astral-sh/ruff

Tools
^^^^^

* Linter: `flake8 <http://flake8.pycqa.org/en/latest/>`_
* Linter: `ruff`_

* With these plugins:
* With these extensions:

* `pep8-naming <https://github.com/PyCQA/pep8-naming>`_
* `flake8-docstrings <https://gitlab.com/pycqa/flake8-docstrings>`_
* `flake8-rst-docstrings <https://github.com/peterjc/flake8-rst-docstrings>`_
* `D <https://docs.astral.sh/ruff/rules/#pydocstyle-d>`_
* `E, W <https://docs.astral.sh/ruff/rules/#pycodestyle-e-w>`_
* `F <https://docs.astral.sh/ruff/rules/#pyflakes-f>`_
* `N <https://docs.astral.sh/ruff/rules/#pep8-naming-n>`_
* `NPY <https://docs.astral.sh/ruff/rules/#numpy-specific-rules-npy>`_
* `RUF200 <https://docs.astral.sh/ruff/rules/invalid-pyproject-toml>`_

* Configure flake8 in your editor to see violations on save.
* The ``ruff.toml`` included with the package automatically configures these rules.

* Autoformatter: `yapf <https://github.com/google/yapf>`_
* Autoformatter: `ruff <https://github.com/astral-sh/ruff>`_

* Run: ``pre-commit run --all-files`` to apply style changes to the whole
repository.
Expand Down Expand Up @@ -67,14 +69,14 @@ C++/CUDA

* Naming conventions:

* Namespaces: All lowercase ``somenamespace``
* Class names: ``UpperCamelCase``
* Methods: ``lowerCamelCase``
* Member variables: ``m_`` prefix followed by lowercase with words
separated by underscores ``m_member_variable``
* Constants: all upper-case with words separated by underscores
``SOME_CONSTANT``
* Functions: ``lowerCamelCase``
* Namespaces: All lowercase ``somenamespace``
* Class names: ``UpperCamelCase``
* Methods: ``lowerCamelCase``
* Member variables: ``m_`` prefix followed by lowercase with words
separated by underscores ``m_member_variable``
* Constants: all upper-case with words separated by underscores
``SOME_CONSTANT``
* Functions: ``lowerCamelCase``

Tools
^^^^^
Expand Down
Loading

0 comments on commit 7eac406

Please sign in to comment.