Пример #1
0
def analyzeABF(abf):
    abf = swhlab.ABF(abf)
    EPSCs = []
    IPSCs = []
    Xs = np.arange(abf.sweeps) * float(abf.sweepLength) / 60.0
    for sweep in range(abf.sweeps):
        print("analyzing sweep %d of %d" % (sweep + 1, abf.sweeps))
        plotToo = False
        if 0 < Xs[sweep] < 120:
            plotToo = r'C:\Users\swharden\Documents\temp'
        [hitsPos, hitsNeg] = analyzeSweep(abf,
                                          sweep=sweep,
                                          m1=.3,
                                          plotToo=plotToo)
        EPSCs.append(hitsPos / (float(abf.sweepLength) - .3))
        IPSCs.append(hitsNeg / (float(abf.sweepLength) - .3))
    EPSCsmooth = np.convolve(EPSCs, kernel_gaussian(20), mode='same')
    IPSCsmooth = np.convolve(IPSCs, kernel_gaussian(20), mode='same')
    plt.figure(figsize=(10, 5))
    plt.grid()
    plt.plot(Xs, EPSCsmooth, '.', color='r', label="EPSCs", ms=10, alpha=.5)
    plt.plot(Xs, IPSCsmooth, '.', color='b', label="IPSCs", ms=10, alpha=.5)
    plt.axhline(0, color='k', lw=2)
    plt.legend()
    for t in abf.comment_times:
        plt.axvline(t / 60, color='k', lw=2, ls='--', alpha=.5)
    plt.margins(0, .1)
    plt.ylabel("event frequency (Hz)")
    plt.xlabel("time (minutes)")
    plt.show()

    indexPics(r'C:\Users\swharden\Documents\temp')
Пример #2
0
 def test_0021_detectAndPlot2(self):
     abf = swhlab.ABF(testAbfPath)
     APs = swhlab.AP(abf)
     APs.detect()
     Xs = APs.get_bySweep("times")
     Ys = APs.get_bySweep("freqs")
     plt.figure(figsize=(10, 8))
     for sweep in range(abf.sweeps):
         if not len(Ys[sweep]):
             continue
         abf.setsweep(sweep)
         plt.subplot(212)
         plt.plot(Xs[sweep][1:],
                  Ys[sweep],
                  '.',
                  alpha=.5,
                  label="sweep %d" % sweep,
                  ms=10)
         plt.subplot(211)
         plt.plot(abf.sweepX2, abf.sweepY, alpha=.5)
     plt.ylabel(abf.units2)
     plt.subplot(212)
     plt.grid(alpha=.5)
     plt.legend(loc=3)
     plt.margins(.1, .1)
     plt.ylabel("frequency (Hz)")
     plt.xlabel("time (seconds)")
     plt.tight_layout()
     plt.savefig('./output/APsFreqs.jpg')
     plt.close('all')
Пример #3
0
def inspectABF(abf=exampleABF,saveToo=False,justPlot=False):
    """May be given an ABF object or filename."""
    pylab.close('all')
    print(" ~~ inspectABF()")
    if type(abf) is str:
        abf=swhlab.ABF(abf)
    swhlab.plot.new(abf,forceNewFigure=True)
    if abf.sweepInterval*abf.sweeps<60*5: #shorter than 5 minutes
        pylab.subplot(211)
        pylab.title("%s [%s]"%(abf.ID,abf.protoComment))
        swhlab.plot.sweep(abf,'all')
        pylab.subplot(212)
        swhlab.plot.sweep(abf,'all',continuous=True)
        swhlab.plot.comments(abf)
    else:
        print(" -- plotting as long recording")
        swhlab.plot.sweep(abf,'all',continuous=True,minutes=True)
        swhlab.plot.comments(abf,minutes=True)
        pylab.title("%s [%s]"%(abf.ID,abf.protoComment))
    swhlab.plot.annotate(abf)
    if justPlot:
        return
    if saveToo:
        path=os.path.split(abf.fname)[0]
        basename=os.path.basename(abf.fname)
        pylab.savefig(os.path.join(path,"_"+basename.replace(".abf",".png")))
    pylab.show()
    return
