Skip to content

Commit

Permalink
Refactor code, udpate github actions. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Jan 5, 2024
1 parent cf43b8d commit 325c03d
Show file tree
Hide file tree
Showing 20 changed files with 408 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ jobs:
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_PASSWORD }}
password: ${{ secrets.PYPI_PASSWORD }}
13 changes: 9 additions & 4 deletions .github/workflows/pypi-test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: test the library
name: Test the library

on:
push:
Expand All @@ -13,13 +13,18 @@ jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10', '3.11', '3.12' ]

name: Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -32,4 +37,4 @@ jobs:
# # flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with tox
run: |
tox
tox
52 changes: 52 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
exclude: '^docs/conf.py'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
- id: check-ast
- id: check-json
- id: check-merge-conflict
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: requirements-txt-fixer
- id: mixed-line-ending
args: ['--fix=auto'] # replace 'auto' with 'lf' to enforce Linux/Mac line endings or 'crlf' for Windows

- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
hooks:
- id: docformatter
additional_dependencies: [tomli]
args: [--in-place, --wrap-descriptions=120, --wrap-summaries=120]
# --config, ./pyproject.toml

- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black
language_version: python3

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.9
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

## If like to embrace black styles even in the docs:
# - repo: https://github.com/asottile/blacken-docs
# rev: v1.13.0
# hooks:
# - id: blacken-docs
# additional_dependencies: [black]

## Check for misspells in documentation files:
# - repo: https://github.com/codespell-project/codespell
# rev: v2.2.5
# hooks:
# - id: codespell
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Changelog


## Version 0.2.0
## Version 0.2.0
- Support multi apply

## Version 0.1 (development)

- first release
- performs some basic operations over numpy or scipy matrices.
- performs some basic operations over numpy or scipy matrices.
- provides apply method so user can extend the underlying logic to any function
9 changes: 9 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ def setup(app):
# If this is True, todo emits a warning for each TODO entries. The default is False.
todo_emit_warnings = True

autodoc_default_options = {
'special-members': True,
'undoc-members': False,
'exclude-members': '__weakref__, __dict__, __str__, __module__, __init__'
}

autosummary_generate = True
autosummary_imported_members = True


# -- Options for HTML output -------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
furo
# Requirements file for ReadTheDocs, check .readthedocs.yml.
# To build the module reference correctly, make sure every external package
# under `install_requires` in `setup.cfg` is also listed here!
# sphinx_rtd_theme
recommonmark
sphinx>=3.2.1
furo
4 changes: 2 additions & 2 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ apply(nz_func, mat, axis=1)

## Multiple functions

`mopsy` also supports applying multiple functions at the same time.
`mopsy` also supports applying multiple functions at the same time.

```python
from mopsy import multi_apply
Expand All @@ -73,4 +73,4 @@ tmat_wrow = append_row(mat, np.array([0, 0, 0, 0, 0]))
tmat_wcol = append_col(mat, np.array([[0], [0], [0], [0], [0]]))
```

That's all for today!
That's all for today!
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,18 @@ build-backend = "setuptools.build_meta"
[tool.setuptools_scm]
# See configuration details in https://github.com/pypa/setuptools_scm
version_scheme = "no-guess-dev"

[tool.ruff]
line-length = 120
src = ["src"]
exclude = ["tests"]
extend-ignore = ["F821"]

[tool.ruff.pydocstyle]
convention = "google"

[tool.ruff.per-file-ignores]
"__init__.py" = ["E402", "F401"]

[tool.black]
force-exclude = "__init__.py"
10 changes: 4 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""
Setup file for mopsy.
Use setup.cfg to configure your project.
"""Setup file for mopsy. Use setup.cfg to configure your project.
This file was generated with PyScaffold 4.1.1.
PyScaffold helps you to put up the scaffold of your new Python project.
Learn more under: https://pyscaffold.org/
This file was generated with PyScaffold 4.1.1.
PyScaffold helps you to put up the scaffold of your new Python project.
Learn more under: https://pyscaffold.org/
"""
from setuptools import setup

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 *
from .adders import *
from .helpers import * # noqa: F403
from .adders import * # noqa: F403
71 changes: 42 additions & 29 deletions src/mopsy/adders.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from .sops import Sops
from .checkutils import check_axis

from typing import Union

import scipy as sp
import numpy as np
import scipy as sp

