Ejemplo n.º 1
0
def sync_jaaba_with_ros(FMF_DIR, BAGS, JAABA):

    print "Processing: ", FMF_DIR
    
    JAABA_CSV               = FMF_DIR + '/registered_trx.csv'
    
    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)
    
    BAG_FILE                = utilities.match_fmf_and_bag(FMF_DIR, BAGS)
    
    WIDE_FMF                = utilities.match_wide_to_zoom(FMF_DIR, JAABA)
    
    jaaba_data = pd.read_csv(JAABA_CSV, sep=',', names=['Timestamp','Length','Width','Theta','Left','Right'], index_col=False)
    jaaba_data[['Length','Width','Left','Right']] = jaaba_data[['Length','Width','Left','Right']].astype(np.float64)
    jaaba_data = utilities.convert_timestamps(jaaba_data)

    # ALIGN LASER STATE DATA
    jaaba_data['Laser1_state'] = binarize_laser_data(BAG_FILE, 'laser1')['Laser_state'].asof(jaaba_data.index).fillna(value=0)  #YAY! 
    jaaba_data['Laser2_state'] = binarize_laser_data(BAG_FILE, 'laser2')['Laser_state'].asof(jaaba_data.index).fillna(value=0)
    
    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)
    
    jaaba_data['Timestamp'] = jaaba_data.index  #silly pandas bug for subtracting from datetimeindex...
    jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data['Timestamp'][jaaba_data[jaaba_data['Laser2_state'] > 0].index[0]]    

    jaaba_data['maxWingAngle'] = get_absmax(jaaba_data[['Left','Right']])


    return jaaba_data
Ejemplo n.º 2
0
    def get_stims_from_bag(self, FMF_DIR, BAGS, EXPDIR):

        print "Processing: ", FMF_DIR

        #JAABA_CSV               = FMF_DIR + '/registered_trx.csv'

        FLY_ID, FMF_TIME, GROUP = utilities.parse_fmftime(FMF_DIR)

        BAG_FILE = utilities.match_fmf_and_bag(FMF_DIR, BAGS)

        WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, EXPDIR)

        #jaaba_data = pd.read_csv(JAABA_CSV, sep=',', names=['Timestamp','Length','Width','Theta','Left','Right'], index_col=False)
        #jaaba_data[['Length','Width','Left','Right']] = jaaba_data[['Length','Width','Left','Right']].astype(np.float64)

        # ALIGN LASER STATE DATA
        jaaba_data = DataFrame(index=self._zoomfmf.get_all_timestamps())
        jaaba_data['Timestamp'] = self._zoomfmf.get_all_timestamps()
        jaaba_data = utilities.convert_timestamps(jaaba_data)
        jaaba_data['Laser1_state'] = utilities.binarize_laser_data(
            BAG_FILE, 2).resample('10ms', how='max')['Laser_state'].asof(
                jaaba_data.index).fillna(value=0)  #YAY!
        jaaba_data['Laser2_state'] = utilities.binarize_laser_data(
            BAG_FILE, 4).resample('10ms', how='max')['Laser_state'].asof(
                jaaba_data.index).fillna(value=0)
        """
        # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
        targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
        targets.plot_targets_on_background()
        jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)
        """
        jaaba_data[
            'Timestamp'] = jaaba_data.index  #silly pandas bug for subtracting from datetimeindex...
        try:
            jaaba_data['synced_time'] = jaaba_data[
                'Timestamp'] - jaaba_data.Timestamp[
                    (jaaba_data.Laser2_state +
                     jaaba_data.Laser1_state) > 0].index[0]
        except:
            print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
            jaaba_data['synced_time'] = jaaba_data[
                'Timestamp'] - jaaba_data.Timestamp.index[0]
        """
        #DISCARD BOGUS WING ANGLE VALUES:
        jaaba_data['Left'][jaaba_data['Left'] < -2.09 ]= np.nan   #2.09 for 120degrees
        jaaba_data['Right'][jaaba_data['Right'] > 2.09] = np.nan
        
        jaaba_data['maxWingAngle'] = jaaba_data[['Left','Right']].abs().max(axis=1)
        
        """
        return jaaba_data
Ejemplo n.º 3
0
def sync_jaaba_with_ros(FMF_DIR, BAGS, JAABA):

    print "Processing: ", FMF_DIR

    JAABA_CSV = FMF_DIR + '/registered_trx.csv'

    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)

    BAG_FILE = utilities.match_fmf_and_bag(FMF_DIR, BAGS)

    WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, JAABA)

    jaaba_data = pd.read_csv(
        JAABA_CSV,
        sep=',',
        names=['Timestamp', 'Length', 'Width', 'Theta', 'Left', 'Right'],
        index_col=False)
    jaaba_data[['Length', 'Width', 'Left',
                'Right']] = jaaba_data[['Length', 'Width', 'Left',
                                        'Right']].astype(np.float64)
    jaaba_data = utilities.convert_timestamps(jaaba_data)

    # ALIGN LASER STATE DATA
    jaaba_data['Laser1_state'] = binarize_laser_data(
        BAG_FILE,
        'laser1')['Laser_state'].asof(jaaba_data.index).fillna(value=0)  #YAY!
    jaaba_data['Laser2_state'] = binarize_laser_data(
        BAG_FILE,
        'laser2')['Laser_state'].asof(jaaba_data.index).fillna(value=0)

    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    print targets.get_targets()
    jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(
        BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)

    jaaba_data[
        'Timestamp'] = jaaba_data.index  #silly pandas bug for subtracting from datetimeindex...
    jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data[
        'Timestamp'][jaaba_data[jaaba_data['Laser2_state'] > 0].index[0]]

    #DISCARD BOGUS WING ANGLE VALUES:
    jaaba_data['Left'][
        jaaba_data['Left'] < -2.3] = np.nan  #2.09 for 120degrees
    jaaba_data['Right'][jaaba_data['Right'] > 2.3] = np.nan

    jaaba_data['maxWingAngle'] = get_absmax(jaaba_data[['Left', 'Right']])

    return jaaba_data
Ejemplo n.º 4
0
    def __init__(self, fmf_dir, savedir, fly_id, plot_overlays=False):

        if (savedir[-1] == '/'):
            savedir = savedir[:-1]

        if not os.path.exists(savedir + '/temp_png'):
            os.makedirs(savedir + '/temp_png')

        self._savedir = savedir

        if (fmf_dir[-1] == '/'):
            self._fmf_dir = fmf_dir[:-1]
        else:
            self._fmf_dir = fmf_dir

        self._plot_overlays = plot_overlays

        self._expdir = fmf_dir.rsplit('/', 1)[0]

        self._bag = utilities.match_fmf_and_bag(self._fmf_dir,
                                                (self._expdir + '/BAGS'))

        self._wide = utilities.match_wide_to_zoom(self._fmf_dir,
                                                  (self._expdir + '/'))

        self._widefmf = FMF.FlyMovie(self._wide)

        self._handle, __, ___ = utilities.parse_fmftime(self._fmf_dir)

        self._zoomfmf, self._data, self._Tzero = self.get_data()

        if self._data.Laser0_state.mean(
        ) >= 0.5:  #SOME EXPERIMENTS ARE DONE WITH lASER0 OFF.
            if self._data.Laser0_state.mean(
            ) <= 0.99:  #SOME BAGFILES HAVE A FEW MSGS BEFORE LASER CONFIG
                self.ILLUMINATED_WITH_LASER0 = 1
            else:
                self.ILLUMINATED_WITH_LASER0 = 0
        else:
            self.ILLUMINATED_WITH_LASER0 = 0

        self._image_height, self._image_width = self._zoomfmf.get_frame(
            0)[0].shape
