Пример #1
0
def plot_trajectory(ax,arena,ldf,tdf,hdf,geom,debug):
    madplot.plot_tracked_trajectory(ax, tdf, arena,
                debug_plot=debug,
                color='k',
    )
    ax.add_patch(arena.get_patch(color='k', alpha=0.1))

    patch = arena.get_intersect_patch(geom, fill=True, color='r', closed=True, alpha=0.2)
    if patch is not None:
        ax.add_patch(patch)
Пример #2
0
def plot_trajectory(ax, arena, ldf, tdf, hdf, geom, debug):
    madplot.plot_tracked_trajectory(
        ax,
        tdf,
        arena,
        debug_plot=debug,
        color='k',
    )
    ax.add_patch(arena.get_patch(color='k', alpha=0.1))

    patch = arena.get_intersect_patch(geom,
                                      fill=True,
                                      color='r',
                                      closed=True,
                                      alpha=0.2)
    if patch is not None:
        ax.add_patch(patch)
Пример #3
0
BAG = '/mnt/strawscience/data/FlyMAD/velocity_test/2013-11-21-18-00-00.bag'
oid = 52251

smooth = True
smoothstr = {True:'smooth',False:'nosmooth'}[smooth]

arena = madplot.Arena('mm')
geom, dfs = madplot.load_bagfile(BAG, arena, smooth=smooth)
l_df = dfs["targeted"]
t_df = dfs["tracked"]
h_df = dfs["ttm"]

fig = plt.figure('traj %s' % smoothstr)
fig.suptitle('trajectory %s' % smoothstr)
ax = fig.add_subplot(1,1,1)
madplot.plot_tracked_trajectory(ax, t_df, arena, debug_plot=False)
ax.add_patch(arena.get_patch(fill=False))
ax.set_xlabel('position (%s)' % arena.unit)
fig.savefig('dorothea_pos_%s.png' % smoothstr)

df = t_df[t_df['tobj_id'] == oid].resample('100L')

#print df.iloc[39].index[0] - df.iloc[0]

times = [(ix - df.index[0]).total_seconds() for ix in df.index]

#print times[39]

######yyyyyyyyyy
fig = plt.figure('y %s' % smoothstr,figsize=(10,8))
fig.suptitle('y %s' % smoothstr)
Пример #4
0
def get_df(smooth):

    smoothstr = {True:'smooth',False:'nosmooth'}[smooth]

    geom, dfs = madplot.load_bagfile(BAG, arena, smooth=smooth)
    t_df = dfs["tracked"]

    fig = plt.figure('traj %s' % smoothstr)
    fig.suptitle('trajectory %s' % smoothstr)
    ax = fig.add_subplot(1,1,1)
    madplot.plot_tracked_trajectory(ax, t_df, arena, debug_plot=False)
    ax.add_patch(arena.get_patch(fill=False))
    ax.set_xlabel('position (%s)' % arena.unit)
    fig.savefig('testsmooth_mwb_pos_%s.png' % smoothstr)

    df = t_df[t_df['tobj_id'] == oid]

    #print df.iloc[39].index[0] - df.iloc[0]

    times = [(ix - df.index[0]).total_seconds() for ix in df.index]

    #print times[39]

    ######yyyyyyyyyy
    fig = plt.figure('y %s' % smoothstr,figsize=(10,8))
    fig.suptitle('y %s' % smoothstr)
    ax = fig.add_subplot(2,2,1)
    ax.set_title('position')
    ax.plot(times, df['y_px'].values)
    ax.set_ylabel('y (px/s)')
    ax = fig.add_subplot(2,2,2)
    ax.set_title('converted position')
    ax.plot(times, df['y'].values)
    ax.set_ylabel('y (%s)' % arena.unit)
    ax = fig.add_subplot(2,2,3)
    ax.set_title('velocity')
    ax.plot(times, df['vy_px'].values)
    ax.set_ylabel('y-velocity (px/s)')
    ax = fig.add_subplot(2,2,4)
    ax.set_title('converted velocity')
    ax.plot(times, df['vy'].values)
    ax.set_ylabel('y-velocity (%s/s)' % arena.unit)
    fig.savefig('testsmooth_mwb_y_%s.png' % smoothstr)

    ######xxxxxxxxxxx
    fig = plt.figure('x %s' % smoothstr,figsize=(10,8))
    fig.suptitle('x %s' % smoothstr)
    ax = fig.add_subplot(2,2,1)
    ax.set_title('position')
    ax.plot(times, df['x_px'].values)
    ax.set_ylabel('x (px/s)')
    ax = fig.add_subplot(2,2,2)
    ax.set_title('converted position')
    ax.plot(times, df['x'].values)
    ax.set_ylabel('x (%s)' % arena.unit)
    ax = fig.add_subplot(2,2,3)
    ax.set_title('velocity')
    ax.plot(times, df['vx_px'].values)
    ax.set_ylabel('x-velocity (px/s)')
    ax = fig.add_subplot(2,2,4)
    ax.set_title('converted velocity')
    ax.plot(times, df['vx'].values)
    ax.set_ylabel('x-velocity (%s/s)' % arena.unit)
    fig.savefig('testsmooth_mwb_x_%s.png' % smoothstr)

    #######
    df = t_df[t_df['tobj_id'] == oid].resample('500L')
    times = [(ix - df.index[0]).total_seconds() for ix in df.index]

    fig = plt.figure('speed %s' % smoothstr,figsize=(8,4))
    fig.suptitle('speed %s' % smoothstr)
    ax = fig.add_subplot(1,2,1)
    ax.plot(times, df['v_px'].values)
    ax.set_ylabel('speed (px/s)')
    ax = fig.add_subplot(1,2,2)
    ax.plot(times, df['v'].values)
    ax.set_ylabel('speed (%s/s)' % arena.unit)
    fig.savefig('testsmooth_mwb_speed_%s.png' % smoothstr)

    return t_df[t_df['tobj_id'] == oid]
