Skip to content

Commit 29f61f4

Browse files
dev: Add pre-commit hooks for code quality
1 parent 49c3a40 commit 29f61f4

12 files changed

+65
-39
lines changed

.flake8

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 127

.github/workflows/unit-test.yml

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ jobs:
3131
python -m pip install --upgrade pip
3232
pip install -r requirements.txt
3333
34-
- name: Lint with flake8
34+
- name: Run pre-commit hooks
3535
run: |
36-
pip install flake8
37-
# stop the build if there are Python syntax errors or undefined names
38-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
39-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
40-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
36+
pip install -r requirements-dev.txt
37+
pre-commit install
38+
pre-commit run --all-files
4139
4240
- name: Run unit tests with pytest
4341
run: |

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ visualime.egg-info/
1010
.idea/
1111
**/__pycache__/**
1212
**/.ipynb_checkpoints/**
13-

.pre-commit-config.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.2.0
4+
hooks:
5+
- id: check-yaml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- id: mixed-line-ending
9+
10+
- repo: https://github.com/pycqa/flake8
11+
rev: 4.0.1
12+
hooks:
13+
- id: flake8
14+
15+
- repo: https://github.com/pycqa/isort
16+
rev: 5.10.1
17+
hooks:
18+
- id: isort
19+
args: [ "--profile", "black" ]
20+
21+
- repo: https://github.com/psf/black
22+
rev: 22.3.0
23+
hooks:
24+
- id: black

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ from visualime.explain import explain_classification, render_explanation
3030

3131
image = ... # a numpy array of shape (width, height, 3) representing an RGB image
3232

33-
def predict_fn(images: np.ndarray) -> np.ndarray:
33+
def predict_fn(images: np.ndarray) -> np.ndarray:
3434
# a function that takes a numpy array of shape (num_of_samples, width, height, 3)
3535
# representing num_of_samples RGB images and returns a numpy array of
3636
# shape (num_of_samples, num_of_classes) where each entry corresponds to the
3737
# classifiers output for the respective image
38-
predictions = ...
38+
predictions = ...
3939
return predictions
4040

4141
segment_mask, segment_weights = explain_classification(image, predict_fn)

example.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,4 @@
305305
},
306306
"nbformat": 4,
307307
"nbformat_minor": 5
308-
}
308+
}

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
notebook
2+
pre-commit

requirements-test.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pytest
22
pytest-coverage
33
tensorflow-cpu
4-

visualime/explain.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from typing import Dict, Optional, Union, Tuple, Callable, Any
1+
from typing import Any, Callable, Dict, Optional, Tuple, Union
22

33
import numpy as np
44
from PIL import Image
55

6+
from .feature_selection import forward_selection, select_by_weight
67
from .lime import (
78
create_segments,
8-
generate_samples,
99
generate_images,
10-
predict_images,
10+
generate_samples,
1111
image_distances,
12+
predict_images,
1213
weigh_segments,
1314
)
14-
from .visualize import select_segments, generate_overlay, scale_opacity
15-
from .feature_selection import select_by_weight, forward_selection
15+
from .visualize import generate_overlay, scale_opacity, select_segments
1616

1717

1818
def explain_classification(
@@ -122,7 +122,7 @@ def explain_classification(
122122
)
123123
else:
124124
raise ValueError(
125-
f"Segment selection method has to be either 'by_weight' or 'forward_selection'."
125+
"Segment selection method has to be either 'by_weight' or 'forward_selection'."
126126
)
127127

128128
segment_weights = weigh_segments(

visualime/feature_selection.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
from typing import Optional, List
1+
from typing import List, Optional
22

33
import numpy as np
44
from sklearn.linear_model import lars_path
55

66
from .lime import (
7-
weigh_segments,
8-
default_distance,
7+
DISTANCES_DOC,
98
LINEAR_MODELS,
10-
SAMPLES_PREDICTIONS_LABEL_IDX_DOC,
119
MODEL_TYPE_DOC,
12-
DISTANCES_DOC,
10+
SAMPLES_PREDICTIONS_LABEL_IDX_DOC,
11+
default_distance,
12+
weigh_segments,
1313
)
1414

1515

@@ -46,13 +46,13 @@ def select_by_weight(
4646
Parameters
4747
----------
4848
{SAMPLES_PREDICTIONS_LABEL_IDX_DOC}
49-
49+
5050
{MODEL_TYPE_DOC}
51-
51+
5252
It is generally advisable to use the same model as for the final `weigh_segments()` function.
53-
53+
5454
{DISTANCES_DOC}
55-
55+
5656
num_segments_to_select : int, optional
5757
The number of segments to select. If not given, select all segments.
5858
@@ -61,7 +61,7 @@ def select_by_weight(
6161
selected_segments : List[int]
6262
List of the indices of the selected segments.
6363
Segments are ordered by descending weight.
64-
64+
6565
"""
6666

6767

@@ -122,13 +122,13 @@ def score(current_features: List[int], next_feature_idx: int):
122122
Parameters
123123
----------
124124
{SAMPLES_PREDICTIONS_LABEL_IDX_DOC}
125-
125+
126126
{MODEL_TYPE_DOC}
127-
127+
128128
It is generally advisable to use the same model as for the final `weigh_segments()` function.
129-
129+
130130
{DISTANCES_DOC}
131-
131+
132132
num_segments_to_select : int, optional
133133
The number of segments to select. If not given, select all segments.
134134
@@ -192,7 +192,7 @@ def lars_selection(
192192
Parameters
193193
----------
194194
{SAMPLES_PREDICTIONS_LABEL_IDX_DOC}
195-
195+
196196
num_segments_to_select : int, optional
197197
The maximum number of segments to select.
198198
If not given, this value is set to the total number of segments.

visualime/lime.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from typing import Dict, Callable, Any, Optional, Union, List
1+
from typing import Any, Callable, Dict, List, Optional, Union
22

33
import numpy as np
44
from skimage.color import rgb2gray
55
from skimage.filters import sobel
6-
from skimage.segmentation import felzenszwalb, slic, quickshift, watershed
7-
from sklearn.linear_model import Ridge, BayesianRidge, Lasso, LinearRegression
6+
from skimage.segmentation import felzenszwalb, quickshift, slic, watershed
7+
from sklearn.linear_model import BayesianRidge, Lasso, LinearRegression, Ridge
88

99
SAMPLES_PREDICTIONS_LABEL_IDX_DOC = """
1010
samples : np.ndarray
@@ -30,7 +30,8 @@
3030
The type of linear model to fit.
3131
Available options are `"bayesian_ridge"`, `"lasso"`, and `"linear_regression"`.
3232
33-
See [the `scikit-learn` documentation](https://scikit-learn.org/stable/modules/classes.html#module-sklearn.linear_model)
33+
See the
34+
[`scikit-learn` documentation](https://scikit-learn.org/stable/modules/classes.html#module-sklearn.linear_model)
3435
for details on each of the methods.
3536
"""
3637

@@ -83,7 +84,8 @@ def create_segments(
8384
Which method and settings are appropriate is highly use-case specific.
8485
8586
For an introduction into image segmentation and a comparison of the different methods, see
86-
[this tutorial in the `scikit-learn` documentation](https://scikit-image.org/docs/stable/auto_examples/segmentation/plot_segmentations.html).
87+
[this tutorial](https://scikit-image.org/docs/stable/auto_examples/segmentation/plot_segmentations.html)
88+
in the `scikit-learn` documentation.
8789
8890
Parameters
8991
----------
@@ -349,5 +351,5 @@ def weigh_segments(
349351
segment_weights : np.ndarray
350352
Array of length `num_of_segments` where each entry corresponds to the segment's coefficient
351353
in the fitted linear model.
352-
354+
353355
"""

visualime/visualize.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from typing import List, Optional, Tuple, Union
2+
13
import numpy as np
2-
from typing import Union, Tuple, List, Optional
34

45

56
def select_segments(

0 commit comments

Comments
 (0)