Skip to content

Commit

Permalink
setup pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ohjeah committed Jan 23, 2020
1 parent 8633112 commit 9052f29
Show file tree
Hide file tree
Showing 26 changed files with 133 additions and 161 deletions.
30 changes: 24 additions & 6 deletions .github/workflows/examples.yml → .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
name: Check Examples
name: Tests

on: [pull_request]
on: [push, pull_request]

jobs:
run:
Linting:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Linting
run: |
pip install pre-commit
pre-commit run --all-files
Linux:
needs: Linting
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.6, 3.7, 3.8]

steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -18,12 +34,14 @@ jobs:
- name: Install dependencies
run: |
pip install -r requirements-dev.txt
pip install papermill
- name: Execute notebooks with papermill
- name: Test with pytest
run: |
py.test test --cov=pysindy
- name: Execute feature notebook with papermill
run: |
pip install papermill
cd example
papermill --report-mode feature_overview.ipynb out.json
# for nb in *.ipynb; do papermill --report-mode $nb out.json; done
- uses: actions/cache@v1
with:
path: ~/.cache/pip
Expand Down
31 changes: 0 additions & 31 deletions .github/workflows/pythonpackage.yml

This file was deleted.

5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ repos:
rev: stable
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: master
hooks:
- id: flake8
args: ["--config=setup.cfg"]
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ Community guidelines

Contributing code
^^^^^^^^^^^^^^^^^
We welcome contributions to PySINDy. To contribute a new feature please submit a pull request. To be accepted your code should conform to PEP8 (you may choose to use flake8 to test this before submitting your pull request). Your contributed code should pass all unit tests. Upon submission of a pull request, your code will be tested automatically, but you may also choose to test it yourself by running
We welcome contributions to PySINDy. To contribute a new feature please submit a pull request. To be accepted your code should conform to PEP8 (you may choose to use flake8 to test this before submitting your pull request). Your contributed code should pass all unit tests. Upon submission of a pull request, your code will be linted and tested automatically, but you may also choose to list it yourself invoking

``pre-commit -a -v``

as well as test it yourself by running

``pytest``

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
project = "pysindy" # package name


#### no need to edit below this line ##
# no need to edit below this line

copyright = f"{datetime.datetime.now().year}, {author}"

Expand Down
3 changes: 2 additions & 1 deletion pysindy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pkg_resources import get_distribution, DistributionNotFound
from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution

try:
__version__ = get_distribution(__name__).version
Expand Down
1 change: 0 additions & 1 deletion pysindy/differentiation/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Base class for numerical differentiation methods
"""

import abc

from pysindy.utils.base import validate_input
Expand Down
4 changes: 2 additions & 2 deletions pysindy/feature_library/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .custom_library import CustomLibrary
from .feature_library import BaseFeatureLibrary
from .polynomial_library import PolynomialLibrary
from .fourier_library import FourierLibrary
from .custom_library import CustomLibrary
from .polynomial_library import PolynomialLibrary
12 changes: 6 additions & 6 deletions pysindy/feature_library/custom_library.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from pysindy.feature_library import BaseFeatureLibrary
from itertools import combinations
from itertools import combinations_with_replacement as combinations_w_r

import numpy as np
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted

import numpy as np
from itertools import combinations
from itertools import combinations_with_replacement as combinations_w_r
from pysindy.feature_library import BaseFeatureLibrary


class CustomLibrary(BaseFeatureLibrary):
Expand All @@ -23,9 +23,9 @@ class CustomLibrary(BaseFeatureLibrary):
a variable name), and output a string depiction of the respective
mathematical function applied to that variable. For example, if the
first library function is sine, the name function might return
:math:`\sin(x)` given :math:`x` as input. The function_names list must be the
:math:`\\sin(x)` given :math:`x` as input. The function_names list must be the
same length as library_functions. If no list of function names is
provided, defaults to using :math:`[ f_0(x),f_1(x), f_2(x), \ldots ]`.
provided, defaults to using :math:`[ f_0(x),f_1(x), f_2(x), \\ldots ]`.
Attributes
----------
Expand Down
17 changes: 5 additions & 12 deletions pysindy/feature_library/fourier_library.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pysindy.feature_library import BaseFeatureLibrary

import numpy as np
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted

import numpy as np
from pysindy.feature_library import BaseFeatureLibrary


class FourierLibrary(BaseFeatureLibrary):
Expand Down Expand Up @@ -38,9 +37,7 @@ class FourierLibrary(BaseFeatureLibrary):
def __init__(self, n_frequencies=1, include_sin=True, include_cos=True):
super(FourierLibrary, self).__init__()
if not (include_sin or include_cos):
raise ValueError(
"include_sin and include_cos cannot both be False"
)
raise ValueError("include_sin and include_cos cannot both be False")
if n_frequencies < 1 or not isinstance(n_frequencies, int):
raise ValueError("n_frequencies must be a positive integer")
self.n_frequencies = n_frequencies
Expand Down Expand Up @@ -68,13 +65,9 @@ def get_feature_names(self, input_features=None):
for i in range(self.n_frequencies):
for feature in input_features:
if self.include_sin:
feature_names.append(
"sin(" + str(i + 1) + " " + feature + ")"
)
feature_names.append("sin(" + str(i + 1) + " " + feature + ")")
if self.include_cos:
feature_names.append(
"cos(" + str(i + 1) + " " + feature + ")"
)
feature_names.append("cos(" + str(i + 1) + " " + feature + ")")
return feature_names

def fit(self, X, y=None):
Expand Down
11 changes: 7 additions & 4 deletions pysindy/feature_library/polynomial_library.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from itertools import chain
from itertools import combinations
from itertools import combinations_with_replacement as combinations_w_r

import numpy as np
from scipy import sparse
from itertools import chain, combinations
from itertools import combinations_with_replacement as combinations_w_r
from sklearn.preprocessing import _csr_polynomial_expansion
from sklearn.preprocessing import PolynomialFeatures
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted, FLOAT_DTYPES
from sklearn.preprocessing import _csr_polynomial_expansion
from sklearn.utils.validation import check_is_fitted
from sklearn.utils.validation import FLOAT_DTYPES

from pysindy.feature_library import BaseFeatureLibrary

Expand Down
6 changes: 3 additions & 3 deletions pysindy/optimizers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .base import BaseOptimizer
from .stlsq import STLSQ
from .sr3 import SR3
from .lasso import LASSO
from .elastic_net import ElasticNet
from .lasso import LASSO
from .sr3 import SR3
from .stlsq import STLSQ
15 changes: 4 additions & 11 deletions pysindy/optimizers/base.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
"""
Base class for SINDy optimizers.
"""

import abc

import numpy as np
from scipy import sparse
from sklearn.linear_model import LinearRegression
from sklearn.utils.validation import check_X_y
from sklearn.utils.extmath import safe_sparse_dot
from sklearn.utils.validation import check_X_y


def _rescale_data(X, y, sample_weight):
"""Rescale data so as to support sample_weight"""
n_samples = X.shape[0]
sample_weight = np.asarray(sample_weight)
if sample_weight.ndim == 0:
sample_weight = np.full(
n_samples, sample_weight, dtype=sample_weight.dtype
)
sample_weight = np.full(n_samples, sample_weight, dtype=sample_weight.dtype)
sample_weight = np.sqrt(sample_weight)
sw_matrix = sparse.dia_matrix(
(sample_weight, 0), shape=(n_samples, n_samples)
)
sw_matrix = sparse.dia_matrix((sample_weight, 0), shape=(n_samples, n_samples))
X = safe_sparse_dot(sw_matrix, X)
y = safe_sparse_dot(sw_matrix, y)
return X, y
Expand Down Expand Up @@ -95,9 +90,7 @@ def fit(self, x_, y, sample_weight=None, **reduce_kws):
-------
self : returns an instance of self
"""
x_, y = check_X_y(
x_, y, accept_sparse=[], y_numeric=True, multi_output=False
)
x_, y = check_X_y(x_, y, accept_sparse=[], y_numeric=True, multi_output=False)

