Ejemplo n.º 1
0
    def __init__(self, fmf_filepath, tempdir=None, sample_rate=500):
        """
        
        """
        self._fmf_filepath = fmf_filepath

        if tempdir is not None:
            if tempdir[-1] == '/':
                pass
            else:
                tempdir = tempdir + '/'
            self._tempdir = tempdir
        else:
            self._tempdir = fmf_filepath.split('.fmf')[0] + '/targets/'

        if not os.path.exists(self._tempdir) == True:
            os.makedirs(self._tempdir)

        self._calibration_file = utilities.get_calibration_asof_filename(
            self._fmf_filepath)

        self._arena = madplot.Arena(
            convert=False,
            **flymad_analysis.get_arena_conf(
                calibration_file=self._calibration_file))

        self._sample_rate = sample_rate  # = n for every nth frame

        self._targets = self.detect_targets()
Ejemplo n.º 2
0
                        default=False,
                        help='perform pairwise comparison and show in plotly')
    parser.add_argument('--min-experiment-duration', default=70, type=float,
                        help='minimum experiment duration for a bag file to be considered valid. '\
                             'should be about 10 seconds shorter than the actual experiment')

    args = parser.parse_args()
    path = args.path[0]

    bags = args.path
    genotypes = args.genotypes.split(',')

    medfilt = args.median_filter
    smoothstr = '%s' % {True: 'smooth', False: 'nosmooth'}[args.smooth]

    arena = madplot.Arena('mm')

    note = "%s %s\n%r\nmedfilt %s" % (arena.unit, smoothstr, arena, medfilt)

    cache_fname = os.path.join(os.path.dirname(path), 'speed.madplot-cache')
    cache_args = ([os.path.basename(b) for b in bags], smoothstr, args.smooth,
                  medfilt, genotypes, args.min_experiment_duration)
    data = None
    if args.only_plot:
        data = madplot.load_bagfile_cache(cache_args, cache_fname)
    if data is None:
        data = prepare_data(bags, arena, smoothstr, args.smooth, medfilt,
                            genotypes, args.min_experiment_duration)
        madplot.save_bagfile_cache(data, cache_args, cache_fname)

    if args.stats:
Ejemplo n.º 3
0
    parser.add_argument('--zoom-fmf',
                        help='wide fmf file to render the trajectory over',
                        required=True)
    parser.add_argument('--outdir', help='destination directory for mp4')

    args = parser.parse_args()

    BAG_FILE = args.path[0]
    ZOOM_FMF = args.zoom_fmf
    WIDE_FMF = args.wide_fmf

    wfmf = madplot.FMFTrajectoryPlotter(WIDE_FMF)
    zfmf = madplot.FMFTTLPlotter(ZOOM_FMF)
    zfmf.enable_color_correction(brightness=20, contrast=1.5)

    arena = madplot.Arena(False)

    print "loading data"
    df = madplot.load_bagfile_single_dataframe(BAG_FILE, arena, ffill=True)
    wt = wfmf.fmf.get_all_timestamps()
    zt = zfmf.fmf.get_all_timestamps()

    if len(df['tobj_id'].dropna().unique()) != 1:
        print "TTM movies require single unique object IDs, I think..."
        sys.exit(1)

    frames = build_framedesc_list(df, wt, zt)
    if not frames:
        print "no frames to render"
        sys.exit(0)
    else:
