Skip to content

Commit

Permalink
pre-commit: run pyupgrade (#2264)
Browse files Browse the repository at this point in the history
Set up pre-commit hook and run [pyupgrade](https://github.com/asottile/pyupgrade) (update type annotations, ...).
  • Loading branch information
dweindl authored Jan 15, 2024
1 parent 8077915 commit 3efd9f9
Show file tree
Hide file tree
Showing 29 changed files with 363 additions and 385 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ repos:
- --config
- python/sdist/pyproject.toml

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--py39-plus"]
additional_dependencies: [pyupgrade==3.15.0]

exclude: '^(ThirdParty|models)/'
3 changes: 1 addition & 2 deletions documentation/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand All @@ -15,7 +14,7 @@
import amici
import exhale.deploy
import exhale_multiproject_monkeypatch
import mock
from unittest import mock
import pandas as pd
import sphinx
import sympy as sp
Expand Down
2 changes: 1 addition & 1 deletion documentation/recreate_reference_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
def get_keys_by_year(bibfile):
"""Get bibtex entry keys as dict by year"""

with open(bibfile, "r") as f:
with open(bibfile) as f:
db = biblib.bib.Parser().parse(f, log_fp=sys.stderr).get_entries()
recoverer = biblib.messages.InputErrorRecoverer()
by_year = {}
Expand Down
41 changes: 21 additions & 20 deletions python/sdist/amici/conserved_quantities_demartino.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import math
import random
import sys
from typing import List, MutableSequence, Optional, Sequence, Tuple, Union
from typing import Optional, Union
from collections.abc import MutableSequence, Sequence

from .logging import get_logger

Expand All @@ -22,7 +23,7 @@ def compute_moiety_conservation_laws(
max_num_monte_carlo: int = 20,
rng_seed: Union[None, bool, int] = False,
species_names: Optional[Sequence[str]] = None,
) -> Tuple[List[List[int]], List[List[float]]]:
) -> tuple[list[list[int]], list[list[float]]]:
"""Compute moiety conservation laws.
According to the algorithm proposed by De Martino et al. (2014)
Expand Down Expand Up @@ -112,9 +113,9 @@ def compute_moiety_conservation_laws(
def _output(
int_kernel_dim: int,
kernel_dim: int,
int_matched: List[int],
species_indices: List[List[int]],
species_coefficients: List[List[float]],
int_matched: list[int],
species_indices: list[list[int]],
species_coefficients: list[list[float]],
species_names: Optional[Sequence[str]] = None,
verbose: bool = False,
log_level: int = logging.DEBUG,
Expand Down Expand Up @@ -203,7 +204,7 @@ def _qsort(

def _kernel(
stoichiometric_list: Sequence[float], num_species: int, num_reactions: int
) -> Tuple[int, List[int], int, List[int], List[List[int]], List[List[float]]]:
) -> tuple[int, list[int], int, list[int], list[list[int]], list[list[float]]]:
"""
Kernel (left nullspace of :math:`S`) calculation by Gaussian elimination
Expand All @@ -227,8 +228,8 @@ def _kernel(
kernel dimension, MCLs, integer kernel dimension, integer MCLs and
indices to species and reactions in the preceding order as a tuple
"""
matrix: List[List[int]] = [[] for _ in range(num_species)]
matrix2: List[List[float]] = [[] for _ in range(num_species)]
matrix: list[list[int]] = [[] for _ in range(num_species)]
matrix2: list[list[float]] = [[] for _ in range(num_species)]
i_reaction = 0
i_species = 0
for val in stoichiometric_list:
Expand All @@ -243,7 +244,7 @@ def _kernel(
matrix[i].append(num_reactions + i)
matrix2[i].append(1)

order: List[int] = list(range(num_species))
order: list[int] = list(range(num_species))
pivots = [
matrix[i][0] if len(matrix[i]) else _MAX for i in range(num_species)
]
Expand Down Expand Up @@ -283,7 +284,7 @@ def _kernel(
if pivots[order[j + 1]] == pivots[order[j]] != _MAX:
k1 = order[j + 1]
k2 = order[j]
column: List[float] = [0] * (num_species + num_reactions)
column: list[float] = [0] * (num_species + num_reactions)
g = matrix2[k2][0] / matrix2[k1][0]
for i in range(1, len(matrix[k1])):
column[matrix[k1][i]] = matrix2[k1][i] * g
Expand Down Expand Up @@ -369,7 +370,7 @@ def _fill(
stoichiometric_list: Sequence[float],
matched: Sequence[int],
num_species: int,
) -> Tuple[List[List[int]], List[List[int]], List[int]]:
) -> tuple[list[list[int]], list[list[int]], list[int]]:
"""Construct interaction matrix
Construct the interaction matrix out of the given stoichiometric matrix
Expand Down Expand Up @@ -454,8 +455,8 @@ def _is_linearly_dependent(
boolean indicating linear dependence (true) or not (false)
"""
K = int_kernel_dim + 1
matrix: List[List[int]] = [[] for _ in range(K)]
matrix2: List[List[float]] = [[] for _ in range(K)]
matrix: list[list[int]] = [[] for _ in range(K)]
matrix2: list[list[float]] = [[] for _ in range(K)]
# Populate matrices with species ids and coefficients for CLs
for i in range(K - 1):
for j in range(len(cls_species_idxs[i])):
Expand Down Expand Up @@ -508,7 +509,7 @@ def _is_linearly_dependent(
if pivots[order[j + 1]] == pivots[order[j]] != _MAX:
k1 = order[j + 1]
k2 = order[j]
column: List[float] = [0] * num_species
column: list[float] = [0] * num_species
g = matrix2[k2][0] / matrix2[k1][0]
for i in range(1, len(matrix[k1])):
column[matrix[k1][i]] = matrix2[k1][i] * g
Expand Down Expand Up @@ -540,7 +541,7 @@ def _monte_carlo(
initial_temperature: float = 1,
cool_rate: float = 1e-3,
max_iter: int = 10,
) -> Tuple[bool, int, Sequence[int]]:
) -> tuple[bool, int, Sequence[int]]:
"""MonteCarlo simulated annealing for finding integer MCLs
Finding integer solutions for the MCLs by Monte Carlo, see step (b) in
Expand Down Expand Up @@ -712,8 +713,8 @@ def _relax(
(``False``)
"""
K = len(int_matched)
matrix: List[List[int]] = [[] for _ in range(K)]
matrix2: List[List[float]] = [[] for _ in range(K)]
matrix: list[list[int]] = [[] for _ in range(K)]
matrix2: list[list[float]] = [[] for _ in range(K)]
i_reaction = 0
i_species = 0
for val in stoichiometric_list:
Expand Down Expand Up @@ -767,7 +768,7 @@ def _relax(
if pivots[order[j + 1]] == pivots[order[j]] != _MAX:
k1 = order[j + 1]
k2 = order[j]
column: List[float] = [0] * num_reactions
column: list[float] = [0] * num_reactions
g = matrix2[k2][0] / matrix2[k1][0]
for i in range(1, len(matrix[k1])):
column[matrix[k1][i]] = matrix2[k1][i] * g
Expand Down Expand Up @@ -807,7 +808,7 @@ def _relax(

# subtract rows
# matrix2[k] = matrix2[k] - matrix2[j] * matrix2[k][i]
row_k: List[float] = [0] * num_reactions
row_k: list[float] = [0] * num_reactions
for a in range(len(matrix[k])):
row_k[matrix[k][a]] = matrix2[k][a]
for a in range(len(matrix[j])):
Expand Down Expand Up @@ -955,7 +956,7 @@ def _reduce(
k1 = order[i]
for j in range(i + 1, K):
k2 = order[j]
column: List[float] = [0] * num_species
column: list[float] = [0] * num_species
for species_idx, coefficient in zip(
cls_species_idxs[k1], cls_coefficients[k1]
):
Expand Down
4 changes: 2 additions & 2 deletions python/sdist/amici/conserved_quantities_rref.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Find conserved quantities deterministically"""

from typing import List, Literal, Optional, Union
from typing import Literal, Optional, Union

import numpy as np

Expand Down Expand Up @@ -67,7 +67,7 @@ def _round(mat):
return mat


def pivots(mat: np.array) -> List[int]:
def pivots(mat: np.array) -> list[int]:
"""Get indices of pivot columns in ``mat``, assumed to be in reduced row
echelon form"""
pivot_cols = []
Expand Down
17 changes: 9 additions & 8 deletions python/sdist/amici/cxxcodeprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import itertools
import os
import re
from typing import Dict, Iterable, List, Optional, Tuple
from typing import Optional
from collections.abc import Iterable

import sympy as sp
from sympy.codegen.rewriting import Optimization, optimize
Expand Down Expand Up @@ -73,7 +74,7 @@ def _print_min_max(self, expr, cpp_fun: str, sympy_fun):
)
if len(expr.args) == 1:
return self._print(arg0)
return "%s%s(%s, %s)" % (
return "{}{}({}, {})".format(
self._ns,
cpp_fun,
self._print(arg0),
Expand All @@ -92,7 +93,7 @@ def _print_Max(self, expr):

def _get_sym_lines_array(
self, equations: sp.Matrix, variable: str, indent_level: int
) -> List[str]:
) -> list[str]:
"""
Generate C++ code for assigning symbolic terms in symbols to C++ array
`variable`.
Expand Down Expand Up @@ -122,7 +123,7 @@ def _get_sym_lines_symbols(
equations: sp.Matrix,
variable: str,
indent_level: int,
) -> List[str]:
) -> list[str]:
"""
Generate C++ code for where array elements are directly replaced with
their corresponding macro symbol
Expand Down Expand Up @@ -209,11 +210,11 @@ def format_line(symbol: sp.Symbol):
def csc_matrix(
self,
matrix: sp.Matrix,
rownames: List[sp.Symbol],
colnames: List[sp.Symbol],
rownames: list[sp.Symbol],
colnames: list[sp.Symbol],
identifier: Optional[int] = 0,
pattern_only: Optional[bool] = False,
) -> Tuple[List[int], List[int], sp.Matrix, List[str], sp.Matrix]:
) -> tuple[list[int], list[int], sp.Matrix, list[str], sp.Matrix]:
"""
Generates the sparse symbolic identifiers, symbolic identifiers,
sparse matrix, column pointers and row values for a symbolic
Expand Down Expand Up @@ -298,7 +299,7 @@ def print_bool(expr) -> str:

def get_switch_statement(
condition: str,
cases: Dict[int, List[str]],
cases: dict[int, list[str]],
indentation_level: Optional[int] = 0,
indentation_step: Optional[str] = " " * 4,
):
Expand Down
Loading

0 comments on commit 3efd9f9

Please sign in to comment.