Ejemplo n.º 5
0
    def get_stims_from_bag(self, FMF_DIR, BAGS, EXPDIR):

        print "Processing: ", FMF_DIR
        
        #JAABA_CSV               = FMF_DIR + '/registered_trx.csv'
        
        FLY_ID, FMF_TIME, GROUP = utilities.parse_fmftime(FMF_DIR)
        
        BAG_FILE                = utilities.match_fmf_and_bag(FMF_DIR, BAGS)
        
        WIDE_FMF                = utilities.match_wide_to_zoom(FMF_DIR, EXPDIR)
        
        #jaaba_data = pd.read_csv(JAABA_CSV, sep=',', names=['Timestamp','Length','Width','Theta','Left','Right'], index_col=False)
        #jaaba_data[['Length','Width','Left','Right']] = jaaba_data[['Length','Width','Left','Right']].astype(np.float64)
        
        
        # ALIGN LASER STATE DATA
        jaaba_data = DataFrame(index=self._zoomfmf.get_all_timestamps())
        jaaba_data['Timestamp'] = self._zoomfmf.get_all_timestamps()
        jaaba_data = utilities.convert_timestamps(jaaba_data)
        jaaba_data['Laser1_state'] = utilities.binarize_laser_data(BAG_FILE, 2).resample('10ms', how='max')['Laser_state'].asof(jaaba_data.index).fillna(value=0)  #YAY! 
        jaaba_data['Laser2_state'] = utilities.binarize_laser_data(BAG_FILE, 4).resample('10ms', how='max')['Laser_state'].asof(jaaba_data.index).fillna(value=0)
        """
        # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
        targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
        targets.plot_targets_on_background()
        jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)
        """
        jaaba_data['Timestamp'] = jaaba_data.index  #silly pandas bug for subtracting from datetimeindex...
        try:
            jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp[(jaaba_data.Laser2_state + jaaba_data.Laser1_state) > 0].index[0]
        except:
            print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
            jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp.index[0]   
        """
        #DISCARD BOGUS WING ANGLE VALUES:
        jaaba_data['Left'][jaaba_data['Left'] < -2.09 ]= np.nan   #2.09 for 120degrees
        jaaba_data['Right'][jaaba_data['Right'] > 2.09] = np.nan
        
        jaaba_data['maxWingAngle'] = jaaba_data[['Left','Right']].abs().max(axis=1)
        
        """
        return jaaba_data
Ejemplo n.º 6
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR
    
    JAABA_CSV               = FMF_DIR + '/registered_trx.csv'
    
    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)
    
    BAG_FILE                = match_fmf_and_bag(FMF_TIME)
    
    WIDE_FMF                = utilities.match_wide_to_zoom(FMF_DIR, JAABA)
    
    jaaba_data = pd.read_csv(JAABA_CSV, sep=',', names=['Timestamp','Length','Width','Theta','Left','Right'], index_col=False)
    jaaba_data[['Length','Width','Left','Right']] = jaaba_data[['Length','Width','Left','Right']].astype(np.float64)
    jaaba_data = convert_timestamps(jaaba_data)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        jaaba_data['Laser0_state'] = laser_states['Laser0_state'].asof(jaaba_data.index).fillna(value=1)
        jaaba_data['Laser1_state'] = laser_states['Laser1_state'].asof(jaaba_data.index).fillna(value=0)  #YAY! 
        jaaba_data['Laser2_state'] = laser_states['Laser2_state'].asof(jaaba_data.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        jaaba_data['Laser0_state'] = 0
        jaaba_data['Laser2_state'] = 0
        jaaba_data['Laser1_state'] = 0
        
    
    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)
    
    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    jaaba_data['fly_x'] = positions['fly_x'].asof(jaaba_data.index).fillna(value=0)
    jaaba_data['fly_y'] = positions['fly_y'].asof(jaaba_data.index).fillna(value=0)
    
    
    jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)
    
    
    jaaba_data['Timestamp'] = jaaba_data.index #silly pandas bug for subtracting from datetimeindex...
    try:
        jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp[(jaaba_data.Laser2_state + jaaba_data.Laser1_state) > 0.001].index[0]
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
        jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp.index[0]
    jaaba_data.index = jaaba_data.synced_time
    jaaba_data.index = pd.to_datetime(jaaba_data.index)

    ###    WING EXTENSION    ###
    jaaba_data['maxWingAngle'] = get_absmax(jaaba_data[['Left','Right']])
    #jaaba_data[jaaba_data['maxWingAngle'] > 3.1] = np.nan
    
    program = None
    

    
    if program == 'IRR':
        BEGINNING =jaaba_data.Timestamp[jaaba_data.synced_time >= -30000000000].index[0]#jaaba_data.Timestamp.index[0]
        #FIRST_IR_ON = jaaba_data.Timestamp[((jaaba_data.Laser1_state > 0.001) & (jaaba_data.synced_time >= -1))].index[0]
        FIRST_IR_ON = jaaba_data.Timestamp[jaaba_data.synced_time >= 0].index[0]
        #FIRST_IR_OFF = jaaba_data.Timestamp[((jaaba_data.Laser1_state > 0.001) & (jaaba_data.synced_time <= 120))].index[-1]
        FIRST_IR_OFF = jaaba_data.Timestamp[jaaba_data.synced_time >= 60000000000].index[0]
        RED_ON = jaaba_data.Timestamp[jaaba_data.Laser2_state > 0.001].index[0]
        RED_OFF = jaaba_data.Timestamp[jaaba_data.Laser2_state > 0.001].index[-1]
        SECOND_IR_ON = jaaba_data.Timestamp[jaaba_data.synced_time >=320000000000].index[0]
        #SECOND_IR_ON = jaaba_data.Timestamp[((jaaba_data.Laser1_state > 0.001) & (jaaba_data.synced_time >= 120))].index[0]
        SECOND_IR_OFF = jaaba_data.Timestamp[jaaba_data.Laser1_state > 0.001].index[-1]
        END = jaaba_data.Timestamp.index[-1]
        
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, BEGINNING, FIRST_IR_ON, '1-prestim', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, FIRST_IR_ON, FIRST_IR_OFF, '2-IR1', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, FIRST_IR_OFF, RED_ON, '3-post-IR1', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, RED_ON,RED_OFF, '4-red', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE,RED_OFF, SECOND_IR_ON,'5-post-red', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE,SECOND_IR_ON,SECOND_IR_OFF,'6-IR2', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE,SECOND_IR_OFF,END,'7-post-IR2', background=False)
    
    
    
    if program == 'dark':
        BEGINNING =jaaba_data.Timestamp[jaaba_data.synced_time >= -30000000000].index[0]
        print set(jaaba_data.Laser0_state), set(jaaba_data.Laser1_state), set(jaaba_data.Laser2_state)
        STIM_ON = jaaba_data.Timestamp[jaaba_data.Laser1_state > 0.001].index[0]
        STIM_OFF = jaaba_data.Timestamp[jaaba_data.Laser1_state > 0.001].index[-1]
        LIGHTS_OUT = jaaba_data.Timestamp[jaaba_data.Laser0_state < 0.001].index[0]
        LIGHTS_ON = jaaba_data.Timestamp[jaaba_data.Laser0_state < 0.001].index[-1]
        END = jaaba_data.Timestamp.index[-1]
        
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, BEGINNING, STIM_ON, '1-prestim', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, STIM_ON,STIM_OFF, '2-stim', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, STIM_OFF, LIGHTS_OUT,'3-post-stim', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, LIGHTS_OUT, LIGHTS_ON,'4-DARK', background=False)
        targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, LIGHTS_ON, END,'7-light', background=False)
        
    
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE)
    
    ### ABDOMINAL BENDING   ###
    jaaba_data[jaaba_data['Length'] > 110] = np.nan  #discard frames with bogus length.  *************
    jaaba_data[jaaba_data['Length'] < 60] = np.nan  #discard frames with bogus length.
    
    trace = plot_single_trace(jaaba_data)
    trace.savefig(JAABA + 'TRACES/' + FLY_ID + '.png')
    plt.close('all')
    
    ###FIRST COURTSHIP BOUT AFTER STIMULUS###
    
    #courted, latency = latency_measures(jaaba_data)
    
    
    if 'binsize' in globals():
        jaaba_data = bin_data(jaaba_data, binsize)
        jaaba_data.to_pickle(JAABA + 'JAR/' + FLY_ID + '_' + binsize + '_fly.pickle')
    else:
        return jaaba_data, courted, latency
