Skip to content

Commit

Permalink
Black
Browse files Browse the repository at this point in the history
  • Loading branch information
arvkevi committed Jul 9, 2023
1 parent 2137403 commit 492ef99
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
26 changes: 13 additions & 13 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
import os
import sys

sys.path.insert(0, os.path.abspath('..'))
sys.path.insert(0, os.path.abspath(".."))

import kneed


# -- Project information -----------------------------------------------------

project = 'kneed'
copyright = '2020, Kevin Arvai'
author = 'Kevin Arvai'
project = "kneed"
copyright = "2020, Kevin Arvai"
author = "Kevin Arvai"

# The full version, including alpha/beta/rc tags
release = kneed.__version__
Expand All @@ -36,22 +36,22 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.coverage',
'sphinx.ext.napoleon',
'sphinx_rtd_theme'
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.coverage",
"sphinx.ext.napoleon",
"sphinx_rtd_theme",
]

pygments_style = "sphinx"

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
Expand All @@ -66,6 +66,6 @@
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = []

autoclass_content = 'both'
autoclass_content = "both"

master_doc = 'index'
master_doc = "index"
25 changes: 21 additions & 4 deletions kneed/knee_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import matplotlib.pyplot as plt
except ImportError:
_has_matplotlib = False
_matplotlib_not_found_err = ModuleNotFoundError("This function needs Matplotlib to be executed. Please run command `pip install kneed[plot]` ")
_matplotlib_not_found_err = ModuleNotFoundError(
"This function needs Matplotlib to be executed. Please run command `pip install kneed[plot]` "
)
else:
_has_matplotlib = True


class KneeLocator(object):
"""
Once instantiated, this class attempts to find the point of maximum
Expand Down Expand Up @@ -236,7 +239,9 @@ def transform_y(y: Iterable[float], direction: str, curve: str) -> float:

return y

def find_knee(self,):
def find_knee(
self,
):
"""This function is called when KneeLocator is instantiated. It identifies the knee value and sets the instance attributes."""
if not self.maxima_indices.size:
warnings.warn(
Expand Down Expand Up @@ -311,7 +316,13 @@ def find_knee(self,):

return knee, norm_knee

def plot_knee_normalized(self, figsize: Optional[Tuple[int, int]] = None, title: str = "Normalized Knee Point", xlabel: Optional[str] = None, ylabel: Optional[str] = None):
def plot_knee_normalized(
self,
figsize: Optional[Tuple[int, int]] = None,
title: str = "Normalized Knee Point",
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
):
"""Plot the normalized curve, the difference curve (x_difference, y_normalized) and the knee, if it exists.
:param figsize: Optional[Tuple[int, int]
Expand Down Expand Up @@ -354,7 +365,13 @@ def plot_knee_normalized(self, figsize: Optional[Tuple[int, int]] = None, title:
)
plt.legend(loc="best")

def plot_knee(self, figsize: Optional[Tuple[int, int]] = None, title: str = "Knee Point", xlabel: Optional[str] = None, ylabel: Optional[str] = None):
def plot_knee(
self,
figsize: Optional[Tuple[int, int]] = None,
title: str = "Knee Point",
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
):
"""
Plot the curve and the knee, if it exists
Expand Down
8 changes: 4 additions & 4 deletions kneed/shape_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def find_shape(x, y):
x1, x2 = int(len(x) * 0.2), int(len(x) * 0.8)
q = np.mean(y[x1:x2]) - np.mean(x[x1:x2] * p[0] + p[1])
if p[0] > 0 and q > 0:
return 'increasing', 'concave'
return "increasing", "concave"
if p[0] > 0 and q <= 0:
return 'increasing', 'convex'
return "increasing", "convex"
if p[0] <= 0 and q > 0:
return 'decreasing', 'concave'
return 'decreasing', 'convex'
return "decreasing", "concave"
return "decreasing", "convex"
1 change: 1 addition & 0 deletions tests/test_no_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def test_plot_knee_normalized():
kl = KneeLocator(x, y, S=1.0, curve="concave", interp_method="interp1d")
kl.plot_knee_normalized()


def test_plot_knee():
"""Test that error is raised when matplotlib is not installed"""
with pytest.raises(ModuleNotFoundError):
Expand Down
25 changes: 16 additions & 9 deletions tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,16 @@ def test_logistic():
98.0,
]
)
kl = KneeLocator(x, y, curve="convex", direction="increasing", online=True,)
kl = KneeLocator(
x,
y,
curve="convex",
direction="increasing",
online=True,
)
assert kl.knee == 73


def test_valid_curve_direction():
"""Test that arguments to curve and direction are valid"""
with pytest.raises(ValueError):
Expand All @@ -538,17 +545,17 @@ def test_find_shape():
"""Test that find_shape can detect the right shape of curve line"""
x, y = dg.concave_increasing()
direction, curve = find_shape(x, y)
assert direction == 'increasing'
assert curve == 'concave'
assert direction == "increasing"
assert curve == "concave"
x, y = dg.concave_decreasing()
direction, curve = find_shape(x, y)
assert direction == 'decreasing'
assert curve == 'concave'
assert direction == "decreasing"
assert curve == "concave"
x, y = dg.convex_decreasing()
direction, curve = find_shape(x, y)
assert direction == 'decreasing'
assert curve == 'convex'
assert direction == "decreasing"
assert curve == "convex"
x, y = dg.convex_increasing()
direction, curve = find_shape(x, y)
assert direction == 'increasing'
assert curve == 'convex'
assert direction == "increasing"
assert curve == "convex"

0 comments on commit 492ef99

Please sign in to comment.