示例#1
0
def prepare_data(bags, arena, smoothstr, smooth, medfilt, gts,
                 min_experiment_duration):

    RESAMPLE_SPEC = '10L'

    found_gts = []

    pooldf = DataFrame()
    for bag in bags:
        df = madplot.load_bagfile_single_dataframe(bag,
                                                   arena,
                                                   ffill=True,
                                                   smooth=smooth)
        df = flymad_analysis.resample(df, resample_specifier=RESAMPLE_SPEC)

        metadata = filename_regexes.parse_filename(
            bag, extract_genotype_and_laser=True)
        dateobj = filename_regexes.parse_date(bag)
        genotype = metadata['genotype'] + '-' + metadata['laser']

        found_gts.append(genotype)

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

        duration = (df.index[-1] - df.index[0]).total_seconds()
        if duration < min_experiment_duration:
            print "\tmissing data", bag
            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=min_experiment_duration,
                align_first_only=True,
                exact_num_ranges=1,
                resample_bin=RESAMPLE_SPEC)
        except flymad_analysis.AlignError, err:
            print "\talign error %s (%s)" % (bag, err)
            pass

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

        df['obj_id'] = calendar.timegm(dateobj)
        df['Genotype'] = genotype

        pooldf = pd.concat([pooldf, df])
示例#2
0
def prepare_data(bags, arena, smoothstr, smooth, medfilt, gts, min_experiment_duration):

    RESAMPLE_SPEC = '10L'

    found_gts = []

    pooldf = DataFrame()
    for bag in bags:
        df = madplot.load_bagfile_single_dataframe(bag, arena,
                                                        ffill=True,
                                                        smooth=smooth)
        df = flymad_analysis.resample(df, resample_specifier=RESAMPLE_SPEC)

        metadata = filename_regexes.parse_filename(bag, extract_genotype_and_laser=True)
        dateobj = filename_regexes.parse_date(bag)
        genotype = metadata['genotype'] + '-' + metadata['laser']

        found_gts.append(genotype)

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

        duration = (df.index[-1] - df.index[0]).total_seconds()
        if duration < min_experiment_duration:
            print "\tmissing data", bag
            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=min_experiment_duration,
                    align_first_only=True,
                    exact_num_ranges=1,
                    resample_bin=RESAMPLE_SPEC)
        except flymad_analysis.AlignError, err:
            print "\talign error %s (%s)" % (bag, err)
            pass

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

        df['obj_id'] = calendar.timegm(dateobj)
        df['Genotype'] = genotype

        pooldf = pd.concat([pooldf, df])
示例#3
0
    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:
        print len(frames), "frames to render"

    moviet0 = frames[0].epoch
