def fig_kernel_lfp(savefolders, params, transient=<NUM_LIT:200>, T=[<NUM_LIT>, <NUM_LIT>], X='<STR_LIT>',<EOL>lags=[<NUM_LIT:20>, <NUM_LIT:20>], channels=[<NUM_LIT:0>,<NUM_LIT:3>,<NUM_LIT:7>,<NUM_LIT:11>,<NUM_LIT>]):
<EOL>zvec = np.r_[params.electrodeParams['<STR_LIT:z>']]<EOL>alphabet = '<STR_LIT>'<EOL>ana_params.set_PLOS_2column_fig_style(ratio=<NUM_LIT:1>)<EOL>fig = plt.figure()<EOL>fig.subplots_adjust(left=<NUM_LIT>, right=<NUM_LIT>, bottom=<NUM_LIT>, top=<NUM_LIT>, hspace=<NUM_LIT>, wspace=<NUM_LIT>) <EOL>gs = gridspec.GridSpec(<NUM_LIT:2>*len(channels)+<NUM_LIT:1>, <NUM_LIT:7>)<EOL>params.savefolder = os.path.join(os.path.split(params.savefolder)[<NUM_LIT:0>],<EOL>'<STR_LIT>')<EOL>params.figures_path = os.path.join(params.savefolder, '<STR_LIT>')<EOL>params.spike_output_path = os.path.join(params.savefolder,<EOL>'<STR_LIT>')<EOL>params.networkSimParams['<STR_LIT>'] = params.spike_output_path<EOL>f = h5py.File(os.path.join(params.savefolder, '<STR_LIT>'))<EOL>srate = f['<STR_LIT>'].value<EOL>tvec = np.arange(f['<STR_LIT:data>'].shape[<NUM_LIT:1>]) * <NUM_LIT> / srate<EOL>inds = (tvec < params.tstop) & (tvec >= transient)<EOL>data_sg_raw = f['<STR_LIT:data>'].value.astype(float)<EOL>data_sg = data_sg_raw[:, inds]<EOL>f.close()<EOL>kwidth = <NUM_LIT:20><EOL>activationtimes = np.array([x*<NUM_LIT:100> for x in range(<NUM_LIT:3>,<NUM_LIT:11>)] + [<NUM_LIT:200>])<EOL>networkSimSpikegen = CachedNetwork(**params.networkSimParams)<EOL>x, y = networkSimSpikegen.get_xy([transient, params.tstop])<EOL>titles = ['<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>]<EOL>COUNTER = <NUM_LIT:0> <EOL>for i, X__ in enumerate(([['<STR_LIT>']]) + list(zip(params.X[<NUM_LIT:1>::<NUM_LIT:2>], params.X[<NUM_LIT:2>::<NUM_LIT:2>]))):        <EOL><INDENT>ax = fig.add_subplot(gs[:len(channels), i])<EOL>if i == <NUM_LIT:0>:<EOL><INDENT>phlp.annotate_subplot(ax, ncols=<NUM_LIT:7>, nrows=<NUM_LIT:4>, letter=alphabet[<NUM_LIT:0>], linear_offset=<NUM_LIT>)<EOL><DEDENT>for j, X_ in enumerate(X__):<EOL><INDENT>cinds = np.arange(activationtimes[np.arange(-<NUM_LIT:1>, <NUM_LIT:8>)][COUNTER]-kwidth,<EOL>activationtimes[np.arange(-<NUM_LIT:1>, <NUM_LIT:8>)][COUNTER]+kwidth+<NUM_LIT:2>)<EOL>x0_sg = np.histogram(x[X_], bins=cinds)[<NUM_LIT:0>].astype(float)<EOL>if X_ == ('<STR_LIT>'):<EOL><INDENT>color='<STR_LIT:k>' if analysis_params.bw else analysis_params.colorE<EOL><DEDENT>else:<EOL><INDENT>color=('<STR_LIT:k>' if analysis_params.bw else analysis_params.colorE,<EOL>'<STR_LIT>' if analysis_params.bw else analysis_params.colorI)[j]<EOL><DEDENT>lw = <NUM_LIT> if color in ['<STR_LIT>', '<STR_LIT:r>', '<STR_LIT:b>'] else plt.rcParams['<STR_LIT>']<EOL>zorder = <NUM_LIT:0> if '<STR_LIT:I>' in X_ else <NUM_LIT:1><EOL>xcorr, vlimround = plotting_correlation(params,<EOL>x0_sg/x0_sg.sum()**<NUM_LIT:2>, <EOL>data_sg_raw[:, cinds[:-<NUM_LIT:1>]]*<NUM_LIT>,<EOL>ax, normalize=False,<EOL>lag=kwidth,<EOL>color=color,<EOL>scalebar=False,<EOL>lw=lw, zorder=zorder)<EOL>if i > <NUM_LIT:0>:<EOL><INDENT>ax.set_yticklabels([])<EOL><DEDENT>ax.plot([kwidth, kwidth],<EOL>[-<NUM_LIT> + j*<NUM_LIT:3>*<NUM_LIT:100>, -<NUM_LIT> + j*<NUM_LIT:3>*<NUM_LIT:100>], lw=<NUM_LIT:2>, color=color,<EOL>clip_on=False)<EOL>ax.text(kwidth*<NUM_LIT>, -<NUM_LIT> + j*<NUM_LIT:3>*<NUM_LIT:100>, '<STR_LIT>' % vlimround,<EOL>rotation='<STR_LIT>', va='<STR_LIT>')<EOL>ax.set_xlim((-<NUM_LIT:5>, kwidth))<EOL>ax.set_xticks([-<NUM_LIT:20>, <NUM_LIT:0>, <NUM_LIT:20>])<EOL>ax.set_xticklabels([-<NUM_LIT:20>, <NUM_LIT:0>, <NUM_LIT:20>])<EOL>COUNTER += <NUM_LIT:1><EOL><DEDENT>ax.set_title(titles[i])<EOL><DEDENT>for i, (savefolder, lag) in enumerate(zip(savefolders, lags)):<EOL><INDENT>params.savefolder = os.path.join(os.path.split(params.savefolder)[<NUM_LIT:0>],<EOL>savefolder)<EOL>params.figures_path = os.path.join(params.savefolder, '<STR_LIT>')<EOL>params.spike_output_path = os.path.join(params.savefolder,<EOL>'<STR_LIT>')<EOL>params.networkSimParams['<STR_LIT>'] = params.spike_output_path<EOL>networkSim = CachedNetwork(**params.networkSimParams)<EOL>f = h5py.File(os.path.join(params.savefolder, '<STR_LIT>'))<EOL>data_raw = f['<STR_LIT:data>'].value<EOL>srate = f['<STR_LIT>'].value<EOL>tvec = np.arange(data_raw.shape[<NUM_LIT:1>]) * <NUM_LIT> / srate<EOL>inds = (tvec < params.tstop) & (tvec >= transient)<EOL>data = data_raw[:, inds]<EOL>dataT = data.T - data.mean(axis=<NUM_LIT:1>)<EOL>data = dataT.T<EOL>f.close()<EOL>f = h5py.File(os.path.join(os.path.split(params.savefolder)[<NUM_LIT:0>], '<STR_LIT>', '<STR_LIT>'))<EOL>data_sg_raw = f['<STR_LIT:data>'].value<EOL>f.close()<EOL>titles = ['<STR_LIT>' % X, '<STR_LIT>' % X]<EOL>ax = fig.add_subplot(gs[:len(channels), <NUM_LIT:5> + i])<EOL>if i == <NUM_LIT:0>:<EOL><INDENT>phlp.annotate_subplot(ax, ncols=<NUM_LIT:15>, nrows=<NUM_LIT:4>, letter=alphabet[i+<NUM_LIT:1>],<EOL>linear_offset=<NUM_LIT>)<EOL><DEDENT>x, y = networkSim.get_xy([<NUM_LIT:0>,params.tstop])<EOL>bins = np.arange(<NUM_LIT:0>, params.tstop+<NUM_LIT:2>) + <NUM_LIT:0.5><EOL>x0_raw = np.histogram(x[X], bins=bins)[<NUM_LIT:0>]<EOL>x0 = x0_raw[inds].astype(float)<EOL>xcorr, vlimround = plotting_correlation(params,<EOL>x0/x0.sum(), <EOL>data*<NUM_LIT>,<EOL>ax, normalize=False, <EOL>lag=lag,<EOL>scalebar=False,<EOL>color='<STR_LIT:k>',<EOL>title=titles[i],<EOL>)<EOL>ax.plot([lag, lag],<EOL>[-<NUM_LIT>, -<NUM_LIT>], lw=<NUM_LIT:2>, color='<STR_LIT:k>',<EOL>clip_on=False)<EOL>ax.text(lag*<NUM_LIT>, -<NUM_LIT>, '<STR_LIT>' % vlimround,<EOL>rotation='<STR_LIT>', va='<STR_LIT>')<EOL>[Xind] = np.where(np.array(networkSim.X) == X)[<NUM_LIT:0>]        <EOL>x0_sg = np.zeros(x0.shape, dtype=float)<EOL>x0_sg[activationtimes[Xind]] += params.N_X[Xind]<EOL>ax.set_yticklabels([])<EOL>ax.set_xticks([-lag, <NUM_LIT:0>, lag])<EOL>ax.set_xticklabels([-lag, <NUM_LIT:0>, lag])<EOL>gsb = gridspec.GridSpec(<NUM_LIT:2>*len(channels)+<NUM_LIT:1>, <NUM_LIT:8>)<EOL>ax = fig.add_subplot(gsb[<NUM_LIT:1>+len(channels):, (i*<NUM_LIT:4>):(i*<NUM_LIT:4>+<NUM_LIT:2>)])<EOL>phlp.annotate_subplot(ax, ncols=<NUM_LIT:8>/<NUM_LIT>, nrows=<NUM_LIT:4>, letter=alphabet[i*<NUM_LIT:3>+<NUM_LIT:2>],<EOL>linear_offset=<NUM_LIT>)<EOL>kernels = np.zeros((len(params.N_X), <NUM_LIT:16>, kwidth*<NUM_LIT:2>))<EOL>for j in range(len(params.X)):<EOL><INDENT>kernels[j, :, kwidth:] = data_sg_raw[:, (j+<NUM_LIT:2>)*<NUM_LIT:100>:kwidth+(j+<NUM_LIT:2>)*<NUM_LIT:100>]/params.N_X[j]<EOL><DEDENT>LFP_reconst_raw = np.zeros(data_raw.shape)<EOL>for j, pop in enumerate(params.X):<EOL><INDENT>x0_raw = np.histogram(x[pop], bins=bins)[<NUM_LIT:0>].astype(float)<EOL>for ch in range(kernels.shape[<NUM_LIT:1>]):<EOL><INDENT>LFP_reconst_raw[ch] += np.convolve(x0_raw, kernels[j, ch],<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>LFP_reconst = LFP_reconst_raw[:, inds]<EOL>LFP_reconstT = LFP_reconst.T - LFP_reconst.mean(axis=<NUM_LIT:1>)<EOL>LFP_reconst = LFP_reconstT.T<EOL>vlimround = plot_signal_sum(ax, params,<EOL>fname=os.path.join(params.savefolder,<EOL>'<STR_LIT>'),<EOL>unit='<STR_LIT>', scalebar=True,<EOL>T=T, ylim=[-<NUM_LIT>, <NUM_LIT:50>],<EOL>color='<STR_LIT:k>', label='<STR_LIT>',<EOL>rasterized=False,<EOL>zorder=<NUM_LIT:1>)<EOL>plot_signal_sum(ax, params, fname=LFP_reconst_raw,<EOL>unit='<STR_LIT>', scaling_factor= <NUM_LIT:1.>, scalebar=False,<EOL>vlimround=vlimround,<EOL>T=T, ylim=[-<NUM_LIT>, <NUM_LIT:50>],<EOL>color='<STR_LIT>' if analysis_params.bw else analysis_params.colorP,<EOL>label='<STR_LIT>',<EOL>rasterized=False,<EOL>lw=<NUM_LIT:1>, zorder=<NUM_LIT:0>)<EOL>ax.set_title('<STR_LIT>')<EOL>if i > <NUM_LIT:0>:<EOL><INDENT>ax.set_yticklabels([])<EOL><DEDENT>ax = fig.add_subplot(gsb[<NUM_LIT:1>+len(channels):, i*<NUM_LIT:4>+<NUM_LIT:2>:i*<NUM_LIT:4>+<NUM_LIT:3>])<EOL>phlp.remove_axis_junk(ax)<EOL>phlp.annotate_subplot(ax, ncols=<NUM_LIT>/<NUM_LIT:1>, nrows=<NUM_LIT:4>, letter=alphabet[i*<NUM_LIT:3>+<NUM_LIT:3>],<EOL>linear_offset=<NUM_LIT>)<EOL>cc = np.zeros(len(zvec))<EOL>for ch in np.arange(len(zvec)):<EOL><INDENT>cc[ch] = np.corrcoef(data[ch], LFP_reconst[ch])[<NUM_LIT:1>, <NUM_LIT:0>]<EOL><DEDENT>ax.barh(zvec, cc, height=<NUM_LIT>, align='<STR_LIT>', color='<STR_LIT>', linewidth=<NUM_LIT:0.5>)<EOL>N = <NUM_LIT:1000><EOL>method = '<STR_LIT>' <EOL>chance = np.zeros((cc.size, N))<EOL>for ch in np.arange(len(zvec)):<EOL><INDENT>x1 = LFP_reconst[ch]<EOL>x1 -= x1.mean()<EOL>if method == '<STR_LIT>':<EOL><INDENT>x0 = data[ch]<EOL>x0 -= x0.mean()<EOL>X00 = np.fft.fft(x0)<EOL><DEDENT>for n in range(N):<EOL><INDENT>if method == '<STR_LIT>':<EOL><INDENT>x0 = np.random.permutation(datas[ch])<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>X0 = np.copy(X00)<EOL>theta = np.random.uniform(<NUM_LIT:0>, <NUM_LIT:2>*np.pi, size=X0.size // <NUM_LIT:2>-<NUM_LIT:1>)<EOL>real = abs(X0[<NUM_LIT:1>:X0.size // <NUM_LIT:2>])*np.cos(theta)<EOL>imag = abs(X0[<NUM_LIT:1>:X0.size // <NUM_LIT:2>])*np.sin(theta)<EOL>X0.imag[<NUM_LIT:1>:imag.size+<NUM_LIT:1>] = imag<EOL>X0.imag[imag.size+<NUM_LIT:2>:] = -imag[::-<NUM_LIT:1>]<EOL>X0.real[<NUM_LIT:1>:real.size+<NUM_LIT:1>] = real<EOL>X0.real[real.size+<NUM_LIT:2>:] = real[::-<NUM_LIT:1>]<EOL>x0 = np.fft.ifft(X0).real<EOL><DEDENT>chance[ch, n] = np.corrcoef(x0, x1)[<NUM_LIT:1>, <NUM_LIT:0>]<EOL><DEDENT><DEDENT>p = []<EOL>for h, x in enumerate(cc):<EOL><INDENT>p += [(chance[h, ] >= x).sum() / float(N)]<EOL><DEDENT>print(('<STR_LIT>', p))<EOL>right = np.percentile(chance, <NUM_LIT>, axis=-<NUM_LIT:1>)<EOL>ax.plot(right, zvec, '<STR_LIT::>', color='<STR_LIT:k>', lw=<NUM_LIT:1.>)    <EOL>ax.set_ylim([-<NUM_LIT>, <NUM_LIT:50>])<EOL>ax.set_yticklabels([])<EOL>ax.set_yticks(zvec)<EOL>ax.set_xlim([<NUM_LIT:0>, <NUM_LIT:1.>])<EOL>ax.set_xticks([<NUM_LIT:0.0>, <NUM_LIT:0.5>, <NUM_LIT:1>])<EOL>ax.yaxis.tick_left()<EOL>ax.set_xlabel('<STR_LIT>', labelpad=<NUM_LIT:0.1>)<EOL>ax.set_title('<STR_LIT>')<EOL>print('<STR_LIT>')<EOL>print(cc)<EOL>freqs, PSD_data = calc_signal_power(params, fname=data,<EOL>transient=transient, Df=None,<EOL>mlab=True,<EOL>NFFT=<NUM_LIT>, noverlap=<NUM_LIT>,<EOL>window=plt.mlab.window_hanning)<EOL>freqs, PSD_LFP_reconst = calc_signal_power(params, fname=LFP_reconst,<EOL>transient=transient, Df=None,<EOL>mlab=True,<EOL>NFFT=<NUM_LIT>, noverlap=<NUM_LIT>,<EOL>window=plt.mlab.window_hanning)<EOL>zv = np.r_[params.electrodeParams['<STR_LIT:z>']]<EOL>zv = np.r_[zv, zv[-<NUM_LIT:1>] + np.diff(zv)[-<NUM_LIT:1>]]<EOL>inds = freqs >= <NUM_LIT:1>  <EOL>for j, ch in enumerate(channels):<EOL><INDENT>ax = fig.add_subplot(gsb[<NUM_LIT:1>+len(channels)+j, (i*<NUM_LIT:4>+<NUM_LIT:3>):(i*<NUM_LIT:4>+<NUM_LIT:4>)])<EOL>if j == <NUM_LIT:0>:<EOL><INDENT>phlp.annotate_subplot(ax, ncols=<NUM_LIT>/<NUM_LIT:1>, nrows=<NUM_LIT>*len(channels),<EOL>letter=alphabet[i*<NUM_LIT:3>+<NUM_LIT:4>], linear_offset=<NUM_LIT>)<EOL>ax.set_title('<STR_LIT>')<EOL><DEDENT>phlp.remove_axis_junk(ax)<EOL>ax.loglog(freqs[inds], PSD_data[ch, inds], '<STR_LIT:k>', label='<STR_LIT>',<EOL>clip_on=True, zorder=<NUM_LIT:1>)<EOL>ax.loglog(freqs[inds], PSD_LFP_reconst[ch, inds],<EOL>'<STR_LIT>' if analysis_params.bw else analysis_params.colorP,<EOL>label='<STR_LIT>', clip_on=True, lw=<NUM_LIT:1>, zorder=<NUM_LIT:0>)<EOL>ax.set_xlim([<NUM_LIT>,<NUM_LIT>])<EOL>ax.set_ylim([<NUM_LIT>, <NUM_LIT>])<EOL>ax.tick_params(axis='<STR_LIT:y>', which='<STR_LIT>', pad=<NUM_LIT:0>)<EOL>ax.set_yticks([<NUM_LIT>,<NUM_LIT>,<NUM_LIT>])<EOL>ax.yaxis.set_minor_locator(plt.NullLocator())<EOL>ax.text(<NUM_LIT>, <NUM_LIT>, '<STR_LIT>' % (ch+<NUM_LIT:1>),<EOL>horizontalalignment='<STR_LIT:left>',<EOL>verticalalignment='<STR_LIT>',<EOL>fontsize=<NUM_LIT:6>, <EOL>transform=ax.transAxes)<EOL>if j == <NUM_LIT:0>:<EOL><INDENT>ax.set_ylabel('<STR_LIT>', labelpad=<NUM_LIT:0.>)<EOL><DEDENT>if j > <NUM_LIT:0>:<EOL><INDENT>ax.set_yticklabels([])<EOL><DEDENT>if j == len(channels)-<NUM_LIT:1>:<EOL><INDENT>ax.set_xlabel(r'<STR_LIT>', labelpad=<NUM_LIT:0.>)<EOL><DEDENT>else:<EOL><INDENT>ax.set_xticklabels([])<EOL><DEDENT><DEDENT><DEDENT>return fig, PSD_LFP_reconst, PSD_data<EOL>
This function calculates the  STA of LFP, extracts kernels and recontructs the LFP from kernels.

Arguments
::
  transient : the time in milliseconds, after which the analysis should begin
            so as to avoid any starting transients
  X : id of presynaptic trigger population