Ejemplo n.º 1
0
def feature_matrix2(trips, n=None):
    velocities = [compute_velocity(trip) for trip in trips]
    speeds = [compute_scalar(velocity) for velocity in velocities]
    maxlen = len(max(speeds, key=len)) if n is None else n
    print("maxlen:", maxlen)
    res = np.array([np.fft.fft(s, n=maxlen) for s in speeds])
    print(res.shape)
    return res
Ejemplo n.º 2
0
def feature_matrix2(trips, n=None):
    velocities = [compute_velocity(trip) for trip in trips]
    speeds = [compute_scalar(velocity) for velocity in velocities]
    maxlen = len(max(speeds, key=len)) if n is None else n
    print("maxlen:", maxlen)
    res = np.array([np.fft.fft(s, n=maxlen) for s in speeds])
    print(res.shape)
    return res
Ejemplo n.º 3
0
def feature_vector(trip):
    velocity = compute_velocity(trip)
    velocity5 = n_step_diff(trip, n=5)
    acceleration = np.gradient(velocity)
    acceleration5 = np.gradient(velocity5)
    cos_dt = [cos_between(a, np.array([1, 0])) for a in acceleration5]
    angle_dt = [np.arccos(c) for c in cos_dt]
    speed = compute_scalar(velocity)
    corner_features = corners_features(velocity)
    acc_features = acceleration_features(velocity, angle_dt, cos_dt, speed)
    return np.hstack((corner_features, acc_features))
Ejemplo n.º 4
0
def feature_vector(trip):
    velocity = compute_velocity(trip)
    velocity5 = n_step_diff(trip, n=5)
    acceleration = np.gradient(velocity)
    acceleration5 = np.gradient(velocity5)
    cos_dt = [cos_between(a, np.array([1, 0])) for a in acceleration5]
    angle_dt = [np.arccos(c) for c in cos_dt]
    speed = compute_scalar(velocity)
    corner_features = corners_features(velocity)
    acc_features = acceleration_features(velocity, angle_dt, cos_dt, speed)
    return np.hstack((corner_features, acc_features))
Ejemplo n.º 5
0
def main():
    trips = read_trips(sys.argv[1])
    velocities = [compute_velocity(trip) for trip in trips]
    speeds = [compute_scalar(v) for v in velocities]
    features = np.array([corners_features(v, s) for v, s in zip(velocities, speeds)])
    #print(features)
    n = random.randint(0, 199)
    n = 29
    print("Trip number is:", n)
    (corners, angles) = identify_corners(velocities[n])
    trip = trips[n]
    import matplotlib.pyplot as plt
    plt.scatter(trips[n][:,0], trips[n][:,1])#, c=speeds[n])
    for start, end in corners:
        t = trip[list(range(start, end+1))]
        plt.plot(t[:,0], t[:,1], 'r', linewidth=4.0)
    plt.axis('equal')
    plt.show()
Ejemplo n.º 6
0
def corners_features(velocity, dists=None):
    if dists is None:
        dists = compute_scalar(velocity)
    corners, angles = identify_corners(velocity, dists)
    if len(corners) == 0:
        return np.zeros(14)
    corner_speeds = [dists[range(c1, c2+1)] for c1, c2 in corners]
    corner_speeds_contig = list(chain(*corner_speeds))
    bins = np.linspace(0, 14, 10)
    corner_speed_hist, bs = np.histogram(corner_speeds_contig, bins=bins)
    mean_corner_speeds = [np.mean(cs) for cs in corner_speeds]
    max_speed = np.max(mean_corner_speeds)
    min_speed = np.min(mean_corner_speeds)
    mean_speed = np.mean(mean_corner_speeds)
    res = np.hstack((
        np.array([float(len(corners))/np.sum(dists), np.mean(angles), max_speed, min_speed, mean_speed]),
        corner_speed_hist
    ))
    return res
Ejemplo n.º 7
0
def main():
    trips = read_trips(sys.argv[1])
    velocities = [compute_velocity(trip) for trip in trips]
    speeds = [compute_scalar(v) for v in velocities]
    features = np.array(
        [corners_features(v, s) for v, s in zip(velocities, speeds)])
    #print(features)
    n = random.randint(0, 199)
    n = 29
    print("Trip number is:", n)
    (corners, angles) = identify_corners(velocities[n])
    trip = trips[n]
    import matplotlib.pyplot as plt
    plt.scatter(trips[n][:, 0], trips[n][:, 1])  #, c=speeds[n])
    for start, end in corners:
        t = trip[list(range(start, end + 1))]
        plt.plot(t[:, 0], t[:, 1], 'r', linewidth=4.0)
    plt.axis('equal')
    plt.show()
