Skip to content

Commit 2aa120e

Browse files
committed
Update and fix pre-commit hooks
1 parent d6d99d1 commit 2aa120e

File tree

6 files changed

+26
-38
lines changed

6 files changed

+26
-38
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
name: Continuous Integration
2-
32
on: [push, pull_request]
43
jobs:
54
docs:
@@ -11,29 +10,24 @@ jobs:
1110
steps:
1211
- name: Checkout code
1312
uses: actions/checkout@v2
14-
1513
- name: Install apt dependencies
1614
run: |
1715
sudo apt update -qq
1816
sudo apt install -qq liblapack3 liblapack-dev
19-
2017
- uses: actions/cache@v2
2118
with:
2219
path: ~/.cache/pip
2320
key: pip-${{ matrix.python-version }}-${{ matrix.install-type }}-${{ hashFiles('**/setup.json')
2421
}}
2522
restore-keys: pip-${{ matrix.python-version }}-${{ matrix.install-type }}
26-
2723
- name: Set up Python
2824
uses: actions/setup-python@v2
2925
with:
3026
python-version: ${{ matrix.python-version }}
31-
3227
- name: Install the python project
3328
env:
3429
INSTALL_TYPE: ${{ matrix.install-type }}
3530
run: .ci/install_script.sh
36-
3731
- name: Build documentation
3832
env:
3933
READTHEDOCS: 'True'
@@ -42,7 +36,6 @@ jobs:
4236
with:
4337
name: doc-build
4438
path: doc/build/html
45-
4639
pre-commit:
4740
runs-on: ubuntu-latest
4841
strategy:
@@ -52,29 +45,24 @@ jobs:
5245
steps:
5346
- name: Checkout code
5447
uses: actions/checkout@v2
55-
5648
- name: Install apt dependencies
5749
run: |
5850
sudo apt update -qq
5951
sudo apt install -qq liblapack3 liblapack-dev
60-
6152
- uses: actions/cache@v2
6253
with:
6354
path: ~/.cache/pip
6455
key: pip-${{ matrix.python-version }}-${{ matrix.install-type }}-${{ hashFiles('**/setup.json')
6556
}}
6657
restore-keys: pip-${{ matrix.python-version }}-${{ matrix.install-type }}
67-
6858
- name: Set up Python
6959
uses: actions/setup-python@v2
7060
with:
7161
python-version: ${{ matrix.python-version }}
72-
7362
- name: Install the python project
7463
env:
7564
INSTALL_TYPE: ${{ matrix.install-type }}
7665
run: .ci/install_script.sh
77-
7866
- name: Run pre-commit
7967
run: pre-commit run --all-files || ( git status --short ; git diff ; exit 1
8068
)
@@ -92,29 +80,24 @@ jobs:
9280
steps:
9381
- name: Checkout code
9482
uses: actions/checkout@v2
95-
9683
- name: Install apt dependencies
9784
run: |
9885
sudo apt update -qq
9986
sudo apt install -qq liblapack3 liblapack-dev
100-
10187
- uses: actions/cache@v2
10288
with:
10389
path: ~/.cache/pip
10490
key: pip-${{ matrix.python-version }}-${{ matrix.install-type }}-${{ hashFiles('**/setup.json')
10591
}}
10692
restore-keys: pip-${{ matrix.python-version }}-${{ matrix.install-type }}
107-
10893
- name: Set up Python
10994
uses: actions/setup-python@v2
11095
with:
11196
python-version: ${{ matrix.python-version }}
112-
11397
- name: Install the python project
11498
env:
11599
INSTALL_TYPE: ${{ matrix.install-type }}
116100
run: .ci/install_script.sh
117-
118101
- name: Run pytest
119102
run: pytest --cov=tbmodels --cov-config=.coveragerc
120103
- name: Run codecov

mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[mypy]
44
python_version = 3.7
5+
plugins = numpy.typing.mypy_plugin
56

67
; Strictness settings
78
; disallow_any_unimported = True

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ python_requires = >=3.7
3030
setup_requires =
3131
fastentrypoints
3232
install_requires =
33-
numpy
33+
numpy>=1.20
3434
scipy>=0.15
3535
h5py>=3.2.0
3636
fsc.export

