Example #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=',')

    # Kneedle classic window
    knees_00 = kneedle_classic(points, args, args.sw)
    cm = evaluation.cm(points, knees_00, expected)
    mcc00 = evaluation.mcc(cm)

    # Kneedle classic all
    knees_01 = kneedle_classic(points, args, -1)
    cm = evaluation.cm(points, knees_01, expected)
    mcc01 = evaluation.mcc(cm)

    # Novel Kneedle
    knees_02 = kneedle_novel(points, args)
    if len(knees_02) > 0:
        cm = evaluation.cm(points, knees_02, expected)
        mcc02 = evaluation.mcc(cm)
    else:
        mcc02 = 0.0

    logger.info(f'{mcc00:10.2E} {mcc01:10.2E} {mcc02:10.2E}')

    # Plot knees
    x = points[:,0]
    y = points[:,1]
    _, (ax1, ax2, ax3) = plt.subplots(1, 3)
    ax1.plot(x, y)
    ax1.plot(x[knees_00], y[knees_00], 'r+')
    ax2.plot(x, y)
    ax2.plot(x[knees_01], y[knees_01], 'r+')
    ax3.plot(x, y)
    if len(knees_02) > 0:
        ax3.plot(x[knees_02], y[knees_02], 'r+')
    plt.show()
Example #2
0
 def test_mcc_2(self):
     points = np.array([[0,0], [1,1], [2,2]])
     knees = np.array([0])
     expected = np.array([[1,1], [2,2]])
     cm = evaluation.cm(points, knees, expected)
     result = evaluation.mcc(cm)
     desired = -1.0
     self.assertAlmostEqual(result, desired)
Example #3
0
def main(args):

    path = os.path.expanduser(args.p)

    if args.tr is Trace.all:
        files = [f for f in os.listdir(path) if re.match(r'w[0-9]*-(lru|arc)\.csv', f)]
    elif args.tr is Trace.arc:
        files = [f for f in os.listdir(path) if re.match(r'w[0-9]*-arc\.csv', f)]
    else:
        files = [f for f in os.listdir(path) if re.match(r'w[0-9]*-lru\.csv', f)]
    
    scores = []

    for i in tqdm(range(len(files))):
        points = np.genfromtxt(f'{path}{files[i]}', delimiter=',')
        # open expected file
        dirname = os.path.dirname(f'{path}{files[i]}')
        filename = os.path.splitext(os.path.basename(files[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)

        # 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, t=args.r, cost=args.c, distance=args.d)
        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]
        knees = rdp.mapping(knees, reduced, removed)
        if len(knees) > 0:
            cm = evaluation.cm(points, knees, expected)
            mcc = evaluation.mcc(cm)
        else:
            mcc = 0.0
        scores.append(mcc)

    # output the results
    dirname = os.path.expanduser(args.p)
    output = os.path.join(os.path.normpath(dirname), f'eval_rdp_metric_output.csv')

    with open(output, 'w') as f:
        writer = csv.writer(f)
        for s in scores:
            writer.writerow([s])        
Example #4
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 = 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()
Example #5
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()
Example #6
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 ##

    # 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()