Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more lints to Ruff for better code quality. #621

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
args: ["--autofix", "--no-ensure-ascii"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.9
rev: v0.1.11
hooks:
- id: ruff
args: ["--fix"]
Expand Down
24 changes: 12 additions & 12 deletions benchmarks/benchmark_coo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

class MatrixMultiplySuite:
def setup(self):
np.random.seed(0)
self.x = sparse.random((100, 100), density=0.01)
self.y = sparse.random((100, 100), density=0.01)
rng = np.random.default_rng(0)
self.x = sparse.random((100, 100), density=0.01, random_state=rng)
self.y = sparse.random((100, 100), density=0.01, random_state=rng)

self.x @ self.y # Numba compilation

Expand All @@ -17,9 +17,9 @@ def time_matmul(self):

class ElemwiseSuite:
def setup(self):
np.random.seed(0)
self.x = sparse.random((100, 100, 100), density=0.01)
self.y = sparse.random((100, 100, 100), density=0.01)
rng = np.random.default_rng(0)
self.x = sparse.random((100, 100, 100), density=0.01, random_state=rng)
self.y = sparse.random((100, 100, 100), density=0.01, random_state=rng)

self.x + self.y # Numba compilation

Expand All @@ -32,9 +32,9 @@ def time_mul(self):

class ElemwiseBroadcastingSuite:
def setup(self):
np.random.seed(0)
self.x = sparse.random((100, 1, 100), density=0.01)
self.y = sparse.random((100, 100), density=0.01)
rng = np.random.default_rng(0)
self.x = sparse.random((100, 1, 100), density=0.01, random_state=rng)
self.y = sparse.random((100, 100), density=0.01, random_state=rng)

def time_add(self):
self.x + self.y
Expand All @@ -45,9 +45,9 @@ def time_mul(self):

class IndexingSuite:
def setup(self):
np.random.seed(0)
self.index = np.random.randint(0, 100, 50)
self.x = sparse.random((100, 100, 100), density=0.01)
rng = np.random.default_rng(0)
self.index = rng.integers(0, 100, 50)
self.x = sparse.random((100, 100, 100), density=0.01, random_state=rng)

# Numba compilation
self.x[5]
Expand Down
24 changes: 12 additions & 12 deletions benchmarks/benchmark_gcxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

class MatrixMultiplySuite:
def setup(self):
np.random.seed(0)
self.x = sparse.random((100, 100), density=0.01, format="gcxs")
self.y = sparse.random((100, 100), density=0.01, format="gcxs")
rng = np.random.default_rng(0)
self.x = sparse.random((100, 100), density=0.01, format="gcxs", random_state=rng)
self.y = sparse.random((100, 100), density=0.01, format="gcxs", random_state=rng)

self.x @ self.y # Numba compilation

Expand All @@ -17,9 +17,9 @@ def time_matmul(self):

class ElemwiseSuite:
def setup(self):
np.random.seed(0)
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs")
self.y = sparse.random((100, 100, 100), density=0.01, format="gcxs")
rng = np.random.default_rng(0)
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs", random_state=rng)
self.y = sparse.random((100, 100, 100), density=0.01, format="gcxs", random_state=rng)

self.x + self.y # Numba compilation

Expand All @@ -32,9 +32,9 @@ def time_mul(self):

class ElemwiseBroadcastingSuite:
def setup(self):
np.random.seed(0)
self.x = sparse.random((100, 1, 100), density=0.01, format="gcxs")
self.y = sparse.random((100, 100), density=0.01, format="gcxs")
rng = np.random.default_rng(0)
self.x = sparse.random((100, 1, 100), density=0.01, format="gcxs", random_state=rng)
self.y = sparse.random((100, 100), density=0.01, format="gcxs", random_state=rng)

def time_add(self):
self.x + self.y
Expand All @@ -45,9 +45,9 @@ def time_mul(self):

class IndexingSuite:
def setup(self):
np.random.seed(0)
self.index = np.random.randint(0, 100, 50)
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs")
rng = np.random.default_rng(0)
self.index = rng.integers(0, 100, 50)
self.x = sparse.random((100, 100, 100), density=0.01, format="gcxs", random_state=rng)

# Numba compilation
self.x[5]
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/benchmark_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class Matmul_Sparse:
params = (["coo", "gcxs"], [0, 1, None])

def setup(self, p, dens_arg):
np.random.seed(0)
self.x = sparse.random((100, 100), density=0.01, format=p)
self.y = sparse.random((100, 100), density=0.01, format=p)
rng = np.random.default_rng(0)
self.x = sparse.random((100, 100), density=0.01, format=p, random_state=rng)
self.y = sparse.random((100, 100), density=0.01, format=p, random_state=rng)

if dens_arg == 0:
self.x = self.x.todense()
Expand Down
18 changes: 9 additions & 9 deletions benchmarks/benchmark_tensordot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class TensordotSuiteDenseSparse:
"""

def setup(self):
np.random.seed(0)
self.n = np.random.random((100, 100))
self.s = sparse.random((100, 100, 100, 100), density=0.01)
rng = np.random.default_rng(0)
self.n = rng.random((100, 100))
self.s = sparse.random((100, 100, 100, 100), density=0.01, random_state=rng)

def time_dense(self):
sparse.tensordot(self.n, self.s, axes=([0, 1], [0, 2]))
Expand All @@ -28,9 +28,9 @@ class TensordotSuiteSparseSparse:
"""

def setup(self):
np.random.seed(0)
self.s1 = sparse.random((100, 100), density=0.01)
self.s2 = sparse.random((100, 100, 100, 100), density=0.01)
rng = np.random.default_rng(0)
self.s1 = sparse.random((100, 100), density=0.01, random_state=rng)
self.s2 = sparse.random((100, 100, 100, 100), density=0.01, random_state=rng)

def time_dense(self):
sparse.tensordot(self.s1, self.s2, axes=([0, 1], [0, 2]), return_type=np.ndarray)
Expand All @@ -46,9 +46,9 @@ class TensordotSuiteSparseDense:
"""

def setup(self):
np.random.seed(0)
self.s = sparse.random((100, 100, 100, 100), density=0.01)
self.n = np.random.random((100, 100))
rng = np.random.default_rng(0)
self.s = sparse.random((100, 100, 100, 100), density=0.01, random_state=rng)
self.n = rng.random((100, 100))

def time_dense(self):
sparse.tensordot(self.s, self.n, axes=([0, 1], [0, 1]))
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ include = ["sparse", "sparse.*"]
version_file = "sparse/_version.py"

[tool.ruff]
select = ["F", "E", "W", "I", "B", "UP"]
select = ["F", "E", "W", "I", "B", "UP", "YTT", "BLE", "C4", "T10", "ISC", "ICN", "PIE", "PYI", "RSE", "RET", "SIM", "PGH", "FLY", "NPY", "PERF"]
exclude = ["sparse/_version.py"]
line-length = 120

[tool.ruff.lint.isort.sections]
numpy = ["numpy", "numpy.*", "scipy", "scipy.*"]

[tool.ruff.format]
quote-style = "double"
docstring-code-format = true

[tool.ruff.lint.isort]
section-order = [
"future",
Expand Down
Loading
Loading