Пример #4
0
def test():
    abf = swhlab.ABF(r'C:\Apps\pythonModules\abfs\16711016.abf')
    swhlab.ap.detect(abf)
    swhlab.ap.check_AP_phase(abf)
    Xs = swhlab.common.matrixValues(abf.APs, "expT")
    Ys = swhlab.common.matrixValues(abf.APs, "downslope")
    cm.show(True)
    pylab.plot(Xs, Ys, '.')
    cm.show(True)
Пример #5
0
 def test_0020_sweepManipulation(self):
     """try setting sweeps to different use cases."""
     abf = swhlab.ABF(testAbfPath)
     abf.setsweep(0)
     abf.setsweep(1)
     abf.setsweep(-1)  # negative numbers start from end
     assert (abf.sweep == abf.sweeps - 1)
     abf.setsweep('-1')  # should convert to 1
     abf.setsweep('-lolz')  # should fail
Пример #6
0
 def test_0030_average(self):
     """test abf.average()"""
     abf = swhlab.ABF(testAbfPath)
     abf.average()
     abf.average(.2, .3)
     abf.average(setsweep=-1)
     assert abf.sweep == abf.sweeps - 1
     abf.average(999, 999)
     abf.average(888, 999)
     abf.average(0, 999)
     abf.average(-999, 999)
     abf.average(-999, 0)
Пример #7
0
def autoABF(fname):
    pylab.close('all') #clean slate for good measure
    abf=swhlab.ABF(fname) #load the ABF
    abf.deleteStuff()
    method=abf.protoComment.replace("-","_")
    method="proto_"+method
    print(" ~~",method+"()")
    try:
        if method in globals():
            globals()[method](abf)
        else:
            print(" ~~","proto_unknown()")
            proto_unknown(abf)
    except:
        print(traceback.format_exc())
        print("~"*50,"CRASHED","~"*50)
    return abf.ID
Пример #8
0
    def test_0020_detectAndPlot1(self):
        abf = swhlab.ABF(testAbfPath)
        APs = swhlab.AP(abf)
        APs.detect()

        plt.figure()
        #freqs firsts times
        for feature in "count average median".split(" "):
            plt.plot(np.arange(abf.sweeps),
                     APs.get_bySweep(feature),
                     '.-',
                     label=feature,
                     alpha=.5)
        plt.legend(loc=2)
        plt.margins(.1, .1)
        plt.xlabel("sweep")
        plt.savefig('./output/APs1.jpg')
        plt.close('all')
Пример #9
0
def analyzeABF(abf):
    abf = swhlab.ABF(abf)
    data = []
    Xs = np.arange(abf.sweeps) * float(abf.sweepLength) / 60.0
    for sweep in range(abf.sweeps):
        print("analyzing sweep %d of %d" % (sweep + 1, abf.sweeps))
        data.append(analyzeSweep(abf, sweep=sweep, m1=.3))
    plt.figure(figsize=(10, 5))
    plt.grid()
    plt.plot(Xs[:len(data)], data, '.', alpha=.5, ms=10)
    plt.axhline(0, color='k', lw=1, ls='--')
    for t in abf.comment_times:
        plt.axvline(t / 60, color='k', lw=2, ls='--', alpha=.5)
    plt.margins(0, .1)
    plt.xlabel("time (minutes)")
    plt.ylabel("excitatory balance")
    plt.show()

    indexPics(r'C:\Users\swharden\Documents\temp')
Пример #10
0
    def test_0005_plotWithOnlyCore(self):
        abf = swhlab.ABF(testAbfPath)
        abf.derivative = True
        abf.setsweep(-1)  # required to do again

        plt.figure(figsize=(10, 10))

        plt.subplot(211)
        plt.grid(alpha=.5)
        plt.title("plotting directly from swhlab.ABF")
        plt.ylabel(abf.units2)
        plt.plot(abf.sweepX, abf.sweepY, alpha=.5)

        plt.subplot(212)
        plt.grid(alpha=.5)
        plt.ylabel(abf.unitsD2)
        plt.xlabel("time (sec)")
        plt.plot(abf.sweepX, abf.sweepD, alpha=.5)

        plt.tight_layout()
        plt.savefig('./output/raw.png')
        plt.close('all')