Пример #5
0
def plot_data(path, dat, debug_plot):

    if os.path.isdir(path):
        plotdir = path
        conf = dat
    else:
        if path.endswith('.json'):
            conf = json.load(open(path))
        else:
            conf = dat
        plotdir = os.path.dirname(path)

    plot_exps = conf.get('_plot', ['coupled'])

    exps = [e for e in plot_exps if e in dat]
    exps_colors = [plt.cm.gnuplot(i) for i in np.linspace(0, 1.0, len(exps))]

    if '_ntrials' in conf:
        ntrials = conf['_ntrials']
    else:
        #check all trials have the same number
        trial_lens = set(map(len, (dat[e] for e in exps)))
        if len(trial_lens) != 1:
            raise Exception("experiments contain different numbers of trials")

        ntrials = trial_lens.pop()

    pct_in_area_total = {k: [] for k in exps}
    pct_in_area_per_time = {k: [] for k in exps}
    pct_in_area_per_time_lbls = {k: [] for k in exps}
    latency_to_first_contact = {k: [] for k in exps}
    velocity_outside_area = {k: [] for k in exps}
    velocity_inside_area = {k: [] for k in exps}
    path_length = {k: [] for k in exps}

    pct_in_area_per_time_bins = range(0, 300, 30)

    for exp in exps:
        ordered_trials = dat[exp][0:ntrials]

        gs = _get_plot_gs(ordered_trials)
        fig = plt.figure("%s Trajectories" % exp.title(), figsize=(16, 8))

        gsh = _get_plot_gs(ordered_trials)
        figh = plt.figure("%s Trajectories (Hist)" % exp.title(),
                          figsize=(16, 8))

        for i, trial in enumerate(ordered_trials):
            label = trial['label']
            ldf, tdf, hdf, geom = trial['data']

            dbg_plot_title = " %s %s" % (exp, label)

            print exp.title(), label

            ax = fig.add_subplot(gs[i])
            madplot.plot_tracked_trajectory(ax,
                                            tdf,
                                            arena,
                                            debug_plot=debug_plot,
                                            title=dbg_plot_title)

            patch = arena.get_intersect_patch(geom,
                                              fill=True,
                                              color='r',
                                              closed=True,
                                              alpha=0.4,
                                              zorder=9)
            if patch is not None:
                ax.add_patch(patch)

            ax.add_patch(arena.get_patch(fill=False, color='k', zorder=10))

            ok = tdf.dropna(axis=0, how='any', subset=['x', 'y'])
            axh = figh.add_subplot(gs[i])
            axh.set_aspect('equal')
            xlim, ylim = arena.get_limits()
            axh.set_xlim(*xlim)
            axh.set_ylim(*ylim)

            #manually scale the lognorm
            lognorm = matplotlib.colors.LogNorm(vmin=1e-7, vmax=1e-3)
            #(counts, xedges, yedges, Image)
            _, _, _, _im = axh.hist2d(ok['x'],
                                      ok['y'],
                                      bins=30,
                                      range=(xlim, ylim),
                                      normed=True,
                                      norm=lognorm)

            l_f = matplotlib.ticker.LogFormatter(10, labelOnlyBase=False)
            figh.colorbar(_im, ax=axh, format=l_f)

            for _ax in (ax, axh):
                _ax.set_title(label)
                _ax.xaxis.set_visible(False)
                _ax.yaxis.set_visible(False)

            pct_in_area_per_time_lbls[exp].append(label)

            pct_in_area_per_time[exp].append(
                madplot.calculate_time_in_area(tdf, 300,
                                               pct_in_area_per_time_bins))

            pct_in_area_total[exp].append(
                madplot.calculate_pct_in_area_per_objid_only_vals(tdf))

            tts, vel_out, vel_in, path_l = madplot.calculate_latency_and_velocity_to_stay(
                tdf,
                holdtime=20,
                tout_reset_time=2,
                arena=arena,
                geom=geom,
                debug_plot=debug_plot,
                title=dbg_plot_title)

            latency_to_first_contact[exp].append(tts)
            velocity_outside_area[exp].append(vel_out)
            velocity_inside_area[exp].append(vel_in)
            path_length[exp].append(path_l)

        fig.savefig(os.path.join(plotdir, '%s_trajectories.png' % exp))

        #need more colors
        fig = plt.figure("%s Time in Area" % exp.title())
        ax = fig.add_subplot(1, 1, 1)

        colormap = plt.cm.gnuplot
        ax.set_color_cycle(madplot.colors_hsv_circle(len(ordered_trials)))

        for lbl, pct in zip(pct_in_area_per_time_lbls[exp],
                            pct_in_area_per_time[exp]):
            mean = pct.mean(axis=0)
            std = pct.mean(axis=0)
            ax.plot(pct_in_area_per_time_bins,
                    mean,
                    linestyle='solid',
                    label=lbl)

        ax.legend()

        plt.savefig(os.path.join(plotdir, '%s_time.png' % exp))

    ind = np.arange(ntrials)  # the x locations for the groups
    width = 1.0 / len(exps)  # no gaps between
    width -= (0.2 * width)  # 20% gap
    ticklabels = [str(i) for i in range(ntrials)]

    #plot time in area percent
    _plot_bar_and_line(pct_in_area_total, exps, 'Time in Area', 'Trial',
                       'Percentage of time spent in area', ind, width, ntrials,
                       ticklabels, exps_colors, 'timeinarea', plotdir)

    #plot latency to first 20s in area
    _plot_bar_and_line(latency_to_first_contact, exps,
                       'Latency to first 20s contact', 'Trial',
                       'Latency to first 20s contact', ind, width, ntrials,
                       ticklabels, exps_colors, 'latency', plotdir)

    #velocity
    _plot_bar_and_line(velocity_outside_area, exps, 'Velocity Outside Area',
                       'Trial', 'Velocity Outside Area', ind, width, ntrials,
                       ticklabels, exps_colors, 'velocity_out', plotdir)
    _plot_bar_and_line(velocity_inside_area, exps, 'Velocity Inside Area',
                       'Trial', 'Velocity Inside Area', ind, width, ntrials,
                       ticklabels, exps_colors, 'velocity_in', plotdir)

    #path length
    _plot_bar_and_line(path_length, exps, 'Path Length', 'Trial',
                       'Path Length', ind, width, ntrials, ticklabels,
                       exps_colors, 'path_length', plotdir)
