def normalized_hist_dataframe(data_column,
                              bin_number=50,
                              output_dir='/var/tmp/'):
    """
    Create a histogram using 2019AStrpi database with just the column name as an input
    Args:
        data_column (str): Name of the column to index
        bin_number (int): Number of bins for the histogram
        output_dir (str): Directory to save figure to

    Returns:
        fig: The figure object the hisotgram was plotted on
        ax: The axis of the figure the histogram was plotted on

    """
    db = celldatabase.load_hdf(
        "/var/tmp/figuresdata/2019astrpi/direct_and_indirect_cells.h5")
    # dbTuned = db.query(studyparams.TUNING_FILTER)
    D1DB = db.query(studyparams.D1_CELLS)
    nD1DB = db.query(studyparams.nD1_CELLS)
    D1DB = D1DB.replace([np.inf, -np.inf], np.nan)
    nD1DB = nD1DB.replace([np.inf, -np.inf], np.nan)
    D1DB = D1DB[D1DB[data_column].notnull()]
    nD1DB = nD1DB[nD1DB[data_column].notnull()]
    D1Hist, D1bins = np.histogram(D1DB[data_column],
                                  bins=bin_number,
                                  density=True)
    nD1Hist, nD1bins = np.histogram(nD1DB[data_column],
                                    bins=bin_number,
                                    density=True)
    center = (D1bins[:-1] + D1bins[1:]) / 2
    width = 0.7 * (D1bins[1] - D1bins[0])
    D1Median = np.median(D1DB[data_column])
    nD1Median = np.median(nD1DB[data_column])

    fig = plt.gcf()
    fig.clf()
    figFilename = "{}".format(data_column)  # Do not include extension
    figFormat = 'png'  # 'pdf' or 'svg'
    figSize = [5, 5]

    ax = fig.add_subplot()
    ax.bar(center, D1Hist, width=width, align='center', label='D1', alpha=0.5)
    ax.bar(center,
           nD1Hist,
           width=width,
           align='center',
           label='nD1',
           alpha=0.5)
    ax.legend()
    ax.set_xlabel('{} value'.format(data_column))
    ax.set_ylabel('Frequency')
    ax.set_title(data_column)
    ymin, ymax = ax.get_ybound()
    ax.vlines(D1Median, ymin, ymax, color="Green")
    ax.vlines(nD1Median, ymin, ymax, color="Red")

    extraplots.save_figure(figFilename, figFormat, figSize, output_dir, 'w')
    plt.show()
    return fig, ax
Exemple #2
0
def merge_dataframes(df1, df2):
    """
    Takes two dataframes and concatenates them so we can process one mouse at a time
    instead of having to regenerate an entire database when we add on a new mouse
    Args:
        df1 (list): List of strings containing full paths to dataframe(s).
        df2 (string): Full path to second dataframe to which the list of dataframes will be appended to the end of

    Returns:
        new_df (pandas.DataFrame): Two given dataframes appended through index value

    """
    df2 = celldatabase.load_hdf(df2)
    for frame in df1:
        appendedFrame = celldatabase.load_hdf(frame)
        df2 = pd.concat([df2, appendedFrame],
                        axis=0,
                        ignore_index=True,
                        sort=False)

    return df2
Exemple #3
0
from __future__ import division
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from jaratoolbox import settings
from jaratoolbox import celldatabase
import studyparams
reload(studyparams)

dbPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME)
dbFilename = os.path.join(
    dbPath, 'signRespCellsdb_{}.h5'.format(studyparams.STUDY_NAME))
# -- Load the database of responsive cells --
signRespCells = celldatabase.load_hdf(dbFilename)

suppressedCells = []

