def save_image_sequence(npmovie, frames, filename): # filename should be path + filename with initial number scheme + .extension desired, ie. jpg or png # the path should exist prior to calling the function imname = filename[filename.rfind("/") + 1 :] impath = filename[0 : filename.rfind("/") + 1] imname_no_extension = imname[0 : imname.rfind(".")] imext = imname[imname.rfind(".") :] imbasename = imname_no_extension.rstrip("0123456789") imnumlen = len(imname_no_extension) - len(imbasename) i = -1 for frame in frames: i += 1 stri = str(i) while len(stri) < imnumlen: stri = "0" + stri fname = impath + imbasename + stri + imext strobe = strobe_from_npmovie(npmovie, 1, frames=[frame, frame + 1]) strobe = nim.rotate_image(strobe, np.array([[0, 1], [-1, 0]])) plt.imsave(fname, strobe, origin="lower")
def plot_movie_data(npmovie, show_wings=False, figure=None, legthresh=50, show_vision=True): calc_frame_of_landing(npmovie) frames = get_active_frames(npmovie) time = np.array(frames) * 1 / float(npmovie.fps) cl = xy_kalman(npmovie, figure=figure, frames=frames, colormap="gray") # get strobe image: strobe_img = strobe_from_npmovie(npmovie, interval=210, frames=frames) strobe_img = nim.rotate_image(strobe_img, np.array([[0, 1], [-1, 0]])) cl.ax0.imshow(strobe_img, pyplot.get_cmap("gray"), origin="lower") # axis parameters for subplots nxticks = 5 nyticks = 3 # subplot parameters n = 8 h = (0.7 - 0.05 * (n - 2)) / float(n) subplots = [] for s in range(n): ax = cl.fig.add_axes([0.47, 0.1 + (h + 0.05) * s, 0.2, h]) subplots.append(ax) xticks = np.linspace(0, 1, num=nxticks, endpoint=True).tolist() p = 0 subplots[p].plot(time, npmovie.flycoord.worldangle[frames]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_xlabel("time, seconds") subplots[p].set_ylabel("world angle") p += 1 subplots[p].plot(time, npmovie.flycoord.postangle[frames]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_xlabel("time, seconds") subplots[p].set_ylabel("angle to post") p += 1 subplots[p].plot(time, npmovie.flycoord.slipangle[frames]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_ylabel("slip angle") p += 1 subplots[p].plot(time, npmovie.flycoord.velocities[frames, 0]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_ylabel("forward vel") p += 1 subplots[p].plot(time, npmovie.flycoord.velocities[frames, 1]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_ylabel("sideways vel") p += 1 subplots[p].plot(time, npmovie.flycoord.speed[frames]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_ylabel("speed") p += 1 subplots[p].plot(time, npmovie.kalmanobj.legs[frames]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_ylabel("leg extension") p += 1 subplots[p].plot(time, npmovie.flycoord.dist_to_post[frames]) subplots[p].set_xticks(xticks) default_yticks = subplots[p].get_yticks() yticks = np.linspace(default_yticks[0], default_yticks[-1], nyticks, endpoint=True).tolist() subplots[p].set_yticks(yticks) subplots[p].set_ylabel("dist to post") interval = 70 i = frames[0] while i < frames[-1]: # plot body orientation vector center = sa1_to_flydra_data_transform(npmovie.kalmanobj.positions[i], fix_sign=True) long_axis = sa1_to_flydra_img_transform(npmovie.kalmanobj.long_axis[i], fix_sign=False) * -1 factor = npmovie.obj.axis_ratio[i][0] * 4.0 factor = min(15.0, factor) dx = long_axis[1] * factor dy = long_axis[0] * factor legs = npmovie.kalmanobj.legs[i] #### get color from kmeans clusters ### colormap = plt.get_cmap("jet") if npmovie.cluster is not None: speed = np.interp(npmovie.timestamps[i], npmovie.trajec.epoch_time, npmovie.trajec.speed) slipangle = npmovie.flycoord.slipangle[i] print npmovie.id, i dyaw = np.interp( npmovie.timestamps[i], npmovie.trajec.epoch_time[npmovie.sync2d3d.frames3d], npmovie.sync2d3d.smoothyaw[:, 1], ) obs = np.nan_to_num(np.array([np.abs(dyaw), speed, np.abs(slipangle)])) cluster = kn.get_cluster_for_data(npmovie, obs) color = colormap((cluster) / float(npmovie.cluster_means.shape[0])) else: speed = np.interp(npmovie.timestamps[i], npmovie.trajec.epoch_time, npmovie.trajec.speed) color = colormap(speed) print "color: ", color arrow = Arrow(center[0], center[1], dx, dy, width=1.0, color=color) cl.ax0.add_artist(arrow) if show_wings: # plot wing orientation vectors wingR = npmovie.kalmanobj.wingcenterR[i] if not np.isnan(wingR[0]): arrow = Arrow(center[0], center[1], wingR[1] - 30, wingR[0] - 30, width=1.0, color="b") cl.ax0.add_artist(arrow) wingL = npmovie.kalmanobj.wingcenterL[i] if not np.isnan(wingL[0]): arrow = Arrow(center[0], center[1], wingL[1] - 30, wingL[0] - 30, width=1.0, color="b") cl.ax0.add_artist(arrow) i += interval # plt.show() try: if npmovie.vision_timeseries is not None: cl.vision_axes = [None for m in range(3)] for m in range(3): print print m, len(npmovie.vision_timeseries) cl.vision_axes[m] = cl.fig.add_axes([0.75, 0.1 + 0.25 * m, 0.2, 0.2]) cl.vision_axes[m].imshow(npmovie.vision_timeseries[m], pyplot.get_cmap("gray"), origin="lower") cl.vision_axes[m].set_xticks([]) time_raw = npmovie.timestamps[npmovie.sync2d3d.frames2d] - npmovie.timestamps[0] time = np.array([float(int(1000.0 * time_raw[i])) / 1000.0 for i in range(len(time_raw))]) cl.vision_axes[m].set_yticks(np.linspace(0, npmovie.vision_timeseries[m].shape[0], 5).tolist()) cl.vision_axes[m].set_yticklabels(np.linspace(time[0], time[-1], 5).tolist()) xlabel = "horizontal slice of fly eye at " + str((m - 1) * 45) + " deg" cl.vision_axes[m].set_xlabel(xlabel) cl.vision_axes[m].set_ylabel("time, sec") except: print "no vision timeseries" return cl