x, y, X_offset, y_offset, X_scale = self._preprocess_data(
x_,
Expand Down
7 changes: 1 addition & 6 deletions pysindy/optimizers/elastic_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ class ElasticNet(BaseOptimizer):
"""

def __init__(
self,
alpha=1.0,
l1_ratio=0.5,
max_iter=1000,
elastic_net_kw={},
**kwargs
self, alpha=1.0, l1_ratio=0.5, max_iter=1000, elastic_net_kw={}, **kwargs
):
super(ElasticNet, self).__init__(**kwargs)

Expand Down
15 changes: 4 additions & 11 deletions pysindy/optimizers/sr3.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import warnings

import numpy as np
from scipy.linalg import cho_factor, cho_solve
from scipy.linalg import cho_factor
from scipy.linalg import cho_solve
from sklearn.exceptions import ConvergenceWarning

from pysindy.optimizers import BaseOptimizer
Expand Down Expand Up @@ -54,13 +55,7 @@ class SR3(BaseOptimizer):
"""

def __init__(
self,
threshold=0.1,
nu=1.0,
tol=1e-5,
thresholder="l0",
max_iter=30,
**kwargs
self, threshold=0.1, nu=1.0, tol=1e-5, thresholder="l0", max_iter=30, **kwargs
):
super(SR3, self).__init__(**kwargs)

Expand Down Expand Up @@ -115,9 +110,7 @@ def _reduce(self, x, y):

# Precompute some objects for upcoming least-squares solves.
# Assumes that self.nu is fixed throughout optimization procedure.
cho = cho_factor(
np.dot(x.T, x) + np.diag(np.full(x.shape[1], 1.0 / self.nu))
)
cho = cho_factor(np.dot(x.T, x) + np.diag(np.full(x.shape[1], 1.0 / self.nu)))
x_transpose_y = np.dot(x.T, y)

for _ in range(self.max_iter):
Expand Down
10 changes: 3 additions & 7 deletions pysindy/optimizers/stlsq.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import warnings

import numpy as np
from sklearn.linear_model import ridge_regression
from sklearn.exceptions import ConvergenceWarning
from sklearn.linear_model import ridge_regression

from pysindy.optimizers import BaseOptimizer

Expand Down Expand Up @@ -42,9 +42,7 @@ class STLSQ(BaseOptimizer):
weight vector have not been masked out.
"""

def __init__(
self, threshold=0.1, alpha=0.0, max_iter=20, ridge_kw=None, **kwargs
):
def __init__(self, threshold=0.1, alpha=0.0, max_iter=20, ridge_kw=None, **kwargs):
super(STLSQ, self).__init__(**kwargs)

if threshold < 0:
Expand Down Expand Up @@ -108,9 +106,7 @@ def _reduce(self, x, y):
break

coef = self._regress(x[:, ind], y)
coef, ind = self._sparse_coefficients(
n_features, ind, coef, self.threshold
)
coef, ind = self._sparse_coefficients(n_features, ind, coef, self.threshold)

if sum(ind) == n_features_selected or self._no_change():
# could not (further) select important features
Expand Down
Loading

0 comments on commit 9052f29

Please sign in to comment.