Пример #6
0
def prepare_data(path, arena, smoothstr, smooth, medfilt, gts):

    pooldf = DataFrame()
    for csvfile in sorted(glob.glob(path + "/*.csv")):
        cache_args = os.path.basename(csvfile), arena, smoothstr
        cache_fname = csvfile+'.madplot-cache'

        results = madplot.load_bagfile_cache(cache_args, cache_fname)
        if results is None:
            results = flymad_analysis.load_and_smooth_csv(csvfile, arena, smooth)
            if results is not None:
                #update the cache
                madplot.save_bagfile_cache(results, cache_args, cache_fname)
            else:
                print "skipping", csvfile
                continue

        df,dt,experimentID,date,time,genotype,laser,repID = results

        #we plot head v thorax v nolaser (so for the same of plotting, consider
        #these the genotypes
        genotype = genotype + '-' + laser

        if genotype not in gts:
            print "\tskipping genotype", genotype
            continue

        if 0:
            fig = plt.figure()
            fig.suptitle(os.path.basename(csvfile))
            ax = fig.add_subplot(1,1,1)
            df['experiment'] = 1
            df['tobj_id'] = 1
            madplot.plot_tracked_trajectory(ax, df, arena,
                        debug_plot=False,
                        color='k',
            )
            ax.add_patch(arena.get_patch(color='k', alpha=0.1))

        duration = (df.index[-1] - df.index[0]).total_seconds()
        if duration < EXPERIMENT_DURATION:
            print "\tmissing data", csvfilefn
            continue

        print "\t%ss experiment" % duration

        #MAXIMUM SPEED = 300:
        df['v'][df['v'] >= 300] = np.nan
        df['v'] = df['v'].fillna(method='ffill')

        try:
            df = flymad_analysis.align_t_by_laser_on(
                    df, min_experiment_duration=EXPERIMENT_DURATION,
                    align_first_only=True,
                    exact_num_ranges=1)
        except flymad_analysis.AlignError, err:
            print "\talign error %s (%s)" % (csvfile, err)
            continue

        #median filter
        if medfilt:
            df['v'] = scipy.signal.medfilt(df['v'].values, medfilt)

        df['obj_id'] = flymad_analysis.create_object_id(date,time)
        df['Genotype'] = genotype
        df['lasergroup'] = laser

        pooldf = pd.concat([pooldf, df])
