Skip to content

Commit 38922c0

Browse files
authored
[MRG] pre-commit with ruff,codespell,yamlint (#681)
* add file and lint * add ignore words * update tests * codespell commit * test should pass * fix demo flow * upate release file and documentaion for conribution * try to fix doctests * fix tests * remove tets on 3.8 * try other mlegacy option * test correctly
1 parent ea841c7 commit 38922c0

File tree

175 files changed

+20385
-9252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+20385
-9252
lines changed

.github/CONTRIBUTING.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ GitHub, clone, and develop on a branch. Steps:
2323
$ cd POT
2424
```
2525

26+
2. Install pre-commit hooks to ensure that your code is properly formatted:
27+
28+
```bash
29+
$ pip install pre-commit
30+
$ pre-commit install
31+
```
32+
33+
This will install the pre-commit hooks that will run on every commit. If the hooks fail, the commit will be aborted.
34+
2635
3. Create a ``feature`` branch to hold your development changes:
2736

2837
```bash
@@ -56,7 +65,7 @@ Pull Request Checklist
5665
We recommended that your contribution complies with the
5766
following rules before you submit a pull request:
5867

59-
- Follow the PEP8 Guidelines.
68+
- Follow the PEP8 Guidelines which should be handles automatically by pre-commit.
6069

6170
- If your pull request addresses an issue, please use the pull request title
6271
to describe the issue and mention the issue number in the pull request description. This will make sure a link back to the original issue is
@@ -101,27 +110,19 @@ following rules before you submit a pull request:
101110
You can also check for common programming errors with the following
102111
tools:
103112

104-
105-
- No pyflakes warnings, check with:
113+
- All lint checks pass. You can run the following command to check:
106114

107115
```bash
108-
$ pip install pyflakes
109-
$ pyflakes path/to/module.py
116+
$ pre-commit run --all-files
110117
```
111118

112-
- No PEP8 warnings, check with:
119+
This will run the pre-commit checks on all files in the repository.
113120

114-
```bash
115-
$ pip install pep8
116-
$ pep8 path/to/module.py
117-
```
118-
119-
- AutoPEP8 can help you fix some of the easy redundant errors:
121+
- All tests pass. You can run the following command to check:
120122

121123
```bash
122-
$ pip install autopep8
123-
$ autopep8 path/to/pep8.py
124-
```
124+
$ pytest --durations=20 -v test/ --doctest-modules
125+
```
125126

126127
Bonus points for contributions that include a performance analysis with
127128
a benchmark script and profiling output (please report on the mailing

.github/workflows/build_tests.yml

+25-21
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,38 @@ on:
1515
- '**'
1616

1717
jobs:
18+
19+
Lint:
20+
runs-on: ubuntu-latest
21+
strategy:
22+
fail-fast: false
23+
defaults:
24+
run:
25+
shell: bash -l {0}
26+
steps:
27+
28+
29+
- name: Checking Out Repository
30+
uses: actions/checkout@v2
31+
# Install Python & Packages
32+
- uses: actions/setup-python@v4
33+
with:
34+
python-version: "3.10"
35+
- run: which python
36+
- name: Lint with pre-commit
37+
run: |
38+
pip install pre-commit
39+
pre-commit install --install-hooks
40+
pre-commit run --all-files
41+
1842
linux:
1943

2044
runs-on: ubuntu-latest
2145
if: "!contains(github.event.head_commit.message, 'no ci')"
2246
strategy:
2347
max-parallel: 4
2448
matrix:
25-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
49+
python-version: ["3.9", "3.10", "3.11", "3.12"]
2650

2751
steps:
2852
- uses: actions/checkout@v4
@@ -44,26 +68,6 @@ jobs:
4468
- name: Upload coverage reports to Codecov with GitHub Action
4569
uses: codecov/codecov-action@v3
4670

47-
pep8:
48-
runs-on: ubuntu-latest
49-
if: "!contains(github.event.head_commit.message, 'no pep8')"
50-
steps:
51-
- uses: actions/checkout@v4
52-
- name: Set up Python
53-
uses: actions/setup-python@v5
54-
with:
55-
python-version: "3.x"
56-
- name: Install dependencies
57-
run: |
58-
python -m pip install --upgrade pip setuptools
59-
pip install flake8
60-
- name: Lint with flake8
61-
run: |
62-
# stop the build if there are Python syntax errors or undefined names
63-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
64-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
65-
flake8 examples/ ot/ test/ --count --max-line-length=127 --statistics
66-
6771
linux-minimal-deps:
6872

6973
runs-on: ubuntu-latest

.pre-commit-config.yaml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
repos:
2+
# Ruff skada
3+
- repo: https://github.com/astral-sh/ruff-pre-commit
4+
rev: v0.5.2
5+
hooks:
6+
- id: ruff
7+
name: ruff lint
8+
args: ["--fix"]
9+
files: ^ot/
10+
- id: ruff
11+
name: ruff lint preview
12+
args: ["--fix", "--preview", "--select=NPY201"]
13+
files: ^ot/
14+
- id: ruff
15+
name: ruff lint doc, tutorials, tests and examples
16+
# D103: missing docstring in public function
17+
# D400: docstring first line must end with period
18+
args: ["--ignore=D103,D400", "--fix"]
19+
files: ^docs/|^examples/^test/
20+
- id: ruff-format
21+
files: ^ot/|^docs/|^examples/|
22+
23+
# Codespell
24+
- repo: https://github.com/codespell-project/codespell
25+
rev: v2.2.6
26+
hooks:
27+
- id: codespell
28+
additional_dependencies:
29+
- tomli
30+
files: ^ot/|^docs/|^examples/
31+
types_or: [python, bib, rst, inc]
32+
args: [
33+
"--ignore-words",
34+
"ignore-words.txt",
35+
]
36+
37+
# yamllint
38+
- repo: https://github.com/adrienverge/yamllint.git
39+
rev: v1.35.1
40+
hooks:
41+
- id: yamllint
42+
# args: [--strict]
43+
44+
# # rstcheck
45+
# - repo: https://github.com/rstcheck/rstcheck.git
46+
# rev: v6.2.0
47+
# hooks:
48+
# - id: rstcheck
49+
# additional_dependencies:
50+
# - tomli
51+
# files: ^docs/source/.*\.(rst|inc)$

.yamllint.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extends: default
2+
3+
ignore: |
4+
.github/workflows/*.yml
5+
.circleci/config.yml
6+
codecov.yml
7+
8+
rules:
9+
line-length: disable
10+
document-start: disable

RELEASES.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Custom functions provided as parameter `line_search` to `ot.optim.generic_conditional_gradient` must now have the signature `line_search(cost, G, deltaG, Mi, cost_G, df_G, **kwargs)`, adding as input `df_G` the gradient of the regularizer evaluated at the transport plan `G`. This change aims at improving speed of solvers having quadratic polynomial functions as regularizer such as the Gromov-Wassertein loss (PR #663).
77

88
#### New features
9+
- New linter based on pre-commit using ruff, codespell and yamllint (PR #681)
910
- Added feature `mass=True` for `nx.kl_div` (PR #654)
1011
- Implemented Gaussian Mixture Model OT `ot.gmm` (PR #649)
1112
- Added feature `semirelaxed_fgw_barycenters` and generic FGW-related barycenter updates `update_barycenter_structure` and `update_barycenter_feature` (PR #659)

benchmarks/benchmark.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
def setup_backends():
99
if jax:
1010
from jax.config import config
11+
1112
config.update("jax_enable_x64", True)
1213

1314
if tf:
1415
from tensorflow.python.ops.numpy_ops import np_config
16+
1517
np_config.enable_numpy_behavior()
1618

1719

@@ -36,10 +38,7 @@ def exec_bench(setup, tested_function, param_list, n_runs, warmup_runs):
3638
print(nx, param_list[i])
3739
args = inputs[i]
3840
results_nx = nx._bench(
39-
tested_function,
40-
*args,
41-
n_runs=n_runs,
42-
warmup_runs=warmup_runs
41+
tested_function, *args, n_runs=n_runs, warmup_runs=warmup_runs
4342
)
4443
gc.collect()
4544
results_nx_with_param_in_key = dict()
@@ -64,10 +63,11 @@ def convert_to_html_table(results, param_name, main_title=None, comments=None):
6463
assert cpus_cols + gpus_cols == len(devices_names)
6564

6665
if main_title is not None:
67-
string += f'<tr><th align="center" colspan="{length}">{str(main_title)}</th></tr>\n'
66+
string += (
67+
f'<tr><th align="center" colspan="{length}">{str(main_title)}</th></tr>\n'
68+
)
6869

6970
for i, bitsize in enumerate(bitsizes):
70-
7171
if i != 0:
7272
string += f'<tr><td colspan="{length}">&nbsp;</td></tr>\n'
7373

benchmarks/emd.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
import numpy as np
55
import ot
6-
from .benchmark import (
7-
setup_backends,
8-
exec_bench,
9-
convert_to_html_table
10-
)
6+
from .benchmark import setup_backends, exec_bench, convert_to_html_table
117

128

139
def setup(n_samples):
@@ -31,10 +27,12 @@ def setup(n_samples):
3127
tested_function=lambda a, M: ot.emd(a, a, M),
3228
param_list=param_list,
3329
n_runs=n_runs,
34-
warmup_runs=warmup_runs
30+
warmup_runs=warmup_runs,
31+
)
32+
print(
33+
convert_to_html_table(
34+
results,
35+
param_name="Sample size",
36+
main_title=f"EMD - Averaged on {n_runs} runs",
37+
)
3538
)
36-
print(convert_to_html_table(
37-
results,
38-
param_name="Sample size",
39-
main_title=f"EMD - Averaged on {n_runs} runs"
40-
))

benchmarks/sinkhorn_knopp.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
import numpy as np
55
import ot
6-
from .benchmark import (
7-
setup_backends,
8-
exec_bench,
9-
convert_to_html_table
10-
)
6+
from .benchmark import setup_backends, exec_bench, convert_to_html_table
117

128

139
def setup(n_samples):
@@ -33,10 +29,12 @@ def setup(n_samples):
3329
tested_function=lambda *args: ot.bregman.sinkhorn(*args, reg=1, stopThr=1e-7),
3430
param_list=param_list,
3531
n_runs=n_runs,
36-
warmup_runs=warmup_runs
32+
warmup_runs=warmup_runs,
33+
)
34+
print(
35+
convert_to_html_table(
36+
results,
37+
param_name="Sample size",
38+
main_title=f"Sinkhorn Knopp - Averaged on {n_runs} runs",
39+
)
3740
)
38-
print(convert_to_html_table(
39-
results,
40-
param_name="Sample size",
41-
main_title=f"Sinkhorn Knopp - Averaged on {n_runs} runs"
42-
))

docs/nb_run_conv

+34-12
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ Created on Fri Sep 1 16:43:45 2017
99
@author: rflamary
1010
"""
1111

12-
import sys
1312
import json
1413
import glob
1514
import hashlib
1615
import subprocess
1716

1817
import os
1918

20-
cache_file = 'cache_nbrun'
19+
cache_file = "cache_nbrun"
2120

22-
path_doc = 'source/auto_examples/'
23-
path_nb = '../notebooks/'
21+
path_doc = "source/auto_examples/"
22+
path_nb = "../notebooks/"
2423

2524

2625
def load_json(fname):
@@ -34,7 +33,7 @@ def load_json(fname):
3433

3534

3635
def save_json(fname, nb):
37-
f = open(fname, 'w')
36+
f = open(fname, "w")
3837
f.write(json.dumps(nb))
3938
f.close()
4039

@@ -60,22 +59,45 @@ def to_update(fname, cache):
6059

6160

6261
def update(fname, cache):
63-
64-
# jupyter nbconvert --to notebook --execute mynotebook.ipynb --output targte
65-
subprocess.check_call(['cp', path_doc + fname, path_nb])
66-
print(' '.join(['jupyter', 'nbconvert', '--to', 'notebook', '--ExecutePreprocessor.timeout=600', '--execute', path_nb + fname, '--inplace']))
67-
subprocess.check_call(['jupyter', 'nbconvert', '--to', 'notebook', '--ExecutePreprocessor.timeout=600', '--execute', path_nb + fname, '--inplace'])
62+
# jupyter nbconvert --to notebook --execute mynotebook.ipynb --output target
63+
subprocess.check_call(["cp", path_doc + fname, path_nb])
64+
print(
65+
" ".join(
66+
[
67+
"jupyter",
68+
"nbconvert",
69+
"--to",
70+
"notebook",
71+
"--ExecutePreprocessor.timeout=600",
72+
"--execute",
73+
path_nb + fname,
74+
"--inplace",
75+
]
76+
)
77+
)
78+
subprocess.check_call(
79+
[
80+
"jupyter",
81+
"nbconvert",
82+
"--to",
83+
"notebook",
84+
"--ExecutePreprocessor.timeout=600",
85+
"--execute",
86+
path_nb + fname,
87+
"--inplace",
88+
]
89+
)
6890
cache[fname] = md5(path_doc + fname)
6991

7092

7193
cache = load_json(cache_file)
7294

73-
lst_file = glob.glob(path_doc + '*.ipynb')
95+
lst_file = glob.glob(path_doc + "*.ipynb")
7496

7597
lst_file = [os.path.basename(name) for name in lst_file]
7698

7799
for fname in lst_file:
78100
if to_update(fname, cache):
79-
print('Updating file: {}'.format(fname))
101+
print("Updating file: {}".format(fname))
80102
update(fname, cache)
81103
save_json(cache_file, cache)

docs/rtd/conf.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from recommonmark.parser import CommonMarkParser
22

3-
source_parsers = {'.md': CommonMarkParser}
3+
source_parsers = {".md": CommonMarkParser}
44

5-
source_suffix = ['.md']
6-
master_doc = 'index'
5+
source_suffix = [".md"]
6+
master_doc = "index"

0 commit comments

Comments
 (0)