Skip to content

Commit

Permalink
Merge pull request #44 from felixriese/dev-updates
Browse files Browse the repository at this point in the history
Dev updates
  • Loading branch information
felixriese committed Jul 22, 2023
2 parents c58a83a + d914b88 commit a16f748
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 54 deletions.
31 changes: 18 additions & 13 deletions .github/workflows/tests_and_linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,32 @@ jobs:
- name: List of installed packages
run: |
pip list
- name: Lint with flake8 (with stopping)
if: ${{ matrix.python-version == '3.10' }}
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Lint with flake8 (without stopping)
if: ${{ matrix.python-version == '3.10' }}
run: |
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Check formatting (black, isort)
if: ${{ matrix.python-version == '3.10' }}
run: |
black --check .
isort --check .
- name: Test with pytest
if: ${{ matrix.python-version != '3.9' }}
if: ${{ matrix.python-version != '3.10' }}
run: |
pytest --nbval
- name: Test with pytest and Codecov
if: ${{ matrix.python-version == '3.9' }}
if: ${{ matrix.python-version == '3.10' }}
run: |
pip install pytest-cov
pytest --nbval --cov=susi --cov-report=xml --cov-config .coveragerc
- name: Upload coverage to Codecov
if: ${{ matrix.python-version == '3.9' }}
if: ${{ matrix.python-version == '3.10' }}
uses: codecov/codecov-action@v1
with:
verbose: true
- name: Lint with flake8 (with stopping)
if: ${{ matrix.python-version == '3.9' }}
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Lint with flake8 (without stopping)
if: ${{ matrix.python-version == '3.9' }}
run: |
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
24 changes: 13 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ default_language_version:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: no-commit-to-branch
args: ['--branch', 'main']
- id: check-added-large-files
args: ["--maxkb=1000"]
- id: detect-private-key
- id: check-merge-conflict
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
language_version: python3.10
args: [--config=pyproject.toml]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
rev: 6.0.0
hooks:
- id: flake8
args: ["--ignore=E501,W503"]
- repo: https://github.com/pycqa/pydocstyle
rev: 6.1.1
rev: 6.3.0
hooks:
- id: pydocstyle
additional_dependencies: [toml]
- repo: local
hooks:
- id: pytest
name: Run tests
entry: pytest --nbval
language: system
types: [python]
pass_filenames: false
8 changes: 2 additions & 6 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
-r requirements.txt
-r test-requirements.txt

# for formatting
black==22.10.0
isort==5.12.0

# for the documentation
sphinx==7.0.1
sphinx-autobuild>=0.7.1
numpydoc==1.5.0
sphinx_rtd_theme==1.2.2

# for linting
mypy==0.991
pre-commit==2.20.0
mypy==1.4.1
pre-commit==3.3.3
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ license_file = "LICENSE"

[tool.black]
line-length = 79
target-version = ['py37', 'py38', 'py39', 'py310']
target-version = ['py38', 'py39', 'py310', 'py311']
include = '\.pyi?$'
exclude = '''
/(
Expand All @@ -24,6 +24,11 @@ exclude = '''

[tool.isort]
profile = "black"
line_length = 79

[tool.mypy]
python_version = "3.11"
ignore_missing_imports = true

[tool.pydocstyle]
convention = "numpy"
Expand Down
4 changes: 1 addition & 3 deletions susi/SOMClassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from scipy.special import softmax
from sklearn.base import ClassifierMixin
from sklearn.preprocessing import LabelBinarizer
from sklearn.utils.validation import check_array, check_is_fitted
from sklearn.utils import class_weight
from sklearn.utils.validation import check_array, check_is_fitted
from tqdm import tqdm

from .SOMEstimator import SOMEstimator
Expand Down Expand Up @@ -224,7 +224,6 @@ def _init_super_som(self) -> None:

# initialize classification SOM
if self.init_mode_supervised == "majority":

