def run_recs_fifos(pardir, outdir, ttime, recnames, fifonames, fifo_trange): stimpar = jspar.load(os.path.join(pardir, 'stimpar.json')) cellpar = jspar.load(os.path.join(pardir, 'cellpar.json')) spkthr = jspar.load(os.path.join(pardir, 'spkthr.json')) if not os.path.exists(outdir): os.makedirs(outdir) simfunc = run.cellpar_wrapper(run.simulation, cellpar, spkthr) spkt, stimvec, recs, fifos = simfunc(ttime, 'OU', stimpar, None, retstim=True, recnames=recnames, fifonames=fifonames, fifo_trange=fifo_trange) with open(os.path.join(outdir, 'fifos.p'), 'w') as f: pickle.dump(fifos, f) with open(os.path.join(outdir, 'recs.p'), 'w') as f: pickle.dump(recs, f) runpar = dict(hpar=run.hpar, ttime=ttime, stimpar=stimpar, cellpar=cellpar, spkthr=spkthr, fifo_trange=fifo_trange) rpfile = os.path.join(outdir, 'runpar.json') jspar.save(rpfile, runpar)
def run_fifos(ttime, pardir, outdir): stimpar = jspar.load(os.path.join(pardir, 'stimpar.json')) cellpar = jspar.load(os.path.join(pardir, 'cellpar.json')) spkthr = jspar.load(os.path.join(pardir, 'spkthr.json')) if not os.path.exists(outdir): os.makedirs(outdir) simfunc = run.cellpar_wrapper(run.simulation, cellpar, spkthr) fifonames = ('time', 'v_ais', 'icap_ais', 'nav_m') fifo_trange = (-40, 0) spkt, fifos = simfunc(ttime, 'OU', stimpar, None, fifonames=fifonames, fifo_trange=fifo_trange) pff.save_fifos(fifos, outdir) runpar = dict(hpar=run.hpar, ttime=ttime, stimpar=stimpar, cellpar=cellpar, spkthr=spkthr, fifo_trange=fifo_trange) rpfile = os.path.join(outdir, 'runpar.json') jspar.save(rpfile, runpar)
def plot_align_thresh(indir, yname, trajcolor, thrcolor, label='', tname='time'): with open(os.path.join(indir, 'fifos.p')) as f: fifos = pickle.load(f) runpar = jspar.load(os.path.join(indir, 'runpar.json')) fifo_trange = runpar['fifo_trange'] dt = runpar['hpar']['dt'] trng = (-20, -0.05) taname = 'time_align' fifos[taname] = np.tile(np.arange(fifo_trange[0], fifo_trange[1], dt), (len(fifos[yname]), 1)) plot_traj(fifos, taname, yname, trng, fifo_trange, dt, label=label, color=trajcolor, alpha=0.7) plot_thresh(fifos, taname, yname, iname='icap_ais', ithresh=0.001, color=thrcolor, label=label + ' thresh', alpha=0.7)
def plot_contour(indir, newfig=True, **kwargs): if newfig: plt.figure() mnvec = np.array(()) sdvec = np.array(()) # for wd in sorted(os.listdir(indir)): for wd in ['']: wpath = os.path.join(indir, wd) ctpar = jspar.load(os.path.join(wpath, 'ctpar.json')) xname = ctpar['xname'] fxvec = np.load(os.path.join(wpath, 'fxvec.npy')) xvec = np.load(os.path.join(wpath, 'xvec.npy')) if xname == 'std': mnvec = np.concatenate((mnvec, fxvec)) sdvec = np.concatenate((sdvec, xvec)) elif xname == 'mean': mnvec = np.concatenate((mnvec, xvec)) sdvec = np.concatenate((sdvec, fxvec)) else: raise ValueError('xname not belong to std or mean') # Remove nan in the arrays nanind = np.logical_or(np.isnan(mnvec), np.isnan(sdvec)) mnvec = mnvec[~nanind] sdvec = sdvec[~nanind] # Sort arrays mnvec, sdvec = zip(*sorted(zip(mnvec, sdvec))) plt.plot(mnvec, sdvec, **kwargs) if newfig: # plt.ylim((0, 0.25)) plt.legend() plt.savefig(os.path.join(indir, 'contour.pdf'))
def plot_traj_thresh(indir, xname, yname): with open(os.path.join(indir, 'fifos.p')) as f: fifos = pickle.load(f) runpar = jspar.load(os.path.join(indir, 'runpar.json')) fifo_trange = runpar['fifo_trange'] dt = runpar['hpar']['dt'] trng = (-40, -0.05) plot_traj(fifos, xname, yname, trng, fifo_trange, dt) plot_thresh(fifos, xname, yname, iname='icap_ais', ithresh=0.001)
def plot_operpoint(gkbar, spdir, frtli, colorli, cv=1, rad=0.005): gkdir = labeldir.gkbar_dir(gkbar) for i, frt in enumerate(frtli): targsdir = labeldir.targs_dir(frt, cv) indir = os.path.join(spdir, targsdir, gkdir) stimpar = jspar.load(os.path.join(indir, 'stimpar.json')) yrad = 2 * rad color = colorli[i] ell = patches.Ellipse((stimpar['mean'], stimpar['std']), rad, yrad, color=color, alpha=0.5) ax = plt.gca() ax.add_artist(ell)
def plot_gains(gkbarli, indir, figpath, normalized=False, statlab=False): plt.figure(figsize=(8, 4)) for gkbar in gkbarli: gkdir = labeldir.gkbar_dir(gkbar) subdir = os.path.join(indir, gkdir) transfdir = os.path.join(subdir, 'transfer') transf = np.load(os.path.join(transfdir, 'transf.npy')) fvec = np.load(os.path.join(transfdir, 'fvec.npy')) gain = np.absolute(transf) phase = np.angle(transf) phase = np.negative( phase ) ## this is used before changing phase to negative in transfer.py lab = labeldir.gkbar_lab(gkbar) if statlab: statfile = os.path.join(subdir, 'spkstat.json') if not os.path.isfile(statfile): spkstat.get_spkstat_from_file(subdir) stat = jspar.load(statfile) stat_lab = '(frt:%.1f cv:%.2f lv:%.2f)' % (stat['frt'], stat['cv'], stat['lv']) lab = lab + ' ' + stat_lab if normalized: gain = gain / gain[1] plt.subplot(121) plt.semilogx(fvec, gain, label=lab) plt.subplot(122) plt.semilogx(fvec, phase, label=lab) plt.subplot(121) plt.xlabel('Frequency (Hz)') if normalized: plt.ylabel('Gain') else: plt.ylabel('Gain (Hz/nA)') # plt.ylim(5e5, 2e6) plt.legend(loc=1, prop={'size': 8}, fancybox=True, framealpha=0.5) plt.subplot(122) plt.xlabel('Frequency (Hz)') plt.ylabel('Phase') # plt.legend(prop={'size':8}, loc=4) plt.legend(loc=1, prop={'size': 8}, fancybox=True, framealpha=0.5) plt.tight_layout() if figpath: plt.savefig(figpath)
def plot_gains(tauli, indir, gkbar, frttarg, cvtarg, figpath, normalized=False, statlab=False): plt.figure(figsize=(8, 4)) gkdir = labeldir.gkbar_dir(gkbar) firgain = 0 for i, tau in enumerate(tauli): optsubdir = labeldir.opt_subdir(tau, frttarg, cvtarg) subdir = os.path.join(indir, optsubdir, gkdir) transfdir = os.path.join(subdir, 'transfer') transf = np.load(os.path.join(transfdir, 'transf.npy')) fvec = np.load(os.path.join(transfdir, 'fvec.npy')) gain = np.absolute(transf) phase = np.angle(transf) if normalized: gain = gain / gain[1] lab = labeldir.tau_lab(tau) if statlab: statfile = os.path.join(subdir, 'stat.json') if not os.path.isfile(statfile): spkstat.get_stat_from_file(subdir) stat = jspar.load(statfile) stat_lab = '(frt:%.1f cv:%.2f lv:%.2f)' % (stat['frt'], stat['cv'], stat['lv']) lab = lab + ' ' + stat_lab plt.subplot(121) plt.semilogx(fvec, gain, label=lab) plt.subplot(122) plt.semilogx(fvec, phase, label=lab) plt.subplot(121) plt.xlabel('$f$ (Hz)') if normalized: plt.ylabel('Gain') else: plt.ylabel('Gain (Hz/nA)') # plt.ylim(5e5, 2e6) plt.legend(loc=1, prop={'size': 8}, fancybox=True, framealpha=0.5) plt.subplot(122) plt.xlabel('$f$ (Hz)') plt.ylabel('Phase') # plt.legend(prop={'size':8}, loc=4) plt.legend(loc=1, prop={'size': 8}, fancybox=True, framealpha=0.5) plt.tight_layout() if figpath: plt.savefig(figpath)
def plot_m_v(indir, label, trajcolor): with open(os.path.join(indir, 'fifos.p')) as f: fifos = pickle.load(f) runpar = jspar.load(os.path.join(indir, 'runpar.json')) fifo_trange = runpar['fifo_trange'] dt = runpar['hpar']['dt'] # trng = (-1, -0.05) trng = (-20, -0.05) rng = map(lambda x: get_ffind(x, fifo_trange[0], dt), trng) pp.plot_multi_traj(fifos['v_ais'], fifos['nav_m'], fir=0, num=20, rng=rng, label=label, color=trajcolor, alpha=0.5)
def run_simulation(ttime, outdir, pardir): spkthrfile = os.path.join(pardir, 'spkthr.json') cpfile = os.path.join(pardir, 'cellpar.json') spfile = os.path.join(pardir, 'stimpar.json') if not (os.path.exists(pardir) and os.path.isfile(cpfile) and os.path.isfile(spfile) and os.path.isfile(spkthrfile)): raise Exception( 'Please run run_optstimpar() before run_simulation()! pardir {}'. format(pardir)) if not os.path.exists(outdir): raise OSError('No such directory: {}'.format(outdir)) spkthr = jspar.load(spkthrfile) stimtyp = 'OU' args = [cpfile, spkthr, ttime, stimtyp, spfile, outdir] submit_to_cluster('sim', '../../run/run_simulation.py', args)
def plot_i_v_thresh(indir, figpath, ithresh=0.001): plt.figure() with open(os.path.join(indir, 'fifos.p')) as f: fifos = pickle.load(f) runpar = jspar.load(os.path.join(indir, 'runpar.json')) fifo_trange = runpar['fifo_trange'] dt = runpar['hpar']['dt'] # trng = (-1, -0.05) trng = (-20, -0.05) rng = map(lambda x: get_ffind(x, fifo_trange[0], dt), trng) pp.plot_multi_traj(fifos['v_ais'], fifos['icap_ais'], fir=15, num=20, rng=rng, color='#1f77b4', alpha=0.7) plt.axhline(ithresh, color='k', linestyle='dashed') plt.xlabel('AIS membrane voltage (mV)') plt.ylabel('AIS current density(mA/cm$^2$)') plt.tight_layout() plt.savefig(figpath)
parser.add_argument('cpfile', help='json file of cell parameter') parser.add_argument('spkthr', type=float, help='json file of cell parameter') parser.add_argument('ttime', type=float, help='total simulation time') parser.add_argument('stimtyp', help='stimulation type') parser.add_argument('spfile', help='json file of stimulation parameter') parser.add_argument('outdir', help='output directory') parser.add_argument('--moddir', help='path for model definition') args = parser.parse_args() if args.moddir: moddir = args.moddir else: moddir = '.' run = imp.load_source('run', os.path.join(moddir, 'run.py')) stimpar = jspar.load(args.spfile) cellpar = jspar.load(args.cpfile) outdir = args.outdir if not os.path.exists(outdir): raise OSError('No such directory: {}'.format(outdir)) spktimes, stimvec = run.simulation(cellpar, args.spkthr, args.ttime, args.stimtyp, stimpar, retstim=True) runpar = dict(hpar=run.hpar, ttime=args.ttime,
parser.add_argument('spkthr', type=float, help='voltage thresh for spike detection') parser.add_argument('outdir', help='output directory') parser.add_argument('--moddir', help='path for model definition') parser.add_argument( '--noise', type=float, help='0: no noise, non-0: standard deviation of OU process') parser.add_argument('--tau', type=float, help='tau for OU process') args = parser.parse_args() if args.moddir: moddir = args.moddir else: moddir = '.' run = imp.load_source('run', os.path.join(moddir, 'run.py')) curtvec = np.load(args.ctvfile) cellpar = jspar.load(args.cpfile) outdir = args.outdir if not os.path.exists(outdir): raise Exception('No such directory: %s' % outdir) simfunc = run.cellpar_wrapper(run.simulation, cellpar, args.spkthr) frtvec = ficurve.get_frtvec(simfunc, curtvec, noise=args.noise, tau=args.tau) np.save(os.path.join(outdir, 'frtvec.npy'), frtvec) jspar.save(os.path.join(outdir, 'fipar.json'), vars(args))
import numpy as np scriptdir = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(scriptdir, '..')) from nrngain import jspar, spktrigavrg parser = argparse.ArgumentParser() parser.add_argument('indir', help='input directory contains spktimes, stimvec, runpar') parser.add_argument('--maxtau', help='time range of stimulus before and after spike (ms)') args = parser.parse_args() indir = args.indir if not os.path.exists(indir): raise OSError('No such directory: {}'.format(indir)) spktimes = np.load(os.path.join(indir, 'spktimes.npy')) stimvec = np.load(os.path.join(indir, 'stimvec.npy')) runpar = jspar.load(os.path.join(indir, 'runpar.json')) if args.maxtau: maxtau = args.maxtau else: maxtau = 500. dt = runpar['hpar']['dt'] stim_mean = runpar['stimpar']['mean'] sta = spktrigavrg.get_sta(stimvec, spktimes, maxtau, dt, stim_mean) np.save(os.path.join(indir, 'sta.npy'), sta)