Ejemplo n.º 7
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR
    
    JAABA_CSV               = FMF_DIR + '/registered_trx.csv'
    
    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)
    
    BAG_FILE                = match_fmf_and_bag(FMF_TIME)
    
    WIDE_FMF                = utilities.match_wide_to_zoom(FMF_DIR, JAABA)
    
    jaaba_data = pd.read_csv(JAABA_CSV, sep=',', names=['Timestamp','Length','Width','Theta','Left','Right'], index_col=False)
    jaaba_data[['Length','Width','Left','Right']] = jaaba_data[['Length','Width','Left','Right']].astype(np.float64)
    jaaba_data = convert_timestamps(jaaba_data)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        jaaba_data['Laser0_state'] = laser_states['Laser0_state'].asof(jaaba_data.index).fillna(value=1)
        jaaba_data['Laser1_state'] = laser_states['Laser1_state'].asof(jaaba_data.index).fillna(value=0)  #YAY! 
        jaaba_data['Laser2_state'] = laser_states['Laser2_state'].asof(jaaba_data.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        jaaba_data['Laser0_state'] = 0
        jaaba_data['Laser2_state'] = 0
        jaaba_data['Laser1_state'] = 0
        
    
    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)
    
    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    jaaba_data['fly_x'] = positions['fly_x'].asof(jaaba_data.index).fillna(value=0)
    jaaba_data['fly_y'] = positions['fly_y'].asof(jaaba_data.index).fillna(value=0)
    
    
    jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)
    
    
    jaaba_data['Timestamp'] = jaaba_data.index #silly pandas bug for subtracting from datetimeindex...
    
    number_of_bouts, bout_duration, first_TS, last_TS = utilities.detect_stim_bouts(jaaba_data, 'Laser1_state')
        
    try:
        jaaba_data['synced_time'] = jaaba_data['Timestamp'] - last_TS
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame 1500. "
        jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp.index[1500]
    jaaba_data.index = jaaba_data.synced_time
    jaaba_data.index = pd.to_datetime(jaaba_data.index)

    ###    WING EXTENSION    ###
    jaaba_data['maxWingAngle'] = get_absmax(jaaba_data[['Left','Right']])
    #jaaba_data[jaaba_data['maxWingAngle'] > 3.1] = np.nan
    
    number_of_bouts, stim_duration, first_TS, last_TS = utilities.detect_stim_bouts(jaaba_data, 'Laser2_state')  #HACK DANNO
    jaaba_data['stim_duration'] = stim_duration
    BEGINNING =jaaba_data.Timestamp.index[0]
    END = jaaba_data.Timestamp.index[-1]
    print BEGINNING
    print first_TS
    print last_TS
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, background=False)
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, BEGINNING, first_TS, '1-prestim', background=False)
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, first_TS,last_TS, '2-red', background=False)
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, last_TS, END,'3-post-red', background=False)

    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE)
    
    ### ABDOMINAL BENDING   ###
    jaaba_data[jaaba_data['Length'] > 110] = np.nan  #discard frames with bogus length.  *************
    jaaba_data[jaaba_data['Length'] < 60] = np.nan  #discard frames with bogus length.
    
    trace = plot_single_trace(jaaba_data)
    trace.savefig(JAABA + 'TRACES/' + FLY_ID + '.png')
    plt.close('all')
    
    
    if 'binsize' in globals():
        jaaba_data = bin_data(jaaba_data, binsize)
        jaaba_data.to_pickle(JAABA + 'JAR/' + FLY_ID + '_' + binsize + '_fly.pickle')
    else:
        return jaaba_data, courted, latency
Ejemplo n.º 8
0
    def sync_jaaba_with_ros(self, FMF_DIR, BAGS, JAABA):

        print "Processing: ", FMF_DIR

        JAABA_CSV = FMF_DIR + '/registered_trx.csv'

        FLY_ID, FMF_TIME, GROUP = utilities.parse_fmftime(FMF_DIR)

        BAG_FILE = utilities.match_fmf_and_bag(FMF_DIR, BAGS)

        WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, JAABA)

        jaaba_data = pd.read_csv(
            JAABA_CSV,
            sep=',',
            names=['Timestamp', 'Length', 'Width', 'Theta', 'Left', 'Right'],
            index_col=False)
        jaaba_data[['Length', 'Width', 'Left', 'Right'
                    ]] = jaaba_data[['Length', 'Width', 'Left',
                                     'Right']].astype(np.float64)
        jaaba_data = utilities.convert_timestamps(jaaba_data)

        # ALIGN LASER STATE DATA

        laser_states = utilities.get_laser_states(BAG_FILE)
        try:
            jaaba_data['Laser0_state'] = laser_states['Laser0_state'].asof(
                jaaba_data.index).fillna(value=0)
            jaaba_data['Laser1_state'] = laser_states['Laser1_state'].asof(
                jaaba_data.index).fillna(value=0)  #YAY!
            jaaba_data['Laser2_state'] = laser_states['Laser2_state'].asof(
                jaaba_data.index).fillna(value=0)
        except:
            print "\t ERROR: problem interpreting laser current values."
            jaaba_data['Laser0_state'] = 0
            jaaba_data['Laser2_state'] = 0
            jaaba_data['Laser1_state'] = 0

        # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
        targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
        targets.plot_targets_on_background()
        jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(
            BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)

        jaaba_data[
            'Timestamp'] = jaaba_data.index  #silly pandas bug for subtracting from datetimeindex...
        try:
            jaaba_data['synced_time'] = jaaba_data[
                'Timestamp'] - jaaba_data.Timestamp[
                    (jaaba_data.Laser2_state +
                     jaaba_data.Laser1_state) > 0].index[0]

        except:
            print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
            jaaba_data['synced_time'] = jaaba_data[
                'Timestamp'] - jaaba_data.Timestamp.index[0]

        #DISCARD BOGUS WING ANGLE VALUES:
        jaaba_data['Left'][
            jaaba_data['Left'] < -2.09] = np.nan  #2.09 for 120degrees
        jaaba_data['Right'][jaaba_data['Right'] > 2.09] = np.nan

        jaaba_data['maxWingAngle'] = jaaba_data[['Left',
                                                 'Right']].abs().max(axis=1)

        return jaaba_data
Ejemplo n.º 9
0
def sync_video_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR
    
        
    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)
    
    BAG_FILE                = match_fmf_and_bag(FMF_TIME)
    
    WIDE_FMF                = utilities.match_wide_to_zoom(FMF_DIR, MAIN_DIRECTORY+"/")
    
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)    
    
    dtarget = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget']
    
    arenaCtr, Arena_radius = targets._arena.circ
    
    for x in utilities.find_files(FMF_DIR, '*zoom*.fmf'):
        ZOOM_FMF = x
    
    if not os.path.exists(MAIN_DIRECTORY + '/JAR/wing_data') ==True:
            os.makedirs(MAIN_DIRECTORY + '/JAR/wing_data')
    if args.save_wingext_pngs == True:
        if not os.path.exists(FMF_DIR + '/TRACKING_RESULTS'):
            os.makedirs(FMF_DIR + '/TRACKING_RESULTS')
        wingImageDir = FMF_DIR + '/TRACKING_RESULTS'
    else:
        wingImageDir=None
    
    
    if not os.path.exists(MAIN_DIRECTORY + '/JAR/wing_data/'+ FLY_ID + '_wing_angles.pickle') ==True:
        wings = wing_detector.WingDetector(ZOOM_FMF, BAG_FILE, dtarget, arenaCtr, wingImageDir )
        wings.execute()
        wings.wingData.to_pickle(MAIN_DIRECTORY + '/JAR/wing_data/'+ FLY_ID + '_wing_angles.pickle')
    
    videoData = pd.read_pickle(MAIN_DIRECTORY + '/JAR/wing_data/'+ FLY_ID + '_wing_angles.pickle')
    videoData[['Length','Width','Left','Right']] = videoData[['Length','Width','Left','Right']].astype(np.float64)
    videoData = convert_timestamps(videoData)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        videoData['Laser0_state'] = laser_states['Laser0_state'].asof(videoData.index).fillna(value=1)
        videoData['Laser1_state'] = laser_states['Laser1_state'].asof(videoData.index).fillna(value=0)  #YAY! 
        videoData['Laser2_state'] = laser_states['Laser2_state'].asof(videoData.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        videoData['Laser0_state'] = 0
        videoData['Laser2_state'] = 0
        videoData['Laser1_state'] = 0
        
    
    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET

    
    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    videoData['fly_x'] = positions['fly_x'].asof(videoData.index).fillna(value=0)
    videoData['fly_y'] = positions['fly_y'].asof(videoData.index).fillna(value=0)
    
    
    videoData['dtarget'] = dTarget.asof(videoData.index).fillna(value=0)
    
    
    videoData['Timestamp'] = videoData.index #silly pandas bug for subtracting from datetimeindex...
    try:
        videoData['synced_time'] = videoData['Timestamp'] - videoData.Timestamp[(videoData.Laser2_state + videoData.Laser1_state) > 0.001].index[0]
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
        videoData['synced_time'] = videoData['Timestamp'] - videoData.Timestamp.index[0]
    videoData.index = videoData.synced_time
    videoData.index = pd.to_datetime(videoData.index)

    ###    WING EXTENSION    ###
    videoData['maxWingAngle'] = get_absmax(videoData[['Left','Right']])
    #videoData[videoData['maxWingAngle'] > 3.1] = np.nan
    
    program = 'dark' #FIXME
    

    
    if program == 'IRR':
        BEGINNING =videoData.Timestamp[videoData.synced_time >= -30000000000].index[0]#videoData.Timestamp.index[0]
        #FIRST_IR_ON = videoData.Timestamp[((videoData.Laser1_state > 0.001) & (videoData.synced_time >= -1))].index[0]
        FIRST_IR_ON = videoData.Timestamp[videoData.synced_time >= 0].index[0]
        #FIRST_IR_OFF = videoData.Timestamp[((videoData.Laser1_state > 0.001) & (videoData.synced_time <= 120))].index[-1]
        FIRST_IR_OFF = videoData.Timestamp[videoData.synced_time >= 60000000000].index[0]
        RED_ON = videoData.Timestamp[videoData.Laser2_state > 0.001].index[0]
        RED_OFF = videoData.Timestamp[videoData.Laser2_state > 0.001].index[-1]
        SECOND_IR_ON = videoData.Timestamp[videoData.synced_time >=320000000000].index[0]
        #SECOND_IR_ON = videoData.Timestamp[((videoData.Laser1_state > 0.001) & (videoData.synced_time >= 120))].index[0]
        SECOND_IR_OFF = videoData.Timestamp[videoData.Laser1_state > 0.001].index[-1]
        END = videoData.Timestamp.index[-1]
        
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, BEGINNING, FIRST_IR_ON, '1-prestim', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, FIRST_IR_ON, FIRST_IR_OFF, '2-IR1', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, FIRST_IR_OFF, RED_ON, '3-post-IR1', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, RED_ON,RED_OFF, '4-red', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE,RED_OFF, SECOND_IR_ON,'5-post-red', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE,SECOND_IR_ON,SECOND_IR_OFF,'6-IR2', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE,SECOND_IR_OFF,END,'7-post-IR2', background=False)
    
    
    
    if program == 'dark':
        BEGINNING =videoData.Timestamp[videoData.synced_time >= -30000000000].index[0]
        print set(videoData.Laser0_state), set(videoData.Laser1_state), set(videoData.Laser2_state)
        STIM_ON = videoData.Timestamp[videoData.Laser1_state > 0.001].index[0]
        STIM_OFF = videoData.Timestamp[videoData.Laser1_state > 0.001].index[-1]
        LIGHTS_OUT = videoData.Timestamp[videoData.Laser0_state < 0.001].index[0]
        LIGHTS_ON = videoData.Timestamp[videoData.Laser0_state < 0.001].index[-1]
        END = videoData.Timestamp.index[-1]
        
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, BEGINNING, STIM_ON, '1-prestim', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, STIM_ON,STIM_OFF, '2-stim', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, STIM_OFF, LIGHTS_OUT,'3-post-stim', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, LIGHTS_OUT, LIGHTS_ON,'4-DARK', background=False)
        targets.plot_trajectory_and_wingext(videoData, BAG_FILE, LIGHTS_ON, END,'7-light', background=False)
        
    
    targets.plot_trajectory_and_wingext(videoData, BAG_FILE)
    
    ### ABDOMINAL BENDING   ###
    videoData[videoData['Length'] > 110] = np.nan  #discard frames with bogus length.  *************
    videoData[videoData['Length'] < 60] = np.nan  #discard frames with bogus length.
    
    trace = plot_single_trace(videoData)
    trace.savefig(MAIN_DIRECTORY + '/TRACES/' + FLY_ID + '.png')
    plt.close('all')
    
    ###FIRST COURTSHIP BOUT AFTER STIMULUS###
    
    #courted, latency = latency_measures(videoData)
    
    
    if 'binsize' in globals():
        videoData = bin_data(videoData, binsize)
        videoData.to_pickle(MAIN_DIRECTORY + '/JAR/' + FLY_ID + '_' + binsize + '_fly.pickle')
    else:
        return videoData, courted, latency