# define dtype
if self.class_dtype_ in [str, np.str_]:
init_dtype = "U" + str(
Expand Down Expand Up @@ -376,7 +375,6 @@ def _modify_weight_matrix_supervised(
"""
if self.train_mode_supervised == "online":

# require valid values for true_vector and learning_rate
if not isinstance(true_vector, np.ndarray) or not isinstance(
learning_rate, float
Expand Down
5 changes: 0 additions & 5 deletions susi/SOMClustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def _init_unsuper_som(self) -> None:
)

elif self.init_mode_unsupervised == "pca":

# fixed number of components
pca = PCA(n_components=2, random_state=self.random_state)

Expand Down Expand Up @@ -237,7 +236,6 @@ def _train_unsupervised_som(self) -> None:
desc="unsuper",
**self.tqdm_params_,
):

# select one input vector & calculate best matching unit (BMU)
dp = np.random.randint(low=0, high=len(self.X_))
bmu_pos = self.get_bmu(self.X_[dp], self.unsuper_som_)
Expand Down Expand Up @@ -267,7 +265,6 @@ def _train_unsupervised_som(self) -> None:
desc="unsuper",
**self.tqdm_params_,
):

# calculate BMUs
bmus = self.get_bmus(self.X_)

Expand Down Expand Up @@ -813,7 +810,6 @@ def _calc_u_matrix_distances(self) -> None:
for u_node in itertools.product(
range(self.n_rows * 2 - 1), range(self.n_columns * 2 - 1)
):

# neighbor vector
nb = (0, 0)

Expand Down Expand Up @@ -845,7 +841,6 @@ def _calc_u_matrix_means(self) -> None:
for u_node in itertools.product(
range(self.n_rows * 2 - 1), range(self.n_columns * 2 - 1)
):

if not (u_node[0] % 2) and not (u_node[1] % 2):
# SOM nodes -> mean over 2-4 values

Expand Down
3 changes: 0 additions & 3 deletions susi/SOMEstimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ def _modify_weight_matrix_supervised(
"""
if self.train_mode_supervised == "online":

# require valid values for true_vector and learning_rate
if not isinstance(true_vector, np.ndarray) or not isinstance(
learning_rate, float
Expand Down Expand Up @@ -397,7 +396,6 @@ def _train_supervised_som(self):
desc="super",
**self.tqdm_params_,
):

# select one input vector & calculate best matching unit (BMU)
dp_index = self._get_random_datapoint_index()
bmu_pos = self.bmus_[dp_index]
Expand Down Expand Up @@ -426,7 +424,6 @@ def _train_supervised_som(self):
desc="super",
**self.tqdm_params_,
):

# calculate BMUs with the unsupervised (!) SOM
bmus = self.get_bmus(self.X_)

Expand Down
4 changes: 3 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
black==23.7.0
codecov>=2.1.10
coverage>=5.3
flake8==5.0.4
flake8==6.0.0
isort==5.12.0
nbval>=0.9.5
pytest>=7.2.0
pytest-cov>=2.10.1
Expand Down
3 changes: 1 addition & 2 deletions tests/test_MultiOutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
)
import susi

import susi # noqa

# define test dataset
cali = fetch_california_housing()
Expand Down
7 changes: 4 additions & 3 deletions tests/test_SOMClassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
python -m pytest tests/test_SOMClassifier.py
"""
import pytest
import itertools
import os
import sys

import numpy as np
import itertools
import pytest
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
Expand All @@ -17,7 +18,7 @@
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
)
import susi
import susi # noqa

# define test dataset
iris = load_iris()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_SOMEstimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
python -m pytest tests/test_SOMEstimator.py
"""
import pytest
import os
import sys

import numpy as np
import pytest

sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
)
import susi
import susi # noqa


@pytest.mark.parametrize(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_SOMRegressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
python -m pytest tests/test_SOMRegressor.py
"""
import pytest
import itertools
import os
import sys
import itertools

import numpy as np
import pytest
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
Expand All @@ -17,7 +18,7 @@
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
)
import susi
import susi # noqa


@pytest.fixture
Expand Down Expand Up @@ -124,7 +125,6 @@ def test_init_super_som_regressor(X, y, init_mode):
def test_predict(
training_data, train_mode_unsupervised, train_mode_supervised
):

X_train, X_test, y_train, y_test = training_data

som_reg = susi.SOMRegressor(
Expand Down

0 comments on commit a16f748

Please sign in to comment.