Пример #11
0
 def test_0040_averageSweep(self):
     """test abf.averageSweep()"""
     abf = swhlab.ABF(testAbfPath)
     abf.averageSweep()
     abf.averageSweep(0, 1)
Пример #12
0
        plt.subplot(212)
        plt.plot(Xs, chunks[sweep] + 100 * sweep, alpha=.5, color='b', lw=2)

    plt.subplot(211)
    plt.plot(Xs, np.average(chunks, axis=0), alpha=.5, lw=2)
    plt.title("%s.abf - BLS - average of %d sweeps" % (abf.ID, abf.sweeps))
    plt.ylabel(abf.units2)
    plt.axvspan(T1, T2, alpha=.1, color='y', lw=0)
    plt.axis([X1, X2, None, None])

    plt.subplot(212)
    plt.xlabel("time (sec)")
    plt.ylabel("stacked sweeps")
    plt.axvspan(T1, T2, alpha=.1, color='y', lw=0)
    plt.axis([X1, X2, None, None])

    plt.tight_layout()
    plt.show()
    plt.close('all')


if __name__ == "__main__":

    fname = r"X:\Data\2P01\2016\2017-01-09 AT1\17109009.abf"  #0501
    #    fname=r"X:\Data\2P01\2016\2017-01-09 AT1\"17109013.abf"#0502
    abf = swhlab.ABF(fname)

    if abf.protocomment in ['0501', '0502']:
        BLS_average(abf)

    print("DONE")
Пример #13
0
def test2():
    """current clamp MT."""
    abf = swhlab.ABF(r'C:\Apps\pythonModules\abfs\16701006.abf')
    swhlab.memtest.memtest(abf)  #performs memtest on all sweeps
    swhlab.memtest.checkSweep(abf)  #lets you eyeball check how it did
    pylab.show()
Пример #14
0
        plt.axhline(0, color='k', lw=2, alpha=.5)

        plt.tight_layout()
        plt.show()

    # TEST GAUSS
    hist, bin_edges = np.histogram(Yflat, density=True, bins=200)
    peakPa = bin_edges[np.where(hist == max(hist))[0][0] + 1]

    if plotToo:
        plt.figure()
        plt.grid()
        plt.plot(bin_edges[1:], hist, alpha=.5)
        plt.axvline(0, color='k')
        plt.axvline(peakPa, color='r', ls='--', lw=2, alpha=.5)
        plt.semilogy()
        plt.title("sweep data distribution")
        plt.ylabel("power")
        plt.xlabel("pA deviation")
        plt.show()

    return peakPa


if __name__ == "__main__":
    #analyzeABF(r"X:\Data\2P01\2016\2016-09-01 PIR TGOT\16d07022.abf")
    abf = swhlab.ABF(r"X:\Data\2P01\2016\2016-09-01 PIR TGOT\16d07022.abf")
    analyzeSweep(abf, sweep=174, m1=.3, plotToo=True)
    analyzeSweep(abf, sweep=199, m1=.3, plotToo=True)
    print("DONE")
Пример #15
0
 def test_0010_loadAndClose(self):
     """just load an ABF"""
     abf = swhlab.ABF(testAbfPath)
     assert (len(abf.sweepY))  # ensure we have data
Пример #16
0

def atf_empty(lengthSec=5, rate=20000):
    data = np.empty((rate * lengthSec, 2))
    data[:, 0] = np.arange(len(data)) / rate
    return data


def atf_sine(lengthSec=5, rate=20000):
    data = np.empty((rate * lengthSec, 2))
    data[:, 0] = np.arange(len(data)) / rate
    data[:, 1] = np.sin((data[:, 0]) * np.pi * 2)  #1hz sine wave
    return data


def atf_ipsc():
    data = atf_sine()
    pylab.plot(data[:, 0], data[:, 1])
    pylab.show()
    fname = "%d.atf" % time.time()
    np.savetxt(fname,
               data,
               fmt="%.6f",
               header=atf_header,
               comments='',
               delimiter='\t')


if __name__ == "__main__":
    abf = swhlab.ABF('../abfs/group/16701010.abf')
    print("DONE")