Пример #7
0
def prepare_data(path, arena, smoothstr, smooth, medfilt, gts):

    pooldf = DataFrame()
    for csvfile in sorted(glob.glob(path + "/*.csv")):
        cache_args = os.path.basename(csvfile), arena, smoothstr
        cache_fname = csvfile + '.madplot-cache'

        results = madplot.load_bagfile_cache(cache_args, cache_fname)
        if results is None:
            results = flymad_analysis.load_and_smooth_csv(
                csvfile, arena, smooth)
            if results is not None:
                #update the cache
                madplot.save_bagfile_cache(results, cache_args, cache_fname)
            else:
                print "skipping", csvfile
                continue

        df, dt, experimentID, date, time, genotype, laser, repID = results

        #we plot head v thorax v nolaser (so for the same of plotting, consider
        #these the genotypes
        genotype = genotype + '-' + laser

        if genotype not in gts:
            print "\tskipping genotype", genotype
            continue

        if 0:
            fig = plt.figure()
            fig.suptitle(os.path.basename(csvfile))
            ax = fig.add_subplot(1, 1, 1)
            df['experiment'] = 1
            df['tobj_id'] = 1
            madplot.plot_tracked_trajectory(
                ax,
                df,
                arena,
                debug_plot=False,
                color='k',
            )
            ax.add_patch(arena.get_patch(color='k', alpha=0.1))

        duration = (df.index[-1] - df.index[0]).total_seconds()
        if duration < EXPERIMENT_DURATION:
            print "\tmissing data", csvfilefn
            continue

        print "\t%ss experiment" % duration

        #MAXIMUM SPEED = 300:
        df['v'][df['v'] >= 300] = np.nan
        df['v'] = df['v'].fillna(method='ffill')

        try:
            df = flymad_analysis.align_t_by_laser_on(
                df,
                min_experiment_duration=EXPERIMENT_DURATION,
                align_first_only=True,
                exact_num_ranges=1)
        except flymad_analysis.AlignError, err:
            print "\talign error %s (%s)" % (csvfile, err)
            continue

        #median filter
        if medfilt:
            df['v'] = scipy.signal.medfilt(df['v'].values, medfilt)

        df['obj_id'] = flymad_analysis.create_object_id(date, time)
        df['Genotype'] = genotype
        df['lasergroup'] = laser

        pooldf = pd.concat([pooldf, df])