for indRow, dbRow in signRespCells.iterrows():
    highBaseA = signRespCells['meanBaseHighA'][indRow]
    highEvokedA = signRespCells['meanEvokedHighA'][indRow]
    midBaseA = signRespCells['meanBaseMidA'][indRow]
    midEvokedA = signRespCells['meanEvokedMidA'][indRow]
    lowBaseA = signRespCells['meanBaseLowA'][indRow]
    lowEvokedA = signRespCells['meanEvokedLowA'][indRow]

    if signRespCells['mostRespFreqPValueOddStdA'][indRow] == signRespCells[
            'pValHighFRA'][indRow]:
        if highBaseA > highEvokedA:
            suppressedCells.append(signRespCells.iloc[indRow])
        #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)

####################################################################################
####################################################################################
####################################################################################

# import os, sys
# from jaratoolbox import celldatabase
# from jaratoolbox import extraplots
# from jaratoolbox import spikesanalysis
# from jaratoolbox import spikesorting
# from jaratoolbox import behavioranalysis
# from jaratoolbox import loadbehavior
Exemple #5
0
if not os.path.exists(dataDir):
    os.mkdir(dataDir)

####################################################################################
scriptFullPath = os.path.realpath(__file__)
brainAreas = ['rightAC', 'rightAStr']
maxZThreshold = 3
alphaLevel = 0.05
movementWindow = [0.0, 0.3]  #[0.05, 0.15] # in seconds
removeSideIn = True
###################################################################################
#dbKey = 'reward_change'
dbFolder = os.path.join(settings.FIGURES_DATA_PATH, STUDY_NAME)
celldbPath = os.path.join(dbFolder, 'rc_database.h5')
celldb = celldatabase.load_hdf(celldbPath)

for brainArea in brainAreas:
    #goodQualCells = celldb.query("keepAfterDupTest==1 and brainArea=='{}'".format(brainArea))
    goodQualCells = celldb.query(
        "keepAfterDupTest==1 and cellInTargetArea==1 and brainArea=='{}'".
        format(brainArea))

    if removeSideIn:
        movementModI = goodQualCells['movementModI_{}_removedsidein'.format(
            movementWindow)]
        movementModS = goodQualCells['movementModS_{}_removedsidein'.format(
            movementWindow)]
        encodeMv = (
            goodQualCells['movementSelective_moredif_Mv'] +
            goodQualCells['movementSelective_samedif_MvSd']).astype(bool)
