Skip to content

Commit

Permalink
pre-commit ruff (#2257)
Browse files Browse the repository at this point in the history
Replace black&isort by [ruff](https://docs.astral.sh/ruff/) (faster, additional functionality).

* Enable (99%) black-compatible formatting 
* Enable linting with autofixing
* Fix some linting issues
* `pre-commit run --all-files`
  • Loading branch information
dweindl authored Jan 5, 2024
1 parent dd07008 commit a253b17
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 48 deletions.
30 changes: 15 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
args: ["--profile", "black", "--filter-files", "--line-length", "79"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
Expand All @@ -16,15 +10,21 @@ repos:
args: [--allow-multiple-documents]
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 23.7.0
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.11
hooks:
- id: black-jupyter
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.11
args: ["--line-length", "79"]
# Run the linter.
- id: ruff
args:
- --fix
- --config
- python/sdist/pyproject.toml

# Run the formatter.
- id: ruff-format
args:
- --config
- python/sdist/pyproject.toml

exclude: '^(ThirdParty|models)/'
2 changes: 1 addition & 1 deletion documentation/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/stable/config

import os
import re
import subprocess
Expand All @@ -18,6 +17,7 @@
import exhale_multiproject_monkeypatch
import mock
import pandas as pd
import sphinx
import sympy as sp
from exhale import configs as exhale_configs
from sphinx.transforms.post_transforms import ReferencesResolver
Expand Down
11 changes: 7 additions & 4 deletions python/sdist/amici/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,21 @@ def _imported_from_setup() -> bool:
# from .swig_wrappers
hdf5_enabled = "readSolverSettingsFromHDF5" in dir()
# These modules require the swig interface and other dependencies
from .numpy import ExpDataView, ReturnDataView
from .numpy import ExpDataView, ReturnDataView # noqa: F401
from .pandas import *
from .swig_wrappers import *

# These modules don't require the swig interface
from typing import Protocol, runtime_checkable

from .de_export import DEExporter, DEModel
from .sbml_import import SbmlImporter, assignmentRules2observables
from .de_export import DEExporter, DEModel # noqa: F401
from .sbml_import import ( # noqa: F401
SbmlImporter,
assignmentRules2observables,
)

@runtime_checkable
class ModelModule(Protocol):
class ModelModule(Protocol): # noqa: F811
"""Type of AMICI-generated model modules.
To enable static type checking."""
Expand Down
4 changes: 2 additions & 2 deletions python/sdist/amici/__init__.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"version currently installed."
)

from .TPL_MODELNAME import *
from .TPL_MODELNAME import getModel as get_model
from .TPL_MODELNAME import * # noqa: F403, F401
from .TPL_MODELNAME import getModel as get_model # noqa: F401

__version__ = "TPL_PACKAGE_VERSION"
2 changes: 1 addition & 1 deletion python/sdist/amici/de_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def smart_multiply(


def smart_is_zero_matrix(
x: Union[sp.MutableDenseMatrix, sp.MutableSparseMatrix]
x: Union[sp.MutableDenseMatrix, sp.MutableSparseMatrix],
) -> bool:
"""A faster implementation of sympy's is_zero_matrix
Expand Down
4 changes: 2 additions & 2 deletions python/sdist/amici/gradient_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import copy
from typing import Callable, List, Optional, Sequence
from typing import List, Optional, Sequence

import numpy as np

Expand Down Expand Up @@ -331,7 +331,7 @@ def _check_results(
"""

result = rdata[field]
if type(result) is float:
if type(result) is float: # noqa E721
result = np.array(result)

_check_close(
Expand Down
6 changes: 3 additions & 3 deletions python/sdist/amici/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ObservableTransformation(str, enum.Enum):


def noise_distribution_to_observable_transformation(
noise_distribution: Union[str, Callable]
noise_distribution: Union[str, Callable],
) -> ObservableTransformation:
"""
Parse noise distribution string and extract observable transformation
Expand All @@ -93,7 +93,7 @@ def noise_distribution_to_observable_transformation(


def noise_distribution_to_cost_function(
noise_distribution: Union[str, Callable]
noise_distribution: Union[str, Callable],
) -> Callable[[str], str]:
"""
Parse noise distribution string to a cost function definition amici can
Expand Down Expand Up @@ -423,7 +423,7 @@ def _parse_special_functions(sym: sp.Expr, toplevel: bool = True) -> sp.Expr:


def _denest_piecewise(
args: Sequence[Union[sp.Expr, sp.logic.boolalg.Boolean, bool]]
args: Sequence[Union[sp.Expr, sp.logic.boolalg.Boolean, bool]],
) -> Tuple[Union[sp.Expr, sp.logic.boolalg.Boolean, bool]]:
"""
Denest piecewise functions that contain piecewise as condition
Expand Down
7 changes: 5 additions & 2 deletions python/sdist/amici/petab/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Various helper functions for working with PEtab problems."""
import re
from typing import Dict, Tuple, Union
from typing import TYPE_CHECKING, Dict, Tuple, Union

import libsbml
import pandas as pd
Expand All @@ -9,6 +9,9 @@
from petab.mapping import resolve_mapping
from petab.models import MODEL_TYPE_PYSB, MODEL_TYPE_SBML

if TYPE_CHECKING:
pysb = None


def get_states_in_condition_table(
petab_problem: petab.Problem,
Expand Down Expand Up @@ -63,7 +66,7 @@ def get_states_in_condition_table(
spm = pysb.pattern.SpeciesPatternMatcher(
model=petab_problem.model.model
)
except NotImplementedError as e:
except NotImplementedError:
raise NotImplementedError(
"Requires https://github.com/pysb/pysb/pull/570. "
"To use this functionality, update pysb via "
Expand Down
3 changes: 1 addition & 2 deletions python/sdist/amici/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def plot_state_trajectories(
elif model is not None and prefer_names:
labels = np.asarray(model.getStateNames())[list(state_indices)]
labels = [
l if l else model.getStateIds()[ix]
for ix, l in enumerate(labels)
l if l else model.getStateIds()[ix] for ix, l in enumerate(labels)
]
elif model is not None:
labels = np.asarray(model.getStateIds())[list(state_indices)]
Expand Down
4 changes: 2 additions & 2 deletions python/sdist/amici/pysb_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def _process_pysb_species(pysb_model: pysb.Model, ode_model: DEModel) -> None:
sp.Symbol(f"__s{ix}"), f"{specie}", init, xdot[ix]
)
)
logger.debug(f"Finished Processing PySB species ")
logger.debug("Finished Processing PySB species ")


@log_execution_time("processing PySB parameters", logger)
Expand Down Expand Up @@ -1353,7 +1353,7 @@ def has_fixed_parameter_ic(


def extract_monomers(
complex_patterns: Union[pysb.ComplexPattern, List[pysb.ComplexPattern]]
complex_patterns: Union[pysb.ComplexPattern, List[pysb.ComplexPattern]],
) -> List[str]:
"""
Constructs a list of monomer names contained in complex patterns.
Expand Down
9 changes: 4 additions & 5 deletions python/sdist/amici/sbml_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
List,
Optional,
Sequence,
Set,
Tuple,
Union,
)
Expand Down Expand Up @@ -1165,7 +1164,7 @@ def _process_reactions(self):
# accounts for possibly variable compartments.
self.stoichiometric_matrix[
species["index"], reaction_index
] += (sign * stoichiometry * species["conversion_factor"])
] += sign * stoichiometry * species["conversion_factor"]
if reaction.isSetId():
sym_math = self._local_symbols[reaction.getId()]
else:
Expand Down Expand Up @@ -2360,9 +2359,9 @@ def _replace_in_all_expressions(
# rule (at the end of the _process_species method), hence needs to be
# processed here too.
self.compartments = {
smart_subs(c, old, new)
if replace_identifiers
else c: smart_subs(v, old, self._make_initial(new))
smart_subs(c, old, new) if replace_identifiers else c: smart_subs(
v, old, self._make_initial(new)
)
for c, v in self.compartments.items()
}

Expand Down
2 changes: 1 addition & 1 deletion python/sdist/amici/sbml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def mathml2sympy(


def _parse_logical_operators(
math_str: Union[str, float, None]
math_str: Union[str, float, None],
) -> Union[str, float, None]:
"""
Parses a math string in order to replace logical operators by a form
Expand Down
5 changes: 4 additions & 1 deletion python/sdist/amici/splines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,10 @@ def spline_user_functions(
"AmiciSplineSensitivity": [
(
lambda *args: True,
lambda spline_id, x, param_id, *p: f"sspl_{spline_ids.index(spline_id)}_{p_index[param_id]}",
lambda spline_id,
x,
param_id,
*p: f"sspl_{spline_ids.index(spline_id)}_{p_index[param_id]}",
)
],
}
Expand Down
2 changes: 1 addition & 1 deletion python/sdist/amici/swig_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _capture_cstdout():


def _get_ptr(
obj: Union[AmiciModel, AmiciExpData, AmiciSolver, AmiciReturnData]
obj: Union[AmiciModel, AmiciExpData, AmiciSolver, AmiciReturnData],
) -> Union[
"amici_swig.Model",
"amici_swig.ExpData",
Expand Down
5 changes: 5 additions & 0 deletions python/sdist/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ build-backend = "setuptools.build_meta"

[tool.black]
line-length = 79

[tool.ruff]
line-length = 79
ignore = ["E402", "F403", "F405", "E741"]
extend-include = ["*.ipynb"]
1 change: 0 additions & 1 deletion python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import copy
import importlib
import os
import shutil
import sys

import amici
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pathlib import Path

import petab
import yaml2sbml

yaml2sbml_yaml = "lotka_volterra.yaml"
Expand Down
4 changes: 2 additions & 2 deletions python/tests/test_edata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import amici
import numpy as np
from amici.testing import skip_on_valgrind
from test_sbml_import import model_units_module
from test_sbml_import import model_units_module # noqa: F401


@skip_on_valgrind
def test_edata_sensi_unscaling(model_units_module):
def test_edata_sensi_unscaling(model_units_module): # noqa: F811
"""
ExpData parameters should be used for unscaling initial state
sensitivities.
Expand Down
1 change: 1 addition & 0 deletions python/tests/test_pysb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""PYSB model tests"""
# flake8: noqa: F821

import importlib
import logging
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import re
import sys
from pathlib import Path
from typing import List, Set, Tuple
from typing import TYPE_CHECKING, List, Set, Tuple

import pytest

if TYPE_CHECKING:
from _pytest.reports import TestReport

# stores passed SBML semantic test suite IDs
passed_ids = []

Expand Down
2 changes: 1 addition & 1 deletion tests/generateTestConfig/example_steadystate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys

import numpy as np
from example import AmiciExample, dict2attrs
from example import AmiciExample


class ExampleSteadystate(AmiciExample):
Expand Down

0 comments on commit a253b17

Please sign in to comment.