Esempio n. 1
0
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()
Esempio n. 2
0
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 = kneedle.auto_knees(points_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)

    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()
Esempio n. 3
0
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}'
    )