示例#4
0
                        help='show epoch')
    parser.add_argument('--show-arena',
                        action='store_true',
                        default=False,
                        help='show arena')
    parser.add_argument('--fps', type=int, default=20, help='framenumber')
    parser.add_argument('--hist',
                        type=int,
                        default=400,
                        help='show this many points of trajectory history')

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

    arena = madplot.Arena(False)
    df = madplot.load_bagfile_single_dataframe(path, arena, ffill=False)

    objids = df['tobj_id'].dropna().unique()

    wfmf = madplot.FMFMultiTrajectoryPlotter(args.wide_fmf,
                                             objids,
                                             maxlen=args.hist)
    if args.show_arena:
        wfmf.show_arena = arena
    wfmf.show_lxly = args.show_laser
    wfmf.show_fxfy = args.show_target
    wfmf.show_timestamp = args.show_timestamp
    wfmf.show_epoch = args.show_epoch

    fmfwidth = wfmf.width
    fmfheight = wfmf.height
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"
    elif targets[0] == 2:
        target_name = "body"
    else:
        raise Exception("Only head or body may be targeted")

    ldf_all = pool_df[~pool_df['lobj_id'].isnull()]
    hdf_all = pool_df[~pool_df['h_framenumber'].isnull()]

    #the first fly targeted
    prev = pool_df['lobj_id'].dropna().head(1)
    prev_id = prev.values[0]
    prev_ix = prev.index[0]

    target_ranges = {
        (0, 100): [],
    }

    for ix, row in pool_df.iterrows():
        lobj_id = row['lobj_id']
        if np.isnan(lobj_id):
            continue
        elif lobj_id != prev_id:
            df = pool_df[prev_ix:ix]

            #mean speed of fly over its whole turn
            v = df['v'].mean()

            #details of the TrackedObj message
            ldf = df[~df['lobj_id'].isnull()]

            #we want to know when we go to TTM mode
            gbs = ldf.groupby('mode')
            try:
                #this is the time we first switch to TTM
                to_ttm = gbs.groups[2.0][0]

                print "------------", v

                ttm_errs = []
                ttm_dts_thisfly = []
                ttm_dts = []
                wf_errs = []
                wf_dts_thisfly = []
                wf_dts = []

                #get the head detection data once we start ttm
                hdf = pool_df[to_ttm:ix]
                #extract only the head messages
                for i, (hix, hrow) in enumerate(
                        hdf[~hdf['h_framenumber'].isnull()].iterrows()):

                    #skip when we miss the head
                    if hrow['head_x'] == 1e6:
                        continue

                    if target_name == "head":
                        ttm_err = math.sqrt( (hrow['head_x']-hrow['target_x'])**2 +\
                                             (hrow['head_y']-hrow['target_y'])**2)
                    elif target_name == "body":
                        ttm_err = math.sqrt( (hrow['body_x']-hrow['target_x'])**2 +\
                                             (hrow['body_y']-hrow['target_y'])**2)

                    #if the first value is < 10 it is likely actually associated
                    #with the last fly TTM, so ignore it....
                    if (i == 0) and (ttm_err < 100):
                        continue

                    #the time since we switched to this fly
                    thisfly_dt = (hix - prev_ix).total_seconds()
                    #the time since we switched to TTM
                    ttm_dt = (hix - to_ttm).total_seconds()
                    ttm_errs.append(ttm_err)
                    ttm_dts_thisfly.append(thisfly_dt)
                    ttm_dts.append(ttm_dt)

                    wdf = ldf[:hix].tail(1)
                    wf_err = math.sqrt(  (wdf['fly_x']-wdf['laser_x'])**2 +\
                                         (wdf['fly_y']-wdf['laser_y'])**2)

                    #the time since we switched to this fly
                    thisfly_dt = (wdf.index[0] - prev_ix).total_seconds()
                    #the time since we switched to TTM
                    ttm_dt = (wdf.index[0] - to_ttm).total_seconds()
                    wf_errs.append(wf_err)
                    wf_dts_thisfly.append(thisfly_dt)
                    wf_dts.append(ttm_dt)

                    print thisfly_dt, ttm_dt, ttm_err

                ttm_err = Err(ttm_errs, ttm_dts_thisfly, ttm_dts)
                wf_err = Err(wf_errs, wf_dts_thisfly, wf_dts)
                trg = Target(lobj_id, prev_ix, ix, v, ttm_err, wf_err)

            except KeyError:
                #never switched to TTM
                print "never switched to ttm"
                trg = None
                pass

            #except Exception:
            #    print "UNKNOWN ERROR"
            #    trg = None

            #classify the target into which speed range
            if trg is not None:
                for k in target_ranges:
                    vmin, vmax = k
                    if vmin < trg.v <= vmax:
                        target_ranges[k].append(trg)

            prev_id = lobj_id
            prev_ix = ix

    data = {
        'target_name': target_name,
        'pooldf': pool_df,
        'ldf': ldf_all,
        'hdf': hdf_all,
        'target_ranges': target_ranges
    }

    pkl_fname = "%s_%s.pkl" % (path, target_name)
    cPickle.dump(data, open(pkl_fname, 'wb'), -1)

    return data
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()
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"
    elif targets[0] == 2:
        target_name = "body"
    else:
        raise Exception("Only head or body may be targeted")

    ldf_all = pool_df[~pool_df['lobj_id'].isnull()]
    hdf_all = pool_df[~pool_df['h_framenumber'].isnull()]

    #the first fly targeted
    prev = pool_df['lobj_id'].dropna().head(1)
    prev_id = prev.values[0]
    prev_ix = prev.index[0]

    target_ranges = {
        (0,100):[],
    }

    for ix, row in pool_df.iterrows():
        lobj_id = row['lobj_id']
        if np.isnan(lobj_id):
            continue
        elif lobj_id != prev_id:
            df = pool_df[prev_ix:ix]

            #mean speed of fly over its whole turn
            v = df['v'].mean()

            #details of the TrackedObj message
            ldf = df[~df['lobj_id'].isnull()]

            #we want to know when we go to TTM mode
            gbs = ldf.groupby('mode')
            try:
                #this is the time we first switch to TTM
                to_ttm = gbs.groups[2.0][0]

                print "------------",v

                ttm_errs = []
                ttm_dts_thisfly = []
                ttm_dts = []
                wf_errs = []
                wf_dts_thisfly = []
                wf_dts = []

                #get the head detection data once we start ttm
                hdf = pool_df[to_ttm:ix]
                #extract only the head messages
                for i,(hix,hrow) in enumerate(hdf[~hdf['h_framenumber'].isnull()].iterrows()):

                    #skip when we miss the head
                    if hrow['head_x'] == 1e6:
                        continue

                    if target_name == "head":
                        ttm_err = math.sqrt( (hrow['head_x']-hrow['target_x'])**2 +\
                                             (hrow['head_y']-hrow['target_y'])**2)
                    elif target_name == "body":
                        ttm_err = math.sqrt( (hrow['body_x']-hrow['target_x'])**2 +\
                                             (hrow['body_y']-hrow['target_y'])**2)

                    #if the first value is < 10 it is likely actually associated
                    #with the last fly TTM, so ignore it....
                    if (i == 0) and (ttm_err < 100):
                        continue

                    #the time since we switched to this fly
                    thisfly_dt = (hix-prev_ix).total_seconds()
                    #the time since we switched to TTM
                    ttm_dt = (hix-to_ttm).total_seconds()
                    ttm_errs.append(ttm_err)
                    ttm_dts_thisfly.append(thisfly_dt)
                    ttm_dts.append(ttm_dt)

                    wdf = ldf[:hix].tail(1)
                    wf_err = math.sqrt(  (wdf['fly_x']-wdf['laser_x'])**2 +\
                                         (wdf['fly_y']-wdf['laser_y'])**2)

                    #the time since we switched to this fly
                    thisfly_dt = (wdf.index[0]-prev_ix).total_seconds()
                    #the time since we switched to TTM
                    ttm_dt = (wdf.index[0]-to_ttm).total_seconds()
                    wf_errs.append(wf_err)
                    wf_dts_thisfly.append(thisfly_dt)
                    wf_dts.append(ttm_dt)

                    print thisfly_dt, ttm_dt, ttm_err

                ttm_err = Err(ttm_errs, ttm_dts_thisfly, ttm_dts)
                wf_err = Err(wf_errs, wf_dts_thisfly, wf_dts)
                trg = Target(lobj_id, prev_ix, ix, v, ttm_err, wf_err)

            except KeyError:
                #never switched to TTM
                print "never switched to ttm"
                trg = None
                pass

            #except Exception:
            #    print "UNKNOWN ERROR"
            #    trg = None

            #classify the target into which speed range
            if trg is not None:
                for k in target_ranges:
                    vmin,vmax = k
                    if vmin < trg.v <= vmax:
                        target_ranges[k].append(trg)

            prev_id = lobj_id
            prev_ix = ix

    data = {'target_name':target_name,
            'pooldf':pool_df,
            'ldf':ldf_all,
            'hdf':hdf_all,
            'target_ranges':target_ranges}

    pkl_fname = "%s_%s.pkl" % (path, target_name)
    cPickle.dump(data, open(pkl_fname,'wb'), -1)

    return data
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()
示例#9
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()
示例#10
0
def prepare_data(arena, path, smoothstr, smooth):

    path = os.path.abspath(path)
    print 'opening bag files in %r'%path

    GENOTYPES = {"NINE":"*.bag",
                 "CSSHITS":"ctrl/CSSHITS/*.bag",
                 "NINGal4":"ctrl/NINGal4/*.bag"}

    CHUNKS = collections.OrderedDict()
    CHUNKS["1_loff+ve"] = (0,30)
    CHUNKS["2_loff-ve"] = (30,60)
    CHUNKS["3_lon-ve"] = (60,120)
    CHUNKS["4_lon+ve"] = (120,180)
    CHUNKS["5_loff+ve"] = (180,240)
    CHUNKS["6_loff-ve"] = (240,300)
    CHUNKS["7_loff+ve"] = (300,360)

    save_times = None

    data = {gt:dict(chunk={}) for gt in GENOTYPES}
    for gti,gt in enumerate(GENOTYPES):
        #if gti>=1: break
        pattern = os.path.join(path, GENOTYPES[gt])
        bags = glob.glob(pattern)

        for bag in bags:

            df = madplot.load_bagfile_single_dataframe(
                        bag, arena,
                        ffill=['laser_power','e_rotator_velocity_data'],
                        extra_topics={'/rotator/velocity':['data']},
                        smooth=smooth
            )
            supplement_angles(df)
            supplement_times(df)

            if 1:
                # John's category stuff

                #there might be multiple object_ids in the same bagfile for the same
                #fly, so calculate dtheta for all of them
                dts = []
                for obj_id, group in df.groupby('tobj_id'):
                    if np.isnan(obj_id):
                        continue
                    dts.append( calc_dtheta(group) )

                #join the dthetas vertically (axis=0) and insert as a column into
                #the full dataframe
                df['dtheta'] = pd.concat(dts, axis=0)


            good = ~np.isnan(df['time_since_start'])
            if save_times is None:
                save_times = np.arange(0,
                                       (df['time_since_start'][good].values[-1]+1e-6),
                                       1.0 ) # every second

            if 'timeseries_angular_vel' not in data[gt]:
                data[gt]['bag_fname'] = []
                data[gt]['timeseries_angular_vel'] = []
                data[gt]['timeseries_vel'] = []
                data[gt]['save_times'] = save_times
                good_stim = ~np.isnan(df['e_rotator_velocity_data'].values)
                good_both = good & good_stim
                data[gt]['stimulus_velocity'] = (
                    df['time_since_start'].values[good_both],
                    df['e_rotator_velocity_data'].values[good_both])
            if gt=='CSSHITS' and 'laser_power' not in data[gt]:
                interp = scipy.interpolate.interp1d( df['time_since_start'][good],
                                                     df['laser_power'][good],
                                                     bounds_error=False,
                                                     fill_value=np.nan,
                                                     )
                save_laser = interp(save_times)
                data[gt]['laser_power'] = save_laser

            interp = scipy.interpolate.interp1d( df['time_since_start'][good],
                                                 df['angular_velocity'][good],
                                                 bounds_error=False,
                                                 fill_value=np.nan,
                                                 )
            save_angular_vel = interp(save_times)
            data[gt]['timeseries_angular_vel'].append( save_angular_vel )
            data[gt]['bag_fname'].append( bag )

            interp = scipy.interpolate.interp1d( df['time_since_start'][good],
                                                 df['v'][good],
                                                 bounds_error=False,
                                                 fill_value=np.nan,
                                                 )
            save_vel = interp(save_times)
            data[gt]['timeseries_vel'].append( save_vel )

            # if 1:
            #     break

            #calculate mean_dtheta over each phase of the experiment
            t00 = df.index[0]
            for c in CHUNKS:
                s0,s1 = CHUNKS[c]

                t0 = datetime.timedelta(seconds=s0)
                t1 = datetime.timedelta(seconds=s1)

                mean_dtheta = df[t00+t0:t00+t1]["dtheta"].mean()
                mean_v = df[t00+t0:t00+t1]["v"].mean()

                if c not in data[gt]:
                    data[gt]['chunk'][c] = []
                data[gt]['chunk'][c].append( (mean_dtheta, mean_v) )

    pickle.dump(data, open(CACHE_FNAME,'wb'), -1)
    print 'saved cache to %s'%CACHE_FNAME
    return data
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()
示例#12
0
    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:
        print len(frames),"frames to render"

    moviet0 = frames[0].epoch