Ejemplo n.º 10
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR
    
    DATADIR_CSV               = FMF_DIR + '/registered_trx.csv'
    
    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)
    
    BAG_FILE                = match_fmf_and_bag(FMF_TIME)
    
    WIDE_FMF                = utilities.match_wide_to_zoom(FMF_DIR, DATADIR)

    FLY_IDx = FMF_DIR.split('/')[-1]
    EXP_ID, DATE, TIME = FLY_IDx.split('_', 4)[0:3]    
    
    
    for x in glob.glob(FMF_DIR +'/*zoom*'+'*.fmf'):
        ZOOM_FMF = x

    
    if not os.path.exists(FMF_DIR + '/TRACKING_FRAMES') == True:
        os.makedirs(FMF_DIR + '/TRACKING_FRAMES')
    
    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    if (os.path.exists(FMF_DIR + '/frame_by_frame_synced.pickle')) and not RETRACK:
        datadf = pd.read_pickle(FMF_DIR + '/frame_by_frame_synced.pickle')
        exp_group = EXP_ID #QUICKFIX. FIXME
    else:
        targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
        targets.plot_targets_on_background()
        targets.plot_trajectory_on_background(BAG_FILE)
        
        dtarget = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget']
        (arena_centre), arena_radius = targets._arena.circ
        
        if MAKE_MOVIES:
            TRACKING_DIRECTORY = FMF_DIR + '/TRACKING_FRAMES/'
        else:
            TRACKING_DIRECTORY = None
        
        if (os.path.exists(FMF_DIR + '/wingdata.pickle')) and not RETRACK:
            datadf = pd.read_pickle(FMF_DIR + '/wingdata.pickle')
            datadf.columns= ['BodyAxis','leftAngle','leftWingLength','Length','rightAngle','rightWingLength','target_angle_TTM',
                                     'target_distance_TTM','Timestamp','Width']
        else:
            try:
                wings = wing_detector.WingDetector(ZOOM_FMF, BAG_FILE, dtarget, arena_centre, RETRACK, TRACKING_DIRECTORY )
                
                wings.execute()
                wings.wingData.columns= ['BodyAxis','leftAngle','leftWingLength','Length','rightAngle','rightWingLength','target_angle_TTM',
                                         'target_distance_TTM','Timestamp','Width']
                wings.wingData.to_pickle(FMF_DIR + '/wingdata.pickle')
                wings.tracking_info.to_pickle(FMF_DIR + '/tracking_info.pickle')
                datadf = DataFrame(wings.wingData)
            
            
                if MAKE_MOVIES:
                    utilities.call_command("ffmpeg -f image2 -r 15 -i "+ TRACKING_DIRECTORY + "_tmp%05d.png -vf scale=iw/2:-1 -vcodec mpeg4 -b 8000k -y " + FMF_DIR + "/tracked_movie.mp4;")
                    utilities.call_command("rm -r " + TRACKING_DIRECTORY)
                    #wings.make_movie(wings._tempdir, wings.fmf_file.rsplit('/',1)[0]+'/tracked_movie.mp4',15)
                
            except Exception,e:
                traceback.print_exc() 
                print 'ERROR PROCESSING WING TRACKING...', FMF_DIR
                print str(e)
                
                return
                
        datadf['Frame_number'] = datadf.index
        datadf = convert_timestamps(datadf)

        # ALIGN LASER STATE DATA
        laser_states = utilities.get_laser_states(BAG_FILE)
        try:
            datadf['Laser0_state'] = laser_states['Laser0_state'].asof(datadf.index).fillna(value=1)
            datadf['Laser1_state'] = laser_states['Laser1_state'].asof(datadf.index).fillna(value=0)  #YAY! 
            datadf['Laser2_state'] = laser_states['Laser2_state'].asof(datadf.index).fillna(value=0)
        except:
            print "\t ERROR: problem interpreting laser current values."
            datadf['Laser0_state'] = 0
            datadf['Laser2_state'] = 0
            datadf['Laser1_state'] = 0
            

        
        positions = utilities.get_positions_from_bag(BAG_FILE)
        positions = utilities.convert_timestamps(positions)
        datadf['fly_x'] = positions['fly_x'].asof(datadf.index).fillna(method='pad')
        datadf['fly_y'] = positions['fly_y'].asof(datadf.index).fillna(method='pad')
        
        datadf['dcentre'] = np.sqrt(((datadf['fly_x']-arena_centre[0])/4.8)**2 + ((datadf['fly_y']-arena_centre[1])/5.2)**2)
        datadf['dtarget_temp'] = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget'].asof(datadf.index).fillna(method='pad')
        datadf.loc[datadf['dtarget_temp'] >=6.0, 'target_distance_TTM'] = np.nan 
        datadf['dtarget'] = datadf['target_distance_TTM'].fillna(datadf['dtarget_temp'])
        
        
        datadf['Timestamp'] = datadf.index #silly pandas bug for subtracting from datetimeindex...
        
        #number_of_bouts, bout_duration, first_TS, last_TS = utilities.detect_stim_bouts(datadf, 'Laser1_state')

        number_of_bouts, stim_duration, first_TS, last_TS = utilities.detect_stim_bouts(datadf, 'Laser2_state')  #HACK DANNO
        datadf['stim_duration'] = stim_duration
            
        try:
            if stim_duration >= 0:
                datadf['synced_time'] = datadf['Timestamp'] - datadf.Timestamp[(datadf.Laser2_state + datadf.Laser1_state) > 0.001].index[0]
            #elif stim_duration == 0:
            #    datadf['synced_time'] = datadf['Timestamp'] - datadf.Timestamp.index[0] - pd.to_datetime('60s')
        except Exception,e:
            traceback.print_exc() 
            print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame 900. "
            datadf['synced_time'] = datadf['Timestamp'] - datadf.Timestamp.index[900]
            print str(e)
