def simplify(x, y, maxpoints=20): x = map(float, x) y = map(float, y) pts = np.array(zip(x, y)) simplifier = VWSimplifier(pts) sqd = [] iter_range = range(2, maxpoints + 1) for i in iter_range: VWpts = simplifier.from_number(i) xn, yn = zip(*VWpts) out = np.sum((y - np.interp(x, xn, yn))**2) sqd.append(out) # sqd /= max(sqd) if min(sqd) == max(sqd): VWpts = simplifier.from_number(2) return VWpts else: sqd = rescale(sqd) # plt.plot(sqd) # plt.show() # iter = (np.array(iter_range) - 2) / (maxpoints - 2.) # plt.plot(iter_range, sqd, label='residual') # plt.plot(iter_range, iter, color='r', label='iteration') # plt.plot(iter_range, iter + sqd, color='g', label='residual+iteration') # plt.legend(loc='upper center', shadow=True) # plt.show() # npoints = np.argmin(iter + sqd) + 2 npoints = np.argmax(np.array(sqd) < 0.01) + 2 VWpts = simplifier.from_number(npoints) return VWpts
def test_rescale(): assert anc.rescale([1000, 2000, 3000], [1, 3]) == [1, 2, 3] with pytest.raises(RuntimeError): anc.rescale([1000, 1000])