Пример #17
0
def processAbf(abfFname,saveAs=False,dpi=100,show=True):
    """
    automatically generate a single representative image for an ABF.
    If saveAs is given (full path of a jpg of png file), the image will be saved.
    Otherwise, the image will pop up in a matplotlib window.
    """
    if not type(abfFname) is str or not len(abfFname)>3:
        return
    abf=swhlab.ABF(abfFname)
    plot=swhlab.plotting.ABFplot(abf)
    plot.figure_height=6
    plot.figure_width=10
    plot.subplot=False
    plot.figure(True)
    if abf.get_protocol_sequence(0)==abf.get_protocol_sequence(1) or abf.sweeps<2:
        # same protocol every time
        if abf.lengthMinutes<2:
            # short (probably a memtest or tau)
            ax1=plt.subplot(211)
            plot.figure_sweeps()
            plt.title("{} ({} sweeps)".format(abf.ID,abf.sweeps))
            plt.gca().get_xaxis().set_visible(False)
            plt.subplot(212,sharex=ax1)
            plot.figure_protocol()
            plt.title("")
        else:
            # long (probably a drug experiment)
            plot.figure_chronological()
    else:
        # protocol changes every sweep
        plots=[211,212] # assume we want 2 images
        if abf.units=='mV': # maybe it's something with APs?
            ap=swhlab.AP(abf) # go ahead and do AP detection
            ap.detect() # try to detect APs
            if len(ap.APs): # if we found some
                plots=[221,223,222,224] # get ready for 4 images
        ax1=plt.subplot(plots[0])
        plot.figure_sweeps()
        plt.title("{} ({} sweeps)".format(abf.ID,abf.sweeps))
        plt.gca().get_xaxis().set_visible(False)
        plt.subplot(plots[1],sharex=ax1)
        plot.figure_protocols()
        plt.title("protocol")
        if len(plots)>2:
            # assume we want to look at the first AP
            ax2=plt.subplot(plots[2])
            plot.rainbow=False
            plot.kwargs["color"]='b'
            plot.figure_chronological()
            plt.gca().get_xaxis().set_visible(False)
            plt.title("first AP magnitude")
            # velocity plot
            plt.subplot(plots[3],sharex=ax2)
            plot.abf.derivative=True
            plot.rainbow=False
            plot.traceColor='r'
            plot.figure_chronological()
            plt.axis([ap.APs[0]["T"]-.05,ap.APs[0]["T"]+.05,None,None])
            plt.title("first AP velocity")

    if saveAs:
        print("saving",os.path.abspath(saveAs))
        plt.savefig(os.path.abspath(saveAs),dpi=dpi)
        return
    if show:
        plot.show()
Пример #18
0
import os
import swhlab
import matplotlib.pyplot as plt
import numpy as np
import pickle