Ejemplo n.º 11
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR

    JAABA_CSV = FMF_DIR + '/registered_trx.csv'

    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)

    BAG_FILE = match_fmf_and_bag(FMF_TIME)

    WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, JAABA)

    jaaba_data = pd.read_csv(
        JAABA_CSV,
        sep=',',
        names=['Timestamp', 'Length', 'Width', 'Theta', 'Left', 'Right'],
        index_col=False)
    jaaba_data[['Length', 'Width', 'Left',
                'Right']] = jaaba_data[['Length', 'Width', 'Left',
                                        'Right']].astype(np.float64)
    jaaba_data = convert_timestamps(jaaba_data)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        jaaba_data['Laser0_state'] = laser_states['Laser0_state'].asof(
            jaaba_data.index).fillna(value=0)
        jaaba_data['Laser1_state'] = laser_states['Laser1_state'].asof(
            jaaba_data.index).fillna(value=0)  #YAY!
        jaaba_data['Laser2_state'] = laser_states['Laser2_state'].asof(
            jaaba_data.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        jaaba_data['Laser0_state'] = 0
        jaaba_data['Laser2_state'] = 0
        jaaba_data['Laser1_state'] = 0

    print '\t acquired stimulus data...'
    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)

    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    jaaba_data['fly_x'] = positions['fly_x'].asof(
        jaaba_data.index).fillna(value=0)
    jaaba_data['fly_y'] = positions['fly_y'].asof(
        jaaba_data.index).fillna(value=0)

    jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(
        BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)
    print '\t computed distance to target...'
    ###    WING EXTENSION    ###
    jaaba_data['maxWingAngle'] = get_absmax(jaaba_data[['Left', 'Right']])
    jaaba_data[jaaba_data['maxWingAngle'] > 2.1] = np.nan
    print '\t computed maximum wing angle...'

    ### PLOT TRAJECTORIES DURING EXPERIMENT SECTIONS ###

    first_IR = jaaba_data[(jaaba_data.Laser1_state) == 1].index[0]
    last_IR = jaaba_data[(jaaba_data.Laser1_state) == 1].index[-1]
    #first_RED = jaaba_data.Timestamp[(jaaba_data.Laser2_state) == 1].index[0]
    #last_RED = jaaba_data.Timestamp[(jaaba_data.Laser2_state) == 1].index[-1]
    first_L0 = jaaba_data[(jaaba_data.Laser0_state) == 0].index[0]
    last_L0 = jaaba_data[(jaaba_data.Laser0_state) == 0].index[-1]

    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE,
                                        jaaba_data.index[0], first_IR,
                                        '1_pre-stim')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, first_IR,
                                        last_IR, '2_stim')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, last_IR,
                                        first_L0, '3_post-stim')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, first_L0,
                                        last_L0, '4_dark')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, last_L0,
                                        jaaba_data.index[-1], '5_post-dark')

    print '\t generated wing extension 2D trajectory plots...'

    ### ABDOMINAL BENDING   ###
    jaaba_data[jaaba_data['Length'] >
               110] = np.nan  #discard frames with bogus length.  *************
    jaaba_data[
        jaaba_data['Length'] < 60] = np.nan  #discard frames with bogus length.

    #SYNCHRONIZE BY FIRST STIMULUS TIME

    jaaba_data[
        'Timestamp'] = jaaba_data.index  #silly pandas bug for subtracting from datetimeindex...
    try:
        jaaba_data[
            'synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp[
                (jaaba_data.Laser2_state +
                 jaaba_data.Laser1_state) > 0.001].index[0]
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
        jaaba_data['synced_time'] = jaaba_data[
            'Timestamp'] - jaaba_data.Timestamp.index[0]
    jaaba_data.index = jaaba_data.synced_time
    jaaba_data.index = pd.to_datetime(jaaba_data.index)

    trace = plot_single_trace(jaaba_data)
    trace.savefig(JAABA + 'TRACES/' + FLY_ID + '.png')
    plt.close('all')

    ###FIRST COURTSHIP BOUT AFTER STIMULUS###

    #courted, latency = latency_measures(jaaba_data)

    if 'binsize' in globals():
        jaaba_data = bin_data(jaaba_data, binsize)
        jaaba_data.to_pickle(JAABA + 'JAR/' + FLY_ID + '_' + binsize +
                             '_fly.pickle')
    else:
        return jaaba_data, courted, latency
Ejemplo n.º 12
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR
    
    JAABA_CSV               = FMF_DIR + '/registered_trx.csv'
    
    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)
    
    BAG_FILE                = match_fmf_and_bag(FMF_TIME)
    
    WIDE_FMF                = utilities.match_wide_to_zoom(FMF_DIR, JAABA)
    
    jaaba_data = pd.read_csv(JAABA_CSV, sep=',', names=['Timestamp','Length','Width','Theta','Left','Right'], index_col=False)
    jaaba_data[['Length','Width','Left','Right']] = jaaba_data[['Length','Width','Left','Right']].astype(np.float64)
    jaaba_data = convert_timestamps(jaaba_data)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        jaaba_data['Laser0_state'] = laser_states['Laser0_state'].asof(jaaba_data.index).fillna(value=0)
        jaaba_data['Laser1_state'] = laser_states['Laser1_state'].asof(jaaba_data.index).fillna(value=0)  #YAY! 
        jaaba_data['Laser2_state'] = laser_states['Laser2_state'].asof(jaaba_data.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        jaaba_data['Laser0_state'] = 0
        jaaba_data['Laser2_state'] = 0
        jaaba_data['Laser1_state'] = 0
        
    print '\t acquired stimulus data...'
    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)
    
    
    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    jaaba_data['fly_x'] = positions['fly_x'].asof(jaaba_data.index).fillna(value=0)
    jaaba_data['fly_y'] = positions['fly_y'].asof(jaaba_data.index).fillna(value=0)
    
    
    jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)
    print '\t computed distance to target...'
    ###    WING EXTENSION    ###
    jaaba_data['maxWingAngle'] = get_absmax(jaaba_data[['Left','Right']])
    jaaba_data[jaaba_data['maxWingAngle'] > 2.1] = np.nan
    print '\t computed maximum wing angle...'
    
    ### PLOT TRAJECTORIES DURING EXPERIMENT SECTIONS ###
    
    first_IR = jaaba_data[(jaaba_data.Laser1_state) == 1].index[0]
    last_IR = jaaba_data[(jaaba_data.Laser1_state) == 1].index[-1]
    #first_RED = jaaba_data.Timestamp[(jaaba_data.Laser2_state) == 1].index[0]
    #last_RED = jaaba_data.Timestamp[(jaaba_data.Laser2_state) == 1].index[-1]
    first_L0 = jaaba_data[(jaaba_data.Laser0_state) == 0].index[0]
    last_L0 = jaaba_data[(jaaba_data.Laser0_state) == 0].index[-1]
    
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, jaaba_data.index[0], first_IR, '1_pre-stim')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, first_IR, last_IR, '2_stim')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, last_IR, first_L0, '3_post-stim')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, first_L0, last_L0, '4_dark')
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, last_L0, jaaba_data.index[-1], '5_post-dark')
    
    print '\t generated wing extension 2D trajectory plots...'
    
    ### ABDOMINAL BENDING   ###
    jaaba_data[jaaba_data['Length'] > 110] = np.nan  #discard frames with bogus length.  *************
    jaaba_data[jaaba_data['Length'] < 60] = np.nan  #discard frames with bogus length.
    

    
    
    #SYNCHRONIZE BY FIRST STIMULUS TIME
    
    jaaba_data['Timestamp'] = jaaba_data.index #silly pandas bug for subtracting from datetimeindex...
    try:
        jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp[(jaaba_data.Laser2_state + jaaba_data.Laser1_state) > 0.001].index[0]
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
        jaaba_data['synced_time'] = jaaba_data['Timestamp'] - jaaba_data.Timestamp.index[0]
    jaaba_data.index = jaaba_data.synced_time
    jaaba_data.index = pd.to_datetime(jaaba_data.index)
    
    trace = plot_single_trace(jaaba_data)
    trace.savefig(JAABA + 'TRACES/' + FLY_ID + '.png')
    plt.close('all')
    
    ###FIRST COURTSHIP BOUT AFTER STIMULUS###
    
    #courted, latency = latency_measures(jaaba_data)
    
    
    if 'binsize' in globals():
        jaaba_data = bin_data(jaaba_data, binsize)
        jaaba_data.to_pickle(JAABA + 'JAR/' + FLY_ID + '_' + binsize + '_fly.pickle')
    else:
        return jaaba_data, courted, latency
