def davies_quick(df, p=bt_parms, mode=0):
    # strip any trailing zeros
    while df[-1] == 0:
        df = df[0:len(df) - 1]

    # get periodicity path
    ppath = periodicity_path(df, p)

    # find beat locations
    beats = dynamic_programming(df, p, ppath, mode)
Exemple #2
0
def FPTAS(number, capacity, weight_cost, scaling_factor=4):
    """Fully polynomial-time approximation scheme method for solving knapsack problem

    :param number: number of existing items
    :param capacity: the capacity of knapsack
    :param weight_cost: list of tuples like: [(weight, cost), (weight, cost), ...]
    :param scaling_factor: how much we want to be precise, bigger factor means coarser solution
    :return: tuple like: (best cost, best combination list(contains 1 and 0))
    """
    new_capacity = int(float(capacity) / scaling_factor)
    new_weight_cost = [(round(float(weight) / scaling_factor) + 1, cost) for weight, cost in weight_cost]
    return dynamic_programming(number, new_capacity, new_weight_cost)
Exemple #3
0
def FPTAS(number, capacity, weight_cost, scaling_factor=4):
    """Fully polynomial-time approximation scheme method for solving knapsack problem

    :param number: number of existing items
    :param capacity: the capacity of knapsack
    :param weight_cost: list of tuples like: [(weight, cost), (weight, cost), ...]
    :param scaling_factor: how much we want to be precise, bigger factor means coarser solution
    :return: tuple like: (best cost, best combination list(contains 1 and 0))
    """
    new_capacity = int(float(capacity) / scaling_factor)
    new_weight_cost = [(round(float(weight) / scaling_factor) + 1, cost)
                       for weight, cost in weight_cost]
    return dynamic_programming(number, new_capacity, new_weight_cost)
def davies_standard(x, fs):
    # % read wave file
    # %[x fs] = audioread(input);

    # convert to mono
    #x = np.mean(x,axis = 1)

    # if audio is not at 44khz resample
    if fs != 44100:
        x = resample(x, 44100, fs)

    # read beat tracking parameters
    p = bt_parms(0.01161)

    # generate the onset detection function
    # st_time = time.time()
    # df = librosa.onset.onset_strength(y=x,sr=44100)
    df = onset_detection_function(x,p)
    # print 'onset_detection_function runtime'
    # print time.time()-st_time

    # strip any trailing zeros
    while not df[-1]:
        df = df[0:len(df) - 1]

    # get periodicity path
    # st_time = time.time()
    ppath = periodicity_path(df, p)
    # print 'periodicity_path runtime'
    # print time.time()-st_time
    mode = 0  # use this to run normal algorithm
    # find beat locations
    # st_time = time.time()
    beats = dynamic_programming(df, p, ppath, mode)
    # print 'dynamic_programming runtime'
    # print time.time()-st_time
    return beats
Exemple #5
0
def FPTAS(number, capacity, weight_cost, scaling_factor=4):

    new_capacity = int(float(capacity) / scaling_factor)
    new_weight_cost = [(round(float(weight) / scaling_factor) + 1, cost)
                       for weight, cost in weight_cost]
    return dynamic_programming(number, new_capacity, new_weight_cost)
