def plot_trajectories_from_dataset(dataset, keys, interpolated_data='hide'): ''' interpolated_data - if trajectories contain interpolated data, use 'hide' to hide that data, 'dotted' to show it as a dotted line, or 'show' to show it in its entirety ''' import fly_plot_lib.plot as fpl # download at: https://github.com/florisvb/FlyPlotLib from fly_plot_lib.colormaps import viridis as viridis path = dataset.config.path bgimg_filename = get_filename(path, 'bgimg_N1.png') bgimg = plt.imread(bgimg_filename) fig = plt.figure() ax = fig.add_subplot(111) ax.set_aspect('equal') ax.imshow(bgimg, cmap='gray') for key in keys: trajec = dataset.trajec(key) if 'interpolated' in trajec.__dict__.keys(): interpolated_indices = np.where(trajec.interpolated==1)[0] if interpolated_data == 'dotted': r = np.arange(0, len(interpolated_indices), 5) interpolated_indices = interpolated_indices[r] if interpolated_data != 'show': trajec.position_x[interpolated_indices] = np.nan trajec.position_y[interpolated_indices] = np.nan l = -1 fpl.colorline(ax, trajec.position_x[0:l], trajec.position_y[0:l], trajec.time_epoch[0:l]-trajec.time_epoch[0:l][0], linewidth=2, colormap='none', norm=None, zorder=1, alpha=1, linestyle='solid', cmap=viridis) fpl.adjust_spines(ax, []) ax.set_frame_on(False)
def plot_trajectories_from_dataset(dataset, keys, interpolated_data='hide'): ''' interpolated_data - if trajectories contain interpolated data, use 'hide' to hide that data, 'dotted' to show it as a dotted line, or 'show' to show it in its entirety ''' import matplotlib.pyplot as plt # download at: https://github.com/florisvb/FlyPlotLib import fly_plot_lib.plot as fpl from fly_plot_lib.colormaps import viridis as viridis path = dataset.config.path bgimg_filename = get_filename(path, 'bgimg_N1.png') bgimg = plt.imread(bgimg_filename) fig = plt.figure() ax = fig.add_subplot(111) ax.set_aspect('equal') ax.imshow(bgimg, cmap='gray') for key in keys: trajec = dataset.trajec(key) if 'interpolated' in trajec.__dict__.keys(): interpolated_indices = np.where(trajec.interpolated==1)[0] if interpolated_data == 'dotted': r = np.arange(0, len(interpolated_indices), 5) interpolated_indices = interpolated_indices[r] if interpolated_data != 'show': trajec.position_x[interpolated_indices] = np.nan trajec.position_y[interpolated_indices] = np.nan l = -1 fpl.colorline(ax, trajec.position_x[0:l], trajec.position_y[0:l], trajec.time_epoch[0:l]-trajec.time_epoch[0:l][0], linewidth=2, colormap='none', norm=None, zorder=1, alpha=1, linestyle='solid', cmap=viridis) fpl.adjust_spines(ax, []) ax.set_frame_on(False)
def cartesian_spagetti(ax, dataset, axes=[0,1], keys=None, nkeys=300, start_key=0, show_saccades=False, keys_to_highlight=[], colormap='jet', color='gray', color_attribute=None, norm=None, show_start=True): if keys is None: keys = dataset.trajecs.keys() else: if type(keys) is not list: keys = [keys] if nkeys < len(keys): last_key = np.min([len(keys), start_key+nkeys]) keys = keys[start_key:last_key] keys.extend(keys_to_highlight) for key in keys: trajec = dataset.trajecs[key] frames = np.arange(trajec.frame_range_roi[0], trajec.frame_range_roi[-1]) if key in keys_to_highlight: alpha = 1 linewidth = 1 color = 'black' zorder = 500 ax.plot(trajec.positions[frames,axes[0]], trajec.positions[frames,axes[1]], '-', color='black', alpha=1, linewidth=linewidth, zorder=zorder) else: alpha = 1 linewidth = 0.5 color = color zorder = 100 if color_attribute is not None: c = trajec.__getattribute__(color_attribute) fpl.colorline(ax,trajec.positions[frames,axes[0]], trajec.positions[frames,axes[1]], c[frames], colormap=colormap, linewidth=linewidth, alpha=alpha, zorder=zorder, norm=norm) else: ax.plot(trajec.positions[frames,axes[0]], trajec.positions[frames,axes[1]], '-', color=color, alpha=1, linewidth=linewidth, zorder=zorder) if show_saccades: if len(trajec.sac_ranges) > 0: for sac_range in trajec.sac_ranges: angle_subtended = trajec.angle_subtended_by_post[sac_range[0]] if sac_range[0] in frames and angle_subtended < 100.*np.pi/180.: #if 1: ax.plot(trajec.positions[sac_range,0], trajec.positions[sac_range,1], '-', color='red', alpha=1, linewidth=linewidth, zorder=zorder+1) #sac = patches.Circle( (trajec.positions[sac_range[0],0], trajec.positions[sac_range[0],1]), radius=0.001, facecolor='red', edgecolor='none', alpha=alpha2, zorder=zorder+1) #ax.add_artist(sac) if show_start: start = patches.Circle( (trajec.positions[frames[0],axes[0]], trajec.positions[frames[0],axes[1]]), radius=0.004, facecolor='green', edgecolor='none', linewidth=0, alpha=1, zorder=zorder+1) ax.add_artist(start)
def calc_positions_normalized_by_speed(trajec, normspeed=0.2, plot=False): distance_travelled = np.cumsum(trajec.speed) distance_travelled_normalized = np.arange(distance_travelled[0], distance_travelled[-1], normspeed) warped_time = np.interp(distance_travelled_normalized, distance_travelled, trajec.time_fly) trajec.positions_normalized_by_speed = np.zeros([len(warped_time), 3]) for i in range(3): trajec.positions_normalized_by_speed[:,i] = np.interp(warped_time, trajec.time_fly, trajec.positions[:,i]) trajec.time_normalized_by_speed = warped_time if plot: fig = plt.figure() ax = fig.add_subplot(111) x = trajec.positions[:,0] y = trajec.positions[:,1] speed = trajec.speed fpl.colorline(ax,x,y,speed) x_warped = trajec.positions_normalized_by_speed[:,0] y_warped = trajec.positions_normalized_by_speed[:,1] ax.plot(x_warped, y_warped, '.')