Exemple #6
0
Plotting histogram of number of cells vs expectation index along with which of those are significantly responsive for each of the three frequencies and for both sessions for five auditory regions (primary AC(0-1300um), ventral AC(1300-1800um), TeA(1800-2175um), Ectorhinal(2175-2675um), and Perirhinal(2675-3050um)).
"""

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from jaratoolbox import settings
from jaratoolbox import celldatabase
import studyparams
reload(studyparams)

dbPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME)
# -- Loading responsive cell database --
dbFilename = os.path.join(dbPath,'responsivedb_{}.h5'.format(studyparams.STUDY_NAME))
responsivedb = celldatabase.load_hdf(dbFilename)
# -- Loading suppressed cell database --
dbFilename = os.path.join(dbPath,'suppressedRespCellsdb_{}.h5'.format(studyparams.STUDY_NAME))
suppresseddb = celldatabase.load_hdf(dbFilename)

def expectation_index_hist_by_region(database, region, title):
    bins = 30
    plt.figure(figsize=(10,4.5)).suptitle(region[0] + ' ' + title, fontsize=9, y=1.01)
    ax = plt.subplot2grid((2,3), (0,0))

    area = database.query(region[1])
    # -- Reindexing the responsive database --
    area = area.reset_index(drop=True)

    # -- Ascending --
    highFreqRespCellsA = []
import numpy as np
from jaratoolbox import celldatabase
from jaratoolbox import ephyscore
from jaratoolbox import spikesanalysis
import pandas as pd
import figparams  #TODO: Remove this once we move plotting code to a figure file
import matplotlib.pyplot as plt
from scipy import stats

db = celldatabase.load_hdf('/tmp/database_with_normResponse.h5')

db['noiseOnsetivityIndex'] = (
    db['sustainedRateNoise'] -
    db['onsetRateNoise']) / (db['sustainedRateNoise'] + db['onsetRateNoise'])

plt.clf()


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


def medline(ax, yval, midline, width, color='k', linewidth=3):
    start = midline - (width / 2)
    end = midline + (width / 2)
    ax.plot([start, end], [yval, yval], color=color, lw=linewidth)


goodFit = db.query('rsquaredFit > 0.04')
import numpy as np
import pandas as pd
import matplotlib.gridspec as gridspec
from scipy import stats
import studyparams
from jaratoolbox import behavioranalysis
from jaratoolbox import celldatabase
from jaratoolbox import ephyscore
from jaratoolbox import extraplots
from jaratoolbox import spikesanalysis
import database_generation_funcs as funcs
import figparams
from extras import figure_R2_comparison as histDraw

#%% Calculations of various data to plot
db = celldatabase.load_hdf("/var/tmp/figuresdata/2019astrpi/ttDBR2.h5")

D1DB = db.query(studyparams.D1_CELLS)
nD1DB = db.query(studyparams.nD1_CELLS)
emptyD1 = np.zeros(len(D1DB))
emptyND1 = np.zeros(len(nD1DB))
D1Results = pd.DataFrame({
    "noiseburstBase": emptyD1,
    "noiseburstResp": emptyD1,
    "tuningTestBase": emptyD1,
    "tuningTestResp": emptyD1,
    "tuningCurveBase": emptyD1,
    "tuningCurveResp": emptyD1,
    "amBase": emptyD1,
    "amResp": emptyD1,
})
Exemple #9
0
from jaratoolbox import behavioranalysis
from jaratoolbox import spikesanalysis
from jaratoolbox import celldatabase
from scipy import stats
import pandas as pd
import figparams
import studyparams

FIGNAME = 'figure_am'
d1mice = studyparams.ASTR_D1_CHR2_MICE
nameDB = '_'.join(d1mice) + '.h5'
# pathtoDB = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME, nameDB)
pathtoDB = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME,
                        '{}.h5'.format('temp'))
# os.path.join(studyparams.PATH_TO_TEST,nameDB)
db = celldatabase.load_hdf(pathtoDB)

outputDir = '/var/tmp/figuresdata/2019astrpi/output'

# # db = pd.read_hdf(dbPath, key='dataframe')
# db = celldatabase.load_hdf(dbPath)
# # db = db.query("subject=='pinp015'")
# # goodLaser = db.query('pulsePval<0.05 and pulseZscore>0 and trainRatio>0.8')
# # goodLaser = db[db['taggedCond']==0]
# goodISI = db.query('isiViolations<0.02 or modifiedISI<0.02')
# goodShape = goodISI.query('spikeShapeQuality > 2')
# goodLaser = goodShape.query("autoTagged==1 and subject != 'pinp018'")
# # goodLaser = goodShape.query("autoTagged==1 and subject != 'pinp018' and summaryPulseLatency < 0.01")
# goodNSpikes = goodLaser.query('nSpikes>2000')
# goodPulseLatency = goodNSpikes.query('summaryPulseLatency<0.006')
import numpy as np

from jaratoolbox import ephyscore
from jaratoolbox import celldatabase


db = celldatabase.load_hdf('/tmp/inactivation_cells.h5')
import time

ticTime = time.time()
anArray = np.empty(len(db))
for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
    cellObj = ephyscore.Cell(dbRow)
    
    try:
        laserEphysData, noBehav = cellObj.load('lasernoisebursts')
    except IndexError:
        pass
    
    anArray[indRow] = 0
db['trash'] = anArray
     
print 'Elapsed Time: ' + str(time.time()-ticTime)

ticTime = time.time()

for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
    cellObj = ephyscore.Cell(dbRow)
    
    try:
        laserEphysData, noBehav = cellObj.load('lasernoisebursts')
from jaratoolbox import ephyscore
from jaratoolbox import celldatabase


db = celldatabase.load_hdf('/home/jarauser/data/database/inactivation_cells2.h5')
import time

ticTime = time.time()
for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
    cellObj = ephyscore.Cell(dbRow)
    try:
        laserEphysData, noBehav = cellObj.load('laserPulse')
    except IndexError:
        pass
    try:
        bandEphysData, bandBehavData = cellObj.load('bandwidth')
    except IndexError:
        pass
        
print 'Elapsed Time: ' + str(time.time()-ticTime)

ticTime = time.time()

for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
    cellObj = ephyscore.Cell(dbRow)
    try:
        laserEphysData, noBehav = cellObj.load('laserPulse')
    except IndexError:
        pass
        
for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
        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',
            minBlockNum=minBlockNum,
            minTrialNumEndBlock=minTrialNumEndBlock,
            performanceThreshold=performanceThreshold)
        fullDb['metBehavCriteria'] = metBehavCriteria

        # -- check if cell depth is inside target region range -- #
        print 'Checking whether cell in target range'
        actualDepthEachCell, inTargetArea = inTargetRangeCheck.celldb_in_target_range_check(
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)
from jaratoolbox import spikesorting
from jaratoolbox import ephyscore
from jaratoolbox import extraplots
from jaratoolbox import colorpalette as cp
from jaratoolbox import settings
from scipy import stats
from scipy import signal

STUDY_NAME = '2018thstr'
SAVE=0

# dbPath = os.path.join(settings.FIGURES_DATA_PATH, STUDY_NAME, 'celldatabase_ALLCELLS_MODIFIED_CLU.h5')
# dbPath = os.path.join(settings.FIGURES_DATA_PATH, STUDY_NAME, 'celldatabase_ALLCELLS_MODIFIED_CLU_newtagged.h5')
dbPath = os.path.join(settings.FIGURES_DATA_PATH, STUDY_NAME, 'celldatabase_calculated_columns.h5')
# database = pd.read_hdf(dbPath, key='dataframe')
database = celldatabase.load_hdf(dbPath)

# dbPath = os.path.join(settings.FIGURES_DATA_PATH, STUDY_NAME, 'celldatabase_NBQX.h5')
# database = celldatabase.load_hdf(dbPath)

### Init new database columns ###
database['summaryPulsePval'] = 1 # Whether or not the cell responds to the laser pulse
database['summaryTrainResponses'] = np.nan # How many of the pulses in the train the cell responds to.
database['summaryTrainLatency'] = np.nan
database['summaryPulseLatency'] = np.nan

#DEBUG
import ipdb

#Neurons with 4 laser pulse responses that are not tagged
# database = database.loc[[136, 1034]]
        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)

Exemple #16
0
import os
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
import matplotlib.colors

from jaratoolbox import celldatabase
from jaratoolbox import extraplots
from jaratoolbox import settings

FONTSIZE = 14

dbFilename = os.path.join(settings.FIGURES_DATA_PATH, '2018acsup',
                          'photoidentification_cells.h5')
#db = celldatabase.load_hdf(dbFilename)
db = celldatabase.load_hdf('/tmp/photoidentification_cells_0.h5')
bestCells = db.query('isiViolations<0.02 or modifiedISI<0.02')
#bestCells = bestCells.query('spikeShapeQuality>2.5 and tuningFitR2>0.1 and octavesFromPrefFreq<0.3 and sustainedSoundResponsePVal<0.05')
bestCells = bestCells.query(
    'spikeShapeQuality>2.5 and tuningFitR2>0.1 and octavesFromPrefFreq<0.3 and onsetSoundResponsePVal<0.05'
)

LASER_RESPONSE_PVAL = 0.001  #want to be EXTRA sure not to include false positives

EXC_LASER_RESPONSE_PVAL = 0.5  #for selecting putative excitatory cells NOT responsive to laser
EXC_SPIKE_WIDTH = 0.0004

PV_CHR2_MICE = ['band004', 'band026', 'band032', 'band033']
SOM_CHR2_MICE = [
    'band005', 'band015', 'band016', 'band027', 'band028', 'band029',
    'band030', 'band031', 'band034', 'band037', 'band038', 'band044',
    #databaseFullPath = os.path.join(settings.DATABASE_PATH, '{}_database.h5'.format(mouseName))
    databaseFullPath = os.path.join(settings.DATABASE_PATH, NEW_DATABASE_FOLDER, '{}_database.h5'.format(mouseName))
    outFilename = '/var/tmp/{}_reward_change_modulation_{}.h5'.format(mouseName,processedDate)
    if os.path.isfile(outFilename):
        print 'Analysis for this mouse was saved before.'
        processed = pd.read_hdf(outFilename, key='reward_change')
        processedSessions = np.unique(processed['date'])
    else:
        processed = pd.DataFrame({'date':[]})
        processedSessions = []
    modulationDict = {'subject': [],
                      'date': [],
                      'tetrode': [],
                      'cluster': []}

    cellDb = celldatabase.load_hdf(databaseFullPath) #pd.read_hdf(databaseFullPath, key=dbKey)

    newTime=time.time(); print 'Elapsed time: {0:0.2f}  ALLCELLS'.format(newTime-zeroTime); zeroTime=newTime; sys.stdout.flush()  ### PROFILER

    # Process each mouse in chunks
    chunkSize = 20
    #chunks = [allcells.cellDB[ind:ind+chunkSize] for ind in range(0, len(allcells.cellDB), chunkSize)]
    chunks = [cellDb.iloc[ind:ind+chunkSize] for ind in range(0, len(cellDb), chunkSize)]

    for chunk in chunks:

        modulationDict = {'subject': [],
                          'date': [],
                          'tetrode': [],
                          'cluster': []}
Exemple #18
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')

Exemple #19
0
#         '''
        
        #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)





####################################################################################
####################################################################################
####################################################################################


# import os, sys
# from jaratoolbox import celldatabase
import os
import importlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from jaratoolbox import settings
from jaratoolbox import celldatabase
import studyparams
importlib.reload(studyparams)

#dbPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME)
dbPath = settings.FIGURES_DATA_PATH
dbFilename = os.path.join(dbPath,
                          'responsivedb_{}.h5'.format(studyparams.STUDY_NAME))
# -- Load the database of responsive cells --
responsivedb = celldatabase.load_hdf(dbFilename)

dbFilenamex = os.path.join(dbPath,
                           'celldb_{}.h5'.format(studyparams.STUDY_NAME))
# -- Load the database of cells --
celldb = celldatabase.load_hdf(dbFilenamex)


def comparing_odd_std_significance(responsivedb):
    """
    Counts how many of the responsive cells show a significant difference between oddball and standard firing rates for their most responsive frequency.

    Inputs:
        responsivedb: Cell database that contains only the cells that are sound responsive and that allows for loading of ephys and behavior data and calculated base stats and indices.

    Outputs:
