Ejemplo n.º 1
0
    cellInfo = signRespCells[[
        'subject', 'date', 'depth', 'tetrode', 'cluster'
    ]]
    #print(cellInfo)
    numSignRespCells = len(signRespCells)

    print('Number of cells recorded from: {}'.format(len(celldb)))
    print('Number of responsive cells: {}'.format(len(responsivedb)))
    print(
        'Number of cells that showed a significant difference between expected and unexpected firing rates: {}'
        .format(numSignRespCells))
    percentSignRespCells = numSignRespCells / len(responsivedb) * 100
    print('Percentage of significantly responsive cells: {:.2f}%'.format(
        percentSignRespCells))

    return signRespCells


signRespCellsdb = comparing_odd_std_significance(responsivedb)
'''
# -- Saving the responsive database --
celldatabase.save_hdf(responsivedb, dbFilename)
print('Saved responsive database to {}'.format(dbFilename))

# -- Saving the database of significantly responsive cells. --
dbPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME)
dbFilename = os.path.join(dbPath,'signRespCellsdb_{}.h5'.format(studyparams.STUDY_NAME))
celldatabase.save_hdf(signRespCellsdb, dbFilename)
print('Saved significantly responsive cell database to {}'.format(dbFilename))
'''
Ejemplo n.º 2
0
            continue

    highBaseD = signRespCells['meanBaseHighD'][indRow]
    highEvokedD = signRespCells['meanEvokedHighD'][indRow]
    midBaseD = signRespCells['meanBaseMidD'][indRow]
    midEvokedD = signRespCells['meanEvokedMidD'][indRow]
    lowBaseD = signRespCells['meanBaseLowD'][indRow]
    lowEvokedD = signRespCells['meanEvokedLowD'][indRow]

    if signRespCells['mostRespFreqPValueOddStdD'][indRow] == signRespCells[
            'pValHighFRD'][indRow]:
        if highBaseD > highEvokedD:
            suppressedCells.append(signRespCells.iloc[indRow])
    elif signRespCells['mostRespFreqPValueOddStdD'][indRow] == signRespCells[
            'pValMidFRD'][indRow]:
        if midBaseD > midEvokedD:
            suppressedCells.append(signRespCells.iloc[indRow])
    else:
        if lowBaseD > lowEvokedD:
            suppressedCells.append(signRespCells.iloc[indRow])

suppresseddb = pd.DataFrame(suppressedCells)

# -- Saving the database of suppressed significantly responsive cells. --
dbPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME)
dbFilename = os.path.join(
    dbPath, 'suppressedRespCellsdb_{}.h5'.format(studyparams.STUDY_NAME))
celldatabase.save_hdf(suppresseddb, dbFilename)
print('Saved suppressed firing significantly responsive cell database to {}'.
      format(dbFilename))
Ejemplo n.º 3
0
            maxSpikesThisInten = 0

        meanSpikesAllInten[indInten] = meanSpikesThisInten
        maxSpikesAllInten[indInten] = maxSpikesThisInten
        baseSpikesAllInten[indInten] = meanBaselineSpikesThisInten

    baseline = np.mean(baseSpikesAllInten)
    monoIndex = (meanSpikesAllInten[-1] - baseline) / (np.max(meanSpikesAllInten)-baseline)

    dbToUse.loc[indRow, 'monotonicityIndex'] = monoIndex

    overallMaxSpikes = np.max(maxSpikesAllInten)
    maxSpikes.append(overallMaxSpikes)

if SAVE:
    celldatabase.save_hdf(database, dbPath)

if PLOT:

    def jitter(arr, frac):
        jitter = (np.random.random(len(arr))-0.5)*2*frac
        jitteredArr = arr + jitter
        return jitteredArr

    plt.clf()
    ax = plt.subplot(111)
    colorATh = 'b'
    colorAC = 'r'

    ac = dbToUse.groupby('brainArea').get_group('rightAC')
    thal = dbToUse.groupby('brainArea').get_group('rightThal')
Ejemplo n.º 4
0
        # TODO: Need to loop through d1mice as it is a list
        inforecFile = os.path.join(settings.INFOREC_PATH, '{}_inforec.py'.format(d1mice))
        clusteringObj = spikesorting.ClusterInforec(inforecFile)
        clusteringObj.process_all_experiments()
        pass

    # Generate_cell_database_filters cells with the followings: isi < 0.05, spike quality > 2
    basicDB = celldatabase.generate_cell_database_from_subjects(d1mice)
    firstDB = basicDB
    d1DBFilename = os.path.join(settings.FIGURES_DATA_PATH, '{}_d1mice.h5'.format(studyparams.STUDY_NAME))
    # Create and save a database, computing first the base stats and then the indices
    if calc_stats:
        firstDB = append_base_stats(basicDB, filename=d1DBFilename)
        # bestCells = calculate_indices(firstDB, filename = d1DBFilename)
        histDB = firstDB
        celldatabase.save_hdf(histDB, os.path.join(dbLocation, '{}.h5'.format('temp_rescue_db')))
    if calc_locations:
        histDB = histologyanalysis.cell_locations(firstDB, brainAreaDict=studyparams.BRAIN_AREA_DICT)

    if concat_mice:
        first_mouse, *list_of_mice = studyparams.ASTR_D1_CHR2_MICE
        histDB = merge_dataframes(list_of_mice, first_mouse)
        dbpath = os.path.join(dbLocation, '{}.h5'.format('direct_and_indirect_cells'))

    if SAVE:
        if os.path.isdir(dbLocation):
            celldatabase.save_hdf(histDB, dbpath)
            print("SAVED DATAFRAME to {}".format(dbpath))
        elif not os.path.isdir(dbLocation):
            answer = input_func("Save folder is not present. Would you like to make the desired directory now? (y/n) ")
            if answer.upper() in ['Y', 'YES']:
        nspkBase = spikesanalysis.spiketimes_to_spikecounts(
            spikeTimesFromEventOnset, indexLimitsEachTrial, baseRange)

        nspkResp = spikesanalysis.spiketimes_to_spikecounts(
            spikeTimesFromEventOnset, indexLimitsEachTrial, responseRange)

        avgResponse = nspkResp.mean(axis=0)
        onsetSpikes = avgResponse[0]
        sustainedSpikes = avgResponse[1]
        onsetRate = onsetSpikes / (responseRange[1] - responseRange[0])
        sustainedRate = sustainedSpikes / (responseRange[2] - responseRange[1])

        baseSpikes = nspkBase.mean()
        baseRate = baseSpikes / (baseRange[1] - baseRange[0])

        onsetBaseSubtracted = onsetRate - baseRate
        sustainedBaseSubtracted = sustainedRate - baseRate

        # baseSub = avgResponse - avgBase
        # normResponse = baseSub / baseSub[0]
        db.at[indRow, 'onsetRateCF'] = onsetRate
        db.at[indRow, 'sustainedRateCF'] = sustainedRate
        db.at[indRow, 'baselineRateCF'] = baseRate

db['cfOnsetivityIndex'] = (db['onsetRateCF'] - db['sustainedRateCF']) / (
    db['sustainedRateCF'] + db['onsetRateCF'])

if SAVE:
    celldatabase.save_hdf(db, pathtoDB)
