Skip to content

Commit e4c361f

Browse files
Merge pull request #39 from SpinW/more-lints
Adds more lint rules (and lints accordingly)
2 parents 965788f + f86f235 commit e4c361f

21 files changed

+124
-100
lines changed

pyproject.toml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,16 @@ version = {attr = "pyspinw.__version__"}
1111
line-length = 120
1212

1313
[tool.ruff.lint]
14-
select = ["PL"]
15-
ignore = ["PLR"]
14+
select = ["D1", # undocumented objects
15+
"D2", # docstring formating w.r.t. whitespace
16+
"E", # pycodestyle errors
17+
"PL", # pylint
18+
"W", # pycodestyle warnings
19+
]
20+
21+
ignore = ["D107", # undocumented __init__
22+
"D203", # 1 blank line before class docstring
23+
"D210", # whitespace surrounding docstring text
24+
"D213", # multi-line summary starts at second line
25+
"PLR", # pylint refactor suggestions
26+
]

pyspinw/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""pySpinW: spin Hamiltonian calculation in Python
2+
3+
SpinW is a Python library that can plot and numerically simulate
4+
magnetic structures and excitations of given spin Hamiltonian using
5+
classical Monte Carlo simulation and linear spin wave theory.
6+
"""

pyspinw/_base.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Base classes for PySpinW
1+
"""Base classes for PySpinW
22
33
This is an abstract outline, the actual implementations are in different files
44
@@ -13,13 +13,15 @@
1313

1414

1515
class MagneticStructure(ABC):
16-
""" Base class for representations of the Magnetic Structures """
16+
"""Base class for representations of the Magnetic Structures"""
17+
1718
def __init__(self):
1819
pass
1920

2021

