def getBoutDur(mice, eventType, behType, trials):
    """
        DEPRICATED: Needs major revision!
    """
    print "DEPRICATED: Needs major revision!"
    # Load the data
    dataList = loadData(mice)

    # Load the events
    fileList = loopMice(mice, behType)
    eventList = getBeh(mice, fileList['Behaviour'], behType)

    durationData = pd.DataFrame()
    for mus, sess in mice:
        # Find the events
        boutDur = np.array([])
        startList = eventList[mus][eventType[0]].dropna().values
        endList = eventList[mus][eventType[1]].dropna().values
        for i in range(trials[0] - 1, trials[1]):
            ind, start = find_nearest(dataList[mus].index.values, startList[i])
            ind, end = find_nearest(dataList[mus].index.values, endList[i])
            boutDur = np.append(boutDur, end - start)

        durationData[mus] = boutDur

    durationData.set_index(np.arange(trials[0], trials[1] + 1), inplace=True)

    return durationData
def markTrials(mice,
               dataList,
               base,
               duration,
               eventType,
               behType,
               trials,
               baselining=False):
    """
    """
    # Load the events
    fileList = loopMice(mice, behType)
    eventList = getBeh(mice, fileList['Behaviour'], behType)

    eventsData = pd.DataFrame()
    for mus, sess in mice:

        # Find the events
        eventTimes = np.array([])
        df = eventList[mus][eventType].dropna().reset_index(drop=True)
        for i, event in enumerate(df):
            ind, nearest = find_nearest(dataList[mus].index.values, event)
            eventTimes = np.append(eventTimes, nearest)

        fs = dataList[mus].index[1] - dataList[mus].index[0]
        newTime = np.arange(base, 10000, fs)
        for col in dataList[mus].columns:
            if not col == 'Time (s)':
                data = dataList[mus][col]
                for i, event in enumerate(eventTimes[trials[0] - 1:trials[1]]):
                    slicedData = pd.DataFrame()

                    if baselining:
                        basedFF = data.loc[event + base + 0.0001:event +
                                           0.0001].mean()
                    elif not baselining:
                        basedFF = 0

                    dataSlice = data.loc[event + base + 0.0001:event +
                                         duration + 0.0001].values - basedFF
                    slicedData['Fluoro'] = dataSlice
                    slicedData['Cell'] = col
                    slicedData['Event'] = i + 1
                    slicedData['New_Time'] = newTime[0:len(dataSlice)]
                    #slicedData['New_Time'] = np.round(np.linspace(base,((len(slicedData['Fluoro'])-1)*fs)+base,len(slicedData['Fluoro'])), 2)
                    ##print np.linspace(base,((len(slicedData['Fluoro'])-1)*fs)+base,len(slicedData['Fluoro']))
                    ##print np.round(np.linspace(base,((len(slicedData['Fluoro'])-1)*fs)+base,len(slicedData['Fluoro'])), 2)
                    #np.arange(+base,duration,fs)

                    eventsData = eventsData.append(slicedData)

    print "\n", len(mice), " mice were loaded."
    if behType == 'FR1':
        for mus, sess in mice:
            print "Mouse number", mus, " had ", eventList[mus][
                'Right_Count'].max(), " total rewards."

    return eventsData
def markBouts(mice,
              dataList,
              eventType,
              behType,
              trials,
              base,
              dff=True,
              baseline=False):
    """
        DEPRICATED: Use getBouts instead.
    """
    print "DEPRICATED: Use getBouts instead."
    fileList = loopMice(mice, behType)
    eventList = getBeh(mice, fileList['Behaviour'], behType)

    eventsData = pd.DataFrame()
    for mus, sess in mice:

        # Find the events
        eventTimes1 = np.array([])
        eventTimes2 = np.array([])
        startTimes = eventList[mus][eventType[0]].dropna().reset_index(
            drop=True)
        endTimes = eventList[mus][eventType[1]].dropna().reset_index(drop=True)
        for i, event in enumerate(startTimes):
            ind1, nearest1 = find_nearest(dataList[mus].index.values,
                                          startTimes.loc[i])
            ind2, nearest2 = find_nearest(dataList[mus].index.values,
                                          endTimes.loc[i])
            eventTimes1 = np.append(eventTimes1, nearest1)
            eventTimes2 = np.append(eventTimes2, nearest2)

        fs = dataList[mus].index[1] - dataList[mus].index[0]
        for col in dataList[mus].columns:
            if not col == 'Time (s)':
                data = dataList[mus][col]
                for i, event in enumerate(eventTimes1[trials[0] -
                                                      1:trials[1]]):
                    start = eventTimes1[i]
                    end = eventTimes2[i]

                    slicedData = pd.DataFrame()

                    if baseline:
                        basedFF = data.loc[start + base:start + 0.0001].mean()
                    elif not baseline:
                        basedFF = 0.0

                    slicedData['Fluoro'] = data.loc[start + base:end +
                                                    0.0001].values - basedFF
                    slicedData['Cell'] = col
                    slicedData['Event'] = i + 1
                    if len(data.loc[start + base:end + 0.0001].values -
                           basedFF) == len(
                               np.arange(base, end - start + 0.0001, fs)):
                        slicedData['New_Time'] = np.arange(
                            base, end - start + 0.0001, fs)
                    if len(data.loc[start + base:end + 0.0001].values -
                           basedFF) < len(
                               np.arange(base, end - start + 0.0001, fs)):
                        slicedData['New_Time'] = np.arange(
                            base, end - start, fs)

                    eventsData = eventsData.append(slicedData)

    print "\n", len(mice), " mice were loaded."
    if behType == 'FR1':
        for mus, sess in mice:
            print "Mouse number", mus, " had ", eventList[mus][
                'Right_Count'].max(), " total rewards."

    if baseline:
        print "\nBaseline was set", baseline, "sec before the event."
    elif not baseline:
        print "\nTraces were not baselined."

    return eventsData
Exemplo n.º 4
0
    import matplotlib.patches as mpatches
    from imagingIO import loopMice
    from trials import markTrials
    from analysis import dffCalc, filtData, smoothData, normData

    # Start running the analysis
    mice = [(8404, 6), (8857, 3), (8864, 1)]
    behType = 'FR1'
    fs = 0.05
    base = -5.0
    duration = 30
    trials=[1,12]

    # Locate the files
    fileList = loopMice(mice, behType)
    # Load the events
    eventList = getBeh(mice, fileList['Behaviour'], behType)
    # Load the data
    dataList = loadData(mice, behType)
    # dFF and plot again
    filtList = filtData(mice, dffCalc(mice, dataList, lowest=False), cutoff=5.0, order=6)
    dFFList = smoothData(mice, filtList, window=4)

    eventType = 'Eat_Start'
    eventsData = markTrials(mice, dataList, base=base, duration=duration, eventType=eventType, behType=behType, trials=trials, baselining=True)

    sem=eventsData.pivot_table(index=['Event', 'Cell'], columns='New_Time').sem()
    time = np.arange(base,duration,fs)

    print len(sem), len(time)