def detect(edges, points, density, width, B, pobj = .7, pbg = .2):
    """
    Finds the sidelines and an improved midline.
    
    Parameters
    ----------
    edges : array
        Oriented edge maps.
    points : list of pairs
        Points from coarse instantiation (tail to head).
    density : integer
        Density of features.
    width : integer
        Width of worm.
    B : double
        Penalty for deviation from model angle (smoothed coarse detection).
    pobj, pbg : double, optional
        Edge probability on the sidelines and on background.
    
    Returns
    -------
    points : list of pairs
        Smoothed worm points.
    regions : list
        Admissible regions for sideline points.
    angles_segments : array
        Angles between smoothed points.
    side : list
        Sideline.
    smooth_side : list
        Smoothed sideline.
    smooth_midline : list
        Smoothed midline.
    ll_edges : float
        Log-likelihood for edges.
    """

    n = len(points)
    
    # rescale and smooth worm points
    x, y = zip(*points)
    x = [density * __ + density//2 for __ in x]
    y = [density * __ + density//2 for __ in y]
    #points, angles_points, angles_segments = _spline((x,y), s=10, do_angles=True)
    #points, angles_points, angles_segments = _spline((x,y), s=5, do_angles=True)
    points, angles_points, angles_segments = _spline((x,y), s=3, do_angles=True)
    
    # choose admissible regions for dynamic programming
    im_shape = edges.shape[:2]
    regions = []

    # - tail
    _add_region(regions, points[0], angles_points[0] - PI_HALF - PI_FOURTH, im_shape, width, start=0, end=1)
    
    # - tail to head
    for i in range(1, n):
        _add_region(regions, points[i], angles_points[i] - PI_HALF, im_shape, width)
        
    # - head
    _add_region(regions, points[n-1], angles_points[n-1] - PI_FOURTH, im_shape, width)
    _add_region(regions, points[n-1], angles_points[n-1] - PI_EIGHTS, im_shape, width, start=0, end=1)
    _add_region(regions, points[n-1], angles_points[n-1], im_shape, width, start=0, end=1)
    _add_region(regions, points[n-1], angles_points[n-1] + PI_EIGHTS, im_shape, width, start=0, end=1)
    _add_region(regions, points[n-1], angles_points[n-1] + PI_FOURTH, im_shape, width)
    
    # - head to tail
    for i in range(n-1, 0, -1):
        _add_region(regions, points[i], angles_points[i] + PI_HALF, im_shape, width)
    
    # - tail
    _add_region(regions, points[0], angles_points[0] + PI_HALF + PI_FOURTH, im_shape, width, start=0, end=1)
    
    # determine model angles for line segments (no prior for head and tail)
    angles_segments = np.hstack((angles_segments, np.ones(6)*np.NAN, \
                                np.where(angles_segments[::-1] >= 0, angles_segments[::-1] - PI, angles_segments[::-1] + PI)))
    angles_segments[:1] = angles_segments[-1:] = np.NAN
    
    # set edge probabilities
    log_feat = np.log(pobj) - np.log(pbg)
    log_no_feat = np.log(1-pobj) - np.log(1-pbg)
    
    # run dynamic programming and store side lines
    m = len(regions)
    opt_vals, opt_choices = DP.dynamic_programming(edges, log_feat, log_no_feat, regions, angles_segments, B)
    opt_choice = np.argmax(opt_vals)
    ll_edges = opt_vals[opt_choice]
    p2 = regions[m-1][opt_choice, :]
    side = [p2]
    for j in range(m-2, -1, -1):
        opt_choice = opt_choices[j][opt_choice]
        p1 = regions[j][opt_choice, :]
        side.append(p1)
        p2 = p1
    
    # smooth side lines
    smooth_side = _spline(side, nr_of_points=2*150)
    
    # create midline
    midline = []
    for j in range(n+1):
        left = side[j]
        right = side[-(j+1)]
        mid = ((left[0]+right[0])//2, (left[1]+right[1])//2)
        midline.append(mid)
    if midline[-1] == midline[-2]: del midline[-1] # otherwise, spline error
        
    # smooth midline (except tip of tail and head)
    smooth_midline = _spline(midline, nr_of_points=150)
    smooth_midline[0] = midline[0]
    smooth_midline[-1] = midline[-1]
        
    return points, regions, side, smooth_side, smooth_midline, ll_edges
def detect(edges, points, density, width, B, pobj=.7, pbg=.2):
    """
    Finds the sidelines and an improved midline.
    
    Parameters
    ----------
    edges : array
        Oriented edge maps.
    points : list of pairs
        Points from coarse instantiation (tail to head).
    density : integer
        Density of features.
    width : integer
        Width of worm.
    B : double
        Penalty for deviation from model angle (smoothed coarse detection).
    pobj, pbg : double, optional
        Edge probability on the sidelines and on background.
    
    Returns
    -------
    points : list of pairs
        Smoothed worm points.
    regions : list
        Admissible regions for sideline points.
    angles_segments : array
        Angles between smoothed points.
    side : list
        Sideline.
    smooth_side : list
        Smoothed sideline.
    smooth_midline : list
        Smoothed midline.
    ll_edges : float
        Log-likelihood for edges.
    """

    n = len(points)

    # rescale and smooth worm points
    x, y = zip(*points)
    x = [density * __ + density // 2 for __ in x]
    y = [density * __ + density // 2 for __ in y]
    #points, angles_points, angles_segments = _spline((x,y), s=10, do_angles=True)
    #points, angles_points, angles_segments = _spline((x,y), s=5, do_angles=True)
    points, angles_points, angles_segments = _spline((x, y),
                                                     s=3,
                                                     do_angles=True)

    # choose admissible regions for dynamic programming
    im_shape = edges.shape[:2]
    regions = []

    # - tail
    _add_region(regions,
                points[0],
                angles_points[0] - PI_HALF - PI_FOURTH,
                im_shape,
                width,
                start=0,
                end=1)

    # - tail to head
    for i in range(1, n):
        _add_region(regions, points[i], angles_points[i] - PI_HALF, im_shape,
                    width)

    # - head
    _add_region(regions, points[n - 1], angles_points[n - 1] - PI_FOURTH,
                im_shape, width)
    _add_region(regions,
                points[n - 1],
                angles_points[n - 1] - PI_EIGHTS,
                im_shape,
                width,
                start=0,
                end=1)
    _add_region(regions,
                points[n - 1],
                angles_points[n - 1],
                im_shape,
                width,
                start=0,
                end=1)
    _add_region(regions,
                points[n - 1],
                angles_points[n - 1] + PI_EIGHTS,
                im_shape,
                width,
                start=0,
                end=1)
    _add_region(regions, points[n - 1], angles_points[n - 1] + PI_FOURTH,
                im_shape, width)

    # - head to tail
    for i in range(n - 1, 0, -1):
        _add_region(regions, points[i], angles_points[i] + PI_HALF, im_shape,
                    width)

    # - tail
    _add_region(regions,
                points[0],
                angles_points[0] + PI_HALF + PI_FOURTH,
                im_shape,
                width,
                start=0,
                end=1)

    # determine model angles for line segments (no prior for head and tail)
    angles_segments = np.hstack((angles_segments, np.ones(6)*np.NAN, \
                                np.where(angles_segments[::-1] >= 0, angles_segments[::-1] - PI, angles_segments[::-1] + PI)))
    angles_segments[:1] = angles_segments[-1:] = np.NAN

    # set edge probabilities
    log_feat = np.log(pobj) - np.log(pbg)
    log_no_feat = np.log(1 - pobj) - np.log(1 - pbg)

    # run dynamic programming and store side lines
    m = len(regions)
    opt_vals, opt_choices = DP.dynamic_programming(edges, log_feat,
                                                   log_no_feat, regions,
                                                   angles_segments, B)
    opt_choice = np.argmax(opt_vals)
    ll_edges = opt_vals[opt_choice]
    p2 = regions[m - 1][opt_choice, :]
    side = [p2]
    for j in range(m - 2, -1, -1):
        opt_choice = opt_choices[j][opt_choice]
        p1 = regions[j][opt_choice, :]
        side.append(p1)
        p2 = p1

    # smooth side lines
    smooth_side = _spline(side, nr_of_points=2 * 150)

    # create midline
    midline = []
    for j in range(n + 1):
        left = side[j]
        right = side[-(j + 1)]
        mid = ((left[0] + right[0]) // 2, (left[1] + right[1]) // 2)
        midline.append(mid)
    if midline[-1] == midline[-2]: del midline[-1]  # otherwise, spline error

    # smooth midline (except tip of tail and head)
    smooth_midline = _spline(midline, nr_of_points=150)
    smooth_midline[0] = midline[0]
    smooth_midline[-1] = midline[-1]

    return points, regions, side, smooth_side, smooth_midline, ll_edges