tbmodels/_tb_model.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import h5py
2222
import numpy as np
23+
import numpy.typing as npt
2324
import scipy.linalg as la
2425
from scipy.special import factorial
2526
from fsc.export import export
@@ -96,7 +97,7 @@ def __init__(
9697
dim: ty.Optional[int] = None,
9798
occ: ty.Optional[int] = None,
9899
pos: ty.Optional[ty.Sequence[ty.Sequence[float]]] = None,
99-
uc: ty.Optional[np.ndarray] = None,
100+
uc: ty.Optional[npt.ArrayLike] = None,
100101
contains_cc: bool = True,
101102
cc_check_tolerance: float = 1e-12,
102103
sparse: bool = False,
@@ -638,14 +639,14 @@ def from_wannier_files( # pylint: disable=too-many-locals
638639
atom_pos_cartesian = np.array([a.pos for a in atom_list_cartesian])
639640
if pos_kind == "wannier":
640641
pos_cartesian: ty.Union[
641-
ty.List[np.ndarray], np.ndarray
642+
ty.List[npt.NDArray[np.float_]], npt.NDArray[np.float_]
642643
] = wannier_pos_cartesian
643644
elif pos_kind == "nearest_atom":
644645
if distance_ratio_threshold < 1:
645646
raise ValueError(
646647
"Invalid value for 'distance_ratio_threshold': must be >= 1."
647648
)
648-
pos_cartesian = ty.cast(ty.List[np.ndarray], [])
649+
pos_cartesian = ty.cast(ty.List[npt.NDArray[np.float_]], [])
649650
for p in wannier_pos_cartesian:
650651
p_reduced = la.solve(kwargs["uc"].T, np.array(p).T).T
651652
T_base = np.floor(p_reduced)
@@ -1079,7 +1080,7 @@ def hamilton(
10791080
self,
10801081
k: ty.Union[ty.Sequence[float], ty.Sequence[ty.Sequence[float]]],
10811082
convention: int = 2,
1082-
) -> np.ndarray:
1083+
) -> npt.NDArray[np.complex_]:
10831084
"""
10841085
Calculates the Hamilton matrix for a given k-point or list of
10851086
k-points.
@@ -1130,12 +1131,12 @@ def hamilton(
11301131
H = pos_exponential.conjugate().transpose((0, 2, 1)) * H * pos_exponential
11311132

11321133
if single_point:
1133-
return ty.cast(np.ndarray, H[0])
1134+
return ty.cast(npt.NDArray[np.complex_], H[0])
11341135
return H
11351136

11361137
def eigenval(
11371138
self, k: ty.Union[ty.Sequence[float], ty.Sequence[ty.Sequence[float]]]
1138-
) -> ty.Union[np.ndarray, ty.List[np.ndarray]]:
1139+
) -> ty.Union[npt.NDArray[np.float_], ty.List[npt.NDArray[np.float_]]]:
11391140
"""
11401141
Returns the eigenvalues at a given k point, or list of k-points.
11411142
@@ -1149,7 +1150,7 @@ def eigenval(
11491150
hamiltonians = self.hamilton(k)
11501151
if hamiltonians.ndim == 3:
11511152
return [la.eigvalsh(ham) for ham in hamiltonians]
1152-
return ty.cast(np.ndarray, la.eigvalsh(hamiltonians))
1153+
return ty.cast(npt.NDArray[np.float_], la.eigvalsh(hamiltonians))
11531154

11541155
# -------------------MODIFYING THE MODEL ----------------------------#
11551156
def add_hop(
@@ -1442,12 +1443,12 @@ def _apply_operation( # pylint: disable=too-many-locals
14421443
ty.Tuple[int, ...], ty.Tuple[ty.List[int], ty.List[int]]
14431444
] = co.defaultdict(lambda: ([], []))
14441445
for (i, Ti), (j, Tj) in itertools.product(enumerate(uc_shift), repeat=2):
1445-
shift = tuple(np.array(Tj) - np.array(Ti))
1446+
shift_tuple = tuple(np.array(Tj) - np.array(Ti))
14461447
for idx1, idx2 in itertools.product(
14471448
sublattices[i].indices, sublattices[j].indices
14481449
):
1449-
hop_shifts_idx[shift][0].append(idx1)
1450-
hop_shifts_idx[shift][1].append(idx2)
1450+
hop_shifts_idx[shift_tuple][0].append(idx1)
1451+
hop_shifts_idx[shift_tuple][1].append(idx2)
14511452

14521453
# create hoppings with shifted R (by uc_shift[j] - uc_shift[i])
14531454
new_hop: HoppingType = co.defaultdict(self._empty_matrix)
@@ -1456,8 +1457,8 @@ def _apply_operation( # pylint: disable=too-many-locals
14561457
np.rint(np.dot(symmetry_operation.rotation_matrix, R)),
14571458
dtype=int,
14581459
)
1459-
for shift, (idx1, idx2) in hop_shifts_idx.items():
1460-
new_R = tuple(np.array(R_transformed) + np.array(shift))
1460+
for shift_tuple, (idx1, idx2) in hop_shifts_idx.items():
1461+
new_R = tuple(np.array(R_transformed) + np.array(shift_tuple))
14611462
new_hop[new_R][idx1, idx2] += mat[idx1, idx2]
14621463

14631464
# apply D(g) ... D(g)^-1 (since D(g) is unitary: D(g)^-1 == D(g)^H)
@@ -1592,7 +1593,7 @@ def change_unit_cell( # pylint: disable=too-many-branches
15921593
)
15931594
# convert to reduced coordinates
15941595
if uc is None:
1595-
new_uc: ty.Optional[np.ndarray] = self.uc
1596+
new_uc: ty.Optional[npt.NDArray[np.float_]] = self.uc
15961597
uc_reduced = np.eye(self.dim)
15971598
else:
15981599
new_uc = np.array(uc)
@@ -1668,7 +1669,7 @@ def supercell( # pylint: disable=too-many-locals
16681669
new_uc = (self.uc.T * size_array).T
16691670

16701671
# the new positions, normalized to the supercell
1671-
new_pos: ty.List[np.ndarray] = []
1672+
new_pos: ty.List[npt.NDArray[np.float_]] = []
16721673
reduced_pos = np.array([p / size_array for p in self.pos])
16731674
uc_offsets = list(
16741675
np.array(offset)

tbmodels/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import typing as ty
99

1010
import numpy as np
11+
12+
import numpy.typing as npt
1113
from fsc.export import export
1214

1315

@@ -17,7 +19,7 @@ def matrix_to_hop(
1719
orbitals: ty.Optional[ty.Sequence[int]] = None,
1820
R: ty.Sequence[int] = (0, 0, 0),
1921
multiplier: float = 1.0,
20-
) -> ty.List[ty.Tuple[complex, int, int, np.ndarray]]:
22+
) -> ty.List[ty.Tuple[complex, int, int, npt.NDArray[np.int_]]]:
2123
r"""
2224
Turns a square matrix into a series of hopping terms.
2325

tbmodels/kdotp.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import typing as ty
99

1010
import numpy as np
11+
import numpy.typing as npt
1112
import scipy.linalg as la
1213

1314
from fsc.export import export
@@ -49,7 +50,7 @@ def __init__(
4950

5051
def hamilton(
5152
self, k: ty.Union[ty.Sequence[float], ty.Sequence[ty.Sequence[float]]]
52-
) -> np.ndarray:
53+
) -> npt.NDArray[np.complex_]:
5354
"""
5455
Calculates the Hamilton matrix for a given k-point or list of
5556
k-points.
@@ -69,20 +70,20 @@ def hamilton(
6970
single_point = False
7071

7172
ham = ty.cast(
72-
np.ndarray,
73+
npt.NDArray[np.complex_],
7374
sum(
7475
np.prod(k_array ** k_powers, axis=-1).reshape(-1, 1, 1)
7576
* mat[np.newaxis, :, :]
7677
for k_powers, mat in self.taylor_coefficients.items()
7778
),
7879
)
7980
if single_point:
80-
return ty.cast(np.ndarray, ham[0])
81+
return ty.cast(npt.NDArray[np.complex_], ham[0])
8182
return ham
8283

8384
def eigenval(
8485
self, k: ty.Union[ty.Sequence[float], ty.Sequence[ty.Sequence[float]]]
85-
) -> ty.Union[np.ndarray, ty.List[np.ndarray]]:
86+
) -> ty.Union[npt.NDArray[np.float_], ty.List[npt.NDArray[np.float_]]]:
8687
"""
8788
Returns the eigenvalues at a given k point, or list of k-points.
8889
@@ -96,4 +97,4 @@ def eigenval(
9697
hamiltonians = self.hamilton(k)
9798
if hamiltonians.ndim == 3:
9899
return [la.eigvalsh(ham) for ham in hamiltonians]
99-
return ty.cast(np.ndarray, la.eigvalsh(hamiltonians))
100+
return ty.cast(npt.NDArray[np.float_], la.eigvalsh(hamiltonians))

0 commit comments

Comments
 (0)