Ejemplo n.º 4
0
                        help='calibration directory containing yaml files',
                        required=True)

    args = parser.parse_args()
    path = args.path[0]

    medfilt = 51
    smoothstr = '%s' % {True: 'smooth', False: 'nosmooth'}[args.smooth]

    all_data = {k: dict() for k in DAY_1_GENOTYPES + DAY_2_GENOTYPES}

    #### BOTH DAYS EXPERIMENTS WERE RUN WITH DIFFERENT CALIBRATIONS
    #### DAY 1
    calibration_file = os.path.join(args.calibration_dir, DAY_1_CALIBRATION)
    d1_arena = madplot.Arena(
        'mm',
        **flymad_analysis.get_arena_conf(calibration_file=calibration_file))
    cache_fname = os.path.join(path, 'moonwalker_d1.madplot-cache')
    cache_args = (path, DAY_1_GENOTYPES, LASER_POWERS, smoothstr, d1_arena)
    d1_data = None
    if args.only_plot:
        d1_data = madplot.load_bagfile_cache(cache_args, cache_fname)
    if d1_data is None:
        #these loops are braindead inefficient and the wrong way,
        #however, we have limited time, and I cache the intermediate
        #representation anyway...
        d1_data = {k: dict() for k in DAY_1_GENOTYPES}
        for gt in DAY_1_GENOTYPES:
            for lp in LASER_POWERS:
                try:
                    d1_data[gt][lp] = prepare_data(path, d1_arena, args.smooth,
Ejemplo n.º 5
0
if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('path', nargs=1, help='path to bag file')
    parser.add_argument('--unit', default=False)
    parser.add_argument('--no-smooth',
                        dest='smooth',
                        action='store_false',
                        default=True)
    parser.add_argument('--tracking-stats', action='store_true', default=False)

    args = parser.parse_args()
    path = args.path[0]

    arena = madplot.Arena(args.unit)
    geom, dfs = madplot.load_bagfile(path, arena, smooth=args.smooth)
    ldf = dfs["targeted"]
    tdf = dfs["tracked"]
    hdf = dfs["ttm"]

    axt = plt.figure("Trajectory").add_subplot(1, 1, 1)
    plot_trajectory(axt, arena, ldf, tdf, hdf, geom, debug=args.tracking_stats)

    axl = plt.figure("Laser State").add_subplot(1, 1, 1)
    plot_laser(axl, arena, ldf, tdf, hdf, geom)

    for ax in (axt, axl):
        ax.set_xlabel('x (%s)' % arena.unit)
        ax.set_ylabel('y (%s)' % arena.unit)
Ejemplo n.º 6
0
import pandas as pd
import numpy as np

import roslib
roslib.load_manifest('flymad')
import flymad.madplot as madplot

Err = collections.namedtuple('Err', 'err dt_thisfly dt_ttm')
Target = collections.namedtuple('Target',
                                'obj_id from_idx to_idx v ttm_err wf_err')

ttm_unit = 'mm'
ttm_conv = lambda x: np.array(x) * 0.00416  #px -> mm

wide_unit = 'mm'
w_arena = madplot.Arena('mm')
wide_conv = w_arena.scale  #lambda x: x*0.21077 #px -> mm


def prepare_data(arena, path):

    pool_df = madplot.load_bagfile_single_dataframe(path,
                                                    arena,
                                                    ffill=False,
                                                    filter_short_pct=10.0)

    targets = pool_df['target_type'].dropna().unique()
    if len(targets) != 1:
        raise Exception("Only head or body may be targeted")
    if targets[0] == 1:
        target_name = "head"
Ejemplo n.º 7
0
def doit_using_framenumber(user_data):
    match, mp4_dir, show_theta, show_velocity, pre_frames, post_frames = user_data

    zoomf = match.fmf
    rosbagf = match.bag
    maxt = match.maxt

    moviemaker = madplot.MovieMaker(basename=os.path.basename(zoomf), fps=15)
    target_moviefname = moviemaker.get_target_movie_name(mp4_dir)
    if os.path.exists(target_moviefname):
        print 'target %r exists: skipping movie' % (target_moviefname, )
        moviemaker.cleanup()
        return

    arena = madplot.Arena(False)
    zoom = madplot.FMFImagePlotter(zoomf, 'z_frame')
    zoom.enable_color_correction(brightness=15, contrast=1.5)
    wide = madplot.ArenaPlotter(arena, tzname=TZNAME)

    wide.show_theta = show_theta
    wide.show_velocity = show_velocity
    wide.show_epoch = True
    wide.show_framenumber = True
    wide.show_lxly = True
    wide.show_fxfy = True

    renderlist = []

    zoom_ts = zoom.fmf.get_all_timestamps().tolist()
    df = madplot.load_bagfile_single_dataframe(rosbagf,
                                               arena,
                                               ffill=True,
                                               tzname=TZNAME)
    t0 = df.index[0].asm8.astype(np.int64) / 1e9

    # find start and stop frames based on laser transition ----
    laser_power = df['laser_power'].values
    framenumber = df['h_framenumber'].values

    laser_power = np.array(laser_power, copy=True)
    laser_power[np.isnan(laser_power)] = 0

    dlaser = laser_power[1:] - laser_power[:-1]
    laser_transitions = np.nonzero(dlaser)[0]
    startframenumber = -np.inf
    stopframenumber = np.inf
    if pre_frames is None:
        assert post_frames is None
    if pre_frames is not None:
        assert post_frames is not None

        for i, idx in enumerate(laser_transitions):
            if i == 0:
                startframenumber = framenumber[idx - pre_frames]
            elif i == 1:
                stopframenumber = framenumber[idx + post_frames]
            else:
                print 'ERROR: unknown laser transition in %r' % zoomf
                moviemaker.cleanup()
                return

    # got start and stop frames -----

    for idx, group in df.groupby('h_framenumber'):
        # limit movies to only frames we want
        this_framenumber = group['h_framenumber'].values[0]
        if not (startframenumber <= this_framenumber
                and this_framenumber <= stopframenumber):
            continue

        #did we save a frame ?
        try:
            frameoffset = zoom_ts.index(idx)
        except ValueError:
            #missing frame (probbably because the video was not recorded at
            #full frame rate
            continue

        frame = madplot.FMFFrame(offset=frameoffset, timestamp=idx)
        row = group.dropna(subset=['tobj_id']).tail(1)
        if len(row):
            if maxt > 0:
                dt = (row.index[0].asm8.astype(np.int64) / 1e9) - t0
                if dt > maxt:
                    break

            desc = madplot.FrameDescriptor(
                None, frame, row, row.index[0].asm8.astype(np.int64) / 1e9)

            renderlist.append(desc)

    if len(renderlist) == 0:
        moviemaker.cleanup()
        return

    wide.t0 = t0

    panels = {}
    #left half of screen
    panels["wide"] = wide.get_benu_panel(device_x0=0,
                                         device_x1=0.5 * TARGET_OUT_W,
                                         device_y0=0,
                                         device_y1=TARGET_OUT_H)
    panels["zoom"] = zoom.get_benu_panel(device_x0=0.5 * TARGET_OUT_W,
                                         device_x1=TARGET_OUT_W,
                                         device_y0=0,
                                         device_y1=TARGET_OUT_H)

    actual_w, actual_h = benu.utils.negotiate_panel_size_same_height(
        panels, TARGET_OUT_W)

    ass = Assembler(
        actual_w,
        actual_h,
        panels,
        wide,
        zoom,
        moviemaker,
    )

    if not USE_MULTIPROCESSING:
        pbar = madplot.get_progress_bar(moviemaker.movie_fname,
                                        len(renderlist))

    for i, desc in enumerate(renderlist):
        ass.render_frame(desc)
        if not USE_MULTIPROCESSING:
            pbar.update(i)

    if not USE_MULTIPROCESSING:
        pbar.finish()

    if not os.path.exists(mp4_dir):
        os.makedirs(mp4_dir)

    moviefname = moviemaker.render(mp4_dir)
    print "wrote", moviefname

    moviemaker.cleanup()
Ejemplo n.º 8
0
def doit_using_framenumber(user_data):
    match, mdir, show_theta, show_velocity = user_data

    zoomf = match.fmf
    rosbagf = match.bag
    maxt = match.maxt

    arena = madplot.Arena(False)
    zoom = madplot.FMFImagePlotter(zoomf, 'z_frame')
    zoom.enable_color_correction(brightness=15, contrast=1.5)
    wide = madplot.ArenaPlotter(arena)

    wide.show_theta = show_theta
    wide.show_velocity = show_velocity
    wide.show_epoch = True
    wide.show_framenumber = True
    wide.show_lxly = True
    wide.show_fxfy = True

    renderlist = []

    zoom_ts = zoom.fmf.get_all_timestamps().tolist()
    df = madplot.load_bagfile_single_dataframe(rosbagf, arena, ffill=True)
    t0 = df.index[0].asm8.astype(np.int64) / 1e9

    for idx, group in df.groupby('h_framenumber'):
        #did we save a frame ?
        try:
            frameoffset = zoom_ts.index(idx)
        except ValueError:
            #missing frame (probbably because the video was not recorded at
            #full frame rate
            continue

        frame = madplot.FMFFrame(offset=frameoffset, timestamp=idx)
        row = group.dropna(subset=['tobj_id']).tail(1)
        if len(row):
            if maxt > 0:
                dt = (row.index[0].asm8.astype(np.int64) / 1e9) - t0
                if dt > maxt:
                    break

            desc = madplot.FrameDescriptor(
                None, frame, row, row.index[0].asm8.astype(np.int64) / 1e9)

            renderlist.append(desc)

    wide.t0 = t0

    panels = {}
    #left half of screen
    panels["wide"] = wide.get_benu_panel(device_x0=0,
                                         device_x1=0.5 * TARGET_OUT_W,
                                         device_y0=0,
                                         device_y1=TARGET_OUT_H)
    panels["zoom"] = zoom.get_benu_panel(device_x0=0.5 * TARGET_OUT_W,
                                         device_x1=TARGET_OUT_W,
                                         device_y0=0,
                                         device_y1=TARGET_OUT_H)

    actual_w, actual_h = benu.utils.negotiate_panel_size_same_height(
        panels, TARGET_OUT_W)

    moviemaker = madplot.MovieMaker(basename=os.path.basename(zoomf), fps=30)
    target_moviefname = moviemaker.get_target_movie_name(mdir)
    if os.path.exists(target_moviefname):
        print 'target %r exists: skipping movie' % (target_moviefname, )
        return

    ass = Assembler(
        actual_w,
        actual_h,
        panels,
        wide,
        zoom,
        moviemaker,
    )

    if not USE_MULTIPROCESSING:
        pbar = madplot.get_progress_bar(moviemaker.movie_fname,
                                        len(renderlist))

    for i, desc in enumerate(renderlist):
        ass.render_frame(desc)
        if not USE_MULTIPROCESSING:
            pbar.update(i)

    if not USE_MULTIPROCESSING:
        pbar.finish()

    if not os.path.exists(mdir):
        os.makedirs(mdir)

    moviefname = moviemaker.render(mdir)
    print "wrote", moviefname

    moviemaker.cleanup()