Skip to content

Commit

Permalink
dev: Add pre-commit hooks for code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
ionicsolutions committed Dec 24, 2022
1 parent 49c3a40 commit 29f61f4
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 127
10 changes: 4 additions & 6 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Lint with flake8
- name: Run pre-commit hooks
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# 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
pip install -r requirements-dev.txt
pre-commit install
pre-commit run --all-files
- name: Run unit tests with pytest
run: |
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ visualime.egg-info/
.idea/
**/__pycache__/**
**/.ipynb_checkpoints/**

24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: mixed-line-ending

- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
args: [ "--profile", "black" ]

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ from visualime.explain import explain_classification, render_explanation

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

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

segment_mask, segment_weights = explain_classification(image, predict_fn)
Expand Down
2 changes: 1 addition & 1 deletion example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
notebook
pre-commit
1 change: 0 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pytest
pytest-coverage
tensorflow-cpu

12 changes: 6 additions & 6 deletions visualime/explain.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from typing import Dict, Optional, Union, Tuple, Callable, Any
from typing import Any, Callable, Dict, Optional, Tuple, Union

import numpy as np
from PIL import Image

from .feature_selection import forward_selection, select_by_weight
from .lime import (
create_segments,
generate_samples,
generate_images,
predict_images,
generate_samples,
image_distances,
predict_images,
weigh_segments,
)
from .visualize import select_segments, generate_overlay, scale_opacity
from .feature_selection import select_by_weight, forward_selection
from .visualize import generate_overlay, scale_opacity, select_segments


def explain_classification(
Expand Down Expand Up @@ -122,7 +122,7 @@ def explain_classification(
)
else:
raise ValueError(
f"Segment selection method has to be either 'by_weight' or 'forward_selection'."
"Segment selection method has to be either 'by_weight' or 'forward_selection'."
)

segment_weights = weigh_segments(
Expand Down
30 changes: 15 additions & 15 deletions visualime/feature_selection.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Optional, List
from typing import List, Optional

import numpy as np
from sklearn.linear_model import lars_path

from .lime import (
weigh_segments,
default_distance,
DISTANCES_DOC,
LINEAR_MODELS,
SAMPLES_PREDICTIONS_LABEL_IDX_DOC,
MODEL_TYPE_DOC,
DISTANCES_DOC,
SAMPLES_PREDICTIONS_LABEL_IDX_DOC,
default_distance,
weigh_segments,
)


Expand Down Expand Up @@ -46,13 +46,13 @@ def select_by_weight(
Parameters
----------
{SAMPLES_PREDICTIONS_LABEL_IDX_DOC}
{MODEL_TYPE_DOC}
It is generally advisable to use the same model as for the final `weigh_segments()` function.
{DISTANCES_DOC}
num_segments_to_select : int, optional
The number of segments to select. If not given, select all segments.
Expand All @@ -61,7 +61,7 @@ def select_by_weight(
selected_segments : List[int]
List of the indices of the selected segments.
Segments are ordered by descending weight.
"""


Expand Down Expand Up @@ -122,13 +122,13 @@ def score(current_features: List[int], next_feature_idx: int):
Parameters
----------
{SAMPLES_PREDICTIONS_LABEL_IDX_DOC}
{MODEL_TYPE_DOC}
It is generally advisable to use the same model as for the final `weigh_segments()` function.
{DISTANCES_DOC}
num_segments_to_select : int, optional
The number of segments to select. If not given, select all segments.
Expand Down Expand Up @@ -192,7 +192,7 @@ def lars_selection(
Parameters
----------
{SAMPLES_PREDICTIONS_LABEL_IDX_DOC}
num_segments_to_select : int, optional
The maximum number of segments to select.
If not given, this value is set to the total number of segments.
Expand Down
14 changes: 8 additions & 6 deletions visualime/lime.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Dict, Callable, Any, Optional, Union, List
from typing import Any, Callable, Dict, List, Optional, Union

import numpy as np
from skimage.color import rgb2gray
from skimage.filters import sobel
from skimage.segmentation import felzenszwalb, slic, quickshift, watershed
from sklearn.linear_model import Ridge, BayesianRidge, Lasso, LinearRegression
from skimage.segmentation import felzenszwalb, quickshift, slic, watershed
from sklearn.linear_model import BayesianRidge, Lasso, LinearRegression, Ridge

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

Expand Down Expand Up @@ -83,7 +84,8 @@ def create_segments(
Which method and settings are appropriate is highly use-case specific.
For an introduction into image segmentation and a comparison of the different methods, see
[this tutorial in the `scikit-learn` documentation](https://scikit-image.org/docs/stable/auto_examples/segmentation/plot_segmentations.html).
[this tutorial](https://scikit-image.org/docs/stable/auto_examples/segmentation/plot_segmentations.html)
in the `scikit-learn` documentation.
Parameters
----------
Expand Down Expand Up @@ -349,5 +351,5 @@ def weigh_segments(
segment_weights : np.ndarray
Array of length `num_of_segments` where each entry corresponds to the segment's coefficient
in the fitted linear model.
"""
3 changes: 2 additions & 1 deletion visualime/visualize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional, Tuple, Union

import numpy as np
from typing import Union, Tuple, List, Optional


def select_segments(
Expand Down

0 comments on commit 29f61f4

Please sign in to comment.