-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from InCogNiTo124/refactor-to-files
Refactor all methods to their respective files
- Loading branch information
Showing
11 changed files
with
246 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import numpy as np | ||
|
||
|
||
def angle(x, y, **kwargs): | ||
""" | ||
Find a knee by looking at the maximum change of the angle of the line going through consecutive point pairs | ||
Quite sensitive to noise, use with cubic spline smoothing. | ||
Args: | ||
x: npt.NDArray, the x coordinates of the points | ||
y: npt.NDArray, the y coordinates of the points | ||
**kwargs: possible additional arguments (none are used) | ||
Returns: int, the index of the knee | ||
""" | ||
assert len(kwargs) == 0 | ||
assert x.shape == y.shape | ||
d_x = np.diff(x) | ||
d_y = np.diff(y) | ||
angles = np.arctan2(d_y, d_x) | ||
angle_differences = np.abs(np.diff(angles)) | ||
max_diff = angle_differences.argmax().item() | ||
return max_diff + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
def angle(x: npt.NDArray[np.float_], y: npt.NDArray[np.float_], kwargs: Any) -> int: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import numpy as np | ||
|
||
from .util import np_windowed, projection_distance | ||
|
||
|
||
def distance(x, y, **kwargs): | ||
""" | ||
Find a knee by finding a point which is most distant from the line y=x (after normalizing the inputs) | ||
Fun fact: *vertical* distance of a point P from line y=x is just a scaled version of the *orthogonal* distance of | ||
the same point P from line x=y. | ||
Args: | ||
x: np.ndarray, the x coordinates of the points | ||
y: np.ndarray, the y coordinates of the points | ||
**kwargs: possible additional arguments (none are actually used) | ||
Returns: int, the index of the knee | ||
""" | ||
assert len(kwargs) == 0 | ||
assert x.shape == y.shape | ||
distances = abs(y - x) | ||
return np.argmax(distances).item() | ||
|
||
|
||
def distance_adjacent(x, y, **kwargs): | ||
""" | ||
Find a knee by finding a point which is most distant from the line going through the neighbouring points. | ||
Note: I developed a (not so) fancy linear algebra implementation so this should be quite fast. However, this method | ||
is quite sensitive to noise, so only use with cubic spline smoothing. | ||
Args: | ||
x: np.ndarray, the x coordinates of the points | ||
y:, np.ndarray, the y coordinates of the points | ||
**kwargs: possible additional arguments (none are actually used) | ||
Returns: int, the index of the knee | ||
""" | ||
assert len(kwargs) == 0 | ||
indices = np_windowed(len(x), 3) | ||
x_windowed = x[indices] # shape = (len(x), 3) | ||
y_windowed = y[indices] # shape = (len(x), 3) | ||
points = np.stack((x_windowed, y_windowed), axis=-1) # shape = (len(x), 3, 2) | ||
translated_points = points - points[:, [0], :] # anchor all the triplets at the origin. The list is important! | ||
translated_points = translated_points[..., 1:, :] # remove the origin point, now shape = (len(x), 2, 2) | ||
distances = projection_distance(translated_points) | ||
return np.argmax(distances).item() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
def distance(x: npt.NDArray[np.float_], y: npt.NDArray[np.float_], kwargs: Any) -> int: ... | ||
def distance_adjacent(x: npt.NDArray[np.float_], y: npt.NDArray[np.float_], kwargs: Any) -> int: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.