def analyse_ramp(directory): if directory[-1] == '/': directory = directory[:-1] h5_files = list_h5_files(directory, True) if len(h5_files) == 0: print('No H5 files in %s.' % directory) return None rheobase = np.zeros(len(h5_files)) try: Ke = np.loadtxt(directory + 'kernel.dat') except: Ke = None for i, file in enumerate(h5_files): entities, info = lcg.loadH5Trace(file) for ntt in entities: if ntt['name'] == 'Waveform': I = ntt['data'] elif ntt['name'] == 'AnalogInput': if Ke is None: V = ntt['data'] else: V = aec.compensate(ntt['data'], I, Ke) idx = extract_spikes(V) rheobase[i] = I[idx[0]] return (np.mean(rheobase), np.std(rheobase))
def analyse_ramp(directory): if directory[-1] == '/': directory = directory[:-1] h5_files = list_h5_files(directory,True) if len(h5_files) == 0: print('No H5 files in %s.' % directory) return None rheobase = np.zeros(len(h5_files)) try: Ke = np.loadtxt(directory + 'kernel.dat') except: Ke = None for i,file in enumerate(h5_files): entities,info = lcg.loadH5Trace(file) for ntt in entities: if ntt['name'] == 'Waveform': I = ntt['data'] elif ntt['name'] == 'AnalogInput': if Ke is None: V = ntt['data'] else: V = aec.compensate(ntt['data'],I,Ke) idx = extract_spikes(V) rheobase[i] = I[idx[0]] return (np.mean(rheobase),np.std(rheobase))
def compensateWithKernelOffline(ent,kernelFiles=[]): #TODO: Add check for compensation online!!! # names = np.array([e['name'] for e in ent]) units = np.array([e['units'] for e in ent]) findEntityByName = lambda entityName:np.where( names==entityName)[0] findEntityByUnit = lambda entityUnit:np.where( units==entityUnit)[0] if len(kernelFiles) > 0: # Active Electrode Compensation offline Iidx = findEntityByUnit('pA') Vidx = findEntityByUnit('mV') for ii,kfile in enumerate(kernelFiles): Ke = np.loadtxt(kfile) if ii >= len(Vidx): print( '''Specified too many kernel files, contact developers please.''') break if len(Vidx) == 1: #Then there is only one channel, sum the currents I = np.sum([ent[jj]['data'] for jj in Iidx], axis=0) else: I = Iidx[Iidx[ii]]['data'] V = ent[Vidx[ii]]['data'] if not 'metadata' in ent[Vidx[ii]].keys(): ent[Vidx[ii]]['data'] = aec.compensate(V, I, Ke*1e-9)
def compensateVoltage(ent, Ke, entNames=["AnalogInput", "Waveform"]): for e in ent: if e["name"] in [entNames[0]]: V = e["data"] if e["name"] in [entNames[1]]: I = e["data"] metadata = e["metadata"] V = aec.compensate(V, I, Ke) return V, I, metadata
def compensateVoltage(ent, Ke, entNames=['AnalogInput', 'Waveform']): for e in ent: if e['name'] in [entNames[0]]: V = e['data'] if e['name'] in [entNames[1]]: I = e['data'] metadata = e['metadata'] V = aec.compensate(V, I, Ke) return V, I, metadata
def analyse_last_file(): ''' Extracts the spiketrain statistics from the last file. ''' files = glob.glob('*.h5') files.sort() data_file = files[-1] ent, info = lcg.loadH5Trace(data_file) V = ent[1]['data'] I = ent[0]['data'] kernel_file = glob.glob('*.dat') kernel_file.sort() if len(kernel_file): kernel_file = kernel_file[-1] Ke = np.loadtxt(kernel_file) V = aec.compensate(V, I, Ke / 1.0e9) t = np.arange(0, len(V) - 1) * info['dt'] spks = lcg.findSpikes(t, V, thresh=-10) isi = np.diff(spks) return np.mean(isi), np.std(isi) / np.mean(isi), np.mean(V), np.std(V)
def analyse_last_file(): ''' Extracts the spiketrain statistics from the last file. ''' files = glob.glob('*.h5') files.sort() data_file = files[-1] ent,info = lcg.loadH5Trace(data_file) V = ent[1]['data'] I = ent[0]['data'] kernel_file = glob.glob('*.dat') kernel_file.sort() if len(kernel_file): kernel_file = kernel_file[-1] Ke = np.loadtxt(kernel_file) V = aec.compensate(V,I,Ke/1.0e9) t = np.arange(0,len(V)-1)*info['dt'] spks = lcg.findSpikes(t, V, thresh=-10) isi = np.diff(spks) return np.mean(isi), np.std(isi)/np.mean(isi),np.mean(V),np.std(V)
def computeElectrodeKernel(filename, Kdur=5e-3, interval=[], saveFile=True, fullOutput=False): import matplotlib.pyplot as p import aec p.ion() entities,info = loadH5Trace(filename) Ksize = int(Kdur/info['dt']) Kt = np.arange(0,Kdur,info['dt'])[:Ksize] for ntt in entities: if 'metadata' in ntt and ntt['units'] == 'pA': I = ntt['data'] stimtimes = np.cumsum(ntt['metadata'][:,0]) pulse = np.nonzero(ntt['metadata'][:,0] == 0.01)[0][0] if len(interval) != 2: # Find the gaussian noise idx = np.where(ntt['metadata'][:,1] == 11)[0][0] interval = stimtimes[idx-1:idx+1] elif (ntt['name'] == 'AnalogInput' or ntt['name'] == 'InputChannel') and ntt['units'] == 'mV': V = ntt['data'] t = np.arange(len(V)) * info['dt'] idx = np.intersect1d(np.nonzero(t >= interval[0])[0], np.nonzero(t <= interval[1])[0]) Vn = V[idx] V[-Ksize:] = 0 In = I[idx] K,V0 = aec.full_kernel(Vn,In,Ksize,True) # Kernels plot fig = p.figure(1,figsize=[5,5],facecolor='w',edgecolor=None) p.plot(Kt*1e3,K*1e3,'k.-',label='Full') p.xlabel('t (ms)') p.ylabel('R (MOhm)') while True: try: startTail = int(raw_input('Enter index of tail start (1 ms = %d samples): ' % int(1e-3/info['dt']))) if startTail >= len(K): print('The index must be smaller than %d.' % len(K)) continue except: continue Ke,Km = aec.electrode_kernel(K,startTail,True) print('R = %g MOhm.\nV0 = %g mV.' % (np.sum(Ke*1e3),V0)) # Print kernels and compensated traces p.close() fig = p.figure(1,figsize=[10,5],facecolor='w',edgecolor=None) plt = [] plt.append(fig.add_axes([0.1,0.1,0.3,0.8])) plt[0].plot(Kt*1e3,K*1e3,'k.-',label='Full') plt[0].set_xlabel('t (ms)') plt[0].set_ylabel('R (MOhm)') plt.append(fig.add_axes([0.5,0.1,0.4,0.8])) plt[0].plot([Kt[0]*1e3,Kt[-1]*1e3],[0,0],'g') plt[0].plot(Kt*1e3,Km*1e3,'b',label='Membrane') plt[0].plot(Kt[:len(Ke)]*1e3,Ke*1e3,'r',label='Electrode') plt[0].set_xlabel('t (ms)') plt[0].set_ylabel('R (MOhm)') plt[0].legend(loc='best') # AEC offline ndx = np.intersect1d(np.nonzero(t > max(stimtimes[pulse]-20e-3,0))[0], np.nonzero(t < min(stimtimes[pulse]+80e-3,t[-1]))[0]) Vc = aec.compensate(V[ndx],I[ndx],Ke) plt[1].plot(t[ndx]-t[ndx[0]], V[ndx], 'k', label='Recorded') plt[1].plot(t[ndx]-t[ndx[0]], Vc, 'r', label='Compensated') plt[1].set_xlabel('t (ms)') plt[1].set_ylabel('V (mV)') plt[1].legend(loc='best') fig.show() # Ask user what to do ok = raw_input('Ke[-1] / max(Ke) = %.2f %%. Ok? [Y/n] ' % (Ke[-1]/np.max(Ke)*100)) if len(ok) == 0 or ok.lower() == 'y' or ok.lower() == 'yes': break p.close() if saveFile: np.savetxt(filename[:-3] + '_kernel.dat', Ke*1e9, '%.10e') if fullOutput: return Ke*1e3,V0,data.values()[0][idx],Vm else: return Ke*1e3
def computeElectrodeKernel(filename, Kdur=5e-3, interval=[], saveFile=True, fullOutput=False): import matplotlib.pyplot as p import aec p.ion() entities, info = loadH5Trace(filename) Ksize = int(Kdur / info['dt']) Kt = np.arange(0, Kdur, info['dt'])[:Ksize] for ntt in entities: if 'metadata' in ntt and ntt['units'] == 'pA': I = ntt['data'] stimtimes = np.cumsum(ntt['metadata'][:, 0]) pulse = np.nonzero(ntt['metadata'][:, 0] == 0.01)[0][0] if len(interval) != 2: # Find the gaussian noise idx = np.where(ntt['metadata'][:, 1] == 11)[0][0] interval = stimtimes[idx - 1:idx + 1] elif (ntt['name'] == 'AnalogInput' or ntt['name'] == 'InputChannel') and ntt['units'] == 'mV': V = ntt['data'] t = np.arange(len(V)) * info['dt'] idx = np.intersect1d( np.nonzero(t >= interval[0])[0], np.nonzero(t <= interval[1])[0]) Vn = V[idx] V[-Ksize:] = 0 In = I[idx] K, V0 = aec.full_kernel(Vn, In, Ksize, True) # Kernels plot fig = p.figure(1, figsize=[5, 5], facecolor='w', edgecolor=None) p.plot(Kt * 1e3, K * 1e3, 'k.-', label='Full') p.xlabel('t (ms)') p.ylabel('R (MOhm)') while True: try: startTail = int( raw_input('Enter index of tail start (1 ms = %d samples): ' % int(1e-3 / info['dt']))) if startTail >= len(K): print('The index must be smaller than %d.' % len(K)) continue except: continue Ke, Km = aec.electrode_kernel(K, startTail, True) print('R = %g MOhm.\nV0 = %g mV.' % (np.sum(Ke * 1e3), V0)) # Print kernels and compensated traces p.close() fig = p.figure(1, figsize=[10, 5], facecolor='w', edgecolor=None) plt = [] plt.append(fig.add_axes([0.1, 0.1, 0.3, 0.8])) plt[0].plot(Kt * 1e3, K * 1e3, 'k.-', label='Full') plt[0].set_xlabel('t (ms)') plt[0].set_ylabel('R (MOhm)') plt.append(fig.add_axes([0.5, 0.1, 0.4, 0.8])) plt[0].plot([Kt[0] * 1e3, Kt[-1] * 1e3], [0, 0], 'g') plt[0].plot(Kt * 1e3, Km * 1e3, 'b', label='Membrane') plt[0].plot(Kt[:len(Ke)] * 1e3, Ke * 1e3, 'r', label='Electrode') plt[0].set_xlabel('t (ms)') plt[0].set_ylabel('R (MOhm)') plt[0].legend(loc='best') # AEC offline ndx = np.intersect1d( np.nonzero(t > max(stimtimes[pulse] - 20e-3, 0))[0], np.nonzero(t < min(stimtimes[pulse] + 80e-3, t[-1]))[0]) Vc = aec.compensate(V[ndx], I[ndx], Ke) plt[1].plot(t[ndx] - t[ndx[0]], V[ndx], 'k', label='Recorded') plt[1].plot(t[ndx] - t[ndx[0]], Vc, 'r', label='Compensated') plt[1].set_xlabel('t (ms)') plt[1].set_ylabel('V (mV)') plt[1].legend(loc='best') fig.show() # Ask user what to do ok = raw_input('Ke[-1] / max(Ke) = %.2f %%. Ok? [Y/n] ' % (Ke[-1] / np.max(Ke) * 100)) if len(ok) == 0 or ok.lower() == 'y' or ok.lower() == 'yes': break p.close() if saveFile: np.savetxt(filename[:-3] + '_kernel.dat', Ke * 1e9, '%.10e') if fullOutput: return Ke * 1e3, V0, data.values()[0][idx], Vm else: return Ke * 1e3