Пример #8
0
def plot_data(path, dat, debug_plot):

    if os.path.isdir(path):
        plotdir = path
        conf = dat
    else:
        if path.endswith('.json'):
            conf = json.load(open(path))
        else:
            conf = dat
        plotdir = os.path.dirname(path)

    plot_exps = conf.get('_plot', ['coupled'])

    exps = [e for e in plot_exps if e in dat]
    exps_colors = [plt.cm.gnuplot(i) for i in np.linspace(0, 1.0, len(exps))]

    if '_ntrials' in conf:
        ntrials = conf['_ntrials']
    else:
        #check all trials have the same number
        trial_lens = set(map(len, (dat[e] for e in exps)))
        if len(trial_lens) != 1:
            raise Exception("experiments contain different numbers of trials")

        ntrials = trial_lens.pop()

    pct_in_area_total = {k:[] for k in exps}
    pct_in_area_per_time = {k:[] for k in exps}
    pct_in_area_per_time_lbls = {k:[] for k in exps}
    latency_to_first_contact = {k:[] for k in exps}
    velocity_outside_area = {k:[] for k in exps}
    velocity_inside_area = {k:[] for k in exps}
    path_length = {k:[] for k in exps}

    pct_in_area_per_time_bins = range(0,300,30)

    for exp in exps:
        ordered_trials = dat[exp][0:ntrials]

        gs = _get_plot_gs(ordered_trials)
        fig = plt.figure("%s Trajectories" % exp.title(), figsize=(16,8))

        gsh = _get_plot_gs(ordered_trials)
        figh = plt.figure("%s Trajectories (Hist)" % exp.title(), figsize=(16,8))

        for i,trial in enumerate(ordered_trials):
            label = trial['label']
            ldf, tdf, hdf, geom = trial['data']

            dbg_plot_title = " %s %s" % (exp,label)

            print exp.title(), label

            ax = fig.add_subplot(gs[i])
            madplot.plot_tracked_trajectory(ax, tdf, arena, debug_plot=debug_plot, title=dbg_plot_title)

            patch = arena.get_intersect_patch(geom, fill=True, color='r', closed=True, alpha=0.4, zorder=9)
            if patch is not None:
                ax.add_patch(patch)

            ax.add_patch(arena.get_patch(fill=False, color='k', zorder=10))

            ok = tdf.dropna(axis=0,how='any',subset=['x','y'])
            axh = figh.add_subplot(gs[i])
            axh.set_aspect('equal')
            xlim,ylim = arena.get_limits()
            axh.set_xlim(*xlim)
            axh.set_ylim(*ylim)

            #manually scale the lognorm
            lognorm = matplotlib.colors.LogNorm(vmin=1e-7, vmax=1e-3)
            #(counts, xedges, yedges, Image)
            _,_,_,_im = axh.hist2d(ok['x'],ok['y'],bins=30,range=(xlim,ylim), normed=True, norm=lognorm)

            l_f = matplotlib.ticker.LogFormatter(10, labelOnlyBase=False)
            figh.colorbar(_im,ax=axh,format=l_f)

            for _ax in (ax,axh):
                _ax.set_title(label)
                _ax.xaxis.set_visible(False)
                _ax.yaxis.set_visible(False)

            pct_in_area_per_time_lbls[exp].append( label )

            pct_in_area_per_time[exp].append(
                        madplot.calculate_time_in_area(tdf, 300,
                                                       pct_in_area_per_time_bins))

            pct_in_area_total[exp].append(
                        madplot.calculate_pct_in_area_per_objid_only_vals(tdf))

            tts, vel_out, vel_in, path_l = madplot.calculate_latency_and_velocity_to_stay(
                                                tdf, holdtime=20,
                                                tout_reset_time=2, arena=arena, geom=geom,
                                                debug_plot=debug_plot, title=dbg_plot_title)

            latency_to_first_contact[exp].append(tts)
            velocity_outside_area[exp].append(vel_out)
            velocity_inside_area[exp].append(vel_in)
            path_length[exp].append(path_l)

        fig.savefig(os.path.join(plotdir,'%s_trajectories.png' % exp))

        #need more colors
        fig = plt.figure("%s Time in Area" % exp.title())
        ax = fig.add_subplot(1,1,1)

        colormap = plt.cm.gnuplot
        ax.set_color_cycle(madplot.colors_hsv_circle(len(ordered_trials)))

        for lbl,pct in zip(pct_in_area_per_time_lbls[exp], pct_in_area_per_time[exp]):
            mean = pct.mean(axis=0)
            std = pct.mean(axis=0)
            ax.plot(pct_in_area_per_time_bins, mean, linestyle='solid', label=lbl)

        ax.legend()

        plt.savefig(os.path.join(plotdir,'%s_time.png' % exp))

    ind = np.arange(ntrials)  # the x locations for the groups
    width = 1.0 / len(exps)   # no gaps between
    width -= (0.2 * width)    # 20% gap
    ticklabels = [str(i) for i in range(ntrials)]

    #plot time in area percent
    _plot_bar_and_line(pct_in_area_total, exps,
                       'Time in Area', 'Trial', 'Percentage of time spent in area',
                        ind, width, ntrials,
                        ticklabels, exps_colors,
                        'timeinarea', plotdir)

    #plot latency to first 20s in area
    _plot_bar_and_line(latency_to_first_contact, exps,
                       'Latency to first 20s contact', 'Trial', 'Latency to first 20s contact',
                        ind, width, ntrials,
                        ticklabels, exps_colors,
                        'latency', plotdir)

    #velocity
    _plot_bar_and_line(velocity_outside_area, exps,
                       'Velocity Outside Area', 'Trial', 'Velocity Outside Area',
                        ind, width, ntrials,
                        ticklabels, exps_colors,
                        'velocity_out', plotdir)
    _plot_bar_and_line(velocity_inside_area, exps,
                       'Velocity Inside Area', 'Trial', 'Velocity Inside Area',
                        ind, width, ntrials,
                        ticklabels, exps_colors,
                        'velocity_in', plotdir)

    #path length
    _plot_bar_and_line(path_length, exps,
                       'Path Length', 'Trial', 'Path Length',
                        ind, width, ntrials,
                        ticklabels, exps_colors,
                        'path_length', plotdir)