Ejemplo n.º 13
0
def sync_video_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR

    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)

    BAG_FILE = match_fmf_and_bag(FMF_TIME)

    WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, MAIN_DIRECTORY + "/")

    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)

    dtarget = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget']

    arenaCtr, Arena_radius = targets._arena.circ

    for x in utilities.find_files(FMF_DIR, '*zoom*.fmf'):
        ZOOM_FMF = x

    if not os.path.exists(MAIN_DIRECTORY + '/JAR/wing_data') == True:
        os.makedirs(MAIN_DIRECTORY + '/JAR/wing_data')
    if args.save_wingext_pngs == True:
        if not os.path.exists(FMF_DIR + '/TRACKING_RESULTS'):
            os.makedirs(FMF_DIR + '/TRACKING_RESULTS')
        wingImageDir = FMF_DIR + '/TRACKING_RESULTS'
    else:
        wingImageDir = None

    if not os.path.exists(MAIN_DIRECTORY + '/JAR/wing_data/' + FLY_ID +
                          '_wing_angles.pickle') == True:
        wings = wing_detector.WingDetector(ZOOM_FMF, BAG_FILE, dtarget,
                                           arenaCtr, wingImageDir)
        wings.execute()
        wings.wingData.to_pickle(MAIN_DIRECTORY + '/JAR/wing_data/' + FLY_ID +
                                 '_wing_angles.pickle')

    videoData = pd.read_pickle(MAIN_DIRECTORY + '/JAR/wing_data/' + FLY_ID +
                               '_wing_angles.pickle')
    videoData[['Length', 'Width', 'Left',
               'Right']] = videoData[['Length', 'Width', 'Left',
                                      'Right']].astype(np.float64)
    videoData = convert_timestamps(videoData)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        videoData['Laser0_state'] = laser_states['Laser0_state'].asof(
            videoData.index).fillna(value=1)
        videoData['Laser1_state'] = laser_states['Laser1_state'].asof(
            videoData.index).fillna(value=0)  #YAY!
        videoData['Laser2_state'] = laser_states['Laser2_state'].asof(
            videoData.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        videoData['Laser0_state'] = 0
        videoData['Laser2_state'] = 0
        videoData['Laser1_state'] = 0

    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET

    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    videoData['fly_x'] = positions['fly_x'].asof(
        videoData.index).fillna(value=0)
    videoData['fly_y'] = positions['fly_y'].asof(
        videoData.index).fillna(value=0)

    videoData['dtarget'] = dTarget.asof(videoData.index).fillna(value=0)

    videoData[
        'Timestamp'] = videoData.index  #silly pandas bug for subtracting from datetimeindex...
    try:
        videoData['synced_time'] = videoData['Timestamp'] - videoData.Timestamp[
            (videoData.Laser2_state + videoData.Laser1_state) > 0.001].index[0]
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
        videoData['synced_time'] = videoData[
            'Timestamp'] - videoData.Timestamp.index[0]
    videoData.index = videoData.synced_time
    videoData.index = pd.to_datetime(videoData.index)

    ###    WING EXTENSION    ###
    videoData['maxWingAngle'] = get_absmax(videoData[['Left', 'Right']])
    #videoData[videoData['maxWingAngle'] > 3.1] = np.nan

    program = 'dark'  #FIXME

    if program == 'IRR':
        BEGINNING = videoData.Timestamp[
            videoData.synced_time >= -30000000000].index[
                0]  #videoData.Timestamp.index[0]
        #FIRST_IR_ON = videoData.Timestamp[((videoData.Laser1_state > 0.001) & (videoData.synced_time >= -1))].index[0]
        FIRST_IR_ON = videoData.Timestamp[videoData.synced_time >= 0].index[0]
        #FIRST_IR_OFF = videoData.Timestamp[((videoData.Laser1_state > 0.001) & (videoData.synced_time <= 120))].index[-1]
        FIRST_IR_OFF = videoData.Timestamp[
            videoData.synced_time >= 60000000000].index[0]
        RED_ON = videoData.Timestamp[videoData.Laser2_state > 0.001].index[0]
        RED_OFF = videoData.Timestamp[videoData.Laser2_state > 0.001].index[-1]
        SECOND_IR_ON = videoData.Timestamp[
            videoData.synced_time >= 320000000000].index[0]
        #SECOND_IR_ON = videoData.Timestamp[((videoData.Laser1_state > 0.001) & (videoData.synced_time >= 120))].index[0]
        SECOND_IR_OFF = videoData.Timestamp[
            videoData.Laser1_state > 0.001].index[-1]
        END = videoData.Timestamp.index[-1]

        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            BEGINNING,
                                            FIRST_IR_ON,
                                            '1-prestim',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            FIRST_IR_ON,
                                            FIRST_IR_OFF,
                                            '2-IR1',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            FIRST_IR_OFF,
                                            RED_ON,
                                            '3-post-IR1',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            RED_ON,
                                            RED_OFF,
                                            '4-red',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            RED_OFF,
                                            SECOND_IR_ON,
                                            '5-post-red',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            SECOND_IR_ON,
                                            SECOND_IR_OFF,
                                            '6-IR2',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            SECOND_IR_OFF,
                                            END,
                                            '7-post-IR2',
                                            background=False)

    if program == 'dark':
        BEGINNING = videoData.Timestamp[
            videoData.synced_time >= -30000000000].index[0]
        print set(videoData.Laser0_state), set(videoData.Laser1_state), set(
            videoData.Laser2_state)
        STIM_ON = videoData.Timestamp[videoData.Laser1_state > 0.001].index[0]
        STIM_OFF = videoData.Timestamp[
            videoData.Laser1_state > 0.001].index[-1]
        LIGHTS_OUT = videoData.Timestamp[
            videoData.Laser0_state < 0.001].index[0]
        LIGHTS_ON = videoData.Timestamp[
            videoData.Laser0_state < 0.001].index[-1]
        END = videoData.Timestamp.index[-1]

        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            BEGINNING,
                                            STIM_ON,
                                            '1-prestim',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            STIM_ON,
                                            STIM_OFF,
                                            '2-stim',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            STIM_OFF,
                                            LIGHTS_OUT,
                                            '3-post-stim',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            LIGHTS_OUT,
                                            LIGHTS_ON,
                                            '4-DARK',
                                            background=False)
        targets.plot_trajectory_and_wingext(videoData,
                                            BAG_FILE,
                                            LIGHTS_ON,
                                            END,
                                            '7-light',
                                            background=False)

    targets.plot_trajectory_and_wingext(videoData, BAG_FILE)

    ### ABDOMINAL BENDING   ###
    videoData[videoData['Length'] >
              110] = np.nan  #discard frames with bogus length.  *************
    videoData[
        videoData['Length'] < 60] = np.nan  #discard frames with bogus length.

    trace = plot_single_trace(videoData)
    trace.savefig(MAIN_DIRECTORY + '/TRACES/' + FLY_ID + '.png')
    plt.close('all')

    ###FIRST COURTSHIP BOUT AFTER STIMULUS###

    #courted, latency = latency_measures(videoData)

    if 'binsize' in globals():
        videoData = bin_data(videoData, binsize)
        videoData.to_pickle(MAIN_DIRECTORY + '/JAR/' + FLY_ID + '_' + binsize +
                            '_fly.pickle')
    else:
        return videoData, courted, latency
