def test_rmspe_2(self): points = np.array([[0,0], [1,1], [2,2]]) knees = np.array([1]) expected = np.array([[1,1], [2,2]]) result = evaluation.rmspe(points, knees, expected) desired = 0.3535533905755961 self.assertAlmostEqual(result, desired, 3)
def test_rmspe_1(self): points = np.array([[0,0], [1,1], [2,2]]) knees = np.array([0,1,2]) expected = np.array([[1,1], [2,2]]) result = evaluation.rmspe(points, knees, expected, evaluation.Strategy.worst) desired = 5773502691896258.0 self.assertAlmostEqual(result, desired, 3)
def test_rmspe_0(self): points = np.array([[0,0], [1,1], [2,2]]) knees = np.array([0,1,2]) expected = np.array([[0,0], [1,1], [2,2]]) result = evaluation.rmspe(points, knees, expected) desired = 0.0 self.assertAlmostEqual(result, desired)
def main(args): # get the expected file from the input file dirname = os.path.dirname(args.i) filename = os.path.splitext(os.path.basename(args.i))[0] expected_file = os.path.join(os.path.normpath(dirname), f'{filename}_expected.csv') expected = None if os.path.exists(expected_file): with open(expected_file, 'r') as f: reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC) expected = list(reader) else: expected = [] expected = np.array(expected) points = np.genfromtxt(args.i, delimiter=',') ## Knee detection code ## reduced, removed = rdp.rdp(points, args.r) points_reduced = points[reduced] knees = np.arange(1, len(reduced)) t_k = pp.filter_worst_knees(points_reduced, knees) t_k = pp.filter_corner_knees(points_reduced, t_k, t=args.c) filtered_knees = pp.filter_clusters(points_reduced, t_k, clustering.average_linkage, args.t, args.k) ########################################################################################## # add even points if args.a: knees = pp.add_points_even(points, reduced, filtered_knees, removed) else: knees = rdp.mapping(filtered_knees, reduced, removed) rmspe_k = evaluation.rmspe(points, knees, expected, evaluation.Strategy.knees) rmspe_e = evaluation.rmspe(points, knees, expected, evaluation.Strategy.expected) cm = evaluation.cm(points, knees, expected, t = 0.01) mcc = evaluation.mcc(cm) logger.info(f'RMSE(knees) RMSE(exp) MCC') logger.info(f'-------------------------------------------') logger.info(f'{rmspe_k:10.2E} {rmspe_e:10.2E} {mcc:10.2E}') # store outpout if args.o: dirname = os.path.dirname(args.i) filename = os.path.splitext(os.path.basename(args.i))[0] output = os.path.join(os.path.normpath(dirname), f'{filename}_output.csv') dataset = points[knees] with open(output, 'w') as f: writer = csv.writer(f) writer.writerows(dataset) # display result if args.g: x = points[:, 0] y = points[:, 1] plt.plot(x, y) plt.plot(x[knees], y[knees], 'r+') plt.show()
def main(args): # get the expected file from the input file dirname = os.path.dirname(args.i) filename = os.path.splitext(os.path.basename(args.i))[0] expected_file = os.path.join(os.path.normpath(dirname), f'{filename}_expected.csv') expected = None if os.path.exists(expected_file): with open(expected_file, 'r') as f: reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC) expected = list(reader) else: expected = [] expected = np.array(expected) points = np.genfromtxt(args.i, delimiter=',') # get original x_max and y_ranges x_max = [max(x) for x in zip(*points)][0] y_range = [[max(y), min(y)] for y in zip(*points)][1] # run rdp reduced, removed = rdp.rdp(points, args.r) points_reduced = points[reduced] ## Knee detection code ## knees = zmethod.knees(points_reduced, dx=args.x, dy=args.y, dz=args.z, x_max=x_max, y_range=y_range) knees = knees[knees > 0] ########################## # add even points if args.a: knees = pp.add_points_even(points, reduced, knees, removed) else: knees = rdp.mapping(knees, reduced, removed) rmspe_k = evaluation.rmspe(points, knees, expected, evaluation.Strategy.knees) rmspe_e = evaluation.rmspe(points, knees, expected, evaluation.Strategy.expected) cm = evaluation.cm(points, knees, expected, t=0.01) mcc = evaluation.mcc(cm) logger.info(f'RMSE(knees) RMSE(exp) MCC') logger.info(f'-------------------------------------------') logger.info(f'{rmspe_k:10.2E} {rmspe_e:10.2E} {mcc:10.2E}') # store outpout if args.o: dirname = os.path.dirname(args.i) filename = os.path.splitext(os.path.basename(args.i))[0] output = os.path.join(os.path.normpath(dirname), f'{filename}_output.csv') dataset = points[knees] with open(output, 'w') as f: writer = csv.writer(f) writer.writerows(dataset) # display result if args.g: x = points[:, 0] y = points[:, 1] plt.plot(x, y) plt.plot(x[knees], y[knees], 'r+') plt.show()
def main(args): # get the expected file from the input file dirname = os.path.dirname(args.i) filename = os.path.splitext(os.path.basename(args.i))[0] expected_file = os.path.join(os.path.normpath(dirname), f'{filename}_expected.csv') expected = None if os.path.exists(expected_file): with open(expected_file, 'r') as f: reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC) expected = list(reader) else: expected = [] expected = np.array(expected) points = np.genfromtxt(args.i, delimiter=',') rs = [0.75, 0.80, 0.85, 0.90, 0.95] ts = [0.01, 0.02, 0.03, 0.04, 0.05] evaluations = [] for r in rs: ## Knee detection code ## points_reduced, points_removed = rdp.rdp(points, r) knees = np.arange(1, len(points_reduced)) t_k = pp.filter_worst_knees(points_reduced, knees) t_k = pp.filter_corner_knees(points_reduced, t_k) for t in ts: ## Clustering ## filtered_knees = pp.filter_clustring(points_reduced, t_k, clustering.average_linkage, t, ClusterRanking.left) final_knees = pp.add_points_even(points, points_reduced, filtered_knees, points_removed) ## Evaluation ## error_rmspe = evaluation.rmspe(points, final_knees, expected, evaluation.Strategy.knees) error_rmspe_exp = evaluation.rmspe(points, final_knees, expected, evaluation.Strategy.expected) _, _, _, _, cost_trace = evaluation.accuracy_trace( points, final_knees) _, _, _, _, cost_knee = evaluation.accuracy_knee( points, final_knees) evaluations.append( [error_rmspe, error_rmspe_exp, cost_trace, cost_knee]) ## Compute the Correlation ## evaluations = np.array(evaluations) rho = np.corrcoef(evaluations.T) rmspe_rmspe_exp = rho[0, 1] rmspe_cost_trace = rho[0, 2] rmspe_cost_knee = rho[0, 3] rmspe_exp_cost_trace = rho[1, 2] rmspe_exp_cost_knee = rho[1, 3] cost_trace_cost_knee = rho[2, 3] #logger.info(f'{rho}') logger.info( f'{rmspe_rmspe_exp}, {rmspe_cost_trace}, {rmspe_cost_knee}, {rmspe_exp_cost_trace}, {rmspe_exp_cost_knee}, {cost_trace_cost_knee}' )
def main(args): # get the expected file from the input file dirname = os.path.dirname(args.i) filename = os.path.splitext(os.path.basename(args.i))[0] expected_file = os.path.join(os.path.normpath(dirname), f'{filename}_expected.csv') expected = None if os.path.exists(expected_file): with open(expected_file, 'r') as f: reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC) expected = list(reader) else: expected = [] expected = np.array(expected) points = np.genfromtxt(args.i, delimiter=',') ## Knee detection code ## # For all possible sliding windows left = 0 right = 0 knees = [] while right < len(points): if args.sw == -1: right = len(points) else: right = min(left+args.sw, len(points)) logger.info(f'[{left}, {right}]') window_points = points[left:right+1] if args.d: window_knees = kneedle.knees(window_points, args.t, args.cd, args.cc, args.s, debug=True) dd = window_knees['dd'] x = dd[:, 0] y = dd[:, 1] peaks = window_knees['peaks'] window_knees = window_knees['knees'] plt.plot(x, y) plt.plot(x[peaks], y[peaks], 'ro') plt.plot(x[window_knees], y[window_knees], 'yx') plt.show() else: window_knees = kneedle.knees(window_points, args.t, args.cd, args.cc, args.s, debug=False) window_knees += left left = left + args.so knees.extend(window_knees.tolist()) knees = np.unique(np.array(knees)) #knees = np.array(knees) #t_k = pp.filter_worst_knees(points_reduced, knees) #t_k = pp.filter_corner_knees(points_reduced, t_k, t=args.c) #filtered_knees = pp.filter_clustring(points_reduced, t_k, clustering.average_linkage, args.t, knee_ranking.ClusterRanking.left) ########################################################################################## # add even points #if args.a: # knees = pp.add_points_even(points, points_reduced, filtered_knees, points_removed, 0.009, 0.009) #else: # knees = rdp.mapping(filtered_knees, points_reduced, points_removed) nk = len(knees) if nk > 0: rmspe_k = evaluation.rmspe(points, knees, expected, evaluation.Strategy.knees) rmspe_e = evaluation.rmspe(points, knees, expected, evaluation.Strategy.expected) cm = evaluation.cm(points, knees, expected, t = 0.01) mcc = evaluation.mcc(cm) else: rmspe_k = 999 rmspe_e = 999 mcc = -1 logger.info(f'RMSE(knees) RMSE(exp) MCC N_Knees') logger.info(f'-------------------------------------------') logger.info(f'{rmspe_k:10.2E} {rmspe_e:10.2E} {mcc:10.2E} {nk}') # store outpout if args.o: dirname = os.path.dirname(args.i) filename = os.path.splitext(os.path.basename(args.i))[0] output = os.path.join(os.path.normpath(dirname), f'{filename}_output.csv') dataset = points[knees] with open(output, 'w') as f: writer = csv.writer(f) writer.writerows(dataset) # display result if args.g: x = points[:, 0] y = points[:, 1] plt.plot(x, y) plt.plot(x[knees], y[knees], 'r+') plt.show()