Beispiel #1
0
def calc_dtheta(df):
    #naieve method for calculating dtheta assuming wraparound.

    dtheta = []
    dtheta_idx = []

    for i0,i1 in madplot.pairwise(df.iterrows()):
        t0idx,t0row = i0
        t1idx,t1row = i1
        t0 = t0row['theta']
        t1 = t1row['theta']

        dt = (t1-t0)
        #dtheta ranges from -pi/2.0 -> pi/2.0
        dt = wrap_dtheta_plus_minus(dt, around=np.pi/2.0)

        dtheta.append(dt)
        dtheta_idx.append(t0idx)

    return pd.Series(dtheta,index=dtheta_idx)
Beispiel #2
0
def prepare_data(path, resample_bin, gts):

    LASER_THORAX_MAP = {True: THORAX, False: HEAD}

    #PROCESS SCORE FILES:
    pooldf = pd.DataFrame()
    for df, metadata in flymad_analysis.load_courtship_csv(path):
        csvfilefn, experimentID, date, time, genotype, laser, repID = metadata

        dlaser = np.gradient(df['laser_state'].values)
        num_on_periods = (dlaser == 0.5).sum()
        if num_on_periods != 12:
            print "\tskipping file %s (%d laser on periods)" % (
                csvfilefn, num_on_periods / 2)
            continue

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

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

        #make new columns that indicates HEAD/THORAX targeting
        thorax = True
        laser_state = False

        trg = []
        for i0, i1 in madplot.pairwise(df.iterrows()):
            t0idx, t0row = i0
            t1idx, t1row = i1
            if t1row['laser_state'] >= 0.5 and t0row['laser_state'] == 0:
                thorax ^= True
                laser_state = True
            elif t0row['laser_state'] >= 0.5 and t1row['laser_state'] == 0:
                laser_state = False
            trg.append(OFF if not laser_state else LASER_THORAX_MAP[thorax])
        trg.append(OFF)
        df['ttm'] = trg

        #resample into 5S bins
        df = df.resample(resample_bin, fill_method='ffill')
        #trim dataframe
        df = df.head(
            flymad_analysis.get_num_rows(EXPERIMENT_DURATION, resample_bin))
        tb = flymad_analysis.get_resampled_timebase(EXPERIMENT_DURATION,
                                                    resample_bin)

        #fix cols due to resampling
        df['laser_state'][df['laser_state'] > 0] = 1
        df['zx_binary'] = (df['zx'] > 0).values.astype(float)
        df['ttm'][df['ttm'] < 0] = HEAD
        df['ttm'][df['ttm'] > 0] = THORAX

        dlaser = np.gradient((df['laser_state'].values > 0).astype(int)) > 0
        t0idx = np.argmax(dlaser)
        t0 = tb[t0idx - 1]
        df['t'] = tb - t0

        #groupby on float times is slow. make a special align column
        df['t_align'] = np.array(range(0, len(df))) - t0idx

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

        pooldf = pd.concat([pooldf, df])

    data = {}
    for gt in gts:
        gtdf = pooldf[pooldf['Genotype'] == gt]

        lgs = gtdf['lasergroup'].unique()
        if len(lgs) != 1:
            raise Exception("only one lasergroup handled for gt %s: not %s" %
                            (gt, lgs))

        grouped = gtdf.groupby(['t'], as_index=False)
        data[gt] = dict(mean=grouped.mean().astype(float),
                        std=grouped.std().astype(float),
                        n=grouped.count().astype(float),
                        first=grouped.first(),
                        df=gtdf)

    return data
Beispiel #3
0
    wfmf.show_timestamp = args.show_timestamp
    wfmf.show_epoch = args.show_epoch

    fmfwidth = wfmf.width
    fmfheight = wfmf.height

    try:
        print 'loading w timestamps'
        wts = wfmf.fmf.get_all_timestamps()
    except AttributeError:
        wts = df['t_framenumber'].dropna().unique()

    pbar = madplot.get_progress_bar("computing frames", len(wts))

    frames = []
    for i, (wt0, wt1) in enumerate(madplot.pairwise(wts)):

        pbar.update(i)

        fdf = madplot.get_framedf(df, wt1)
        lfdf = len(fdf)

        if lfdf:
            try:
                most_recent_time = madplot.get_framedf(df, wt1).index[-1]

                minidf = df[madplot.get_framedf(df, wt0).
                            index[0]:most_recent_time]
                last_head_row = minidf[minidf['h_framenumber'].notnull()].tail(
                    1)
Beispiel #4
0
def prepare_data(path, resample_bin, gts):

    LASER_THORAX_MAP = {True:THORAX,False:HEAD}

    #PROCESS SCORE FILES:
    pooldf = pd.DataFrame()
    for df,metadata in flymad_analysis.load_courtship_csv(path):
        csvfilefn,experimentID,date,time,genotype,laser,repID = metadata

        dlaser = np.gradient(df['laser_state'].values)
        num_on_periods = (dlaser == 0.5).sum()
        if num_on_periods != 12:
            print "\tskipping file %s (%d laser on periods)" % (csvfilefn, num_on_periods/2)
            continue

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

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

        #make new columns that indicates HEAD/THORAX targeting
        thorax = True
        laser_state = False

        trg = []
        for i0,i1 in madplot.pairwise(df.iterrows()):
            t0idx,t0row = i0
            t1idx,t1row = i1
            if t1row['laser_state'] >= 0.5 and t0row['laser_state'] == 0:
                thorax ^= True
                laser_state = True
            elif t0row['laser_state'] >= 0.5 and t1row['laser_state'] == 0:
                laser_state = False
            trg.append(OFF if not laser_state else LASER_THORAX_MAP[thorax])
        trg.append(OFF)
        df['ttm'] = trg

        #resample into 5S bins
        df = df.resample(resample_bin, fill_method='ffill')
        #trim dataframe
        df = df.head(flymad_analysis.get_num_rows(EXPERIMENT_DURATION, resample_bin))
        tb = flymad_analysis.get_resampled_timebase(EXPERIMENT_DURATION, resample_bin)

        #fix cols due to resampling
        df['laser_state'][df['laser_state'] > 0] = 1
        df['zx_binary'] = (df['zx'] > 0).values.astype(float)
        df['ttm'][df['ttm'] < 0] = HEAD
        df['ttm'][df['ttm'] > 0] = THORAX

        dlaser = np.gradient( (df['laser_state'].values > 0).astype(int) ) > 0
        t0idx = np.argmax(dlaser)
        t0 = tb[t0idx-1]
        df['t'] = tb - t0

        #groupby on float times is slow. make a special align column
        df['t_align'] = np.array(range(0,len(df))) - t0idx

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

        pooldf = pd.concat([pooldf, df])

    data = {}
    for gt in gts:
        gtdf = pooldf[pooldf['Genotype'] == gt]

        lgs = gtdf['lasergroup'].unique()
        if len(lgs) != 1:
            raise Exception("only one lasergroup handled for gt %s: not %s" % (
                             gt, lgs))

        grouped = gtdf.groupby(['t'], as_index=False)
        data[gt] = dict(mean=grouped.mean().astype(float),
                        std=grouped.std().astype(float),
                        n=grouped.count().astype(float),
                        first=grouped.first(),
                        df=gtdf)

    return data