Ejemplo n.º 14
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR

    JAABA_CSV = FMF_DIR + '/registered_trx.csv'

    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)

    BAG_FILE = match_fmf_and_bag(FMF_TIME)

    WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, JAABA)

    jaaba_data = pd.read_csv(
        JAABA_CSV,
        sep=',',
        names=['Timestamp', 'Length', 'Width', 'Theta', 'Left', 'Right'],
        index_col=False)
    jaaba_data[['Length', 'Width', 'Left',
                'Right']] = jaaba_data[['Length', 'Width', 'Left',
                                        'Right']].astype(np.float64)
    jaaba_data = convert_timestamps(jaaba_data)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        jaaba_data['Laser0_state'] = laser_states['Laser0_state'].asof(
            jaaba_data.index).fillna(value=1)
        jaaba_data['Laser1_state'] = laser_states['Laser1_state'].asof(
            jaaba_data.index).fillna(value=0)  #YAY!
        jaaba_data['Laser2_state'] = laser_states['Laser2_state'].asof(
            jaaba_data.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        jaaba_data['Laser0_state'] = 0
        jaaba_data['Laser2_state'] = 0
        jaaba_data['Laser1_state'] = 0

    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)

    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    jaaba_data['fly_x'] = positions['fly_x'].asof(
        jaaba_data.index).fillna(value=0)
    jaaba_data['fly_y'] = positions['fly_y'].asof(
        jaaba_data.index).fillna(value=0)

    jaaba_data['dtarget'] = targets.get_dist_to_nearest_target(
        BAG_FILE)['dtarget'].asof(jaaba_data.index).fillna(value=0)

    jaaba_data[
        'Timestamp'] = jaaba_data.index  #silly pandas bug for subtracting from datetimeindex...

    number_of_bouts, bout_duration, first_TS, last_TS = utilities.detect_stim_bouts(
        jaaba_data, 'Laser1_state')

    try:
        jaaba_data['synced_time'] = jaaba_data['Timestamp'] - last_TS
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame 1500. "
        jaaba_data['synced_time'] = jaaba_data[
            'Timestamp'] - jaaba_data.Timestamp.index[1500]
    jaaba_data.index = jaaba_data.synced_time
    jaaba_data.index = pd.to_datetime(jaaba_data.index)

    ###    WING EXTENSION    ###
    jaaba_data['maxWingAngle'] = get_absmax(jaaba_data[['Left', 'Right']])
    #jaaba_data[jaaba_data['maxWingAngle'] > 3.1] = np.nan

    number_of_bouts, stim_duration, first_TS, last_TS = utilities.detect_stim_bouts(
        jaaba_data, 'Laser2_state')  #HACK DANNO
    jaaba_data['stim_duration'] = stim_duration
    BEGINNING = jaaba_data.Timestamp.index[0]
    END = jaaba_data.Timestamp.index[-1]
    print BEGINNING
    print first_TS
    print last_TS
    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE, background=False)
    targets.plot_trajectory_and_wingext(jaaba_data,
                                        BAG_FILE,
                                        BEGINNING,
                                        first_TS,
                                        '1-prestim',
                                        background=False)
    targets.plot_trajectory_and_wingext(jaaba_data,
                                        BAG_FILE,
                                        first_TS,
                                        last_TS,
                                        '2-red',
                                        background=False)
    targets.plot_trajectory_and_wingext(jaaba_data,
                                        BAG_FILE,
                                        last_TS,
                                        END,
                                        '3-post-red',
                                        background=False)

    targets.plot_trajectory_and_wingext(jaaba_data, BAG_FILE)

    ### ABDOMINAL BENDING   ###
    jaaba_data[jaaba_data['Length'] >
               110] = np.nan  #discard frames with bogus length.  *************
    jaaba_data[
        jaaba_data['Length'] < 60] = np.nan  #discard frames with bogus length.

    trace = plot_single_trace(jaaba_data)
    trace.savefig(JAABA + 'TRACES/' + FLY_ID + '.png')
    plt.close('all')

    if 'binsize' in globals():
        jaaba_data = bin_data(jaaba_data, binsize)
        jaaba_data.to_pickle(JAABA + 'JAR/' + FLY_ID + '_' + binsize +
                             '_fly.pickle')
    else:
        return jaaba_data, courted, latency
Ejemplo n.º 15
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR

    DATADIR_CSV = FMF_DIR + '/registered_trx.csv'

    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)

    BAG_FILE = match_fmf_and_bag(FMF_TIME)

    WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, DATADIR)

    for x in glob.glob(FMF_DIR + '/*zoom*' + '*.fmf'):
        ZOOM_FMF = x

    if not os.path.exists(FMF_DIR + '/TRACKING_FRAMES') == True:
        os.makedirs(FMF_DIR + '/TRACKING_FRAMES')

    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
    targets.plot_targets_on_background()
    targets.plot_trajectory_on_background(BAG_FILE)

    dtarget = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget']
    (arena_centre), arena_radius = targets._arena.circ

    if MAKE_MOVIES:
        TRACKING_DIRECTORY = FMF_DIR + '/TRACKING_FRAMES/'
    else:
        TRACKING_DIRECTORY = None

    if os.path.exists(FMF_DIR + '/wingdata.pickle'):
        datadf = pd.read_pickle(FMF_DIR + '/wingdata.pickle')
        datadf.columns = [
            'BodyAxis', 'leftAngle', 'leftWingLength', 'Length', 'rightAngle',
            'rightWingLength', 'Timestamp', 'Width'
        ]
    else:
        try:
            wings = wing_detector.WingDetector(ZOOM_FMF, BAG_FILE, dtarget,
                                               arena_centre,
                                               TRACKING_DIRECTORY)

            wings.execute()
            wings.wingData.columns = [
                'BodyAxis', 'leftAngle', 'leftWingLength', 'Length',
                'rightAngle', 'rightWingLength', 'Timestamp', 'Width'
            ]
            wings.wingData.to_pickle(FMF_DIR + '/wingdata.pickle')

            datadf = DataFrame(wings.wingData)
        except:
            print 'ERROR PROCESSING WING TRACKING...', FMF_DIR
            return

    datadf['Frame_number'] = datadf.index
    datadf = convert_timestamps(datadf)

    # ALIGN LASER STATE DATA
    laser_states = utilities.get_laser_states(BAG_FILE)
    try:
        datadf['Laser0_state'] = laser_states['Laser0_state'].asof(
            datadf.index).fillna(value=1)
        datadf['Laser1_state'] = laser_states['Laser1_state'].asof(
            datadf.index).fillna(value=0)  #YAY!
        datadf['Laser2_state'] = laser_states['Laser2_state'].asof(
            datadf.index).fillna(value=0)
    except:
        print "\t ERROR: problem interpreting laser current values."
        datadf['Laser0_state'] = 0
        datadf['Laser2_state'] = 0
        datadf['Laser1_state'] = 0

    positions = utilities.get_positions_from_bag(BAG_FILE)
    positions = utilities.convert_timestamps(positions)
    datadf['fly_x'] = positions['fly_x'].asof(datadf.index).fillna(value=0)
    datadf['fly_y'] = positions['fly_y'].asof(datadf.index).fillna(value=0)

    datadf['dcentre'] = np.sqrt((datadf['fly_x'] - arena_centre[0])**2 +
                                (datadf['fly_y'] - arena_centre[1])**2)
    datadf['dtarget'] = targets.get_dist_to_nearest_target(
        BAG_FILE)['dtarget'].asof(datadf.index).fillna(value=0)

    datadf[
        'Timestamp'] = datadf.index  #silly pandas bug for subtracting from datetimeindex...
    try:
        datadf['synced_time'] = datadf['Timestamp'] - datadf.Timestamp[
            (datadf.Laser2_state + datadf.Laser1_state) > 0.001].index[0]
    except:
        print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame0. "
        datadf['synced_time'] = datadf['Timestamp'] - datadf.Timestamp.index[0]
    datadf.index = datadf.synced_time
    datadf.index = pd.to_datetime(datadf.index)

    ###    WING EXTENSION    ###
    datadf['maxWingAngle'] = get_absmax(datadf[['leftAngle', 'rightAngle']])
    datadf['maxWingLength'] = get_absmax(
        datadf[['leftWingLength', 'rightWingLength']])
    #datadf[datadf['maxWingAngle'] > 3.1] = np.nan
    """
    program = 'dark'
    
    plt.plot(datadf.Timestamp, datadf.Laser0_state, 'b')
    plt.plot(datadf.Timestamp, datadf.Laser1_state, 'k')
    plt.plot(datadf.Timestamp, datadf.Laser2_state, 'r')
    plt.show()
    
    if program == 'IRR':
        BEGINNING =datadf.Timestamp[datadf.synced_time >= -30000000000].index[0]#datadf.Timestamp.index[0]
        #FIRST_IR_ON = datadf.Timestamp[((datadf.Laser1_state > 0.001) & (datadf.synced_time >= -1))].index[0]
        FIRST_IR_ON = datadf.Timestamp[datadf.synced_time >= 0].index[0]
        #FIRST_IR_OFF = datadf.Timestamp[((datadf.Laser1_state > 0.001) & (datadf.synced_time <= 120))].index[-1]
        FIRST_IR_OFF = datadf.Timestamp[datadf.synced_time >= 60000000000].index[0]
        RED_ON = datadf.Timestamp[datadf.Laser2_state > 0.001].index[0]
        RED_OFF = datadf.Timestamp[datadf.Laser2_state > 0.001].index[-1]
        SECOND_IR_ON = datadf.Timestamp[datadf.synced_time >=320000000000].index[0]
        #SECOND_IR_ON = datadf.Timestamp[((datadf.Laser1_state > 0.001) & (datadf.synced_time >= 120))].index[0]
        SECOND_IR_OFF = datadf.Timestamp[datadf.Laser1_state > 0.001].index[-1]
        END = datadf.Timestamp.index[-1]
        
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, BEGINNING, FIRST_IR_ON, '1-prestim', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, FIRST_IR_ON, FIRST_IR_OFF, '2-IR1', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, FIRST_IR_OFF, RED_ON, '3-post-IR1', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, RED_ON,RED_OFF, '4-red', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE,RED_OFF, SECOND_IR_ON,'5-post-red', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE,SECOND_IR_ON,SECOND_IR_OFF,'6-IR2', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE,SECOND_IR_OFF,END,'7-post-IR2', background=False)
    
    
    
    if program == 'dark':
        BEGINNING =datadf.Timestamp[datadf.synced_time >= -30000000000].index[0]
        print set(datadf.Laser0_state), set(datadf.Laser1_state), set(datadf.Laser2_state)
        STIM_ON = datadf.Timestamp[datadf.Laser1_state > 0.001].index[0]
        STIM_OFF = datadf.Timestamp[datadf.Laser1_state > 0.001].index[-1]
        LIGHTS_OUT = datadf.Timestamp[datadf.Laser0_state < 0.001].index[0]
        LIGHTS_ON = datadf.Timestamp[datadf.Laser0_state < 0.001].index[-1]
        END = datadf.Timestamp.index[-1]
        
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, BEGINNING, STIM_ON, '1-prestim', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, STIM_ON,STIM_OFF, '2-stim', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, STIM_OFF, LIGHTS_OUT,'3-post-stim', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, LIGHTS_OUT, LIGHTS_ON,'4-DARK', background=False)
        targets.plot_trajectory_and_wingext(datadf, BAG_FILE, LIGHTS_ON, END,'7-light', background=False)
        
    """
    targets.plot_trajectory_and_wingext(datadf, BAG_FILE)

    ### ABDOMINAL BENDING   ###
    #datadf[datadf['Length'] > 110] = np.nan  #discard frames with bogus length.  *************
    #datadf[datadf['Length'] < 60] = np.nan  #discard frames with bogus length.

    trace = plot_single_trace(datadf)
    trace.savefig(DATADIR + 'TRACES/' + FLY_ID + '.png')
    plt.close('all')

    ###FIRST COURTSHIP BOUT AFTER STIMULUS###

    #courted, latency = latency_measures(datadf)

    datadf.to_pickle(FMF_DIR + '/frame_by_frame_synced.pickle')
    datadf.to_csv(FMF_DIR + '/frame_by_frame_synced.csv', sep=',')

    if 'binsize' in globals():
        datadf = bin_data(datadf, binsize)
        datadf.to_pickle(DATADIR + 'JAR/' + FLY_ID + '_' + binsize +
                         '_fly.pickle')
    else:
        return datadf, courted, latency
