From 4751a218e2fe8ff4d40fbd8c7587094e100e9e9c Mon Sep 17 00:00:00 2001 From: vhu43 <67619615+vhu43@users.noreply.github.com> Date: Sun, 5 Mar 2023 12:07:10 -0800 Subject: [PATCH] Added x_transform for unevenly spaced data My proposed solution for #98 --- kneed/knee_locator.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/kneed/knee_locator.py b/kneed/knee_locator.py index 8c04b79..2fe8b83 100644 --- a/kneed/knee_locator.py +++ b/kneed/knee_locator.py @@ -188,6 +188,11 @@ def __init__( self.y_normalized = self.transform_y( self.y_normalized, self.direction, self.curve ) + + self.x_normalized = self.transform_x( + self.x_normalized, self.direction, self.curve + ) + # normalized difference curve # normalized difference curve self.y_difference = self.y_normalized - self.x_normalized self.x_difference = self.x_normalized.copy() @@ -228,16 +233,22 @@ def __normalize(a: Iterable[float]) -> Iterable[float]: def transform_y(y: Iterable[float], direction: str, curve: str) -> float: """transform y to concave, increasing based on given direction and curve""" # convert elbows to knees - if direction == "decreasing": - if curve == "concave": - y = np.flip(y) - elif curve == "convex": - y = y.max() - y - elif direction == "increasing" and curve == "convex": - y = np.flip(y.max() - y) + if curve == "convex": + y = y.max() - y return y + @staticmethod + def transform_x(x: Iterable[float], direction: str, curve: str) -> float: + """transform x to concave, increasing based on given direction and curve""" + # convert elbows to knees + if direction == "decreasing" and curve == "concave": + x = x.max - x + elif direction == "increasing" and curve == "convex": + x = x.max - x + + return x + 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: