Skip to content

Commit

Permalink
add inclusive exclusive clause to sep data
Browse files Browse the repository at this point in the history
  • Loading branch information
cjekel committed Oct 31, 2017
1 parent 70ea8d4 commit 9d2cd3a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Global optimization is used to find the best location for the user defined numbe
All other methods require the user to specify the specific location of break points, but in most cases the best location for these break points is unknown. It makes more sense to rather have the user specify the desired number of line segments, and then to quantitatively choose the best location for the ends of these line segments.

# Changelog
- 2017/10/31 bug fix related to the case where break points exactly equal a data point as per issue https://github.com/cjekel/piecewise_linear_fit_py/issues/1
- 2017/10/20 remove determinant calculation and use try-except instead, this will offer a larger performance boost for big problems. Change library name to something more Pythonic. Add version attribute.
- 2017/08/03 gradients (slopes of the line segments) now stored as piecewise_lin_fit.slopes (or myPWLF.slopes) after they have been calculated by performing a fit or predicting
- 2017/04/01 initial release
Expand Down
18 changes: 18 additions & 0 deletions examples/test_if_breaks_exact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
import pwlf

x = np.array((0.0,1.0,1.5,2.0))
y = np.array((0.0,1.0,1.1,1.5))

my_fit1 = pwlf.piecewise_lin_fit(x,y)
x0 = x.copy()
# check that I can fit when break poitns spot on a
ssr = my_fit1.fitWithBreaks(x0)

# check that i can fit when I slightly modify x0
my_fit2 = pwlf.piecewise_lin_fit(x,y)
x0[1] = 1.00001
x0[2] = 1.50001
ssr2 = my_fit2.fitWithBreaks(x0)


16 changes: 13 additions & 3 deletions pwlf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from scipy.optimize import differential_evolution

# add rudimentary version tracking
__version__ = '0.0.3'
__version__ = '0.0.4'

# piecewise linerar fit library
class piecewise_lin_fit:
Expand Down Expand Up @@ -113,7 +113,12 @@ def seperateData(self, breaks):
for i in range(0, self.numberOfSegments):
dataX = []
dataY = []
aTest = self.xData >= breaks[i]
if i == 0:
# the first index should always be inclusive
aTest = self.xData >= breaks[i]
else:
# the rest of the indexies should be exclusive
aTest = self.xData > breaks[i]
dataX = np.extract(aTest, self.xData)
dataY = np.extract(aTest, self.yData)
bTest = dataX <= breaks[i+1]
Expand All @@ -137,7 +142,12 @@ def seperateDataX(self, breaks, x):

for i in range(0, self.numberOfSegments):
dataX = []
aTest = x >= breaks[i]
if i == 0:
# the first index should always be inclusive
aTest = x >= breaks[i]
else:
# the rest of the indexies should be exclusive
aTest = x > breaks[i]
dataX = np.extract(aTest, x)
bTest = dataX <= breaks[i+1]
dataX = np.extract(bTest, dataX)
Expand Down

0 comments on commit 9d2cd3a

Please sign in to comment.