Ejemplo n.º 16
0
def sync_jaaba_with_ros(FMF_DIR):

    print "Processing: ", FMF_DIR

    DATADIR_CSV = FMF_DIR + '/registered_trx.csv'

    FLY_ID, FMF_TIME, GROUP = parse_fmftime(FMF_DIR)

    BAG_FILE = match_fmf_and_bag(FMF_TIME)

    WIDE_FMF = utilities.match_wide_to_zoom(FMF_DIR, DATADIR)

    FLY_IDx = FMF_DIR.split('/')[-1]
    EXP_ID, DATE, TIME = FLY_IDx.split('_', 4)[0:3]

    for x in glob.glob(FMF_DIR + '/*zoom*' + '*.fmf'):
        ZOOM_FMF = x

    if not os.path.exists(FMF_DIR + '/TRACKING_FRAMES') == True:
        os.makedirs(FMF_DIR + '/TRACKING_FRAMES')

    # COMPUTE AND ALIGN DISTANCE TO NEAREST TARGET
    if (os.path.exists(FMF_DIR +
                       '/frame_by_frame_synced.pickle')) and not RETRACK:
        datadf = pd.read_pickle(FMF_DIR + '/frame_by_frame_synced.pickle')
        exp_group = EXP_ID  #QUICKFIX. FIXME
    else:
        targets = target_detector.TargetDetector(WIDE_FMF, FMF_DIR)
        targets.plot_targets_on_background()
        targets.plot_trajectory_on_background(BAG_FILE)

        dtarget = targets.get_dist_to_nearest_target(BAG_FILE)['dtarget']
        (arena_centre), arena_radius = targets._arena.circ

        if MAKE_MOVIES:
            TRACKING_DIRECTORY = FMF_DIR + '/TRACKING_FRAMES/'
        else:
            TRACKING_DIRECTORY = None

        if (os.path.exists(FMF_DIR + '/wingdata.pickle')) and not RETRACK:
            datadf = pd.read_pickle(FMF_DIR + '/wingdata.pickle')
            datadf.columns = [
                'BodyAxis', 'leftAngle', 'leftWingLength', 'Length',
                'rightAngle', 'rightWingLength', 'target_angle_TTM',
                'target_distance_TTM', 'Timestamp', 'Width'
            ]
        else:
            try:
                wings = wing_detector.WingDetector(ZOOM_FMF, BAG_FILE, dtarget,
                                                   arena_centre, RETRACK,
                                                   TRACKING_DIRECTORY)

                wings.execute()
                wings.wingData.columns = [
                    'BodyAxis', 'leftAngle', 'leftWingLength', 'Length',
                    'rightAngle', 'rightWingLength', 'target_angle_TTM',
                    'target_distance_TTM', 'Timestamp', 'Width'
                ]
                wings.wingData.to_pickle(FMF_DIR + '/wingdata.pickle')
                wings.tracking_info.to_pickle(FMF_DIR +
                                              '/tracking_info.pickle')
                datadf = DataFrame(wings.wingData)

                if MAKE_MOVIES:
                    utilities.call_command(
                        "ffmpeg -f image2 -r 15 -i " + TRACKING_DIRECTORY +
                        "_tmp%05d.png -vf scale=iw/2:-1 -vcodec mpeg4 -b 8000k -y "
                        + FMF_DIR + "/tracked_movie.mp4;")
                    utilities.call_command("rm -r " + TRACKING_DIRECTORY)
                    #wings.make_movie(wings._tempdir, wings.fmf_file.rsplit('/',1)[0]+'/tracked_movie.mp4',15)

            except Exception, e:
                traceback.print_exc()
                print 'ERROR PROCESSING WING TRACKING...', FMF_DIR
                print str(e)

                return

        datadf['Frame_number'] = datadf.index
        datadf = convert_timestamps(datadf)

        # ALIGN LASER STATE DATA
        laser_states = utilities.get_laser_states(BAG_FILE)
        try:
            datadf['Laser0_state'] = laser_states['Laser0_state'].asof(
                datadf.index).fillna(value=1)
            datadf['Laser1_state'] = laser_states['Laser1_state'].asof(
                datadf.index).fillna(value=0)  #YAY!
            datadf['Laser2_state'] = laser_states['Laser2_state'].asof(
                datadf.index).fillna(value=0)
        except:
            print "\t ERROR: problem interpreting laser current values."
            datadf['Laser0_state'] = 0
            datadf['Laser2_state'] = 0
            datadf['Laser1_state'] = 0

        positions = utilities.get_positions_from_bag(BAG_FILE)
        positions = utilities.convert_timestamps(positions)
        datadf['fly_x'] = positions['fly_x'].asof(
            datadf.index).fillna(method='pad')
        datadf['fly_y'] = positions['fly_y'].asof(
            datadf.index).fillna(method='pad')

        datadf['dcentre'] = np.sqrt((
            (datadf['fly_x'] - arena_centre[0]) / 4.8)**2 + (
                (datadf['fly_y'] - arena_centre[1]) / 5.2)**2)
        datadf['dtarget_temp'] = targets.get_dist_to_nearest_target(
            BAG_FILE)['dtarget'].asof(datadf.index).fillna(method='pad')
        datadf.loc[datadf['dtarget_temp'] >= 6.0,
                   'target_distance_TTM'] = np.nan
        datadf['dtarget'] = datadf['target_distance_TTM'].fillna(
            datadf['dtarget_temp'])

        datadf[
            'Timestamp'] = datadf.index  #silly pandas bug for subtracting from datetimeindex...

        #number_of_bouts, bout_duration, first_TS, last_TS = utilities.detect_stim_bouts(datadf, 'Laser1_state')

        number_of_bouts, stim_duration, first_TS, last_TS = utilities.detect_stim_bouts(
            datadf, 'Laser2_state')  #HACK DANNO
        datadf['stim_duration'] = stim_duration

        try:
            if stim_duration >= 0:
                datadf['synced_time'] = datadf['Timestamp'] - datadf.Timestamp[
                    (datadf.Laser2_state +
                     datadf.Laser1_state) > 0.001].index[0]
            #elif stim_duration == 0:
            #    datadf['synced_time'] = datadf['Timestamp'] - datadf.Timestamp.index[0] - pd.to_datetime('60s')
        except Exception, e:
            traceback.print_exc()
            print "WARNING:   Cannot synchronize by stimulus. Setting T0 to frame 900. "
            datadf['synced_time'] = datadf[
                'Timestamp'] - datadf.Timestamp.index[900]
            print str(e)