from .checkutils import check_axis
from .sops import Sops

__author__ = "jkanche"
__copyright__ = "jkanche"
Expand All @@ -14,56 +14,69 @@
def append_row(
mat: sp.sparse.spmatrix, row: Union[sp.sparse.spmatrix, np.ndarray]
) -> sp.sparse.spmatrix:
"""A generic append function for sparse matrices
"""A generic append function for sparse matrices.
Args:
mat (scipy.sparse.spmatrix): sparse matrix
row (Union[sp.sparse.spmatrix, np.ndarray]): rows to append
mat:
Sparse matrix.
row:
Rows to append.
Raises:
TypeError: if axis is neither 0 nor 1
TypeError:
If axis is neither 0 nor 1.
Returns:
scipy.sparse.spmatrix: new matrix
A new sparse matrix, usually the same type as the input matrix.
"""
return sparse_append(mat=mat, rowOrCols=row, axis=0)
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:
"""A generic append function for sparse matrices
"""A generic append function for sparse matrices.
Args:
mat (scipy.sparse.spmatrix): sparse matrix
col (Union[sp.sparse.spmatrix, np.ndarray]): columns to append
mat:
Sparse matrix.
col:
Columns to append.
Raises:
TypeError: if axis is neither 0 nor 1
TypeError:
If axis is neither 0 nor 1.
Returns:
scipy.sparse.spmatrix: new matrix
A new sparse matrix, usually the same type as the input matrix.
"""
return sparse_append(mat=mat, rowOrCols=col, axis=1)
return sparse_append(mat=mat, row_or_column=col, axis=1)


def sparse_append(
mat: sp.sparse.spmatrix,
rowOrCols: Union[sp.sparse.spmatrix, np.ndarray],
row_or_column: Union[sp.sparse.spmatrix, np.ndarray],
axis: Union[int, bool],
) -> sp.sparse.spmatrix:
"""A generic append function for sparse matrices
"""A generic append function for sparse matrices.
Args:
mat (scipy.sparse.spmatrix): sparse matrix
rowOrCols (Union[sp.sparse.spmatrix, np.ndarray]): rows to append
axis (Union[int, bool]): 0 for rows, 1 for columns.
mat:
Sparse matrix.
row_or_column:
Rows or columns to append.
axis:
0 for rows, 1 for columns.
Raises:
TypeError: if axis is neither 0 nor 1
TypeError:
If axis is neither 0 nor 1.
Returns:
scipy.sparse.spmatrix: new matrix
A new sparse matrix, usually the same type as the input matrix.
"""

if not isinstance(mat, sp.sparse.spmatrix):
Expand All @@ -75,19 +88,19 @@ def sparse_append(

new_mat = None
if axis == 0:
if mat.shape[0] != rowOrCols.shape[0]:
if mat.shape[0] != row_or_column.shape[0]:
raise TypeError(
f"matrix and new row do not have the same length. matrix: {mat.shape[0]}, row: {row.shape[0]}"
"Matrix and new row do not have the same shape along the first dimension."
)

new_mat = sp.sparse.vstack([mat, rowOrCols])
new_mat = sp.sparse.vstack([mat, row_or_column])
else:
if mat.shape[1] != rowOrCols.shape[0]:
if mat.shape[1] != row_or_column.shape[0]:
raise TypeError(
f"matrix and new row do not have the same length. matrix: {mat.shape[1]}, row: {row.shape[1]}"
"Matrix and new row do not have the same shape along the second dimension."
)

new_mat = sp.sparse.hstack([mat, rowOrCols])
new_mat = sp.sparse.hstack([mat, row_or_column])

if new_mat is None:
raise Exception("This should never happen")
Expand Down
10 changes: 6 additions & 4 deletions src/mopsy/checkutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@


def check_axis(axis: Union[int, bool]):
"""Check if axis has a correct value
"""Check if axis has a correct value.
Args:
axis (Union[int, bool]): axis, 0 for rows, 1 for columns
axis:
Axis, 0 for rows, 1 for columns
Raises:
TypeError: if axis is neither 0 nor 1
ValueError:
If axis is neither 0 nor 1
"""
if not (axis == 0 or axis == 1):
raise TypeError(f"axis is neither 0 or 1, provided {axis}")
raise ValueError(f"'axis' is neither 0 or 1, provided {axis}.")
Loading

0 comments on commit 325c03d

Please sign in to comment.