if __name__ == "__main__":
    abfPath = r"X:\Data\2P01\2016\2016-09-01 PIR TGOT"
    abfFile = os.path.join(abfPath, "16d14060.abf")
    abf = swhlab.ABF(abfFile)
    Xs = np.arange(abf.sweeps) * abf.sweepLength

    # figure out if we should calculate or load phasic data
    phasicFname = abf.filename + ".phasic.npy"
    if not os.path.exists(phasicFname):
        data = np.empty((abf.sweeps, 3))
        for sweep in abf.setsweeps():
            print("analyzing sweep %d of %d" % (sweep, abf.sweeps))
            data[sweep] = abf.phasic(returnSeparated=True)
        np.save(phasicFname, data)
    else:
        data = np.load(phasicFname)

    # plot the result with comments
    plt.figure(figsize=(5, 5))

    plt.subplot(211)
    plt.plot(Xs, data[:, 2], '.', color='k', alpha=.1)
    plt.plot(Xs,
             swhlab.common.lowpass(data[:, 2], 40),
             '-',
Пример #19
0
 def test_0050_derivative(self):
     abf = swhlab.ABF(testAbfPath)
     abf.derivative = True
     abf.setsweep(1)
     assert len(abf.sweepD) > 100
Пример #20
0
    downslopes=cm.dictVals(cm.dictFlat(abf.APs),"downslope")
    print("Found %d APs."%len(downslopes))
    print("Avg downslope: %.02f V/S"%np.average(downslopes))
    pylab.show()
"""

import os
import sys
import pylab
import time
import numpy as np
import traceback

import swhlab
import swhlab.core.common as cm
exampleABF = swhlab.ABF()

#TODO: this dictionary thing isn't a good idea long term. Can we make it NPY?


def detect(abf, sweep=None, threshold_upslope=50, dT=.1, saveToo=True):
    """
    An AP will be detected by a upslope that exceeds 50V/s. Analyzed too.
        if type(sweep) is int, graph int(sweep)
        if sweep==None, process all sweeps sweep.
    """
    if type(sweep) is int:
        sweeps = [sweep]
    else:
        sweeps = list(range(abf.sweeps))
    timeStart = time.clock()
Пример #21
0
"""Same as 01.py, but reports speed"""

import os
import sys
if not os.path.abspath('../../../') in sys.path:
    sys.path.append('../../../')
import swhlab
import matplotlib.pyplot as plt
import numpy as np
import time

if __name__=="__main__":
    abfFile=R"X:\Data\DIC1\2013\08-2013\08-16-2013-DP\13816004.abf"
    abf=swhlab.ABF(abfFile) # defaults to sweep 0
    print("analyzing %d sweeps (%.02f sec each)"%(abf.sweeps,abf.sweepLength))
    times=[]
    for sweep in abf.setsweeps():
        t1=time.clock()
        baseFrequency=60 # frequency (Hz) to silence
        FFT=np.fft.fft(abf.sweepY) # frequency data (i/j vectors starting at 0Hz)
        for i in range(50): # first 50 odd harmonics
            I=int(baseFrequency*i+baseFrequency*len(abf.sweepY)/abf.pointsPerSec)
            FFT[I],FFT[-I]=0,0 # remember to silence from both ends of the FFT
        Ys2=np.fft.ifft(FFT) # all done
        times.append(time.clock()-t1)
    times=np.array(times)*1000 # now in ms
    print("analysis took %.02f +/- %.02f ms per sweep"%(np.average(times),np.std(times)))

# analyzing 60 sweeps (5.00 sec each)
# analysis took 6.47 +/- 1.71 ms per sweep
Пример #22
0
import swhlab
import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__":

    abfFname = r'X:\Data\2P01\2016\2016-09-01 PIR TGOT\16831057.abf'
    abf = swhlab.ABF(abfFname)  # now it's a swhlab ABF object
    abf.setsweep(267)  # set the sweep

    # calculate membrane resistance
    baseline = abf.average(.5) / (10**12)  # A
    baselineMT = abf.average(.05, .15) / (10**12)  # A
    dV = 10 / (10**3)  # 10 mV
    dI = abs(baselineMT - baseline)  # A
    Rm = (dV / dI)  # R=E/I MOhm

    # cut off the first half-second (which has memtest)
    Y = abf.sweepY[int(abf.pointsPerSec * .5):] / (10**12)  #A
    X = abf.sweepX2[int(abf.pointsPerSec * .5):]

    # create a new figure
    plt.figure(figsize=(10, 8))

    # design the top plot
    plt.subplot(311)
    plt.grid()
    plt.title("voltage clamp (Rm = %.02f MOhm)" % (Rm / (10**6)))
    plt.ylabel("current (pA)")
    plt.plot(X, Y * (10**12), color='r', alpha=.8)  # plot the data
    plt.axhline(baseline * (10**12), color='k', lw=3, ls='--', alpha=.5)
Пример #23
0
    plt.xlabel("time (sec)")
    plt.margins(0, .1)

    # make the phase plot
    plt.subplot(133)
    plt.grid(alpha=.5)
    plt.plot(Y, dY, alpha=.5, lw=.5, color=cm(abf.sweep / abf.sweeps))
    plt.title("phase plot")
    plt.ylabel("dV (mV/ms)")
    plt.xlabel("V (mV)")
    plt.margins(.1, .1)

    # tighten up the figure
    plt.tight_layout()


if __name__ == "__main__":
    #abf=swhlab.ABF(R"X:\Data\SCOTT\2017-07-13 OXT-Tom OneOff\17713015.abf")
    abf = swhlab.ABF(R"X:\Data\SCOTT\2017-07-13 OXT-Tom OneOff\17713013.abf")

    plt.figure(figsize=(12, 4))
    for sweep in abf.setsweeps():
        #if sweep<abf.sweeps-1: continue
        print("plotting sweep", sweep, '...')
        plt.set_cmap('winter')
        drawPhasePlot(abf)
    plt.savefig("phase_%s.png" % abf.ID, dpi=300)
    plt.show()

    plt.close('all')
    print("DONE")
Пример #24
0
 def test_0010_detect(self):
     abf = swhlab.ABF(testAbfPath)
     APs = swhlab.AP(abf)
     APs.detect()
     assert len(APs.APs)
Пример #25
0
This module is somewhat experiment specific.
The idea is to match protocols (defined by comments) to analysis routines.
This is specific to Scott's current set of experiments.
Probably this doesn't belong in the module at all...
"""
import os
import sys
import glob
import pylab
import numpy as np
import traceback

import swhlab
from swhlab.core import common as cm #shorthand
from swhlab.indexing.indexing import genIndex
exampleABF=swhlab.ABF(None) #this helps for IDE recommendations

def standard_inspect(abf=exampleABF):
    cm.inspectABF(abf,justPlot=True)
    swhlab.plot.save(abf,tag="00-inspect")

def standard_overlayWithAverage(abf=exampleABF):
    swhlab.plot.sweep(abf,'all',alpha=.1)
    Xs,Av,Es=abf.average_sweep()
    pylab.plot(Xs,Av,'k')
    pylab.title("average (n=%d)"%abf.sweeps)
    swhlab.plot.save(abf,tag='overlay')

def standard_groupingForInj(abf,target=200):
    for i in range(abf.sweeps):
        abf.setSweep(i)
Пример #26
0
def test():
    abf = swhlab.ABF('../abfs/aps.abf')
    swhlab.plot.sweep(abf, 8, newFigure=True)  #single sweep
    swhlab.plot.sweep(abf, 'all', newFigure=True)  #overlay
    swhlab.plot.sweep(abf, 'all', newFigure=True, continuous=True)  #continuous
    swhlab.plot.sweep(abf, 'all', newFigure=True, offsetY=100)  #stacked
Пример #27
0
import os
import swhlab
import matplotlib.pyplot as plt

if __name__ == "__main__":
    abfPath = r"X:\Data\2P01\2016\2016-09-01 PIR TGOT"
    abf1 = swhlab.ABF(os.path.join(abfPath, "16d14010.abf"))
    abf2 = swhlab.ABF(os.path.join(abfPath, "16d14011.abf"))
    I1, I2 = abf1.pointsPerSec * .5, abf1.sweepLength
    abf1.kernel = abf1.kernel_gaussian(50, True)
    abf2.kernel = abf2.kernel_gaussian(50, True)
    plt.figure(figsize=(10, 3))

    for i in range(5, 10):
        abf1.setsweep(abf1.sweeps - i)
        abf2.setsweep(i)
        Xoffset = abf1.sweepLength * i
        Y1 = abf1.sweepYfiltered()[I1:]
        Y2 = abf2.sweepYfiltered()[I1:]
        Y1 = Y1 - Y1[0]
        Y2 = Y2 - Y2[0]
        plt.plot(abf1.sweepX2[I1:] + .1 * i, Y1 + 30 * i, alpha=.5, color='b')
        plt.plot(abf2.sweepX2[I1:] + 1.5 + .1 * i,
                 Y2 + 30 * i,
                 alpha=.5,
                 color='g')
    plt.margins()
    plt.title("5 sweeps at -70 vs -50")
    plt.show()

    print("DONE")
Пример #28
0
import sys
sys.path.insert(0, '../')  # force us to import ../swhlab/ module
import swhlab
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    abf = swhlab.ABF(R"X:\Data\2P01\10.7.0.3_modelCell\17217003.abf")
    plt.plot(abf.sweepX2, abf.sweepY)
    plt.show()

    print(steps)