Пример #9
0
BAG = '/mnt/strawscience/data/FlyMAD/velocity_test/2013-11-21-18-00-00.bag'
oid = 52251

smooth = True
smoothstr = {True: 'smooth', False: 'nosmooth'}[smooth]

arena = madplot.Arena('mm')
geom, dfs = madplot.load_bagfile(BAG, arena, smooth=smooth)
l_df = dfs["targeted"]
t_df = dfs["tracked"]
h_df = dfs["ttm"]

fig = plt.figure('traj %s' % smoothstr)
fig.suptitle('trajectory %s' % smoothstr)
ax = fig.add_subplot(1, 1, 1)
madplot.plot_tracked_trajectory(ax, t_df, arena, debug_plot=False)
ax.add_patch(arena.get_patch(fill=False))
ax.set_xlabel('position (%s)' % arena.unit)
fig.savefig('dorothea_pos_%s.png' % smoothstr)

df = t_df[t_df['tobj_id'] == oid].resample('100L')

#print df.iloc[39].index[0] - df.iloc[0]

times = [(ix - df.index[0]).total_seconds() for ix in df.index]

#print times[39]

######yyyyyyyyyy
fig = plt.figure('y %s' % smoothstr, figsize=(10, 8))
fig.suptitle('y %s' % smoothstr)