Ejemplo n.º 6
0
def inactivation_indices(db, filename=''):
    bestCells = db.query("isiViolations<0.02")  # or modifiedISI<0.02")
    bestCells = bestCells.loc[bestCells['spikeShapeQuality'] > 2]
    bestCells = bestCells.query(
        'soundResponsePVal<0.05 or onsetSoundResponsePVal<0.05 or sustainedSoundResponsePVal<0.05'
    )
    bestCells = bestCells.loc[bestCells['tuningFitR2'] > 0.05]
    bestCells = bestCells.loc[bestCells['octavesFromPrefFreq'] < 0.5]

    for dbIndex, dbRow in bestCells.iterrows():

        cell = ephyscore.Cell(dbRow)

        bandEphysData, bandBehavData = cell.load_by_index(
            int(dbRow['bestBandSession']))
        bandEventOnsetTimes = funcs.get_sound_onset_times(
            bandEphysData, 'bandwidth')
        bandSpikeTimestamps = bandEphysData['spikeTimes']

        bandEachTrial = bandBehavData['currentBand']
        secondSort = bandBehavData['laserTrial']

        propOnset, propSustained = funcs.onset_sustained_spike_proportion(
            bandSpikeTimestamps, bandEventOnsetTimes)

        db.at[dbIndex, 'proportionSpikesOnset'] = propOnset
        db.at[dbIndex, 'proportionSpikesSustained'] = propSustained

        #by default: not subtracting baseline, but are replacing pure tone response with baseline for 0 bw condition
        onsetSupInds, onsetSupIndpVals, onsetFacInds, onsetFacIndpVals, onsetPeakInds, onsetSpikeArray = funcs.bandwidth_suppression_from_peak(
            bandSpikeTimestamps,
            bandEventOnsetTimes,
            bandEachTrial,
            secondSort,
            timeRange=[0.0, 0.05],
            baseRange=[-0.05, 0.0])
        db.at[dbIndex, 'onsetSuppressionIndexLaser'] = onsetSupInds[-1]
        db.at[dbIndex, 'onsetSuppressionpValLaser'] = onsetSupIndpVals[-1]
        db.at[dbIndex, 'onsetFacilitationIndexLaser'] = onsetFacInds[-1]
        db.at[dbIndex, 'onsetFacilitationpValLaser'] = onsetFacIndpVals[-1]
        db.at[dbIndex,
              'onsetPrefBandwidthLaser'] = bandEachTrial[onsetPeakInds[-1]]

        db.at[dbIndex, 'onsetSuppressionIndexNoLaser'] = onsetSupInds[0]
        db.at[dbIndex, 'onsetSuppressionpValNoLaser'] = onsetSupIndpVals[0]
        db.at[dbIndex, 'onsetFacilitationIndexNoLaser'] = onsetFacInds[0]
        db.at[dbIndex, 'onsetFacilitationpValNoLaser'] = onsetFacIndpVals[0]
        db.at[dbIndex,
              'onsetPrefBandwidthNoLaser'] = bandEachTrial[onsetPeakInds[0]]

        #base range is right before sound onset so we get estimate for laser baseline
        sustainedSupInds, sustainedSupIndpVals, sustainedFacInds, sustainedFacIndpVals, sustainedPeakInds, sustainedSpikeArray = funcs.bandwidth_suppression_from_peak(
            bandSpikeTimestamps,
            bandEventOnsetTimes,
            bandEachTrial,
            secondSort,
            timeRange=[0.2, 1.0],
            baseRange=[-0.05, 0.0])
        db.at[dbIndex, 'sustainedSuppressionIndexLaser'] = sustainedSupInds[-1]
        db.at[dbIndex,
              'sustainedSuppressionpValLaser'] = sustainedSupIndpVals[-1]
        db.at[dbIndex,
              'sustainedFacilitationIndexLaser'] = sustainedFacInds[-1]
        db.at[dbIndex,
              'sustainedFacilitationpValLaser'] = sustainedFacIndpVals[-1]
        db.at[dbIndex, 'sustainedPrefBandwidthLaser'] = bandEachTrial[
            sustainedPeakInds[-1]]

        db.at[dbIndex,
              'sustainedSuppressionIndexNoLaser'] = sustainedSupInds[0]
        db.at[dbIndex,
              'sustainedSuppressionpValNoLaser'] = sustainedSupIndpVals[0]
        db.at[dbIndex,
              'sustainedFacilitationIndexNoLaser'] = sustainedFacInds[0]
        db.at[dbIndex,
              'sustainedFacilitationpValNoLaser'] = sustainedFacIndpVals[0]
        db.at[dbIndex, 'sustainedPrefBandwidthNoLaser'] = bandEachTrial[
            sustainedPeakInds[0]]

        #no laser fit
        sustainedResponseNoLaser = sustainedSpikeArray[:, 0]

        bandsForFit = np.unique(bandEachTrial)
        bandsForFit[-1] = 6
        mFixed = 1

        fitParams, R2 = fitfuncs.diff_of_gauss_fit(bandsForFit,
                                                   sustainedResponseNoLaser,
                                                   mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0noLaser'] = fitParams[0]
        db.at[dbIndex, 'RDnoLaser'] = fitParams[3]
        db.at[dbIndex, 'RSnoLaser'] = fitParams[4]
        db.at[dbIndex, 'mnoLaser'] = mFixed
        db.at[dbIndex, 'sigmaDnoLaser'] = fitParams[1]
        db.at[dbIndex, 'sigmaSnoLaser'] = fitParams[2]
        db.at[dbIndex, 'bandwidthTuningR2noLaser'] = R2

        testBands = np.linspace(bandsForFit[0], bandsForFit[-1], 500)
        allFitParams = [mFixed]
        allFitParams.extend(fitParams)
        suppInd, prefBW = fitfuncs.extract_stats_from_fit(
            allFitParams, testBands)

        db.at[dbIndex, 'fitSustainedSuppressionIndexNoLaser'] = suppInd
        db.at[dbIndex, 'fitSustainedPrefBandwidthNoLaser'] = prefBW

        #laser fit
        sustainedResponseLaser = sustainedSpikeArray[:, 1]

        fitParamsLaser, R2Laser = fitfuncs.diff_of_gauss_fit(
            bandsForFit, sustainedResponseLaser, mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0laser'] = fitParamsLaser[0]
        db.at[dbIndex, 'RDlaser'] = fitParamsLaser[3]
        db.at[dbIndex, 'RSlaser'] = fitParamsLaser[4]
        db.at[dbIndex, 'mlaser'] = mFixed
        db.at[dbIndex, 'sigmaDlaser'] = fitParamsLaser[1]
        db.at[dbIndex, 'sigmaSlaser'] = fitParamsLaser[2]
        db.at[dbIndex, 'bandwidthTuningR2laser'] = R2Laser

        allFitParamsLaser = [mFixed]
        allFitParamsLaser.extend(fitParamsLaser)
        suppIndLaser, prefBWLaser = fitfuncs.extract_stats_from_fit(
            allFitParamsLaser, testBands)

        db.at[dbIndex, 'fitSustainedSuppressionIndexLaser'] = suppIndLaser
        db.at[dbIndex, 'fitSustainedPrefBandwidthLaser'] = prefBWLaser

        meanLaserDiff = np.mean(sustainedResponseLaser -
                                sustainedResponseNoLaser)
        db.at[dbIndex, 'laserChangeResponse'] = meanLaserDiff

        laserDiff = sustainedResponseLaser - sustainedResponseNoLaser
        peakInd = np.argmax(sustainedResponseNoLaser)

        db.at[dbIndex, 'peakChangeFR'] = laserDiff[peakInd]
        db.at[dbIndex, 'WNChangeFR'] = laserDiff[-1]

        testRespsNoLaser = fitfuncs.diff_gauss_form(testBands, *allFitParams)
        testRespsLaser = fitfuncs.diff_gauss_form(testBands,
                                                  *allFitParamsLaser)

        laserDiffModel = testRespsLaser - testRespsNoLaser
        peakIndModel = np.argmax(testRespsNoLaser)

        db.at[dbIndex, 'fitPeakChangeFR'] = laserDiffModel[peakIndModel]
        db.at[dbIndex, 'fitWNChangeFR'] = laserDiffModel[-1]

        #also calculating fits and suppression with pure tone being 0 bw condition
        toneSustainedSupInds, toneSustainedSupIndpVals, toneSustainedFacInds, toneSustainedFacIndpVals, toneSustainedPeakInds, toneSustainedSpikeArray = funcs.bandwidth_suppression_from_peak(
            bandSpikeTimestamps,
            bandEventOnsetTimes,
            bandEachTrial,
            secondSort,
            timeRange=[0.2, 1.0],
            baseRange=[-0.05, 0.0],
            zeroBWBaseline=False)
        db.at[
            dbIndex,
            'sustainedSuppressionIndexNoLaserPureTone'] = toneSustainedSupInds[
                0]
        db.at[
            dbIndex,
            'sustainedSuppressionpValNoLaserPureTone'] = toneSustainedSupIndpVals[
                0]
        db.at[
            dbIndex,
            'sustainedFacilitationIndexNoLaserPureTone'] = toneSustainedFacInds[
                0]
        db.at[
            dbIndex,
            'sustainedFacilitationpValNoLaserPureTone'] = toneSustainedFacIndpVals[
                0]
        db.at[dbIndex,
              'sustainedPrefBandwidthNoLaserPureTone'] = bandEachTrial[
                  toneSustainedPeakInds[0]]

        db.at[dbIndex,
              'sustainedSuppressionIndexLaserPureTone'] = toneSustainedSupInds[
                  -1]
        db.at[
            dbIndex,
            'sustainedSuppressionpValLaserPureTone'] = toneSustainedSupIndpVals[
                -1]
        db.at[
            dbIndex,
            'sustainedFacilitationIndexLaserPureTone'] = toneSustainedFacInds[
                -1]
        db.at[
            dbIndex,
            'sustainedFacilitationpValLaserPureTone'] = toneSustainedFacIndpVals[
                -1]
        db.at[dbIndex, 'sustainedPrefBandwidthLaserPureTone'] = bandEachTrial[
            toneSustainedPeakInds[-1]]

        toneSustainedResponseNoLaser = toneSustainedSpikeArray[:, 0]

        toneFitParamsNoLaser, toneR2 = fitfuncs.diff_of_gauss_fit(
            bandsForFit, toneSustainedResponseNoLaser, mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0PureToneNoLaser'] = toneFitParamsNoLaser[0]
        db.at[dbIndex, 'RDPureToneNoLaser'] = toneFitParamsNoLaser[3]
        db.at[dbIndex, 'RSPureToneNoLaser'] = toneFitParamsNoLaser[4]
        db.at[dbIndex, 'mPureToneNoLaser'] = mFixed
        db.at[dbIndex, 'sigmaDPureToneNoLaser'] = toneFitParamsNoLaser[1]
        db.at[dbIndex, 'sigmaSPureToneNoLaser'] = toneFitParamsNoLaser[2]
        db.at[dbIndex, 'bandwidthTuningR2PureToneNoLaser'] = toneR2

        allFitParamsToneNoLaser = [mFixed]
        allFitParamsToneNoLaser.extend(toneFitParamsNoLaser)
        suppIndTone, prefBWTone = fitfuncs.extract_stats_from_fit(
            allFitParamsToneNoLaser, testBands)

        db.at[dbIndex,
              'fitSustainedSuppressionIndexPureToneNoLaser'] = suppIndTone
        db.at[dbIndex, 'fitSustainedPrefBandwidthPureToneNoLaser'] = prefBWTone

        toneSustainedResponseLaser = toneSustainedSpikeArray[:, 1]

        toneFitParamsLaser, toneR2Laser = fitfuncs.diff_of_gauss_fit(
            bandsForFit, toneSustainedResponseLaser, mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0PureToneLaser'] = toneFitParamsLaser[0]
        db.at[dbIndex, 'RDPureToneLaser'] = toneFitParamsLaser[3]
        db.at[dbIndex, 'RSPureToneLaser'] = toneFitParamsLaser[4]
        db.at[dbIndex, 'mPureToneLaser'] = mFixed
        db.at[dbIndex, 'sigmaDPureToneLaser'] = toneFitParamsLaser[1]
        db.at[dbIndex, 'sigmaSPureToneLaser'] = toneFitParamsLaser[2]
        db.at[dbIndex, 'bandwidthTuningR2PureToneLaser'] = toneR2Laser

        allFitParamsToneLaser = [mFixed]
        allFitParamsToneLaser.extend(toneFitParamsLaser)
        suppIndToneLaser, prefBWToneLaser = fitfuncs.extract_stats_from_fit(
            allFitParamsToneLaser, testBands)

        db.at[dbIndex,
              'fitSustainedSuppressionIndexPureToneLaser'] = suppIndToneLaser
        db.at[dbIndex,
              'fitSustainedPrefBandwidthPureToneLaser'] = prefBWToneLaser

        testRespsNoLaser = fitfuncs.diff_gauss_form(testBands,
                                                    *allFitParamsToneNoLaser)
        testRespsLaser = fitfuncs.diff_gauss_form(testBands,
                                                  *allFitParamsToneLaser)

        laserDiffModel = testRespsLaser - testRespsNoLaser
        peakIndModel = np.argmax(testRespsNoLaser)

        db.at[dbIndex,
              'fitPeakChangeFRPureTone'] = laserDiffModel[peakIndModel]
        db.at[dbIndex, 'fitWNChangeFRPureTone'] = laserDiffModel[-1]

        #also calculating fits and suppression with nothing being fit for bw 0
        noZeroSustainedResponseNoLaser = sustainedSpikeArray[1:, 0]
        bandsForFitNoZero = bandsForFit[1:]

        noZeroFitParamsNoLaser, noZeroR2 = fitfuncs.diff_of_gauss_fit(
            bandsForFitNoZero, noZeroSustainedResponseNoLaser, mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0noZeroNoLaser'] = noZeroFitParamsNoLaser[0]
        db.at[dbIndex, 'RDnoZeroNoLaser'] = noZeroFitParamsNoLaser[3]
        db.at[dbIndex, 'RSnoZeroNoLaser'] = noZeroFitParamsNoLaser[4]
        db.at[dbIndex, 'mnoZeroNoLaser'] = mFixed
        db.at[dbIndex, 'sigmaDnoZeroNoLaser'] = noZeroFitParamsNoLaser[1]
        db.at[dbIndex, 'sigmaSnoZeroNoLaser'] = noZeroFitParamsNoLaser[2]
        db.at[dbIndex, 'bandwidthTuningR2noZeroNoLaser'] = noZeroR2

        allFitParamsNoZero = [mFixed]
        allFitParamsNoZero.extend(noZeroFitParamsNoLaser)
        testBandsNoZero = np.linspace(bandsForFitNoZero[0],
                                      bandsForFitNoZero[-1], 500)
        suppIndNoZero, prefBWNoZero = fitfuncs.extract_stats_from_fit(
            allFitParamsNoZero, testBandsNoZero)

        db.at[dbIndex,
              'fitSustainedSuppressionIndexNoZeroNoLaser'] = suppIndNoZero
        db.at[dbIndex, 'fitSustainedPrefBandwidthNoZeroNoLaser'] = prefBWNoZero

        noZeroSustainedResponseLaser = sustainedSpikeArray[1:, 1]
        bandsForFitNoZero = bandsForFit[1:]

        noZeroFitParamsLaser, noZeroR2Laser = fitfuncs.diff_of_gauss_fit(
            bandsForFitNoZero, noZeroSustainedResponseLaser, mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0noZeroLaser'] = noZeroFitParamsLaser[0]
        db.at[dbIndex, 'RDnoZeroLaser'] = noZeroFitParamsLaser[3]
        db.at[dbIndex, 'RSnoZeroLaser'] = noZeroFitParamsLaser[4]
        db.at[dbIndex, 'mnoZeroLaser'] = mFixed
        db.at[dbIndex, 'sigmaDnoZeroLaser'] = noZeroFitParamsLaser[1]
        db.at[dbIndex, 'sigmaSnoZeroLaser'] = noZeroFitParamsLaser[2]
        db.at[dbIndex, 'bandwidthTuningR2noZeroLaser'] = noZeroR2Laser

        allFitParamsNoZeroLaser = [mFixed]
        allFitParamsNoZeroLaser.extend(noZeroFitParamsLaser)
        suppIndNoZeroLaser, prefBWNoZeroLaser = fitfuncs.extract_stats_from_fit(
            allFitParamsNoZeroLaser, testBandsNoZero)

        db.at[dbIndex,
              'fitSustainedSuppressionIndexNoZeroLaser'] = suppIndNoZeroLaser
        db.at[dbIndex,
              'fitSustainedPrefBandwidthNoZeroLaser'] = prefBWNoZeroLaser

        testRespsNoLaser = fitfuncs.diff_gauss_form(testBandsNoZero,
                                                    *allFitParamsNoZero)
        testRespsLaser = fitfuncs.diff_gauss_form(testBandsNoZero,
                                                  *allFitParamsNoZeroLaser)

        laserDiffModel = testRespsLaser - testRespsNoLaser
        peakIndModel = np.argmax(testRespsNoLaser)

        db.at[dbIndex, 'fitPeakChangeFRNoZero'] = laserDiffModel[peakIndModel]
        db.at[dbIndex, 'fitWNChangeFRNoZero'] = laserDiffModel[-1]

    if len(filename) != 0:
        celldatabase.save_hdf(db, filename)
        print filename + " saved"

    return db
Ejemplo n.º 7
0

def calculate_cell_locations(db):
    pass


if __name__ == "__main__":

    # -- Spike sort the data (code is left here for reference) --
    '''
    subject = 'testXXX'
    inforecFile = os.path.join(settings.INFOREC_PATH,'{}_inforec.py'.format(subject))
    clusteringObj = spikesorting.ClusterInforec(inforecFile)
    clusteringObj.process_all_experiments()
    '''
    
    # -- Generate cell database (this function excludes clusters with isi>0.05, spikeQuality<2 --
    celldb = celldatabase.generate_cell_database_from_subjects(studyparams.MICE_LIST)

    # -- Compute the base stats and indices for each cell --
    celldb = calculate_base_stats(celldb)  # Calculated for all cells
    celldb = calculate_indices(celldb)     # Calculated for a selected subset of cells

    dbPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME)
    dbFilename = os.path.join(dbPath,'celldb_{}.h5'.format(studyparams.STUDY_NAME))
    if os.path.isdir(dbPath):
        celldatabase.save_hdf(celldb, dbFilename)
        print('Saved database to {}'.format(dbFilename))
    else:
        print('{} does not exist. Please create this folder.'.format(dbPath))
Ejemplo n.º 8
0
    basicDB = celldatabase.generate_cell_database_from_subjects(d1mice)

    d1DBFilename = os.path.join(settings.FIGURES_DATA_PATH,
                                '{}_d1mice.h5'.format(studyparams.STUDY_NAME))
    # Create and save a database, computing first the base stats and then the indices
    firstDB = calculate_base_stats(basicDB, filename=d1DBFilename)
    # bestCells = calculate_indices(firstDB, filename = d1DBFilename)

    if SAVE:
        dbpath = os.path.join(settings.FIGURES_DATA_PATH,
                              studyparams.STUDY_NAME,
                              '{}.h5'.format('_'.join(d1mice)))
        # dbpath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME, '{}.h5'.format('temp'))
        if os.path.isdir(
                os.path.join(settings.FIGURES_DATA_PATH,
                             studyparams.STUDY_NAME)):
            celldatabase.save_hdf(firstDB, dbpath)
            print("SAVED DATAFRAME to {}".format(dbpath))
        elif not os.path.isdir(
                os.path.join(settings.FIGURES_DATA_PATH,
                             studyparams.STUDY_NAME)):
            answer = input_func(
                "Save folder is not present. Would you like to make the desired directory now? (y/n) "
            )
            if answer in ['y', 'Y', 'Yes', 'YES']:
                os.mkdir(
                    os.path.join(settings.FIGURES_DATA_PATH,
                                 studyparams.STUDY_NAME))
                celldatabase.save_hdf(firstDB, dbpath)
                print("SAVED DATAFRAME to {}".format(dbpath))
                pcovs = []
                ind10AboveButNone = []
                # ------------ start of frequency specific calculations -------------
                for indFreq, freq in enumerate(uniqFreq):
                    selectinds = np.flatnonzero((currentFreq == freq) & (
                        currentIntensity == intensity)).tolist()

                    nspkBase, nspkResp = funcs.calculate_firing_rate(
                        ttEventOnsetTimes,
                        ttSpikeTimes,
                        baseRange,
                        selectinds=selectinds)

                    spks = np.concatenate([spks, nspkResp.ravel()])
                    freqs = np.concatenate(
                        [freqs, np.ones(len(nspkResp.ravel())) * freq])
                    respSpikeMean[indInten, indFreq] = np.mean(nspkResp)
                    allIntenBase = np.concatenate(
                        [allIntenBase, nspkBase.ravel()])
                    Rsquared, popt = funcs.calculate_fit(
                        uniqFreq, allIntenBase, freqs, spks)

                    Rsquareds[indInten, indFreq] = Rsquared

            db.at[indRow, 'tuningTest_pVal'] = ttPVal
            db.at[indRow, 'tuningTest_zStat'] = ttZStat
        db.at[indRow, 'ttR2Fit'] = Rsquareds[-1].mean(
        )  # Using the highest (only) intensity

celldatabase.save_hdf(db, '/var/tmp/figuresdata/2019astrpi/ttDBR2.h5')
            onsetStats = bandwidth_suppression_from_peak(onsetTuningDict)
            db.at[dbIndex, 'onsetSuppressionIndexLaser'] = onsetStats['suppressionIndex'][-1]
            db.at[dbIndex, 'onsetSuppressionpValLaser'] = onsetStats['suppressionpVal'][-1]
            db.at[dbIndex, 'onsetFacilitationIndexLaser'] = onsetStats['facilitationIndex'][-1]
            db.at[dbIndex, 'onsetFacilitationpValLaser'] = onsetStats['facilitationpVal'][-1]
             
            db.at[dbIndex, 'onsetSuppressionIndexNoLaser'] = onsetStats['suppressionIndex'][0]
            db.at[dbIndex, 'onsetSuppressionpValNoLaser'] = onsetStats['suppressionpVal'][0]
            db.at[dbIndex, 'onsetFacilitationIndexNoLaser'] = onsetStats['facilitationIndex'][0]
            db.at[dbIndex, 'onsetFacilitationpValNoLaser'] = onsetStats['facilitationpVal'][0]
             
            sustainedStats = bandwidth_suppression_from_peak(sustainedTuningDict)
            db.at[dbIndex, 'sustainedSuppressionIndexLaser'] = sustainedStats['suppressionIndex'][-1]
            db.at[dbIndex, 'sustainedSuppressionpValLaser'] = sustainedStats['suppressionpVal'][-1]
            db.at[dbIndex, 'sustainedFacilitationIndexLaser'] = sustainedStats['facilitationIndex'][-1]
            db.at[dbIndex, 'sustainedFacilitationpValLaser'] = sustainedStats['facilitationpVal'][-1]
             
            db.at[dbIndex, 'sustainedSuppressionIndexNoLaser'] = sustainedStats['suppressionIndex'][0]
            db.at[dbIndex, 'sustainedSuppressionpValNoLaser'] = sustainedStats['suppressionpVal'][0]
            db.at[dbIndex, 'sustainedFacilitationIndexNoLaser'] = sustainedStats['facilitationIndex'][0]
            db.at[dbIndex, 'sustainedFacilitationpValNoLaser'] = sustainedStats['facilitationpVal'][0]
    
    if subType == 'chr2':
        outputName = 'photoidentification_cells2.h5'
    elif subType == 'archt':
        outputName = 'inactivation_cells2.h5'
    dbFilename = os.path.join(settings.DATABASE_PATH,outputName)
    celldatabase.save_hdf(db, dbFilename)
    print('Saved database to: {}'.format(dbFilename))

                                       maxPossibleClusters=12,
                                       recluster=False)

        if FIND_TETRODES_WITH_NO_SPIKES:
            ci = spikesorting.ClusterInforec(inforecFn)
            ci.find_tetrodes_with_no_spikes()
            continue

        fullDb = celldatabase.generate_cell_database(inforecFn)

        fullDbFullPath = os.path.join(
            dbFolder, '{}_database_all_clusters.h5'.format(animal))
        if SAVE_FULL_DB:
            print 'Saving database to {}'.format(fullDbFullPath)
            #fullDb.to_hdf(fullDbFullPath, key=dbKey)
            celldatabase.save_hdf(fullDb, fullDbFullPath)

if CASE == 2:
    # -- check behavior criteria, cell depth is inside target region, consistent firing during behavior(2afc) session, generate a good quality cell db, save only subset of good qual cells on disk -- #
    for animal in animals:
        fullDbFullPath = os.path.join(
            dbFolder, '{}_database_all_clusters.h5'.format(animal))
        #fullDb = pd.read_hdf(fullDbFullPath, key=dbKey)
        fullDb = celldatabase.load_hdf(fullDbFullPath)

        # -- check if cell meets behavior criteria -- #
        print 'Checking behavior criteria'
        metBehavCriteria = behavCriteria.ensure_behav_criteria_celldb(
            fullDb,
            strict=useStrictBehavCriterionWhenSaving,
            sessiontype='behavior',
Ejemplo n.º 12
0
def inactivation_database(db,
                          baseStats=False,
                          computeIndices=True,
                          filename='inactivation_cells.h5'):
    if type(db) == str:
        dbPath = os.path.join(settings.DATABASE_PATH, db)
        db = celldatabase.load_hdf(dbPath)

    if baseStats:
        soundResponseTestStatistic = np.empty(len(db))
        soundResponsePVal = np.empty(len(db))
        onsetSoundResponseTestStatistic = np.empty(len(db))
        onsetSoundResponsePVal = np.empty(len(db))
        sustainedSoundResponseTestStatistic = np.empty(len(db))
        sustainedSoundResponsePVal = np.empty(len(db))

        gaussFit = []
        tuningTimeRange = []
        Rsquared = np.empty(len(db))
        prefFreq = np.empty(len(db))
        octavesFromPrefFreq = np.empty(len(db))
        bestBandSession = np.empty(len(db))

        for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
            cellObj = ephyscore.Cell(dbRow)
            print "Now processing", dbRow['subject'], dbRow['date'], dbRow[
                'depth'], dbRow['tetrode'], dbRow['cluster']

            # --- Determine sound responsiveness during bandwidth sessions and calculate baseline firing rates with and without laser---
            #done in a kind of stupid way because regular and control sessions are handled the same way
            if any(session in dbRow['sessionType']
                   for session in ['laserBandwidth', 'laserBandwidthControl']):
                if 'laserBandwidth' in dbRow['sessionType']:
                    bandEphysData, bandBehavData = cellObj.load(
                        'laserBandwidth')
                    behavSession = 'laserBandwidth'
                    db.at[dbIndex, 'controlSession'] = 0
                elif 'laserBandwidthControl' in dbRow['sessionType']:
                    bandEphysData, bandBehavData = cellObj.load(
                        'laserBandwidthControl')
                    behavSession = 'laserBandwidthControl'
                    db.at[dbIndex, 'controlSession'] = 1
                bandEventOnsetTimes = funcs.get_sound_onset_times(
                    bandEphysData, 'bandwidth')
                bandSpikeTimestamps = bandEphysData['spikeTimes']
                bandEachTrial = bandBehavData['currentBand']
                secondSort = bandBehavData['laserTrial']
                numBands = np.unique(bandEachTrial)
                numSec = np.unique(secondSort)

                trialsEachComb = behavioranalysis.find_trials_each_combination(
                    bandEachTrial, numBands, secondSort, numSec)
                trialsEachBaseCond = trialsEachComb[:, :,
                                                    0]  #using no laser trials to determine sound responsiveness
                testStatistic, pVal = funcs.sound_response_any_stimulus(
                    bandEventOnsetTimes, bandSpikeTimestamps,
                    trialsEachBaseCond, [0.0, 1.0], [-1.2, -0.2])
                onsetTestStatistic, onsetpVal = funcs.sound_response_any_stimulus(
                    bandEventOnsetTimes, bandSpikeTimestamps,
                    trialsEachBaseCond, [0.0, 0.05], [-0.25, 0.2])
                sustainedTestStatistic, sustainedpVal = funcs.sound_response_any_stimulus(
                    bandEventOnsetTimes, bandSpikeTimestamps,
                    trialsEachBaseCond, [0.2, 1.0], [-1.0, 0.2])
                pVal *= len(numBands)  #correction for multiple comparisons
                onsetpVal *= len(numBands)
                sustainedpVal *= len(numBands)
                #pdb.set_trace()

                #find baselines with and without laser
                baselineRange = [-0.05, 0.0]
                baselineRates, baselineSEMs = funcs.inactivated_cells_baselines(
                    bandSpikeTimestamps, bandEventOnsetTimes, secondSort,
                    baselineRange)
                db.at[dbIndex, 'baselineFRnoLaser'] = baselineRates[0]
                db.at[dbIndex, 'baselineFRLaser'] = baselineRates[1]
                db.at[dbIndex, 'baselineFRnoLaserSEM'] = baselineSEMs[0]
                db.at[dbIndex, 'baselineFRLaserSEM'] = baselineSEMs[1]
                db.at[dbIndex,
                      'baselineChangeFR'] = baselineRates[1] - baselineRates[0]
            else:
                print "No bandwidth session for this cell"
                testStatistic = np.nan
                pVal = np.nan
                onsetTestStatistic = np.nan
                onsetpVal = np.nan
                sustainedTestStatistic = np.nan
                sustainedpVal = np.nan
                #pdb.set_trace()

            soundResponseTestStatistic[indRow] = testStatistic
            soundResponsePVal[indRow] = pVal
            onsetSoundResponseTestStatistic[indRow] = onsetTestStatistic
            onsetSoundResponsePVal[indRow] = onsetpVal
            sustainedSoundResponseTestStatistic[
                indRow] = sustainedTestStatistic
            sustainedSoundResponsePVal[indRow] = sustainedpVal

            # --- Determine frequency tuning of cells ---
            try:
                tuningEphysData, tuningBehavData = cellObj.load('tuningCurve')
            except IndexError:
                print "No tuning session for this cell"
                freqFit = np.full(4, np.nan)
                thisRsquared = np.nan
                bestFreq = np.nan
                tuningWindow = np.full(2, np.nan)
                octavesFromBest = np.nan
                bandIndex = np.nan
            else:
                tuningEventOnsetTimes = funcs.get_sound_onset_times(
                    tuningEphysData, 'tuningCurve')
                tuningSpikeTimestamps = tuningEphysData['spikeTimes']
                freqEachTrial = tuningBehavData['currentFreq']
                intensityEachTrial = tuningBehavData['currentIntensity']
                numFreqs = np.unique(freqEachTrial)
                numIntensities = np.unique(intensityEachTrial)
                timeRange = [-0.2, 0.2]
                spikeTimesFromEventOnset, trialIndexForEachSpike, indexLimitsEachTrial = spikesanalysis.eventlocked_spiketimes(
                    tuningSpikeTimestamps, tuningEventOnsetTimes, timeRange)
                trialsEachType = behavioranalysis.find_trials_each_type(
                    intensityEachTrial, numIntensities)
                trialsHighInt = trialsEachType[:, -1]
                trialsEachComb = behavioranalysis.find_trials_each_combination(
                    freqEachTrial, numFreqs, intensityEachTrial,
                    numIntensities)
                trialsEachFreqHighInt = trialsEachComb[:, :, -1]
                tuningWindow = funcs.best_window_freq_tuning(
                    spikeTimesFromEventOnset, indexLimitsEachTrial,
                    trialsEachFreqHighInt)
                tuningWindow = np.array(tuningWindow)
                spikeCountMat = spikesanalysis.spiketimes_to_spikecounts(
                    spikeTimesFromEventOnset, indexLimitsEachTrial,
                    tuningWindow)
                tuningSpikeRates = (spikeCountMat[trialsHighInt].flatten()) / (
                    tuningWindow[1] - tuningWindow[0])
                freqsThisIntensity = freqEachTrial[trialsHighInt]
                freqFit, thisRsquared = funcs.gaussian_tuning_fit(
                    np.log2(freqsThisIntensity), tuningSpikeRates)
                if freqFit is not None:
                    bestFreq = 2**freqFit[0]
                    bandIndex, octavesFromBest = funcs.best_index(
                        cellObj, bestFreq, behavSession)
                else:
                    freqFit = np.full(4, np.nan)
                    bestFreq = np.nan
                    bandIndex = np.nan
                    octavesFromBest = np.nan
            gaussFit.append(freqFit)
            tuningTimeRange.append(tuningWindow)
            Rsquared[indRow] = thisRsquared
            prefFreq[indRow] = bestFreq
            octavesFromPrefFreq[indRow] = octavesFromBest
            bestBandSession[indRow] = bandIndex

        db['soundResponseUStat'] = soundResponseTestStatistic
        db['soundResponsePVal'] = soundResponsePVal
        db['onsetSoundResponseUStat'] = onsetSoundResponseTestStatistic
        db['onsetSoundResponsePVal'] = onsetSoundResponsePVal
        db['sustainedSoundResponseUStat'] = sustainedSoundResponseTestStatistic
        db['sustainedSoundResponsePVal'] = sustainedSoundResponsePVal

        db['gaussFit'] = gaussFit
        db['tuningTimeRange'] = tuningTimeRange
        db['tuningFitR2'] = Rsquared
        db['prefFreq'] = prefFreq
        db['octavesFromPrefFreq'] = octavesFromPrefFreq
        db['bestBandSession'] = bestBandSession

    if computeIndices:
        bestCells = db.query("isiViolations<0.02")  # or modifiedISI<0.02")
        bestCells = bestCells.loc[bestCells['spikeShapeQuality'] > 2]
        bestCells = bestCells.query(
            'soundResponsePVal<0.05 or onsetSoundResponsePVal<0.05 or sustainedSoundResponsePVal<0.05'
        )
        bestCells = bestCells.loc[bestCells['tuningFitR2'] > R2CUTOFF]
        bestCells = bestCells.loc[
            bestCells['octavesFromPrefFreq'] < OCTAVESCUTOFF]

        for dbIndex, dbRow in bestCells.iterrows():

            cell = ephyscore.Cell(dbRow)

            bandEphysData, bandBehavData = cell.load_by_index(
                int(dbRow['bestBandSession']))
            bandEventOnsetTimes = funcs.get_sound_onset_times(
                bandEphysData, 'bandwidth')
            bandSpikeTimestamps = bandEphysData['spikeTimes']

            bandEachTrial = bandBehavData['currentBand']
            secondSort = bandBehavData['laserTrial']

            propOnset, propSustained = funcs.onset_sustained_spike_proportion(
                bandSpikeTimestamps, bandEventOnsetTimes)

            db.at[dbIndex, 'proportionSpikesOnset'] = propOnset
            db.at[dbIndex, 'proportionSpikesSustained'] = propSustained

            #by default: not subtracting baseline, but are replacing pure tone response with baseline for 0 bw condition
            onsetSupInds, onsetSupIndpVals, onsetFacInds, onsetFacIndpVals, onsetPeakInds, onsetSpikeArray = funcs.bandwidth_suppression_from_peak(
                bandSpikeTimestamps,
                bandEventOnsetTimes,
                bandEachTrial,
                secondSort,
                timeRange=[0.0, 0.05],
                baseRange=[-0.05, 0.0])
            db.at[dbIndex, 'onsetSuppressionIndexLaser'] = onsetSupInds[-1]
            db.at[dbIndex, 'onsetSuppressionpValLaser'] = onsetSupIndpVals[-1]
            db.at[dbIndex, 'onsetFacilitationIndexLaser'] = onsetFacInds[-1]
            db.at[dbIndex, 'onsetFacilitationpValLaser'] = onsetFacIndpVals[-1]
            db.at[dbIndex,
                  'onsetPrefBandwidthLaser'] = bandEachTrial[onsetPeakInds[-1]]

            db.at[dbIndex, 'onsetSuppressionIndexNoLaser'] = onsetSupInds[0]
            db.at[dbIndex, 'onsetSuppressionpValNoLaser'] = onsetSupIndpVals[0]
            db.at[dbIndex, 'onsetFacilitationIndexNoLaser'] = onsetFacInds[0]
            db.at[dbIndex,
                  'onsetFacilitationpValNoLaser'] = onsetFacIndpVals[0]
            db.at[dbIndex, 'onsetPrefBandwidthNoLaser'] = bandEachTrial[
                onsetPeakInds[0]]

            #base range is right before sound onset so we get estimate for laser baseline
            sustainedSupInds, sustainedSupIndpVals, sustainedFacInds, sustainedFacIndpVals, sustainedPeakInds, sustainedSpikeArray = funcs.bandwidth_suppression_from_peak(
                bandSpikeTimestamps,
                bandEventOnsetTimes,
                bandEachTrial,
                secondSort,
                timeRange=[0.2, 1.0],
                baseRange=[-0.05, 0.0])
            db.at[dbIndex,
                  'sustainedSuppressionIndexLaser'] = sustainedSupInds[-1]
            db.at[dbIndex,
                  'sustainedSuppressionpValLaser'] = sustainedSupIndpVals[-1]
            db.at[dbIndex,
                  'sustainedFacilitationIndexLaser'] = sustainedFacInds[-1]
            db.at[dbIndex,
                  'sustainedFacilitationpValLaser'] = sustainedFacIndpVals[-1]
            db.at[dbIndex, 'sustainedPrefBandwidthLaser'] = bandEachTrial[
                sustainedPeakInds[-1]]

            db.at[dbIndex,
                  'sustainedSuppressionIndexNoLaser'] = sustainedSupInds[0]
            db.at[dbIndex,
                  'sustainedSuppressionpValNoLaser'] = sustainedSupIndpVals[0]
            db.at[dbIndex,
                  'sustainedFacilitationIndexNoLaser'] = sustainedFacInds[0]
            db.at[dbIndex,
                  'sustainedFacilitationpValNoLaser'] = sustainedFacIndpVals[0]
            db.at[dbIndex, 'sustainedPrefBandwidthNoLaser'] = bandEachTrial[
                sustainedPeakInds[0]]

            #no laser fit
            sustainedResponseNoLaser = sustainedSpikeArray[:, 0]

            bandsForFit = np.unique(bandEachTrial)
            bandsForFit[-1] = 6
            mFixed = 1

            fitParams, R2 = fitfuncs.diff_of_gauss_fit(
                bandsForFit, sustainedResponseNoLaser, mFixed=mFixed)

            #fit params
            db.at[dbIndex, 'R0noLaser'] = fitParams[0]
            db.at[dbIndex, 'RDnoLaser'] = fitParams[3]
            db.at[dbIndex, 'RSnoLaser'] = fitParams[4]
            db.at[dbIndex, 'mnoLaser'] = mFixed
            db.at[dbIndex, 'sigmaDnoLaser'] = fitParams[1]
            db.at[dbIndex, 'sigmaSnoLaser'] = fitParams[2]
            db.at[dbIndex, 'bandwidthTuningR2noLaser'] = R2

            testBands = np.linspace(bandsForFit[0], bandsForFit[-1], 500)
            allFitParams = [mFixed]
            allFitParams.extend(fitParams)
            suppInd, prefBW = fitfuncs.extract_stats_from_fit(
                allFitParams, testBands)

            db.at[dbIndex, 'fitSustainedSuppressionIndexNoLaser'] = suppInd
            db.at[dbIndex, 'fitSustainedPrefBandwidthNoLaser'] = prefBW

            #laser fit
            sustainedResponseLaser = sustainedSpikeArray[:, 1]

            fitParamsLaser, R2Laser = fitfuncs.diff_of_gauss_fit(
                bandsForFit, sustainedResponseLaser, mFixed=mFixed)

            #fit params
            db.at[dbIndex, 'R0laser'] = fitParamsLaser[0]
            db.at[dbIndex, 'RDlaser'] = fitParamsLaser[3]
            db.at[dbIndex, 'RSlaser'] = fitParamsLaser[4]
            db.at[dbIndex, 'mlaser'] = mFixed
            db.at[dbIndex, 'sigmaDlaser'] = fitParamsLaser[1]
            db.at[dbIndex, 'sigmaSlaser'] = fitParamsLaser[2]
            db.at[dbIndex, 'bandwidthTuningR2laser'] = R2Laser

            allFitParamsLaser = [mFixed]
            allFitParamsLaser.extend(fitParamsLaser)
            suppIndLaser, prefBWLaser = fitfuncs.extract_stats_from_fit(
                allFitParamsLaser, testBands)

            db.at[dbIndex, 'fitSustainedSuppressionIndexLaser'] = suppIndLaser
            db.at[dbIndex, 'fitSustainedPrefBandwidthLaser'] = prefBWLaser

            meanLaserDiff = np.mean(sustainedResponseLaser -
                                    sustainedResponseNoLaser)
            db.at[dbIndex, 'laserChangeResponse'] = meanLaserDiff

            laserDiff = sustainedResponseLaser - sustainedResponseNoLaser
            peakInd = np.argmax(sustainedResponseNoLaser)

            db.at[dbIndex, 'peakChangeFR'] = laserDiff[peakInd]
            db.at[dbIndex, 'WNChangeFR'] = laserDiff[-1]

            testRespsNoLaser = fitfuncs.diff_gauss_form(
                testBands, *allFitParams)
            testRespsLaser = fitfuncs.diff_gauss_form(testBands,
                                                      *allFitParamsLaser)

            laserDiffModel = testRespsLaser - testRespsNoLaser
            peakIndModel = np.argmax(testRespsNoLaser)

            db.at[dbIndex, 'fitPeakChangeFR'] = laserDiffModel[peakIndModel]
            db.at[dbIndex, 'fitWNChangeFR'] = laserDiffModel[-1]

            #also calculating fits and suppression with pure tone being 0 bw condition
            toneSustainedSupInds, toneSustainedSupIndpVals, toneSustainedFacInds, toneSustainedFacIndpVals, toneSustainedPeakInds, toneSustainedSpikeArray = funcs.bandwidth_suppression_from_peak(
                bandSpikeTimestamps,
                bandEventOnsetTimes,
                bandEachTrial,
                secondSort,
                timeRange=[0.2, 1.0],
                baseRange=[-0.05, 0.0],
                zeroBWBaseline=False)
            db.at[
                dbIndex,
                'sustainedSuppressionIndexNoLaserPureTone'] = toneSustainedSupInds[
                    0]
            db.at[
                dbIndex,
                'sustainedSuppressionpValNoLaserPureTone'] = toneSustainedSupIndpVals[
                    0]
            db.at[
                dbIndex,
                'sustainedFacilitationIndexNoLaserPureTone'] = toneSustainedFacInds[
                    0]
            db.at[
                dbIndex,
                'sustainedFacilitationpValNoLaserPureTone'] = toneSustainedFacIndpVals[
                    0]
            db.at[dbIndex,
                  'sustainedPrefBandwidthNoLaserPureTone'] = bandEachTrial[
                      toneSustainedPeakInds[0]]

            db.at[
                dbIndex,
                'sustainedSuppressionIndexLaserPureTone'] = toneSustainedSupInds[
                    -1]
            db.at[
                dbIndex,
                'sustainedSuppressionpValLaserPureTone'] = toneSustainedSupIndpVals[
                    -1]
            db.at[
                dbIndex,
                'sustainedFacilitationIndexLaserPureTone'] = toneSustainedFacInds[
                    -1]
            db.at[
                dbIndex,
                'sustainedFacilitationpValLaserPureTone'] = toneSustainedFacIndpVals[
                    -1]
            db.at[dbIndex,
                  'sustainedPrefBandwidthLaserPureTone'] = bandEachTrial[
                      toneSustainedPeakInds[-1]]

            toneSustainedResponseNoLaser = toneSustainedSpikeArray[:, 0]

            toneFitParamsNoLaser, toneR2 = fitfuncs.diff_of_gauss_fit(
                bandsForFit, toneSustainedResponseNoLaser, mFixed=mFixed)

            #fit params
            db.at[dbIndex, 'R0PureToneNoLaser'] = toneFitParamsNoLaser[0]
            db.at[dbIndex, 'RDPureToneNoLaser'] = toneFitParamsNoLaser[3]
            db.at[dbIndex, 'RSPureToneNoLaser'] = toneFitParamsNoLaser[4]
            db.at[dbIndex, 'mPureToneNoLaser'] = mFixed
            db.at[dbIndex, 'sigmaDPureToneNoLaser'] = toneFitParamsNoLaser[1]
            db.at[dbIndex, 'sigmaSPureToneNoLaser'] = toneFitParamsNoLaser[2]
            db.at[dbIndex, 'bandwidthTuningR2PureToneNoLaser'] = toneR2

            allFitParamsToneNoLaser = [mFixed]
            allFitParamsToneNoLaser.extend(toneFitParamsNoLaser)
            suppIndTone, prefBWTone = fitfuncs.extract_stats_from_fit(
                allFitParamsToneNoLaser, testBands)

            db.at[dbIndex,
                  'fitSustainedSuppressionIndexPureToneNoLaser'] = suppIndTone
            db.at[dbIndex,
                  'fitSustainedPrefBandwidthPureToneNoLaser'] = prefBWTone

            toneSustainedResponseLaser = toneSustainedSpikeArray[:, 1]

            toneFitParamsLaser, toneR2Laser = fitfuncs.diff_of_gauss_fit(
                bandsForFit, toneSustainedResponseLaser, mFixed=mFixed)

            #fit params
            db.at[dbIndex, 'R0PureToneLaser'] = toneFitParamsLaser[0]
            db.at[dbIndex, 'RDPureToneLaser'] = toneFitParamsLaser[3]
            db.at[dbIndex, 'RSPureToneLaser'] = toneFitParamsLaser[4]
            db.at[dbIndex, 'mPureToneLaser'] = mFixed
            db.at[dbIndex, 'sigmaDPureToneLaser'] = toneFitParamsLaser[1]
            db.at[dbIndex, 'sigmaSPureToneLaser'] = toneFitParamsLaser[2]
            db.at[dbIndex, 'bandwidthTuningR2PureToneLaser'] = toneR2Laser

            allFitParamsToneLaser = [mFixed]
            allFitParamsToneLaser.extend(toneFitParamsLaser)
            suppIndToneLaser, prefBWToneLaser = fitfuncs.extract_stats_from_fit(
                allFitParamsToneLaser, testBands)

            db.at[
                dbIndex,
                'fitSustainedSuppressionIndexPureToneLaser'] = suppIndToneLaser
            db.at[dbIndex,
                  'fitSustainedPrefBandwidthPureToneLaser'] = prefBWToneLaser

            testRespsNoLaser = fitfuncs.diff_gauss_form(
                testBands, *allFitParamsToneNoLaser)
            testRespsLaser = fitfuncs.diff_gauss_form(testBands,
                                                      *allFitParamsToneLaser)

            laserDiffModel = testRespsLaser - testRespsNoLaser
            peakIndModel = np.argmax(testRespsNoLaser)

            db.at[dbIndex,
                  'fitPeakChangeFRPureTone'] = laserDiffModel[peakIndModel]
            db.at[dbIndex, 'fitWNChangeFRPureTone'] = laserDiffModel[-1]

            #also calculating fits and suppression with nothing being fit for bw 0
            noZeroSustainedResponseNoLaser = sustainedSpikeArray[1:, 0]
            bandsForFitNoZero = bandsForFit[1:]

            noZeroFitParamsNoLaser, noZeroR2 = fitfuncs.diff_of_gauss_fit(
                bandsForFitNoZero,
                noZeroSustainedResponseNoLaser,
                mFixed=mFixed)

            #fit params
            db.at[dbIndex, 'R0noZeroNoLaser'] = noZeroFitParamsNoLaser[0]
            db.at[dbIndex, 'RDnoZeroNoLaser'] = noZeroFitParamsNoLaser[3]
            db.at[dbIndex, 'RSnoZeroNoLaser'] = noZeroFitParamsNoLaser[4]
            db.at[dbIndex, 'mnoZeroNoLaser'] = mFixed
            db.at[dbIndex, 'sigmaDnoZeroNoLaser'] = noZeroFitParamsNoLaser[1]
            db.at[dbIndex, 'sigmaSnoZeroNoLaser'] = noZeroFitParamsNoLaser[2]
            db.at[dbIndex, 'bandwidthTuningR2noZeroNoLaser'] = noZeroR2

            allFitParamsNoZero = [mFixed]
            allFitParamsNoZero.extend(noZeroFitParamsNoLaser)
            testBandsNoZero = np.linspace(bandsForFitNoZero[0],
                                          bandsForFitNoZero[-1], 500)
            suppIndNoZero, prefBWNoZero = fitfuncs.extract_stats_from_fit(
                allFitParamsNoZero, testBandsNoZero)

            db.at[dbIndex,
                  'fitSustainedSuppressionIndexNoZeroNoLaser'] = suppIndNoZero
            db.at[dbIndex,
                  'fitSustainedPrefBandwidthNoZeroNoLaser'] = prefBWNoZero

            noZeroSustainedResponseLaser = sustainedSpikeArray[1:, 1]
            bandsForFitNoZero = bandsForFit[1:]

            noZeroFitParamsLaser, noZeroR2Laser = fitfuncs.diff_of_gauss_fit(
                bandsForFitNoZero, noZeroSustainedResponseLaser, mFixed=mFixed)

            #fit params
            db.at[dbIndex, 'R0noZeroLaser'] = noZeroFitParamsLaser[0]
            db.at[dbIndex, 'RDnoZeroLaser'] = noZeroFitParamsLaser[3]
            db.at[dbIndex, 'RSnoZeroLaser'] = noZeroFitParamsLaser[4]
            db.at[dbIndex, 'mnoZeroLaser'] = mFixed
            db.at[dbIndex, 'sigmaDnoZeroLaser'] = noZeroFitParamsLaser[1]
            db.at[dbIndex, 'sigmaSnoZeroLaser'] = noZeroFitParamsLaser[2]
            db.at[dbIndex, 'bandwidthTuningR2noZeroLaser'] = noZeroR2Laser

            allFitParamsNoZeroLaser = [mFixed]
            allFitParamsNoZeroLaser.extend(noZeroFitParamsLaser)
            suppIndNoZeroLaser, prefBWNoZeroLaser = fitfuncs.extract_stats_from_fit(
                allFitParamsNoZeroLaser, testBandsNoZero)

            db.at[
                dbIndex,
                'fitSustainedSuppressionIndexNoZeroLaser'] = suppIndNoZeroLaser
            db.at[dbIndex,
                  'fitSustainedPrefBandwidthNoZeroLaser'] = prefBWNoZeroLaser

            testRespsNoLaser = fitfuncs.diff_gauss_form(
                testBandsNoZero, *allFitParamsNoZero)
            testRespsLaser = fitfuncs.diff_gauss_form(testBandsNoZero,
                                                      *allFitParamsNoZeroLaser)

            laserDiffModel = testRespsLaser - testRespsNoLaser
            peakIndModel = np.argmax(testRespsNoLaser)

            db.at[dbIndex,
                  'fitPeakChangeFRNoZero'] = laserDiffModel[peakIndModel]
            db.at[dbIndex, 'fitWNChangeFRNoZero'] = laserDiffModel[-1]

    if len(filename) != 0:
        celldatabase.save_hdf(db, dbFilename)
    zStats = np.empty(len(pulseTimes))
    pVals = np.empty(len(pulseTimes))
    respSpikeMean = np.empty(len(pulseTimes))
    for indPulse, pulse in enumerate(pulseTimes):
        responseRange = [pulse, pulse+binTime]
        nspkResp = spikesanalysis.spiketimes_to_spikecounts(spikeTimesFromEventOnset,
                                                           indexLimitsEachTrial,responseRange)
        respSpikeMean[indPulse] = nspkResp.ravel().mean()
        try:
            zStats[indPulse], pVals[indPulse] = stats.mannwhitneyu(nspkResp, nspkBase)
        except ValueError: #All numbers identical will cause mann whitney to fail
            zStats[indPulse], pVals[indPulse] = [0, 1]


    #Save the number of significant train responses
    if (pVals[0] < 0.05) and (sum(pVals[1:]<0.05) >= 3) and (all(respSpikeMean > nspkBase.ravel().mean())):
        databaseNBQX.loc[indRow, 'summarySurvivedNBQX'] = 1

    # #PLOT THE NBQX REPORTS
    # nbqxCells = databaseNBQX.query('isiViolations<0.02 and spikeShapeQuality>2 and summaryPulsePval<0.05')
    # for indRow, dbRow in nbqxCells.iterrows():
    #     plot_NBQX_report(dbRow, saveDir)

if SAVE:
    celldatabase.save_hdf(database, '/tmp/database_with_pulse_responses.h5')
    celldatabase.save_hdf(databaseNBQX, '/tmp/nbqx_database_with_pulse_responses.h5')



    
    databaseFullPath = os.path.join(settings.DATABASE_PATH, NEW_DATABASE_FOLDER, '{}_database.h5'.format(mouseName))
    #outFilename = '/var/tmp/{}_reward_change_modulation_{}.h5'.format(mouseName, processedDate)
    outFilePath = '/var/tmp/'
    outFilenames = [os.path.join(outFilePath,filename) for filename in os.listdir(outFilePath) if mouseName in filename]

    dfThisMouse = celldatabase.load_hdf(databaseFullPath) #pd.read_hdf(databaseFullPath,key='reward_change')
    # -- replacing certain columns -- #
    #colsToDrop = [col for col in dfThisMouse.columns if '-0.2-0s' in col]
    #dfThisMouse = dfThisMouse.drop(columns=colsToDrop)
    #################################
    dfs = [dfThisMouse]
    #allMiceDfs = []
    #for mouseName in np.unique(dfAllMeasures['subject']):
    #dfThisMouse = dfAllMeasures.loc[dfAllMeasures['subject'] == mouseName].reset_index()
    for outFilename in outFilenames:    
        dfModThisMouse = pd.read_hdf(outFilename, key='reward_change')
        dfs.append(dfModThisMouse)

    dfAllThisMouse = reduce(lambda left,right: pd.merge(left,right,on=['subject','date','tetrode','cluster'],how='inner'), dfs)
    dfAllThisMouse.drop_duplicates(['subject','date','tetrode','cluster','depth'],inplace=True)
    if 'level_0' in dfAllThisMouse.columns:
        dfAllThisMouse.drop('level_0', 1, inplace=True)
    dfAllThisMouse.reset_index(inplace=True)
    if 'level_0' in dfAllThisMouse.columns:
        dfAllThisMouse.drop('level_0', 1, inplace=True)
    #dfAllThisMouse.to_hdf(databaseFullPath, key='reward_change')
    celldatabase.save_hdf(dfAllThisMouse, databaseFullPath)
    #when saving to hdf, using (format='table',data_columns=True) is slower but enable on disk queries

Ejemplo n.º 15
0
import pandas as pd
import numpy as np

from jaratoolbox import celldatabase



db = pd.DataFrame()
rand = np.random.random(100)

goodColumn = []
stupidColumn = []

for num in rand:
    if num > 0.5:
        goodColumn.append(np.array([0.0,1.0]))
        stupidColumn.append([0.0,1.0])
    else:
        goodColumn.append(np.full(2, np.nan))
        stupidColumn.append([np.nan, np.nan])
        
db['goodColumn'] = goodColumn
db['stupidColumn'] = stupidColumn
        
celldatabase.save_hdf(db, '/tmp/test_db.h5')

loadDB = celldatabase.load_hdf('/tmp/test_db.h5')

Ejemplo n.º 16
0
        
#         # save db as h5 and csv
        
#         '''
#         dbFilename = os.path.join(settings.DATABASE_PATH,'{0}_test.h5'.format(subject))
#         db.to_hdf(dbFilename, 'database',mode='w')
#         print('Saved database to: {}'.format(dbFilename))
        
#         db.to_csv(dbFilename+'.csv')
#         print('Saved database to: {}'.format(dbFilename+'.csv'))
#         '''
        
        #dbFilenameNew = os.path.join(settings.DATABASE_PATH,'{0}_new.h5'.format(subject))
        dbFilenameNew = '/home/jarauser/data/databases/tmp/{}_new.h5'.format(subject)
        celldatabase.save_hdf(db, dbFilenameNew)
        print('Saved database to: {}'.format(dbFilenameNew))
        
        # -- To load the HDF5 --
        # df = celldatabase.load_hdf('/tmp/band045_new.h5')
        
    #allSubjects = subjects_info.PV_ARCHT_MICE + subjects_info.SOM_ARCHT_MICE
    #allSubjects = ['dapa012', 'dapa013', 'dapa014', 'dapa015']
    allSubjects = []
    fulldb = pd.DataFrame()
    for subject in allSubjects:
        db = celldatabase.load_hdf('/tmp/{}_new.h5'.format(subject))
        fulldb = fulldb.append(db, ignore_index=True)
    fulldbFilename = os.path.join(settings.DATABASE_PATH,'dapa_cells.h5')
    celldatabase.save_hdf(fulldb, fulldbFilename)
Ejemplo n.º 17
0
        onsetRate = onsetSpikes / (responseRange[1] - responseRange[0])
        sustainedRate = sustainedSpikes / (responseRange[2] - responseRange[1])

        baseSpikes = nspkBase.mean()
        baseRate = baseSpikes / (baseRange[1] - baseRange[0])

        onsetBaseSubtracted = onsetRate - baseRate
        sustainedBaseSubtracted = sustainedRate - baseRate

        # baseSub = avgResponse - avgBase
        # normResponse = baseSub / baseSub[0]
        db.loc[indRow, 'onsetRateNoise'] = onsetRate
        db.loc[indRow, 'sustainedRateNoise'] = sustainedRate
        db.loc[indRow, 'baselineRateNoise'] = baseRate

    celldatabase.save_hdf(db, '/tmp/database_with_normResponse.h5')

elif CASE == 1:

    PLOT = 0
    SAVE = 1
    #Compute onsetivity using CF trials with top 5 intensities.
    for indIter, (indRow, dbRow) in enumerate(db.iterrows()):
        cell = ephyscore.Cell(dbRow, useModifiedClusters=True)
        try:
            ephysData, bdata = cell.load('tc')
        except (IndexError, ValueError): #The cell does not have a tc or the tc session has no spikes
            print "No TC for cell {}".format(indRow)

        eventOnsetTimes = ephysData['events']['stimOn']
        spikeTimes = ephysData['spikeTimes']
import os
import pandas as pd
from jaratoolbox import celldatabase

animals = [
    'adap005', 'adap012', 'adap013', 'adap015', 'adap017', 'gosi001',
    'gosi004', 'gosi008', 'gosi010', 'adap067', 'adap071'
]
celldbFolder = '/home/languo/data/database/new_celldb'

for animal in animals:
    celldbPath = os.path.join(celldbFolder,
                              '{}_database_old_format.h5'.format(animal))
    celldb = pd.read_hdf(celldbPath, key='reward_change')
    newCelldbPath = os.path.join(celldbFolder, '{}_database.h5'.format(animal))
    print('resaving celldb as {}'.format(newCelldbPath))
    celldatabase.save_hdf(celldb, newCelldbPath)
def photoID_base_stats(db, filename=''):
    '''
    This function takes as argument a pandas DataFrame and adds new columns.
    The filename should be the full path to where the database will be saved. If a filename is not specified, the database will not be saved.
    
    This function computed basic statistics for all clusters (e.g. laser responsiveness, sound responsiveness, preferred frequency).
    '''
    soundLoc = []
    NaKpeakLatency = np.empty(len(db))

    laserTestStatistic = np.empty(len(db))
    laserPVal = np.empty(len(db))
    laserTrainTestStatistic = np.empty(len(db))
    laserTrainPVal = np.empty(len(db))
    laserChangeFR = np.empty(len(db))

    soundResponseTestStatistic = np.empty(len(db))
    soundResponsePVal = np.empty(len(db))
    onsetSoundResponseTestStatistic = np.empty(len(db))
    onsetSoundResponsePVal = np.empty(len(db))
    sustainedSoundResponseTestStatistic = np.empty(len(db))
    sustainedSoundResponsePVal = np.empty(len(db))
    AMRate = np.empty(len(db))

    gaussFit = []
    tuningTimeRange = []
    Rsquared = np.empty(len(db))
    prefFreq = np.empty(len(db))
    octavesFromPrefFreq = np.empty(len(db))
    bestBandSession = np.empty(len(db))

    for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
        cellObj = ephyscore.Cell(dbRow, useModifiedClusters=True)
        print "Now processing", dbRow['subject'], dbRow['date'], dbRow[
            'depth'], dbRow['tetrode'], dbRow['cluster']

        # --- Determine if sound presentation was ipsi or contra to recording location ---
        soundSide = dbRow['info'][2]
        recordingSide = dbRow['brainArea']

        if (soundSide == 'sound_left' and recordingSide == 'left_AC') or (
                soundSide == 'sound_right' and recordingSide == 'right_AC'):
            soundLoc.append('ipsi')
        else:
            soundLoc.append('contra')

        # --- Determine time difference between Na and K peak (spike width) ---
        peakTimes = dbRow['spikePeakTimes']
        latency = peakTimes[2] - peakTimes[1]
        NaKpeakLatency[indRow] = latency

        # --- Determine laser responsiveness of each cell (using laser pulse) ---
        try:
            laserEphysData, noBehav = cellObj.load('laserPulse')
        except IndexError:
            print "No laser pulse session for this cell"
            testStatistic = np.nan
            pVal = np.nan
            changeFR = np.nan
        else:
            testStatistic, pVal, changeFR = funcs.laser_response(
                laserEphysData)
        laserTestStatistic[indRow] = testStatistic
        laserPVal[indRow] = pVal
        laserChangeFR[indRow] = changeFR

        # --- Determine laser responsiveness of each cell (using laser train) ---
        try:
            laserTrainEphysData, noBehav = cellObj.load('laserTrain')
        except IndexError:
            print "No laser train session for this cell"
            testStatistic = np.nan
            pVal = np.nan
        else:
            testStatistic, pVal, changeFR = funcs.laser_response(
                laserTrainEphysData)
        laserTrainTestStatistic[indRow] = testStatistic
        laserTrainPVal[indRow] = pVal

        # --- Determine sound responsiveness during bandwidth sessions and other statistics about bandwidth session---
        try:
            bandEphysData, bandBehavData = cellObj.load('bandwidth')
        except IndexError:
            print "No bandwidth session for this cell"
            testStatistic = np.nan
            pVal = np.nan
            onsetTestStatistic = np.nan
            onsetpVal = np.nan
            sustainedTestStatistic = np.nan
            sustainedpVal = np.nan
            AM = np.nan
        else:
            bandEventOnsetTimes = funcs.get_sound_onset_times(
                bandEphysData, 'bandwidth')
            bandSpikeTimestamps = bandEphysData['spikeTimes']
            bandEachTrial = bandBehavData['currentBand']
            secondSort = bandBehavData['currentAmp']
            numBands = np.unique(bandEachTrial)
            numSec = np.unique(secondSort)
            AM = np.unique(bandBehavData['modRate'])

            trialsEachComb = behavioranalysis.find_trials_each_combination(
                bandEachTrial, numBands, secondSort, numSec)
            trialsEachBaseCond = trialsEachComb[:, :,
                                                -1]  #using high amp trials for photoidentified, no laser for inactivation
            testStatistic, pVal = funcs.sound_response_any_stimulus(
                bandEventOnsetTimes, bandSpikeTimestamps, trialsEachBaseCond,
                [0.0, 1.0], [-1.2, -0.2])
            onsetTestStatistic, onsetpVal = funcs.sound_response_any_stimulus(
                bandEventOnsetTimes, bandSpikeTimestamps, trialsEachBaseCond,
                [0.0, 0.05], [-0.25, 0.2])
            sustainedTestStatistic, sustainedpVal = funcs.sound_response_any_stimulus(
                bandEventOnsetTimes, bandSpikeTimestamps, trialsEachBaseCond,
                [0.2, 1.0], [-1.0, 0.2])
            pVal *= len(numSec)  #correction for multiple comparisons
            onsetpVal *= len(numSec)
            sustainedpVal *= len(numSec)

        soundResponseTestStatistic[indRow] = testStatistic
        soundResponsePVal[indRow] = pVal
        onsetSoundResponseTestStatistic[indRow] = onsetTestStatistic
        onsetSoundResponsePVal[indRow] = onsetpVal
        sustainedSoundResponseTestStatistic[indRow] = sustainedTestStatistic
        sustainedSoundResponsePVal[indRow] = sustainedpVal
        AMRate[indRow] = AM

        # --- Determine frequency tuning of cells ---
        try:
            tuningEphysData, tuningBehavData = cellObj.load('tuningCurve')
        except IndexError:
            print "No tuning session for this cell"
            freqFit = np.full(4, np.nan)
            thisRsquared = np.nan
            bestFreq = np.nan
            tuningWindow = [np.nan, np.nan]
            octavesFromBest = np.nan
            bandIndex = np.nan
        else:
            tuningEventOnsetTimes = funcs.get_sound_onset_times(
                tuningEphysData, 'tuningCurve')
            tuningSpikeTimestamps = tuningEphysData['spikeTimes']
            freqEachTrial = tuningBehavData['currentFreq']
            intensityEachTrial = tuningBehavData['currentIntensity']
            numFreqs = np.unique(freqEachTrial)
            numIntensities = np.unique(intensityEachTrial)
            timeRange = [-0.2, 0.2]
            spikeTimesFromEventOnset, trialIndexForEachSpike, indexLimitsEachTrial = spikesanalysis.eventlocked_spiketimes(
                tuningSpikeTimestamps, tuningEventOnsetTimes, timeRange)
            trialsEachType = behavioranalysis.find_trials_each_type(
                intensityEachTrial, numIntensities)
            trialsHighInt = trialsEachType[:, -1]
            trialsEachComb = behavioranalysis.find_trials_each_combination(
                freqEachTrial, numFreqs, intensityEachTrial, numIntensities)
            trialsEachFreqHighInt = trialsEachComb[:, :, -1]
            tuningWindow = funcs.best_window_freq_tuning(
                spikeTimesFromEventOnset, indexLimitsEachTrial,
                trialsEachFreqHighInt)
            spikeCountMat = spikesanalysis.spiketimes_to_spikecounts(
                spikeTimesFromEventOnset, indexLimitsEachTrial, tuningWindow)
            tuningSpikeRates = (spikeCountMat[trialsHighInt].flatten()) / (
                tuningWindow[1] - tuningWindow[0])
            freqsThisIntensity = freqEachTrial[trialsHighInt]
            freqFit, thisRsquared = funcs.gaussian_tuning_fit(
                np.log2(freqsThisIntensity), tuningSpikeRates)
            if freqFit is not None:
                bestFreq = 2**freqFit[0]
                bandIndex, octavesFromBest = funcs.best_index(
                    cellObj, bestFreq, 'bandwidth')
            else:
                freqFit = np.full(4, np.nan)
                bestFreq = np.nan
                bandIndex = np.nan
                octavesFromBest = np.nan
        gaussFit.append(freqFit)
        tuningTimeRange.append(tuningWindow)
        Rsquared[indRow] = thisRsquared
        prefFreq[indRow] = bestFreq
        octavesFromPrefFreq[indRow] = octavesFromBest
        bestBandSession[indRow] = bandIndex

    db['soundLocation'] = soundLoc
    db['spikeWidth'] = NaKpeakLatency
    db['AMRate'] = AMRate

    db['laserPVal'] = laserPVal
    db['laserUStat'] = laserTestStatistic
    db['laserTrainPVal'] = laserTrainPVal
    db['laserTrainUStat'] = laserTrainTestStatistic
    db['laserChangeFR'] = laserChangeFR

    db['soundResponseUStat'] = soundResponseTestStatistic
    db['soundResponsePVal'] = soundResponsePVal
    db['onsetSoundResponseUStat'] = onsetSoundResponseTestStatistic
    db['onsetSoundResponsePVal'] = onsetSoundResponsePVal
    db['sustainedSoundResponseUStat'] = sustainedSoundResponseTestStatistic
    db['sustainedSoundResponsePVal'] = sustainedSoundResponsePVal

    db['gaussFit'] = gaussFit
    db['tuningTimeRange'] = tuningTimeRange
    db['tuningFitR2'] = Rsquared
    db['prefFreq'] = prefFreq
    db['octavesFromPrefFreq'] = octavesFromPrefFreq
    db['bestBandSession'] = bestBandSession

    if len(filename) != 0:
        celldatabase.save_hdf(db, filename)
        print filename + " saved"

    return db
Ejemplo n.º 20
0
        db['onsetSoundResponseUStat'] = onsetSoundResponseTestStatistic
        db['onsetSoundResponsePVal'] = onsetSoundResponsePVal
        db['sustainedSoundResponseUStat'] = sustainedSoundResponseTestStatistic
        db['sustainedSoundResponsePVal'] = sustainedSoundResponsePVal

        db['gaussFit'] = gaussFit
        db['tuningTimeRange'] = tuningTimeRange
        db['tuningFitR2'] = Rsquared
        db['prefFreq'] = prefFreq
        db['octavesFromPrefFreq'] = octavesFromPrefFreq
        db['bestBandSession'] = bestBandSession

        # save db as h5
        dbFilenameNew = os.path.join(settings.DATABASE_PATH,
                                     '{0}_clusters.h5'.format(subject))
        celldatabase.save_hdf(db, dbFilenameNew)
        print('Saved database to: {}'.format(dbFilenameNew))

    fulldb = pd.DataFrame()
    for subject in allSubjects:
        dbFilename = os.path.join(settings.DATABASE_PATH,
                                  '{0}_clusters.h5'.format(subject))
        db = celldatabase.load_hdf(dbFilename)
        fulldb = fulldb.append(db, ignore_index=True)
    if subType == 'chr2':
        filename = 'photoidentification_cells.h5'
    elif subType == 'archt':
        filename = 'inactivation_cells.h5'
    fulldbFilename = os.path.join(settings.DATABASE_PATH, filename)
    celldatabase.save_hdf(fulldb, fulldbFilename)
def photoID_indices(db, filename=''):
    '''
    This function takes as argument a pandas DataFrame and adds new columns.
    The filename should be the full path to where the database will be saved. If a filename is not specified, the database will not be saved.
    
    This function computes indices like Suppression Index for all cells passing (generous) thresholds for good cells.
    The thresholds used here are more generous than the ones in studyparams, in case we want to relax our criteria in the future.
    '''

    bestCells = db.query("isiViolations<0.02 or modifiedISI<0.02")
    bestCells = bestCells.loc[bestCells['spikeShapeQuality'] > 2]
    bestCells = bestCells.query(
        'soundResponsePVal<0.05 or onsetSoundResponsePVal<0.05 or sustainedSoundResponsePVal<0.05'
    )
    bestCells = bestCells.loc[bestCells['tuningFitR2'] > 0.05]
    bestCells = bestCells.loc[bestCells['octavesFromPrefFreq'] < 0.5]

    #prepare arrays of NaNs for when we save arrays of data
    bandwidthSpikeArraysHighAmp = np.empty((len(db), 7))
    bandwidthSpikeArraysHighAmp[:] = np.nan

    bandwidthSpikeArraysLowAmp = np.empty((len(db), 7))
    bandwidthSpikeArraysLowAmp[:] = np.nan

    bandwidthSpikeArraysOnset = np.empty((len(db), 6))
    bandwidthSpikeArraysOnset[:] = np.nan

    for dbIndex, dbRow in bestCells.iterrows():

        cell = ephyscore.Cell(dbRow, useModifiedClusters=True)

        bandEphysData, bandBehavData = cell.load_by_index(
            int(dbRow['bestBandSession']))
        bandEventOnsetTimes = funcs.get_sound_onset_times(
            bandEphysData, 'bandwidth')
        bandSpikeTimestamps = bandEphysData['spikeTimes']

        bandEachTrial = bandBehavData['currentBand']
        secondSort = bandBehavData['currentAmp']

        propOnset, propSustained = funcs.onset_sustained_spike_proportion(
            bandSpikeTimestamps, bandEventOnsetTimes)

        db.at[dbIndex, 'proportionSpikesOnset'] = propOnset
        db.at[dbIndex, 'proportionSpikesSustained'] = propSustained

        #by default: not subtracting baseline, but are replacing pure tone response with baseline for 0 bw condition
        onsetSupInds, onsetSupIndpVals, onsetFacInds, onsetFacIndpVals, onsetPeakInds, onsetSpikeArray = funcs.bandwidth_suppression_from_peak(
            bandSpikeTimestamps,
            bandEventOnsetTimes,
            bandEachTrial,
            secondSort,
            timeRange=[0.0, 0.05],
            baseRange=[-0.05, 0.0])
        db.at[dbIndex, 'onsetSuppressionIndex'] = onsetSupInds[-1]
        db.at[dbIndex, 'onsetSuppressionpVal'] = onsetSupIndpVals[-1]
        db.at[dbIndex, 'onsetFacilitationIndex'] = onsetFacInds[-1]
        db.at[dbIndex, 'onsetFacilitationpVal'] = onsetFacIndpVals[-1]
        db.at[dbIndex, 'onsetPrefBandwidth'] = bandEachTrial[onsetPeakInds[-1]]

        sustainedSupInds, sustainedSupIndpVals, sustainedFacInds, sustainedFacIndpVals, sustainedPeakInds, sustainedSpikeArray = funcs.bandwidth_suppression_from_peak(
            bandSpikeTimestamps,
            bandEventOnsetTimes,
            bandEachTrial,
            secondSort,
            timeRange=[0.2, 1.0],
            baseRange=[-1.0, -0.2])
        db.at[dbIndex, 'sustainedSuppressionIndex'] = sustainedSupInds[-1]
        db.at[dbIndex, 'sustainedSuppressionpVal'] = sustainedSupIndpVals[-1]
        db.at[dbIndex, 'sustainedFacilitationIndex'] = sustainedFacInds[-1]
        db.at[dbIndex, 'sustainedFacilitationpVal'] = sustainedFacIndpVals[-1]
        db.at[dbIndex,
              'sustainedPrefBandwidth'] = bandEachTrial[sustainedPeakInds[-1]]

        #only interested in high amp responses
        sustainedResponse = sustainedSpikeArray[:, -1]
        bandsForFit = np.unique(bandEachTrial)
        bandsForFit[-1] = 6
        mFixed = 1

        fitParams, R2 = fitfuncs.diff_of_gauss_fit(bandsForFit,
                                                   sustainedResponse,
                                                   mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0'] = fitParams[0]
        db.at[dbIndex, 'RD'] = fitParams[3]
        db.at[dbIndex, 'RS'] = fitParams[4]
        db.at[dbIndex, 'm'] = mFixed
        db.at[dbIndex, 'sigmaD'] = fitParams[1]
        db.at[dbIndex, 'sigmaS'] = fitParams[2]
        db.at[dbIndex, 'bandwidthTuningR2'] = R2

        testBands = np.linspace(bandsForFit[0], bandsForFit[-1], 500)
        allFitParams = [mFixed]
        allFitParams.extend(fitParams)
        suppInd, prefBW = fitfuncs.extract_stats_from_fit(
            allFitParams, testBands)

        db.at[dbIndex, 'fitSustainedSuppressionIndex'] = suppInd
        db.at[dbIndex, 'fitSustainedPrefBandwidth'] = prefBW

        #also calculating fits and suppression with pure tone being 0 bw condition
        toneSustainedSupInds, toneSustainedSupIndpVals, toneSustainedFacInds, toneSustainedFacIndpVals, toneSustainedPeakInds, toneSustainedSpikeArray = funcs.bandwidth_suppression_from_peak(
            bandSpikeTimestamps,
            bandEventOnsetTimes,
            bandEachTrial,
            secondSort,
            timeRange=[0.2, 1.0],
            baseRange=[-1.0, -0.2],
            zeroBWBaseline=False)
        db.at[dbIndex,
              'sustainedSuppressionIndexPureTone'] = toneSustainedSupInds[-1]
        db.at[
            dbIndex,
            'sustainedSuppressionpValPureTone'] = toneSustainedSupIndpVals[-1]
        db.at[dbIndex,
              'sustainedFacilitationIndexPureTone'] = toneSustainedFacInds[-1]
        db.at[
            dbIndex,
            'sustainedFacilitationpValPureTone'] = toneSustainedFacIndpVals[-1]
        db.at[dbIndex, 'sustainedPrefBandwidthPureTone'] = bandEachTrial[
            toneSustainedPeakInds[-1]]

        #only interested in high amp responses
        toneSustainedResponse = toneSustainedSpikeArray[:, -1]

        toneFitParams, toneR2 = fitfuncs.diff_of_gauss_fit(
            bandsForFit, toneSustainedResponse, mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0PureTone'] = toneFitParams[0]
        db.at[dbIndex, 'RDPureTone'] = toneFitParams[3]
        db.at[dbIndex, 'RSPureTone'] = toneFitParams[4]
        db.at[dbIndex, 'mPureTone'] = mFixed
        db.at[dbIndex, 'sigmaDPureTone'] = toneFitParams[1]
        db.at[dbIndex, 'sigmaSPureTone'] = toneFitParams[2]
        db.at[dbIndex, 'bandwidthTuningR2PureTone'] = toneR2

        allFitParamsTone = [mFixed]
        allFitParamsTone.extend(toneFitParams)
        suppIndTone, prefBWTone = fitfuncs.extract_stats_from_fit(
            allFitParamsTone, testBands)

        db.at[dbIndex, 'fitSustainedSuppressionIndexPureTone'] = suppIndTone
        db.at[dbIndex, 'fitSustainedPrefBandwidthPureTone'] = prefBWTone

        #also calculating fits and suppression with nothing being fit for bw 0, want to do both intensities here
        #doing this for onset responses as well
        noZeroSustainedResponseHighAmp = sustainedSpikeArray[1:, -1]
        noZeroOnsetResponseHighAmp = onsetSpikeArray[1:, -1]
        bandsForFitNoZero = bandsForFit[1:]

        noZeroFitParamsHigh, noZeroR2High = fitfuncs.diff_of_gauss_fit(
            bandsForFitNoZero, noZeroSustainedResponseHighAmp, mFixed=mFixed)

        #fit params
        db.at[dbIndex, 'R0noZeroHighAmp'] = noZeroFitParamsHigh[0]
        db.at[dbIndex, 'RDnoZeroHighAmp'] = noZeroFitParamsHigh[3]
        db.at[dbIndex, 'RSnoZeroHighAmp'] = noZeroFitParamsHigh[4]
        db.at[dbIndex, 'mnoZeroHighAmp'] = mFixed
        db.at[dbIndex, 'sigmaDnoZeroHighAmp'] = noZeroFitParamsHigh[1]
        db.at[dbIndex, 'sigmaSnoZeroHighAmp'] = noZeroFitParamsHigh[2]
        db.at[dbIndex, 'bandwidthTuningR2noZeroHighAmp'] = noZeroR2High

        allFitParamsNoZeroHigh = [mFixed]
        allFitParamsNoZeroHigh.extend(noZeroFitParamsHigh)
        testBandsNoZero = np.linspace(bandsForFitNoZero[0],
                                      bandsForFitNoZero[-1], 500)
        suppIndNoZero, prefBWNoZero = fitfuncs.extract_stats_from_fit(
            allFitParamsNoZeroHigh, testBandsNoZero)

        db.at[dbIndex,
              'fitSustainedSuppressionIndexNoZeroHighAmp'] = suppIndNoZero
        db.at[dbIndex, 'fitSustainedPrefBandwidthNoZeroHighAmp'] = prefBWNoZero

        noZeroFitParamsOnset, noZeroR2Onset = fitfuncs.diff_of_gauss_fit(
            bandsForFitNoZero, noZeroOnsetResponseHighAmp, mFixed=mFixed)

        db.at[dbIndex, 'R0noZeroOnset'] = noZeroFitParamsOnset[0]
        db.at[dbIndex, 'RDnoZeroOnset'] = noZeroFitParamsOnset[3]
        db.at[dbIndex, 'RSnoZeroOnset'] = noZeroFitParamsOnset[4]
        db.at[dbIndex, 'mnoZeroOnset'] = mFixed
        db.at[dbIndex, 'sigmaDnoZeroOnset'] = noZeroFitParamsOnset[1]
        db.at[dbIndex, 'sigmaSnoZeroOnset'] = noZeroFitParamsOnset[2]
        db.at[dbIndex, 'bandwidthTuningR2noZeroOnset'] = noZeroR2Onset

        allFitParamsNoZeroOnset = [mFixed]
        allFitParamsNoZeroOnset.extend(noZeroFitParamsOnset)
        suppIndNoZero, prefBWNoZero = fitfuncs.extract_stats_from_fit(
            allFitParamsNoZeroOnset, testBandsNoZero)

        db.at[dbIndex, 'fitOnsetSuppressionIndexNoZero'] = suppIndNoZero
        db.at[dbIndex, 'fitOnsetPrefBandwidthNoZero'] = prefBWNoZero

        if sustainedSpikeArray.shape[1] > 1:
            noZeroSustainedResponseLowAmp = sustainedSpikeArray[1:, 0]

            if all(noZeroSustainedResponseLowAmp):
                noZeroFitParamsLow, noZeroR2Low = fitfuncs.diff_of_gauss_fit(
                    bandsForFitNoZero,
                    noZeroSustainedResponseLowAmp,
                    mFixed=mFixed)

                #fit params
                db.at[dbIndex, 'R0noZeroLowAmp'] = noZeroFitParamsLow[0]
                db.at[dbIndex, 'RDnoZeroLowAmp'] = noZeroFitParamsLow[3]
                db.at[dbIndex, 'RSnoZeroLowAmp'] = noZeroFitParamsLow[4]
                db.at[dbIndex, 'mnoZeroLowAmp'] = mFixed
                db.at[dbIndex, 'sigmaDnoZeroLowAmp'] = noZeroFitParamsLow[1]
                db.at[dbIndex, 'sigmaSnoZeroLowAmp'] = noZeroFitParamsLow[2]
                db.at[dbIndex, 'bandwidthTuningR2noZeroLowAmp'] = noZeroR2Low

                allFitParamsNoZeroLow = [mFixed]
                allFitParamsNoZeroLow.extend(noZeroFitParamsLow)
                suppIndNoZero, prefBWNoZero = fitfuncs.extract_stats_from_fit(
                    allFitParamsNoZeroLow, testBandsNoZero)

                db.at[
                    dbIndex,
                    'fitSustainedSuppressionIndexNoZeroLowAmp'] = suppIndNoZero
                db.at[dbIndex,
                      'fitSustainedPrefBandwidthNoZeroLowAmp'] = prefBWNoZero

        #save the spike array and baseline rate for each cell in case needed for future calculations
        bandwidthSpikeArraysHighAmp[dbIndex, :] = toneSustainedSpikeArray[:,
                                                                          -1]
        bandwidthSpikeArraysLowAmp[dbIndex, :] = toneSustainedSpikeArray[:, 0]
        bandwidthSpikeArraysOnset[dbIndex, :] = noZeroOnsetResponseHighAmp
        db.at[dbIndex, 'bandwidthBaselineRate'] = sustainedResponse[0]

    db['bandwidthOnsetSpikeArrayHighAmp'] = list(bandwidthSpikeArraysOnset)
    db['bandwidthSustainedSpikeArrayHighAmp'] = list(
        bandwidthSpikeArraysHighAmp)
    db['bandwidthSustainedSpikeArrayLowAmp'] = list(bandwidthSpikeArraysLowAmp)

    if len(filename) != 0:
        celldatabase.save_hdf(db, filename)
        print filename + " saved"

    return db
Ejemplo n.º 22
0
def inactivation_base_stats(db, filename=''):
    laserTestStatistic = np.empty(len(db))
    laserPVal = np.empty(len(db))

    soundResponseTestStatistic = np.empty(len(db))
    soundResponsePVal = np.empty(len(db))
    onsetSoundResponseTestStatistic = np.empty(len(db))
    onsetSoundResponsePVal = np.empty(len(db))
    sustainedSoundResponseTestStatistic = np.empty(len(db))
    sustainedSoundResponsePVal = np.empty(len(db))

    gaussFit = []
    tuningTimeRange = []
    Rsquared = np.empty(len(db))
    prefFreq = np.empty(len(db))
    octavesFromPrefFreq = np.empty(len(db))
    bestBandSession = np.empty(len(db))

    for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
        cellObj = ephyscore.Cell(dbRow)
        print "Now processing", dbRow['subject'], dbRow['date'], dbRow[
            'depth'], dbRow['tetrode'], dbRow['cluster']

        # --- Determine laser responsiveness of each cell (using first 100 ms of noise-in-laser trials) ---
        try:
            laserEphysData, noBehav = cellObj.load('lasernoisebursts')
        except IndexError:
            print "No laser pulse session for this cell"
            testStatistic = np.nan
            pVal = np.nan
            changeFR = np.nan
        else:
            testStatistic, pVal, changeFR = funcs.laser_response(
                laserEphysData,
                baseRange=[-0.3, -0.2],
                responseRange=[0.0, 0.1])
        laserTestStatistic[indRow] = testStatistic
        laserPVal[indRow] = pVal

        # --- Determine sound responsiveness during bandwidth sessions and calculate baseline firing rates with and without laser---
        #done in a kind of stupid way because regular and control sessions are handled the same way
        if any(session in dbRow['sessionType']
               for session in ['laserBandwidth', 'laserBandwidthControl']):
            if 'laserBandwidth' in dbRow['sessionType']:
                bandEphysData, bandBehavData = cellObj.load('laserBandwidth')
                behavSession = 'laserBandwidth'
                db.at[dbIndex, 'controlSession'] = 0
            elif 'laserBandwidthControl' in dbRow['sessionType']:
                bandEphysData, bandBehavData = cellObj.load(
                    'laserBandwidthControl')
                behavSession = 'laserBandwidthControl'
                db.at[dbIndex, 'controlSession'] = 1
            bandEventOnsetTimes = funcs.get_sound_onset_times(
                bandEphysData, 'bandwidth')
            bandSpikeTimestamps = bandEphysData['spikeTimes']
            bandEachTrial = bandBehavData['currentBand']
            secondSort = bandBehavData['laserTrial']
            numBands = np.unique(bandEachTrial)
            numSec = np.unique(secondSort)

            trialsEachComb = behavioranalysis.find_trials_each_combination(
                bandEachTrial, numBands, secondSort, numSec)
            trialsEachBaseCond = trialsEachComb[:, :,
                                                0]  #using no laser trials to determine sound responsiveness
            testStatistic, pVal = funcs.sound_response_any_stimulus(
                bandEventOnsetTimes, bandSpikeTimestamps, trialsEachBaseCond,
                [0.0, 1.0], [-1.2, -0.2])
            onsetTestStatistic, onsetpVal = funcs.sound_response_any_stimulus(
                bandEventOnsetTimes, bandSpikeTimestamps, trialsEachBaseCond,
                [0.0, 0.05], [-0.25, 0.2])
            sustainedTestStatistic, sustainedpVal = funcs.sound_response_any_stimulus(
                bandEventOnsetTimes, bandSpikeTimestamps, trialsEachBaseCond,
                [0.2, 1.0], [-1.0, 0.2])
            pVal *= len(numBands)  #correction for multiple comparisons
            onsetpVal *= len(numBands)
            sustainedpVal *= len(numBands)
            #pdb.set_trace()

            #find baselines with and without laser
            baselineRange = [-0.05, 0.0]
            baselineRates, baselineSEMs = funcs.inactivated_cells_baselines(
                bandSpikeTimestamps, bandEventOnsetTimes, secondSort,
                baselineRange)
            db.at[dbIndex, 'baselineFRnoLaser'] = baselineRates[0]
            db.at[dbIndex, 'baselineFRLaser'] = baselineRates[1]
            db.at[dbIndex, 'baselineFRnoLaserSEM'] = baselineSEMs[0]
            db.at[dbIndex, 'baselineFRLaserSEM'] = baselineSEMs[1]
            db.at[dbIndex,
                  'baselineChangeFR'] = baselineRates[1] - baselineRates[0]
        else:
            print "No bandwidth session for this cell"
            testStatistic = np.nan
            pVal = np.nan
            onsetTestStatistic = np.nan
            onsetpVal = np.nan
            sustainedTestStatistic = np.nan
            sustainedpVal = np.nan
            #pdb.set_trace()

        soundResponseTestStatistic[indRow] = testStatistic
        soundResponsePVal[indRow] = pVal
        onsetSoundResponseTestStatistic[indRow] = onsetTestStatistic
        onsetSoundResponsePVal[indRow] = onsetpVal
        sustainedSoundResponseTestStatistic[indRow] = sustainedTestStatistic
        sustainedSoundResponsePVal[indRow] = sustainedpVal

        # --- Determine frequency tuning of cells ---
        try:
            tuningEphysData, tuningBehavData = cellObj.load('tuningCurve')
        except IndexError:
            print "No tuning session for this cell"
            freqFit = np.full(4, np.nan)
            thisRsquared = np.nan
            bestFreq = np.nan
            tuningWindow = np.full(2, np.nan)
            octavesFromBest = np.nan
            bandIndex = np.nan
        else:
            tuningEventOnsetTimes = funcs.get_sound_onset_times(
                tuningEphysData, 'tuningCurve')
            tuningSpikeTimestamps = tuningEphysData['spikeTimes']
            freqEachTrial = tuningBehavData['currentFreq']
            intensityEachTrial = tuningBehavData['currentIntensity']
            numFreqs = np.unique(freqEachTrial)
            numIntensities = np.unique(intensityEachTrial)
            timeRange = [-0.2, 0.2]
            spikeTimesFromEventOnset, trialIndexForEachSpike, indexLimitsEachTrial = spikesanalysis.eventlocked_spiketimes(
                tuningSpikeTimestamps, tuningEventOnsetTimes, timeRange)
            trialsEachType = behavioranalysis.find_trials_each_type(
                intensityEachTrial, numIntensities)
            trialsHighInt = trialsEachType[:, -1]
            trialsEachComb = behavioranalysis.find_trials_each_combination(
                freqEachTrial, numFreqs, intensityEachTrial, numIntensities)
            trialsEachFreqHighInt = trialsEachComb[:, :, -1]
            tuningWindow = funcs.best_window_freq_tuning(
                spikeTimesFromEventOnset, indexLimitsEachTrial,
                trialsEachFreqHighInt)
            tuningWindow = np.array(tuningWindow)
            spikeCountMat = spikesanalysis.spiketimes_to_spikecounts(
                spikeTimesFromEventOnset, indexLimitsEachTrial, tuningWindow)
            tuningSpikeRates = (spikeCountMat[trialsHighInt].flatten()) / (
                tuningWindow[1] - tuningWindow[0])
            freqsThisIntensity = freqEachTrial[trialsHighInt]
            freqFit, thisRsquared = funcs.gaussian_tuning_fit(
                np.log2(freqsThisIntensity), tuningSpikeRates)
            if freqFit is not None:
                bestFreq = 2**freqFit[0]
                bandIndex, octavesFromBest = funcs.best_index(
                    cellObj, bestFreq, behavSession)
            else:
                freqFit = np.full(4, np.nan)
                bestFreq = np.nan
                bandIndex = np.nan
                octavesFromBest = np.nan
        gaussFit.append(freqFit)
        tuningTimeRange.append(tuningWindow)
        Rsquared[indRow] = thisRsquared
        prefFreq[indRow] = bestFreq
        octavesFromPrefFreq[indRow] = octavesFromBest
        bestBandSession[indRow] = bandIndex

    db['laserPVal'] = laserPVal
    db['laserUStat'] = laserTestStatistic

    db['soundResponseUStat'] = soundResponseTestStatistic
    db['soundResponsePVal'] = soundResponsePVal
    db['onsetSoundResponseUStat'] = onsetSoundResponseTestStatistic
    db['onsetSoundResponsePVal'] = onsetSoundResponsePVal
    db['sustainedSoundResponseUStat'] = sustainedSoundResponseTestStatistic
    db['sustainedSoundResponsePVal'] = sustainedSoundResponsePVal

    db['gaussFit'] = gaussFit
    db['tuningTimeRange'] = tuningTimeRange
    db['tuningFitR2'] = Rsquared
    db['prefFreq'] = prefFreq
    db['octavesFromPrefFreq'] = octavesFromPrefFreq
    db['bestBandSession'] = bestBandSession

    if len(filename) != 0:
        celldatabase.save_hdf(db, filename)
        print filename + " saved"

    return db
def photoDB_cell_locations(db, filename=''):
    '''
    This function takes as argument a pandas DataFrame and adds new columns.
    The filename should be the full path to where the database will be saved. If a filename is not specified, the database will not be saved.
    
    This function computes the depths and cortical locations of all cells with suppression indices computed.
    This function should be run in a virtual environment because the allensdk has weird dependencies that we don't want tainting our computers.
    '''
    from allensdk.core.mouse_connectivity_cache import MouseConnectivityCache

    # lapPath = os.path.join(settings.ATLAS_PATH, 'AllenCCF_25/coronal_laplacian_25.nrrd')
    lapPath = '/mnt/jarahubdata/tmp/coronal_laplacian_25.nrrd'
    lapData = nrrd.read(lapPath)
    lap = lapData[0]

    mcc = MouseConnectivityCache(resolution=25)
    rsp = mcc.get_reference_space()
    rspAnnotationVolumeRotated = np.rot90(rsp.annotation, 1, axes=(2, 0))

    tetrodetoshank = {
        1: 1,
        2: 1,
        3: 2,
        4: 2,
        5: 3,
        6: 3,
        7: 4,
        8: 4
    }  #hardcoded dictionary of tetrode to shank mapping for probe geometry used in this study

    bestCells = db[db['sustainedSuppressionIndex'].notnull(
    )]  #calculate depths for all the cells that we calculated SIs for

    for dbIndex, dbRow in bestCells.iterrows():
        subject = dbRow['subject']

        try:
            fileNameInfohist = os.path.join(settings.INFOHIST_PATH,
                                            '{}_tracks.py'.format(subject))
            tracks = imp.load_source('tracks_module', fileNameInfohist).tracks
        except IOError:
            print("No such tracks file: {}".format(fileNameInfohist))
        else:
            brainArea = dbRow['brainArea']
            if brainArea == 'left_AC':
                brainArea = 'LeftAC'
            elif brainArea == 'right_AC':
                brainArea = 'RightAC'
            tetrode = dbRow['tetrode']
            shank = tetrodetoshank[tetrode]
            recordingTrack = dbRow['info'][0]

            track = next(
                (track
                 for track in tracks if (track['brainArea'] == brainArea) and (
                     track['shank'] == shank) and (
                         track['recordingTrack'] == recordingTrack)), None)

            if track is not None:
                histImage = track['histImage']

                filenameSVG = ha.get_filename_registered_svg(
                    subject, brainArea, histImage, recordingTrack, shank)

                if tetrode % 2 == 0:
                    depth = dbRow['depth']
                else:
                    depth = dbRow['depth'] - 150.0  #odd tetrodes are higher

                brainSurfCoords, tipCoords, siteCoords = ha.get_coords_from_svg(
                    filenameSVG, [depth], dbRow['maxDepth'])

                siteCoords = siteCoords[0]

                atlasZ = track['atlasZ']
                cortexDepthData = np.rot90(lap[:, :, atlasZ], -1)

                # We consider the points with depth > 0.95 to be the bottom surface of cortex
                bottomData = np.where(cortexDepthData > 0.95)

                # Top of cortex is less than 0.02 but greater than 0
                topData = np.where((cortexDepthData < 0.02)
                                   & (cortexDepthData > 0))

                # Distance between the cell and each point on the surface of the brain
                dXTop = topData[1] - siteCoords[0]
                dYTop = topData[0] - siteCoords[1]
                distanceTop = np.sqrt(dXTop**2 + dYTop**2)

                # The index and distance to the closest point on the top surface
                indMinTop = np.argmin(distanceTop)
                minDistanceTop = distanceTop.min()

                # Same for the distance from the cell to the bottom surface of cortex
                dXBottom = bottomData[1] - siteCoords[0]
                dYBottom = bottomData[0] - siteCoords[1]
                distanceBottom = np.sqrt(dXBottom**2 + dYBottom**2)
                minDistanceBottom = distanceBottom.min()

                # The metric we want is the relative distance from the top surface
                cellRatio = minDistanceTop / (minDistanceBottom +
                                              minDistanceTop)
                db.at[dbIndex, 'cortexRatioDepth'] = cellRatio

                # use allen annotated atlas to figure out where recording site is
                thisCoordID = rspAnnotationVolumeRotated[int(siteCoords[0]),
                                                         int(siteCoords[1]),
                                                         atlasZ]
                structDict = rsp.structure_tree.get_structures_by_id(
                    [thisCoordID])
                db.at[dbIndex, 'recordingSiteName'] = structDict[0]['name']

            else:
                print subject, brainArea, shank, recordingTrack

    if len(filename) != 0:
        celldatabase.save_hdf(db, filename)
        print filename + " saved"

    return db
Ejemplo n.º 24
0
'''

initialDB = '/mnt/jarahubdata/figuresdata/2018thstr/celldatabase_ALLCELLS_MODIFIED_CLU.h5'
amDiscrimDB = '/mnt/jarahubdata/figuresdata/2018thstr/figure_am/celldatabase_with_am_discrimination_accuracy.h5'
phaseDiscrimDB = '/mnt/jarahubdata/figuresdata/2018thstr/figure_am/celldatabase_with_phase_discrimination_accuracy.h5'

dbInitial = pd.read_hdf(initialDB, key='dataframe')

# Change the info column to allow saving with the new function
dbInitial['info'] = dbInitial['info'].apply(lambda x: [x] if not isinstance(x, list) else x)

dbAM = pd.read_hdf(amDiscrimDB, key='dataframe')
dbPhase = pd.read_hdf(phaseDiscrimDB, key='dataframe')

# Columns that need to be moved from aux databases to the main database
# dbAM has a column 'accuracy'. This is the discrimination accuracy of AM rate.
# dbPhase has a set of columns called phaseAccuracy_{}Hz for each AM rate

# Rename this column to be more informative. TODO: See what else we need to do if we change this name.
dbInitial['rateDiscrimAccuracy'] = dbAM['accuracy']

possibleRateKeys = np.array([4, 5, 8, 11, 16, 22, 32, 45, 64, 90, 128])
for rate in possibleRateKeys:
    dbInitial['phaseDiscrimAccuracy_{}Hz'.format(rate)] = dbPhase['phaseAccuracy_{}Hz'.format(rate)]

newFn = '/mnt/jarahubdata/figuresdata/2018thstr/celldatabase_calculated_columns.h5'

#Save the database as a new name
celldatabase.save_hdf(dbInitial, newFn)

Ejemplo n.º 25
0
        if 'reward_change_modulation' in filename
    ]

    fullDb = celldatabase.load_hdf(
        databaseFullPath)  #pd.read_hdf(databaseFullPath,key='reward_change')
    # -- replacing certain columns -- #
    #colsToDrop = [col for col in dfThisMouse.columns if '-0.2-0s' in col]
    #dfThisMouse = dfThisMouse.drop(columns=colsToDrop)
    #################################
    dfs = [fullDb]
    for outFilename in outFilenames:
        dfMod = pd.read_hdf(outFilename, key='reward_change')
        dfs.append(dfMod)

    dfAll = reduce(
        lambda left, right: pd.merge(left,
                                     right,
                                     on=
                                     ['subject', 'date', 'tetrode', 'cluster'],
                                     how='inner'), dfs)
    dfAll.drop_duplicates(['subject', 'date', 'tetrode', 'cluster', 'depth'],
                          inplace=True)
    if 'level_0' in dfAll.columns:
        dfAll.drop('level_0', 1, inplace=True)
    dfAll.reset_index(inplace=True)
    if 'level_0' in dfAll.columns:
        dfAll.drop('level_0', 1, inplace=True)
    #dfAllThisMouse.to_hdf(databaseFullPath, key='reward_change')
    celldatabase.save_hdf(dfAll, databaseFullPath)
    #when saving to hdf, using (format='table',data_columns=True) is slower but enable on disk queries