From 6c450291363a26320565a90ce82e82294afd2319 Mon Sep 17 00:00:00 2001
From: Loic-Lenof <loic.lenof@gmail.com>
Date: Fri, 20 Jan 2023 12:52:59 +0100
Subject: [PATCH] Sorting points

+added sorting of points

=> points can be added anywhere on a line
---
 args.py                      |  4 +---
 line_clicker/line_clicker.py | 26 +++++++++++++++++++-------
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/args.py b/args.py
index 7298bce..32eeed4 100644
--- a/args.py
+++ b/args.py
@@ -110,9 +110,7 @@ def fetch_inputs():
 	# verifying arguments
 	try:
 		assert (os.path.exists(outputs)), (
-			f"\nInputError: Could not find dir '{outputs}'."
-			"\n\tCreate a folder that will contain outputs,"
-			" or type in an existing folder with `-out folder_name`.")
+			f"\nInputError: Could not find dir '{outputs}'.")
 		assert (os.path.exists(explore)), (
 			f"\nInputError: Could not find dir '{explore}'.")
 		if modify_file:
diff --git a/line_clicker/line_clicker.py b/line_clicker/line_clicker.py
index 103db70..8b68908 100644
--- a/line_clicker/line_clicker.py
+++ b/line_clicker/line_clicker.py
@@ -3,6 +3,7 @@ Add default parameters for keys and mousebutton so it can be customized
 """
 
 ##### IMPORTATIONS ######
+import warnings
 import numpy as np
 import matplotlib.pyplot as plt
 from matplotlib.backend_bases import MouseButton
@@ -11,7 +12,7 @@ import matplotlib.lines as mlines
 from scipy.interpolate import interp1d
 
 ##### FUNCTIONS #####
-def to_curve(x, y, kind):
+def to_curve(x, y, kind="quadratic"):
 	"""
 	A function to compute a curvy line between two points.
 	It interpolates a line with 100 points.
@@ -36,12 +37,14 @@ def to_curve(x, y, kind):
 	yi : numpy array
 		List of the coordinates of the curve on y-axis.
 	"""
-	i = np.arange(len(x))
 
-	interp_i = np.linspace(0, i.max(), 100 * i.max())
+	y = y[np.argsort(x)]
+	x = np.sort(x)
 
-	xi = interp1d(i, x, kind=kind)(interp_i)  
-	yi = interp1d(i, y, kind=kind)(interp_i)
+	f = interp1d(x,y, kind=kind)
+
+	xi = np.linspace(x.min(), x.max(), 100 * (len(x)-1))
+	yi = f(xi)
 
 	return xi, yi
 
@@ -360,7 +363,16 @@ class clicker(object):
 		None : updates coords, figure_lines, figure.
 		"""
 		if self.legend_labels[self.current_line] in list(self.coords.keys()):
-			self.coords[self.legend_labels[self.current_line]] += [[x, y]]
+			# does this point already exists ?
+			if [x,y] in self.coords[self.legend_labels[self.current_line]]:
+				warnings.warn("This point already exists!", UserWarning, stacklevel=2)
+			else:
+				# where should it be inserted ?
+				here = np.where(np.array(self.coords[self.legend_labels[self.current_line]])[:,0] > x)[0]
+				if len(here):
+					self.coords[self.legend_labels[self.current_line]].insert(here[0] ,[x, y])
+				else:
+					self.coords[self.legend_labels[self.current_line]] += [[x, y]]
 		else:
 			self.coords[self.legend_labels[self.current_line]] = [[x, y]]
 
@@ -392,7 +404,7 @@ class clicker(object):
 				curves = to_curve(
 					np.array(self.coords[legend_label])[:,0], 
 					np.array(self.coords[legend_label])[:,1],
-					kind="quadratic")
+					kind=self.bspline)
 				self.figure_bsplines += [mlines.Line2D(curves[0], curves[1],
 					label=legend_label, 
 					color=self.colors[idx%len(self.colors)], 
-- 
GitLab