2122
class Hamiltonian(ABC):
22-
""" Hamiltonian base class"""
23+
"""Hamiltonian base class"""
24+
2325
def __init__(self,
2426
crystal_structure: BravaisLattice,
2527
magnetic_structure: MagneticStructure):
@@ -29,26 +31,28 @@ def __init__(self,
2931

3032
@abstractmethod
3133
def energies(self, q_vectors: np.ndarray):
32-
""" Get the energy levels of the system at the given q vectors """
34+
"""Get the energy levels of the system at the given q vectors"""
3335

3436
class Sample(ABC):
35-
""" Representation of the macrostructure of a sample used in an experiment (Twin, Powder etc)"""
37+
"""Representation of the macrostructure of a sample used in an experiment (Twin, Powder etc)"""
38+
3639
def __init__(self, hamiltonian: Hamiltonian):
3740
self.hamiltonian = hamiltonian
3841

3942
Identifier = str # temporary choice for now
4043

4144

4245
class Coupling:
43-
""" Coupling between different sites """
46+
"""Coupling between different sites"""
47+
4448
def __init__(self, site_1: Identifier, site_2: Identifier):
4549
self._site_1 = site_1
4650
self._site_2 = site_2
4751
self._coupling_matrix = None
4852

4953
@property
5054
def coupling_matrix(self) -> np.ndarray:
51-
""" The coupling matrix for this coupling
55+
"""The coupling matrix for this coupling
5256
5357
i.e. if H is the energy contribution for this coupling, S is the spin state, and
5458
M is the coupling matrix, we have
@@ -62,24 +66,26 @@ def coupling_matrix(self) -> np.ndarray:
6266

6367

6468
class Anisotropy:
65-
""" Defines the anisotropy at a given site"""
69+
"""Defines the anisotropy at a given site"""
70+
6671
def __init__(self, site: Identifier):
6772
self._site = site
6873
self._anisotropy_matrix = None
6974

7075
@property
7176
def anisotropy_matrix(self) -> np.ndarray:
72-
""" Matrix spefifying the anisotropy - `A` term in the Hamiltonian """
77+
"""Matrix spefifying the anisotropy - `A` term in the Hamiltonian"""
7378
if self._anisotropy_matrix is None:
7479
raise ValueError("Anisotropy matrix not initialised - this shouldn't happen")
7580
else:
7681
return self._anisotropy_matrix
7782

7883

7984
class Data:
80-
""" Placeholder """
85+
"""Placeholder"""
8186

8287
class Experiment:
83-
""" The setup of a neutron experiment (base class)"""
88+
"""The setup of a neutron experiment (base class)"""
89+
8490
def __init__(self, sample: Sample, data: Data | None = None):
8591
pass

pyspinw/anisotropy.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
""" Different kinds of anisotropy """
1+
"""Different kinds of anisotropy"""
22
import numpy as np
33

44
from pyspinw._base import Anisotropy, Identifier
55
from pyspinw.checks import check_sizes
66

77

88
class GeneralAnisotropy(Anisotropy):
9-
""" General anisotropy specification """
9+
"""General anisotropy specification"""
1010

1111
@check_sizes(a=(3,3), force_numpy=True)
1212
def __init__(self, site: Identifier, a: np.ndarray):
@@ -16,11 +16,12 @@ def __init__(self, site: Identifier, a: np.ndarray):
1616

1717
@property
1818
def anisotropy_matrix(self) -> np.ndarray:
19+
"""The matrix defining the anisotropy."""
1920
return self._a
2021

2122

2223
class DiagonalAnisotropy(GeneralAnisotropy):
23-
""" Anisotropy oriented with axes, but variable amount in x, y and z"""
24+
"""Anisotropy oriented with axes, but variable amount in x, y and z"""
2425

2526
@check_sizes(a=(3,), force_numpy=True)
2627
def __init__(self, site: Identifier, a: np.ndarray):
@@ -29,21 +30,24 @@ def __init__(self, site: Identifier, a: np.ndarray):
2930

3031

3132
class XAxisAnisotropy(DiagonalAnisotropy):
32-
""" Pure X anisotropy"""
33+
"""Pure X anisotropy"""
34+
3335
def __init__(self, site: Identifier, a: float):
3436
super().__init__(site, np.array([1,0,0], dtype=float))
3537
self.a_x = a
3638

3739

3840
class YAxisAnisotropy(DiagonalAnisotropy):
39-
""" Pure Y anisotropy"""
41+
"""Pure Y anisotropy"""
42+
4043
def __init__(self, site: Identifier, a: float):
4144
super().__init__(site, np.array([0,1,0], dtype=float))
4245
self.a_y = a
4346

4447

4548
class ZAxisAnisotropy(DiagonalAnisotropy):
46-
""" Pure Z anisotropy"""
49+
"""Pure Z anisotropy"""
50+
4751
def __init__(self, site: Identifier, a: float):
4852
super().__init__(site, np.array([0,0,1], dtype=float))
4953
self.a_z = a

pyspinw/basis.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
""" Functions for working with different basis vectors"""
1+
"""Functions for working with different basis vectors"""
22

33
import numpy as np
44
from pyspinw.checks import check_sizes
55

66
@check_sizes(vectors=(-1, 3))
77
def find_aligned_basis(vectors: np.ndarray, rcond: float | None = None) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
8-
""" Find a set of orthonormal basis vectors aligned with the first being aligned to the input vectors
8+
"""Find a set of orthonormal basis vectors aligned with the first being aligned to the input vectors
99
1010
:param vectors: an n-by-3 numpy array containing the vectors to use to calculate the basis
1111
:param rcond: tolerance on the calculation, default is 3 * machine precision
1212
"""
13-
1413
if rcond is None:
1514
rcond = 3*np.finfo(vectors.dtype).eps # N * floating point epsilon
1615

@@ -51,8 +50,7 @@ def find_aligned_basis(vectors: np.ndarray, rcond: float | None = None) -> tuple
5150

5251

5352
def demo_find_aligned_basis():
54-
""" Example / test for find_aligned_basis"""
55-
53+
"""Example / test for find_aligned_basis"""
5654
test_vectors = np.array([
5755
[2, 0, 0],
5856
[0, 3, 0],

pyspinw/calculations/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Functions for performing spin wave calculations."""

pyspinw/calculations/spinwave.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Spinwave Calculations"""
1+
"""Spinwave Calculations"""
22

33
from dataclasses import dataclass
44
from enum import Enum
@@ -8,22 +8,29 @@
88

99
from pyspinw.checks import check_sizes
1010

11+
12+
# Disable linting for bad variable names, because they should match the docs
13+
# ruff: noqa: E741
14+
1115
@dataclass
1216
class Coupling:
13-
""" Temporary description of the coupling between atoms"""
17+
"""Temporary description of the coupling between atoms"""
18+
1419
index1: int
1520
index2: int
1621
matrix: np.ndarray
1722
inter_site_vector: np.ndarray
1823

1924
class CalculationMethod(Enum):
20-
""" Type of method used (for debugging purposes) """
25+
"""Type of method used (for debugging purposes)"""
26+
2127
CHOLESKY = 0
2228
LDL = 1
2329

2430
@dataclass
2531
class SpinwaveResult:
26-
""" Results from a spinwave calculation"""
32+
"""Results from a spinwave calculation"""
33+
2734
q_vectors: np.ndarray
2835
raw_energies: list[np.ndarray]
2936
method: list[CalculationMethod]
@@ -36,15 +43,11 @@ def spinwave_calculation(rotations: np.ndarray,
3643
magnitudes: np.ndarray,
3744
q_vectors: np.ndarray,
3845
couplings: list[Coupling]):
39-
""" Main calculation step
46+
"""Main calculation step
4047
4148
Unlike the main interface it takes indexed arrays, the meaning of the arrays is set elsewhere
4249
4350
"""
44-
45-
# Disable linting for bad variable names, because they should match the docs
46-
# pylint: disable=C0103
47-
4851
n_sites = rotations.shape[0]
4952

5053
z = rotations[:,:,0] + 1j*rotations[:,:,1] # n-by-3, complex

pyspinw/checks.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Tools for checking input shapes """
1+
"""Tools for checking input shapes"""
22

33
import functools
44

@@ -9,11 +9,11 @@
99

1010

1111
class DimensionalityError(ValueError):
12-
""" The dimensions of a numpy array don't match the specification """
12+
"""The dimensions of a numpy array don't match the specification"""
1313

1414

1515
def check_sizes(force_numpy: bool = False, **kwargs):
16-
""" Decorator to check the dimensionality of a given vector
16+
"""Decorator to check the dimensionality of a given vector
1717
1818
Example usage:
1919
check that a function has an n-by-3 vector input called `a`, and
@@ -29,7 +29,6 @@ def my_function(a, b):
2929
:param force_numpy: default=True, Convert the named arrays into numpy form if they are not already
3030
3131
"""
32-
3332
# If assertions are not enabled, give a decorator that just return the function as is
3433
if not __debug__:
3534
def identity(fun):

pyspinw/coupling.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Coupling Terms """
1+
"""Coupling Terms"""
22

33
import numpy as np
44

@@ -8,7 +8,7 @@
88

99

1010
class HeisenbergCoupling(Coupling):
11-
""" Heisenberg Coupling, which takes the form
11+
"""Heisenberg Coupling, which takes the form
1212
1313
H_ij = J_ij (S_i . S_j)
1414
@@ -26,7 +26,7 @@ def __init__(self, site_1: Identifier, site_2: Identifier, j):
2626

2727

2828
class DiagonalCoupling(Coupling):
29-
""" Diagonal coupling, which takes the form
29+
"""Diagonal coupling, which takes the form
3030
3131
H_ij = J^x_ij S^x_i S^x_j + J^y_ij S^y_i S^y_j + J^z_ij S^z_i S^z_j
3232
@@ -46,8 +46,7 @@ def __init__(self, site_1: Identifier, site_2: Identifier, j: np.ndarray):
4646

4747

4848
class XYCoupling(Coupling):
49-
50-
""" "XY" coupling, which takes the form
49+
""" "XY" coupling, which takes the form
5150
5251
H_ij = J_ij (S^x_i S^x_j + S^y_i S^y_j)
5352
@@ -65,7 +64,6 @@ def __init__(self, site_1: Identifier, site_2: Identifier, j):
6564

6665

6766
class XXZCoupling(Coupling):
68-
6967
""" "XXZ" coupling, which takes the form
7068
7169
H_ij = J_xy (S^x_i S^x_j + S^y_i S^y_j) + J_z (S^z_i S^z_j)
@@ -76,6 +74,7 @@ class XXZCoupling(Coupling):
7674
:param j_z: The coupling coefficient for the z component
7775
7876
"""
77+
7978
def __init__(self, site_1: Identifier, site_2: Identifier, j_xy, j_z):
8079
super().__init__(site_1, site_2)
8180
self._j_xy = j_xy
@@ -84,7 +83,7 @@ def __init__(self, site_1: Identifier, site_2: Identifier, j_xy, j_z):
8483

8584

8685
class IsingCoupling(Coupling):
87-
""" Ising coupling (z component only), which takes the form
86+
"""Ising coupling (z component only), which takes the form
8887
8988
H_ij = J_ij S^z_i S^z_j
9089
@@ -101,7 +100,7 @@ def __init__(self, site_1: Identifier, site_2: Identifier, j):
101100

102101

103102
class DMCoupling(Coupling):
104-
""" Dzyaloshinskii–Moriya coupling, which takes the form
103+
"""Dzyaloshinskii–Moriya coupling, which takes the form
105104
106105
H_ij = D_ij (S_i x S_j)
107106
@@ -110,6 +109,7 @@ class DMCoupling(Coupling):
110109
:param dm_vector: The vector D above
111110
112111
"""
112+
113113
@check_sizes(d_vector=(3,), force_numpy=True)
114114
def __init__(self, site_1: Identifier, site_2: Identifier, dm_vector: np.ndarray):
115115
super().__init__(site_1, site_2)

pyspinw/data/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Data used by pySpinW."""

pyspinw/experiment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Different kinds of experimental setups """
1+
"""Different kinds of experimental setups"""
22

33
#pylint: disable=W0611
44
from pyspinw._base import Experiment

pyspinw/gui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Visualisation and crystal structure editing GUI utilities."""

0 commit comments

Comments
 (0)