Exemple #21
0
from jaratoolbox import ephyscore
from jaratoolbox import celldatabase

db = celldatabase.load_hdf(
    '/home/jarauser/data/database/inactivation_cells2.h5')
import time

ticTime = time.time()
for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
    cellObj = ephyscore.Cell(dbRow)
    try:
        laserEphysData, noBehav = cellObj.load('laserPulse')
    except IndexError:
        pass
    try:
        bandEphysData, bandBehavData = cellObj.load('bandwidth')
    except IndexError:
        pass

print 'Elapsed Time: ' + str(time.time() - ticTime)

ticTime = time.time()

for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
    cellObj = ephyscore.Cell(dbRow)
    try:
        laserEphysData, noBehav = cellObj.load('laserPulse')
    except IndexError:
        pass

for indRow, (dbIndex, dbRow) in enumerate(db.iterrows()):
import numpy as np
from jaratoolbox import celldatabase
from jaratoolbox import ephyscore
from jaratoolbox import spikesanalysis
import pandas as pd
import figparams
import matplotlib.pyplot as plt
from scipy import stats

db = celldatabase.load_hdf('/tmp/database_with_cf_onsetivity.h5')

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

goodFit = db.query('rsquaredFit > 0.04')

plt.clf()


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


def medline(ax, yval, midline, width, color='k', linewidth=3):
    start = midline - (width / 2)
    end = midline + (width / 2)
    ax.plot([start, end], [yval, yval], color=color, lw=linewidth)