Ejemplo n.º 8
0
def corners_features(velocity, dists=None):
    if dists is None:
        dists = compute_scalar(velocity)
    corners, angles = identify_corners(velocity, dists)
    if len(corners) == 0:
        return np.zeros(14)
    corner_speeds = [dists[range(c1, c2 + 1)] for c1, c2 in corners]
    corner_speeds_contig = list(chain(*corner_speeds))
    bins = np.linspace(0, 14, 10)
    corner_speed_hist, bs = np.histogram(corner_speeds_contig, bins=bins)
    mean_corner_speeds = [np.mean(cs) for cs in corner_speeds]
    max_speed = np.max(mean_corner_speeds)
    min_speed = np.min(mean_corner_speeds)
    mean_speed = np.mean(mean_corner_speeds)
    res = np.hstack((np.array([
        float(len(corners)) / np.sum(dists),
        np.mean(angles), max_speed, min_speed, mean_speed
    ]), corner_speed_hist))
    return res
Ejemplo n.º 9
0
def identify_corners(velocity, dist=None):
    """
    Calculates where the corners are from a sequence of velocities
    Arguments:
        velocity: a sequence of 2D velocity vectors
        dist: (Optional) a sequence of scalar distances if this has already been calculated. 
    Returns:
        sequences of indices in a list. Each sequence corresponds to a corner where the indices are positions from the original trip array
    """
    if dist is None:
        dist = compute_scalar(velocity)
    res = []
    angles = []
    i = 0
    while i < len(velocity) - 1:
        if angle_between(velocity[i], velocity[i + 1]) > np.radians(5):
            start = i
            start_vec = velocity[i]
            prev_corner_angle = 0
            currdist = dist[i]
            if dist[i] < 1:
                #if the distance from last is small it's probably noise
                i += 1
                continue
            for j, (v, d) in enumerate(zip(velocity[i + 1:], dist[i + 1:])):
                if d < 1:
                    #if the distance from last is small it's probably noise
                    continue
                currdist += d
                corner_angle = angle_between(start_vec, v)
                if currdist > 70 or corner_angle <= prev_corner_angle + np.radians(
                        1):
                    if corner_angle > np.radians(45):
                        i = i + j
                        res.append([start, i])
                        angles.append(corner_angle)
                        break
                    else:
                        break
                prev_corner_angle = corner_angle
        i += 1
    return (res, angles)
Ejemplo n.º 10
0
def identify_corners(velocity, dist=None):
    """
    Calculates where the corners are from a sequence of velocities
    Arguments:
        velocity: a sequence of 2D velocity vectors
        dist: (Optional) a sequence of scalar distances if this has already been calculated. 
    Returns:
        sequences of indices in a list. Each sequence corresponds to a corner where the indices are positions from the original trip array
    """
    if dist is None:
        dist = compute_scalar(velocity)
    res = []
    angles = []
    i = 0
    while i < len(velocity) - 1:
        if angle_between(velocity[i], velocity[i+1]) > np.radians(5):
            start = i
            start_vec = velocity[i]
            prev_corner_angle = 0
            currdist = dist[i]
            if dist[i] < 1:
                #if the distance from last is small it's probably noise
                i+=1
                continue
            for j, (v, d) in enumerate(zip(velocity[i+1:], dist[i+1:])):
                if d < 1:
                    #if the distance from last is small it's probably noise
                    continue
                currdist += d
                corner_angle = angle_between(start_vec, v)
                if currdist > 70 or corner_angle <= prev_corner_angle + np.radians(1):
                    if corner_angle > np.radians(45):
                        i = i + j
                        res.append([start, i])
                        angles.append(corner_angle)
                        break
                    else:
                        break
                prev_corner_angle = corner_angle
        i += 1
    return (res, angles)
Ejemplo n.º 11
0
def feature_vector(trip):
    velocity = compute_velocity(trip)
    speed = compute_scalar(velocity)
    corner_features = corners_features(velocity)
    acc_features = acceleration_features(speed)
    return np.hstack((corner_features, acc_features))
Ejemplo n.º 12
0
def feature_vector(trip):
    velocity = compute_velocity(trip)
    speed = compute_scalar(velocity)
    corner_features = corners_features(velocity)
    acc_features = acceleration_features(speed)
    return np.hstack((corner_features, acc_features))