Skip to content

Commit

Permalink
Use ruff utility for static analysis (#29)
Browse files Browse the repository at this point in the history
The command `tox -e lint` now relies on `ruff` utility.
  • Loading branch information
tristan0x authored Mar 15, 2024
1 parent c0cdb6c commit 8fc0d28
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 66 deletions.
22 changes: 9 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,23 @@ repos:
stages:
- commit-msg
additional_dependencies: ['conventional-changelog-conventionalcommits']
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
hooks:
- id: codespell
args: ["-x", ".codespellignorelines"]
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.2
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format

#- repo: https://github.com/PyCQA/pydocstyle
# rev: 6.1.1
# hooks:
# - id: pydocstyle
# additional_dependencies: ["toml"]
# exclude: 'examples|tests|scripts'
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
exclude: 'examples|tests|scripts'
25 changes: 10 additions & 15 deletions astrovascpy/bloodflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@

from . import ou
from .exceptions import BloodFlowError
from .scipy_petsc_conversions import PETScVec2array
from .scipy_petsc_conversions import array2PETScVec
from .scipy_petsc_conversions import coomatrix2PETScMat
from .scipy_petsc_conversions import distribute_array
from .scipy_petsc_conversions import (
PETScVec2array,
array2PETScVec,
coomatrix2PETScMat,
distribute_array,
)
from .typing import VasculatureParams
from .utils import Graph
from .utils import comm
from .utils import find_neighbors
from .utils import mpi_mem
from .utils import mpi_timer
from .utils import rank
from .utils import rank0
from .utils import size
from .utils import Graph, comm, find_neighbors, mpi_mem, mpi_timer, rank, rank0, size

# PETSc is compiled with complex number support
# -> many warnings from/to PETSc to/from NumPy/SciPy
Expand Down Expand Up @@ -298,9 +293,9 @@ def set_radius_at_endfoot(graph, endfoot_id, endfoot_radius):
# if (np.asarray(endfoot_radius) <= 0).any():
# raise BloodFlowError("Please provide endfoot_radius > 0.")

graph.edge_properties.loc[
graph.edge_properties.endfeet_id == endfoot_id, ["radius"]
] = endfoot_radius
graph.edge_properties.loc[graph.edge_properties.endfeet_id == endfoot_id, ["radius"]] = (
endfoot_radius
)


def set_endfoot_id(graph, endfoot_id, section_id, segment_id, endfeet_length): # pragma: no cover
Expand Down
6 changes: 2 additions & 4 deletions astrovascpy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
from pathlib import Path

import pandas as pd
from vascpy import PointVasculature
from vascpy import SectionVasculature
from vascpy import PointVasculature, SectionVasculature

from .exceptions import BloodFlowError
from .utils import Graph
from .utils import rank0
from .utils import Graph, rank0


def load_graph(filename):
Expand Down
3 changes: 1 addition & 2 deletions astrovascpy/report_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import numpy as np
import pandas as pd
from cached_property import cached_property
from libsonata import ElementReportReader
from libsonata import SonataError
from libsonata import ElementReportReader, SonataError

from .exceptions import BloodFlowError
from .utils import ensure_list
Expand Down
9 changes: 2 additions & 7 deletions astrovascpy/scipy_petsc_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@
import random
import string

from numpy import concatenate
from numpy import dtype
from numpy import concatenate, dtype
from numpy import zeros as np_zeros
from petsc4py import PETSc
from scipy.sparse import csr_matrix

from . import PetscBinaryIO
from .utils import comm
from .utils import mpi
from .utils import rank
from .utils import rank0
from .utils import size
from .utils import comm, mpi, rank, rank0, size


def _from_numpy_dtype(np_type):
Expand Down
8 changes: 3 additions & 5 deletions examples/compute_static_flow_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

import sys
from functools import partial
from pathlib import Path
from pathlib import PurePath
from pathlib import Path, PurePath

import matplotlib.colors as c
import matplotlib.pyplot as plt
Expand All @@ -31,9 +31,7 @@
# from astrovascpy.io import load_graph_from_csv
# from astrovascpy.io import load_graph_from_h5
from astrovascpy.report_writer import write_simulation_report
from astrovascpy.utils import create_entry_largest_nodes
from astrovascpy.utils import mpi_mem
from astrovascpy.utils import mpi_timer
from astrovascpy.utils import create_entry_largest_nodes, mpi_mem, mpi_timer

petsc4py.init(sys.argv)

Expand Down
8 changes: 2 additions & 6 deletions examples/simulate_OU_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
from mpi4py import MPI
from petsc4py import PETSc

from astrovascpy.bloodflow import generate_endfeet
from astrovascpy.bloodflow import simulate_ou_process
from astrovascpy.bloodflow import generate_endfeet, simulate_ou_process

# from astrovascpy.io import load_graph_from_bin
# from astrovascpy.io import load_graph_from_h5
from astrovascpy.io import load_graph_from_csv
from astrovascpy.report_writer import write_simulation_report
from astrovascpy.utils import create_entry_largest_nodes
from astrovascpy.utils import create_input_speed
from astrovascpy.utils import mpi_mem
from astrovascpy.utils import mpi_timer
from astrovascpy.utils import create_entry_largest_nodes, create_input_speed, mpi_mem, mpi_timer

petsc4py.init(sys.argv)

Expand Down
59 changes: 59 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,62 @@ force_single_line = true
testpaths = [
"tests",
]

[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

# Same as Black.
line-length = 100
indent-width = 4

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I"]
ignore = ["F401", "F403", "F405", "E741", "E721"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[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"
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Setup for the astrovascpy package."""

import importlib.util
from pathlib import Path

from setuptools import find_namespace_packages
from setuptools import setup
from setuptools import find_namespace_packages, setup

spec = importlib.util.spec_from_file_location(
"astrovascpy.version",
Expand Down
1 change: 1 addition & 0 deletions tests/data/reporting/create_reports.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Taken from the libsonata lib."""

import h5py
import numpy as np

Expand Down
5 changes: 1 addition & 4 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import numpy as np
import numpy.testing as npt

from astrovascpy.io import load_graph
from astrovascpy.io import load_graph_from_bin
from astrovascpy.io import load_graph_from_csv
from astrovascpy.io import load_graph_from_h5
from astrovascpy.io import load_graph, load_graph_from_bin, load_graph_from_csv, load_graph_from_h5
from astrovascpy.PetscBinaryIO import get_conf

TEST_DIR = Path(__file__).resolve().parent
Expand Down
12 changes: 7 additions & 5 deletions tests/test_mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from mpi4py import MPI
from scipy.sparse import coo_matrix

from astrovascpy.scipy_petsc_conversions import PETScMat2coo
from astrovascpy.scipy_petsc_conversions import PETScVec2array
from astrovascpy.scipy_petsc_conversions import array2PETScVec
from astrovascpy.scipy_petsc_conversions import coomatrix2PETScMat
from astrovascpy.scipy_petsc_conversions import distribute_array
from astrovascpy.scipy_petsc_conversions import (
PETScMat2coo,
PETScVec2array,
array2PETScVec,
coomatrix2PETScMat,
distribute_array,
)

# pip install pytest-mpi
# mpirun -n 4 pytest --with-mpi tests/test_mpi.py
Expand Down
3 changes: 0 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ allowlist_externals = bash
deps =
codespell
pre-commit
pylint
pylint-exit
commands =
codespell --config .codespellrc -i 3 -x .codespellignorelines -w {[base]files} README.md CHANGELOG.md docs/source
pre-commit run --all-files --show-diff-on-failure
bash -c "pylint -j {env:PYLINT_NPROCS:1} {[base]files} || pylint-exit $?"

[testenv:docs]
changedir = docs
Expand Down

0 comments on commit 8fc0d28

Please sign in to comment.