Exemple #23
0
def medline(ax, yval, midline, width, color='k', linewidth=3):
    start = midline-(width/2)
    end = midline+(width/2)
    ax.plot([start, end], [yval, yval], color=color, lw=linewidth)
# ==========================parameters==========================================


FIGNAME = 'figure_frequency_tuning'

d1mice = studyparams.ASTR_D1_CHR2_MICE
nameDB = studyparams.DATABASE_NAME + '.h5'
pathtoDB = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME, nameDB)
# pathtoDB = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME, '{}.h5'.format('ttDBR2'))
# os.path.join(studyparams.PATH_TO_TEST,nameDB)
db = celldatabase.load_hdf(pathtoDB)
db = db.query(studyparams.TUNING_FILTER)
exampleDataPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME, FIGNAME, 'data_freq_tuning_examples.npz')

# =======================================================================

exData = np.load(exampleDataPath)  # npz data generated from generate_example_freq_tuning
np.random.seed(8)

D1 = db.query(studyparams.D1_CELLS)  # laser activation response
nD1 = db.query(studyparams.nD1_CELLS)  # no laser repsonse or laser inactivation response

PANELS = [1, 1, 1, 1, 1, 1, 1]  # Plot panel i if PANELS[i]==1

SAVE_FIGURE = 1
outputDir = figparams.FIGURE_OUTPUT_DIR
import os
import numpy as np
import copy

