Skip to content
Open
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 @@ -33,7 +33,7 @@ repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.15.6
rev: v0.16.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
if __name__ == "__main__":
try:
setup(use_scm_version={"version_scheme": "no-guess-dev"})
except: # noqa
except:
print(
"\n\nAn error occurred while building the project, "
"please ensure you have the most updated version of setuptools, "
Expand Down
4 changes: 2 additions & 2 deletions src/mopsy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
finally:
del version, PackageNotFoundError

from .helpers import * # noqa: F403
from .adders import * # noqa: F403
from .adders import *
from .helpers import *
10 changes: 4 additions & 6 deletions src/mopsy/adders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Union

import numpy as np
import scipy as sp

Expand All @@ -11,7 +9,7 @@
__license__ = "MIT"


def append_row(mat: sp.sparse.spmatrix, row: Union[sp.sparse.spmatrix, np.ndarray]) -> sp.sparse.spmatrix:
def append_row(mat: sp.sparse.spmatrix, row: sp.sparse.spmatrix | np.ndarray) -> sp.sparse.spmatrix:
"""A generic append function for sparse matrices.

Args:
Expand All @@ -30,7 +28,7 @@ def append_row(mat: sp.sparse.spmatrix, row: Union[sp.sparse.spmatrix, np.ndarra
return sparse_append(mat=mat, row_or_column=row, axis=0)


def append_col(mat: sp.sparse.spmatrix, col: Union[sp.sparse.spmatrix, np.ndarray]) -> sp.sparse.spmatrix:
def append_col(mat: sp.sparse.spmatrix, col: sp.sparse.spmatrix | np.ndarray) -> sp.sparse.spmatrix:
"""A generic append function for sparse matrices.

Args:
Expand All @@ -52,8 +50,8 @@ def append_col(mat: sp.sparse.spmatrix, col: Union[sp.sparse.spmatrix, np.ndarra

def sparse_append(
mat: sp.sparse.spmatrix,
row_or_column: Union[sp.sparse.spmatrix, np.ndarray],
axis: Union[int, bool],
row_or_column: sp.sparse.spmatrix | np.ndarray,
axis: int | bool,
) -> sp.sparse.spmatrix:
"""A generic append function for sparse matrices.

Expand Down
3 changes: 1 addition & 2 deletions src/mopsy/checkutils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# this file only exists because of circular imports error

from typing import Union

__author__ = "jkanche"
__copyright__ = "jkanche"
__license__ = "MIT"


def check_axis(axis: Union[int, bool]):
def check_axis(axis: int | bool):
"""Check if axis has a correct value.

Args:
Expand Down
22 changes: 11 additions & 11 deletions src/mopsy/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Sequence
from statistics import mean, median
from typing import Callable, Sequence, Union

import numpy
import scipy
Expand All @@ -13,7 +13,7 @@


def colsum(
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
mat: numpy.ndarray | scipy.sparse.spmatrix,
group: Sequence = None,
non_zero: bool = False,
) -> numpy.ndarray:
Expand All @@ -39,7 +39,7 @@ def colsum(


def rowsum(
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
mat: numpy.ndarray | scipy.sparse.spmatrix,
group: Sequence = None,
non_zero: bool = False,
) -> numpy.ndarray:
Expand All @@ -64,7 +64,7 @@ def rowsum(


def colmean(
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
mat: numpy.ndarray | scipy.sparse.spmatrix,
group: Sequence = None,
non_zero: bool = False,
) -> numpy.ndarray:
Expand All @@ -89,7 +89,7 @@ def colmean(


def rowmean(
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
mat: numpy.ndarray | scipy.sparse.spmatrix,
group: Sequence = None,
non_zero: bool = False,
) -> numpy.ndarray:
Expand All @@ -114,7 +114,7 @@ def rowmean(


def colmedian(
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
mat: numpy.ndarray | scipy.sparse.spmatrix,
group: Sequence = None,
non_zero: bool = False,
) -> numpy.ndarray:
Expand All @@ -139,7 +139,7 @@ def colmedian(


def rowmedian(
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
mat: numpy.ndarray | scipy.sparse.spmatrix,
group: Sequence = None,
non_zero: bool = False,
) -> numpy.ndarray:
Expand All @@ -165,8 +165,8 @@ def rowmedian(

def apply(
func: Callable,
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
axis: Union[int, bool],
mat: numpy.ndarray | scipy.sparse.spmatrix,
axis: int | bool,
group: Sequence = None,
non_zero: bool = False,
):
Expand Down Expand Up @@ -201,8 +201,8 @@ def apply(

def multi_apply(
funcs: Sequence[Callable],
mat: Union[numpy.ndarray, scipy.sparse.spmatrix],
axis: Union[int, bool],
mat: numpy.ndarray | scipy.sparse.spmatrix,
axis: int | bool,
group: Sequence = None,
non_zero: bool = False,
):
Expand Down
15 changes: 8 additions & 7 deletions src/mopsy/mops.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Callable, Sequence
from itertools import groupby
from typing import Any, Callable, Optional, Sequence, Tuple, Union
from typing import Any

import numpy as np

Expand Down Expand Up @@ -41,7 +42,7 @@ def groupby_indices(self, group: Sequence) -> dict:
"""
return {k: [x[0] for x in v] for k, v in groupby(sorted(enumerate(group), key=lambda x: x[1]), lambda x: x[1])}

def _apply(self, func: Callable[[list], Any], axis: Union[int, bool]):
def _apply(self, func: Callable[[list], Any], axis: int | bool):
if self.non_zero:

def funcwrapper(mat):
Expand All @@ -56,8 +57,8 @@ def apply(
self,
func: Callable[[list], Any],
group: Sequence = None,
axis: Union[int, bool] = 0,
) -> Tuple[np.ndarray, Optional[Sequence]]:
axis: int | bool = 0,
) -> tuple[np.ndarray, Sequence | None]:
"""Apply a function to groups along an axis.

Args:
Expand Down Expand Up @@ -93,7 +94,7 @@ def apply(
rgroups.append(kcat)
result = np.stack(result, axis=axis)
except Exception as e:
raise Exception(f"Error: applying function: {str(e)}")
raise Exception(f"Error: applying function: {e!s}")

return result, rgroups

Expand All @@ -102,7 +103,7 @@ def multi_apply(
funcs: Sequence[Callable[[list], Any]],
group: list = None,
axis: int = 0,
) -> Tuple[np.ndarray, Optional[Sequence]]:
) -> tuple[np.ndarray, Sequence | None]:
"""Apply multiple functions, the first axis of the ndarray specifies the results of the inputs functions in the
same order.

Expand Down Expand Up @@ -145,6 +146,6 @@ def multi_apply(
result = nmats

except Exception as e:
raise Exception(f"Error: applying function: {str(e)}")
raise Exception(f"Error: applying function: {e!s}")

return result, rgroups
4 changes: 2 additions & 2 deletions src/mopsy/nops.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterator, Sequence, Tuple, Union
from collections.abc import Iterator, Sequence

import numpy as np

Expand All @@ -15,7 +15,7 @@ class Nops(Mops):
def __init__(self, mat: np.ndarray, non_zero: bool = False) -> None:
super().__init__(mat, non_zero=non_zero)

def iter(self, group: Sequence[str] = None, axis: Union[int, bool] = 0) -> Iterator[Tuple]:
def iter(self, group: Sequence[str] = None, axis: int | bool = 0) -> Iterator[tuple]:
"""Iterator over groups and an axis.

Args:
Expand Down
15 changes: 8 additions & 7 deletions src/mopsy/sops.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Callable, Iterator, Sequence
from statistics import mean
from typing import Any, Callable, Iterator, Optional, Sequence, Tuple, Union
from typing import Any

import numpy as np
from scipy import sparse as sp
Expand Down Expand Up @@ -28,7 +29,7 @@ def __init__(self, mat: sp.spmatrix, non_zero: bool = False) -> None:
"""
super().__init__(mat, non_zero=non_zero)

def iter(self, group: list = None, axis: Union[int, bool] = 0) -> Iterator[Tuple]:
def iter(self, group: list = None, axis: int | bool = 0) -> Iterator[tuple]:
"""Iterator over groups and an axis.

Args:
Expand Down Expand Up @@ -58,7 +59,7 @@ def iter(self, group: list = None, axis: Union[int, bool] = 0) -> Iterator[Tuple
else:
yield (k, Sops(mat[:, v], self.non_zero))

def _apply(self, func: Callable[[list], Any], axis: Union[int, bool] = 0) -> np.ndarray:
def _apply(self, func: Callable[[list], Any], axis: int | bool = 0) -> np.ndarray:
mat = self.matrix.tocsc() if axis == 0 else self.matrix.tocsr()
if self.non_zero:
# reduction along an axis
Expand Down Expand Up @@ -95,8 +96,8 @@ def apply(
self,
func: Callable[[list], Any],
group: Sequence = None,
axis: Union[int, bool] = 0,
) -> Tuple[np.ndarray, Optional[Sequence]]:
axis: int | bool = 0,
) -> tuple[np.ndarray, Sequence | None]:
"""Apply a function to groups along an axis.

Args:
Expand Down Expand Up @@ -129,8 +130,8 @@ def multi_apply(
self,
funcs: Sequence[Callable[[list], Any]],
group: list = None,
axis: Union[int, bool] = 0,
) -> Tuple[np.ndarray, Optional[Sequence]]:
axis: int | bool = 0,
) -> tuple[np.ndarray, Sequence | None]:
"""Apply multiple functions, the first axis of the ndarray specifies the results of the inputs functions in the
same order.

Expand Down
4 changes: 1 addition & 3 deletions src/mopsy/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Union

import numpy as np
import scipy.sparse as sp

Expand All @@ -11,7 +9,7 @@
__license__ = "MIT"


def get_matrix_type(mat: Union[np.ndarray, sp.spmatrix], non_zero: bool = False):
def get_matrix_type(mat: np.ndarray | sp.spmatrix, non_zero: bool = False):
"""Get an internal matrix state.

Args:
Expand Down
Loading