from jaratoolbox import celldatabase
from jaratoolbox import ephyscore
from jaratoolbox import spikesanalysis
from jaratoolbox import behavioranalysis
from jaratoolbox import settings

db = celldatabase.load_hdf(
    '/mnt/jarahubdata/figuresdata/2018acsup/photoidentification_cells.h5')
bestCells = db.query('isiViolations<0.02 or modifiedISI<0.02')
bestCells = bestCells.query(
    'spikeShapeQuality>2.5 and sustainedSoundResponsePVal<0.05')

LASER_RESPONSE_PVAL = 0.001  # want to be EXTRA sure not to include false positives

EXC_LASER_RESPONSE_PVAL = 0.5  # for selecting putative excitatory cells NOT responsive to laser
EXC_SPIKE_WIDTH = 0.0004

PV_CHR2_MICE = ['band004', 'band026', 'band032', 'band033']
SOM_CHR2_MICE = [
    'band005', 'band015', 'band016', 'band027', 'band028', 'band029',
    'band030', 'band031', 'band034', 'band037', 'band038', 'band044',
    'band045', 'band054', 'band059', 'band060'
]

PV_CELLS = bestCells.query(
    'laserPVal<{} and laserUStat>0 and subject=={}'.format(
        LASER_RESPONSE_PVAL, PV_CHR2_MICE))
from jaratoolbox import celldatabase
from jaratoolbox import spikesorting  # For clustering
from jaratoolbox import spikesanalysis
from jaratoolbox import settings
from jaratoolbox import behavioranalysis
# import database_generation_funcs as funcs
from jaratoolbox import ephyscore
import studyparams

reload(studyparams)

dbPath = os.path.join(settings.FIGURES_DATA_PATH, studyparams.STUDY_NAME)
dbFilename = os.path.join(dbPath,
                          'celldb_{}.h5'.format(studyparams.STUDY_NAME))
# -- Load the database of cells --
celldb = celldatabase.load_hdf(dbFilename)
'''
def calculate_pvalues(celldb):

    Generates p-Values between oddball and standard firing rates during sound presentation and between the high frequency sound 100ms before sound presentation and during sound presentation.

    Inputs:
        celldb: Database that allows for loading of ephys and behavior data.

'''

ratioFR = 1.01

timeRange = [-0.1, 0.3]  # seconds
binWidth = 0.01
responseRange = [0, 0.1]  # sec
Exemple #26
0
                 spikeTimesFromEventOnset=spikeTimesFromEventOnset,
                 trialIndexForEachSpike=trialIndexForEachSpike,
                 indexLimitsEachTrial=indexLimitsEachTrial,
                 timeRange=timeRange,
                 alignment=alignment)
        print 'Saved event-locked data to {0}'.format(newOutputFullPath)


sessiontype = 'behavior'  #2afc behavior
recalculate = False
timeRange = [
    -0.5, 0.5
]  # In seconds. Time range for to calculate spikeTimesFromEventOnset, this time window has to span all the possible count time ranges for generating spike count matrix

newOutputDir = '/home/languo/data/ephys/evlock_spktimes'
oldOutputDir = '/var/tmp/processed_data'

#dbKey = 'reward_change'
NEW_DATABASE_FOLDER = 'new_celldb'
databaseFullPath = os.path.join(settings.DATABASE_PATH, NEW_DATABASE_FOLDER,
                                'rc_database.h5')
cellDb = celldatabase.load_hdf(databaseFullPath)

alignments = ['sound', 'center-out', 'side-in']

for ind, cell in cellDb.iterrows():
    cellObj = ephyscore.Cell(cell)
    for alignment in alignments:
        save_evlock_spktimes_cell(cellObj, sessiontype, alignment, timeRange,
                                  recalculate, oldOutputDir, newOutputDir)
Exemple #27
0
import numpy as np
from jaratoolbox import celldatabase
from jaratoolbox import ephyscore
from jaratoolbox import spikesanalysis
import pandas as pd
import figparams #TODO: Remove this once we move plotting code to a figure file
import matplotlib.pyplot as plt
from scipy import stats

PLOT=1
SAVE = 1


dbPath = '/mnt/jarahubdata/figuresdata/2018thstr/celldatabase_calculated_columns.h5'
database = celldatabase.load_hdf(dbPath)

# goodISI = database.query('isiViolations<0.02 or modifiedISI<0.02')
# goodShape = goodISI.query('spikeShapeQuality > 2')
# goodLaser = goodShape.query("autoTagged==1 and subject != 'pinp018'")
# goodNSpikes = goodLaser.query('nSpikes>2000')

# db = goodNSpikes

plt.clf()
# ax0 = plt.subplot(121)
ax0 = plt.subplot(111)
# ax1 = plt.subplot(122)

mono = []
maxSpikes = []
Exemple #28
0
import numpy as np
import pandas as pd
from jaratoolbox import celldatabase
from matplotlib import pyplot as plt

databaseFn = '/tmp/database_with_pulse_responses.h5'
databaseNBQXFn = '/tmp/nbqx_database_with_pulse_responses.h5'

database = celldatabase.load_hdf(databaseFn)
databaseNBQX = celldatabase.load_hdf(databaseNBQXFn)

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


plt.clf()
fig = plt.gcf()
# ax = fig.add_subplot(111, projection='3d')
ax0 = fig.add_subplot(121)
ax1 = fig.add_subplot(122)
axes = [ax0, ax1]
for ax in axes:
    ax.hold(1)
# lasercells = database.query('isiViolations<0.02 and spikeShapeQuality>2 and autoTagged==1')
# allcells = database.query('isiViolations<0.02 and spikeShapeQuality>2 and summaryPulsePval<0.05')

#Accept as tagged only cells with latency less than 10ms
CASE=0