ax5.scatter(source_xs, source_zs, c=source_amps) if initial is not None: ax5.scatter(cell.xmid[initial], cell.zmid[initial], marker='*', c='r') fig.colorbar(im5, label='[mV / $\mu m^2$]') ax6 = plt.subplot(236, title='V_ext along the cell', xlabel='z [$\mu$m]', ylabel='mV') for i in range(200, pulse_start + pulse_duration + 50, 10): ax6.plot(np.arange(cell.totnsegs), np.asarray(cell.v_ext).T[i][cell.zmid.argsort()][::-1]) plt.tight_layout() pconv.mark_subplots(ax1, 'A', xpos=-.25, ypos=.99) pconv.mark_subplots(ax2, 'B', xpos=-.25, ypos=.99) pconv.mark_subplots(ax4, 'D', xpos=-.25, ypos=.99) pconv.mark_subplots(ax5, 'E', xpos=-.25, ypos=.99) # pconv.mark_subplots(ax3, 'C', xpos=-.25 , ypos=.99) # pconv.mark_subplots(ax3, 'C') pconv.mark_subplots(ax6, 'F', xpos=-.25, ypos=.99) fig.subplots_adjust(left=.08, right=.96, bottom=.07, top=.96, wspace=None, hspace=None) plt.show()
def make_fig_1(totnsegs, imem, syninds, xmids, zmids, zips, cb_LFP_close, cb_LFP_far, multi_dip_LFP_close, multi_dip_LFP_far, db_LFP_close, db_LFP_far, LFP_max, time_max, multi_dips, multi_dip_locs, single_dip, r_mid, X, Z, X_f, Z_f): plt.interactive(1) plt.close('all') fig = plt.figure() ax0 = plt.subplot2grid((3, 3), (0, 0)) ax1 = plt.subplot2grid((3, 3), (1, 0)) ax2 = plt.subplot2grid((3, 3), (2, 0)) ax3 = plt.subplot2grid((3, 3), (0, 1)) ax4 = plt.subplot2grid((3, 3), (1, 1)) ax5 = plt.subplot2grid((3, 3), (2, 1)) ax6 = plt.subplot2grid((3, 3), (0, 2)) ax7 = plt.subplot2grid((3, 3), (1, 2)) ax8 = plt.subplot2grid((3, 3), (2, 2)) # plot neuron morphology in top row plot_neuron(ax0, syninds, xmids, zmids, zips, syn=True, lengthbar=True) plot_neuron(ax3, syninds, xmids, zmids, zips, syn=True, lengthbar=True, lb_clr='w') plot_neuron(ax6, syninds, xmids, zmids, zips, syn=True, lengthbar=True, lb_clr='w') # plot transmembrane currents for idx in range(totnsegs): arrowlength = np.abs(imem[idx, time_max]) * 1e5 wdth = 1. if [idx] == syninds: arrowlength = -700. wdth = 2. ax0.arrow( xmids[idx] - arrowlength, zmids[idx], arrowlength, 0., width=4., head_length=39., head_width=30., length_includes_head=True, color='#0D325F', # alpha=.5 ) else: ax0.arrow(xmids[idx], zmids[idx], arrowlength, 0., width=wdth, head_length=3.4, head_width=7., length_includes_head=True, color='#D90011', alpha=.5) # plt lfp close plot_lfp(fig, ax1, cb_LFP_close, LFP_max, time_max, X, Z, lengthbar=True) plot_lfp(fig, ax4, multi_dip_LFP_close, LFP_max, time_max, X, Z, lengthbar=False) plot_lfp(fig, ax7, db_LFP_close, LFP_max, time_max, X, Z, lengthbar=False) # plot lfp far plot_lfp_far(fig, ax2, cb_LFP_far, LFP_max, time_max, X_f, Z_f, lengthbar=True) plot_lfp_far(fig, ax5, multi_dip_LFP_far, LFP_max, time_max, X_f, Z_f, lengthbar=False) LFP, ep_intervals, ticks = plot_lfp_far(fig, ax8, db_LFP_far, LFP_max, time_max, X_f, Z_f, lengthbar=False, colorax=True) # plot neurons in second row for ax in [ax1, ax4]: plot_neuron(ax, syninds, xmids, zmids, zips, syn=False, clr='w') # plot multi-dipole arrows for i in range(len(multi_dips)): p = multi_dips[i, time_max] * 10 loc = multi_dip_locs[i] w = np.linalg.norm(p) * 2 ax3.arrow(loc[0] - 0.5 * p[0], loc[2] - 0.5 * p[2], p[0], p[2], color='green', alpha=0.8, width=w, head_width=4 * w, length_includes_head=False) # plot single dipole arrow arrow = single_dip[time_max] * 25 arrow_colors = ['gray', 'w'] arrow_axes = [ax6, ax7] for i in range(2): arrow_axes[i].arrow( r_mid[0] - 2 * arrow[0], r_mid[2] - 2 * arrow[2], arrow[0] * 4, arrow[2] * 4, #fc = 'k',ec = 'k', color=arrow_colors[i], alpha=0.8, width=12, #head_width = 60., length_includes_head=True) #, # colorbar cax = fig.add_axes([0.13, 0.03, 0.8, 0.01]) cbar = fig.colorbar(ep_intervals, cax=cax, orientation='horizontal', format='%3.3f', extend='max') cbar.set_ticks(ticks) labels = [ r'$-10^{\/\/2}$', r'$-10^{\/\/1}$', r'$-1.0$', r'$-10^{-1}$', r'$-10^{-2}$', r'$\/\/10^{-2}$', r'$\/\/10^{-1}$', r'$\/\/1.0$', r'$\/\/10^{\/\/1}$', r'$\/\/10^{\/\/2}$' ] cax.set_xticklabels(labels, rotation=40) cbar.set_label('$\phi$ (nV)', labelpad=-0.4) cbar.outline.set_visible(False) ax0.set_title('compartment-based', fontsize='x-small') ax3.set_title('multi-dipole', fontsize='x-small') ax6.set_title('single-dipole', fontsize='x-small') fig.set_size_inches(8, 8) plotting_convention.mark_subplots([ax0, ax3, ax6], letters='ABC', xpos=-0.02, ypos=1.0) plotting_convention.mark_subplots([ax1, ax4, ax7], letters='DEF', xpos=-0.02, ypos=1.05) plotting_convention.mark_subplots([ax2, ax5, ax8], letters='GHI', xpos=-0.02, ypos=0.94) for ax in [ax1, ax2, ax4, ax5, ax7, ax8, ax0, ax3, ax6]: ax.set_aspect('equal', 'datalim') plt.subplots_adjust(wspace=0.05, hspace=0.05, left=0.1, bottom=0.05, right=0.96, top=0.93) return fig
xlabel="x [$\mu$m]", ylabel='y [$\mu$m]') img12 = ax12.imshow(dd_field_xy2.T, extent=[-x_extent, x_extent, -y_extent, y_extent], **imshow_dict) # ax2.scatter(source_xs, np.ones(len(source_xs)) * -300, c=source_amps, s=100, vmin=-1.4, # vmax=1.4, edgecolor='k', lw=2, cmap=plt.cm.bwr, clip_on=False) plt.colorbar(img12, label="mV / $\mu m^2$") fig.subplots_adjust(wspace=0.06, left=.02, right=.95, top=.92, hspace=.5, bottom=.08) pconv.mark_subplots(ax1, 'A', xpos=-.05, ypos=.99) pconv.mark_subplots(ax2, 'B', xpos=-.05, ypos=.99) pconv.mark_subplots(ax3, 'C', xpos=-.05, ypos=.99) pconv.mark_subplots(ax4, 'D', xpos=-.05, ypos=.99) pconv.mark_subplots(ax5, 'E', xpos=-.05, ypos=.99) pconv.mark_subplots(ax6, 'F', xpos=-.05, ypos=.99) pconv.mark_subplots(ax7, 'G', xpos=-.05, ypos=.99) pconv.mark_subplots(ax8, 'H', xpos=-.05, ypos=.99) pconv.mark_subplots(ax9, 'I', xpos=-.05, ypos=.99) pconv.mark_subplots(ax10, 'J', xpos=-.05, ypos=.99) pconv.mark_subplots(ax11, 'K', xpos=-.05, ypos=1.) pconv.mark_subplots(ax12, 'L', xpos=-.05, ypos=1.) # plt.tight_layout() plt.savefig("extracellular_potential_2nd_derivative_" + name_shape_ecog + ".png")
ax.set_title(r'$\sigma_{\mathrm{skull}} = \sigma_{\mathrm{brain}} / 80$', fontsize=17) l1, = plt.plot(theta, phi_80_98, 'g', lw=2) l2, = plt.plot(theta, phi_80, 'k', lw=2) l3, = plt.plot(theta[:50], phi_80_fit[:50], 'm+', lw=2) l4, = plt.plot(theta, phi_80_c, 'b', lw=2) l5, = plt.plot(theta, num_80, 'r.', lw=2) ax.set_xticks(ax.get_xticks()[::2]) ax.tick_params(labelsize=15.) ax.set_yticks(np.linspace(0, 110, 12)) ax.set_yticklabels( ['0', '', '20', '', '40', '', '60', '', '80', '', '100', '']) lines = [l1, l2, l3, l4, l5] line_names = [ 'Srinivasan (1998)', 'Nunez & Srinivasan (2006)', 'Fit - Nunez & Srinivasan (2006) ', 'Present results - analytical', 'Present results - FEM' ] fig.legend(lines, line_names, frameon=False, ncol=1, bbox_to_anchor=[1.01, 0.85], fontsize=12) simplify_axes(fig.axes) mark_subplots(fig.axes, ypos=1.07, xpos=-0.) plt.savefig(os.path.join('results', 'Potentials_scaled.png'), dpi=150) # plt.show()
def simulate_laminar_LFP(): # DEPRECATED, and not updated for LFPy2.2 and newer dt = 2**-5 cell_name = 'hay' elec_z = np.linspace(-200, 1200, 15) elec_x = np.ones(len(elec_z)) * 50 elec_y = np.zeros(len(elec_z)) h = np.abs(elec_z[1] - elec_z[0]) electrode_parameters = { 'sigma': 0.3, # extracellular conductivity 'x': elec_x, # x,y,z-coordinates of contact points 'y': elec_y, 'z': elec_z } elec_clr = lambda idx: plt.cm.viridis(idx / len(elec_z)) num_sims = 10 cells = [] summed_LFP = [] summed_cdm = [] for sim in range(num_sims): print(sim + 1, "/", num_sims) cell_wca, idx_clr, plot_idxs = run_cell_simulation_distributed_input( dt=dt, cell_name=cell_name) cells.append([cell_wca, idx_clr, plot_idxs]) for sim in range(num_sims): cell_wca, idx_clr, plot_idxs = cells[sim] fig = plt.figure(figsize=[7, 7]) fig.subplots_adjust(hspace=0.5, left=0.0, wspace=0.4, right=0.96, top=0.97, bottom=0.1) ax_m = fig.add_axes([-0.01, 0.05, 0.3, 0.97], aspect=1, frameon=False, xlim=[-350, 350], xticks=[], yticks=[]) [ ax_m.plot([cell_wca.xstart[idx], cell_wca.xend[idx]], [cell_wca.zstart[idx], cell_wca.zend[idx]], c='k') for idx in range(cell_wca.totnsegs) ] [ ax_m.plot(cell_wca.xmid[idx], cell_wca.zmid[idx], 'o', c=idx_clr[idx], ms=13) for idx in plot_idxs ] [ ax_m.plot(cell_wca.xmid[idx], cell_wca.zmid[idx], 'rd') for idx in cell_wca.synidx ] ax_top = 0.98 ax_h = 0.25 h_space = 0.1 ax_w = 0.17 ax_left = 0.4 cell = cell_wca elec = LFPy.RecExtElectrode(cell, **electrode_parameters) elec.calc_lfp() ax_vm = fig.add_axes([ax_left, ax_top - ax_h, ax_w, ax_h], ylim=[-80, 50], xlim=[0, 80], xlabel="Time (ms)") ax_eap = fig.add_axes([ax_left + 0.3, 0.1, ax_w, 0.8], xlim=[0, 80], xlabel="Time (ms)") ax_cdm = fig.add_axes( [ax_left, 0.2, ax_w, ax_h], xlabel="Time (ms)", ylim=[-0.5, 1], xlim=[0, 80], ) ax_vm.set_ylabel("Membrane\npotential\n(mV)", labelpad=-3) ax_eap.set_ylabel("Extracellular potential ($\mu$V)", labelpad=-3) ax_cdm.set_ylabel("Curent dipole\nmoment\n($\mu$A$\cdot \mu$m)", labelpad=-3) [ ax_vm.plot(cell.tvec, cell.vmem[idx], c=idx_clr[idx]) for idx in plot_idxs ] elec.LFP -= elec.LFP[:, 0, None] cell.current_dipole_moment -= cell.current_dipole_moment[0, :] summed_LFP.append(elec.LFP) summed_cdm.append(cell.current_dipole_moment) normalize = np.max(np.abs(elec.LFP)) for idx in range(len(elec_x)): ax_eap.plot(cell.tvec, elec.LFP[idx] / normalize * h + elec_z[idx], c=elec_clr(idx)) ax_m.plot(elec_x[idx], elec_z[idx], c=elec_clr(idx), marker='D') ax_cdm.plot(cell.tvec, 1e-3 * cell.current_dipole_moment[:, 2], c='k') mark_subplots([ax_m], xpos=0.1, ypos=0.95) simplify_axes(fig.axes) plt.savefig( join("figures", 'laminar_LFP_ca_spike_{}_{}.png'.format(cell_name, sim))) # plt.savefig(join("figures", 'hay_ca_spike.pdf')) plt.close("all") summed_LFP = np.sum(summed_LFP, axis=0) summed_cdm = np.sum(summed_cdm, axis=0) normalize = np.max(np.abs(summed_LFP)) plt.subplot(121) for idx in range(len(elec_x)): plt.plot(cell.tvec, summed_LFP[idx] / normalize * h + elec_z[idx], c=elec_clr(idx)) plt.subplot(122) plt.plot(cell.tvec, summed_cdm[:, 2]) plt.savefig( join("figures", 'summed_LFP_CDM_{}_num:{}.png'.format(cell_name, num_sims)))
def simulate_spike_current_dipole_moment(): dt = 2**-5 cell_name = 'hay' jitter_std = 10 num_trials = 1000 # Create a grid of measurement locations, in (mum) grid_x, grid_z = np.mgrid[-750:751:25, -750:1301:25] grid_y = np.zeros(grid_x.shape) # Define electrode parameters grid_elec_params = { 'sigma': 0.3, # extracellular conductivity 'x': grid_x.flatten(), # electrode requires 1d vector of positions 'y': grid_y.flatten(), 'z': grid_z.flatten() } elec_x = np.array([ 30, ]) elec_y = np.array([ 0, ]) elec_z = np.array([ 0, ]) elec_params = { 'sigma': 0.3, # extracellular conductivity 'x': elec_x, # x,y,z-coordinates of contact points 'y': elec_y, 'z': elec_z } elec_clr = 'r' cell_woca_data, idx_clr, plot_idxs = run_cell_simulation( make_ca_spike=False, dt=dt, cell_name=cell_name, grid_elec_params=grid_elec_params, elec_params=elec_params) cell_wca_data, idx_clr, plot_idxs = run_cell_simulation( make_ca_spike=True, dt=dt, cell_name=cell_name, grid_elec_params=grid_elec_params, elec_params=elec_params) fig = plt.figure(figsize=[8, 7]) fig.subplots_adjust(hspace=0.5, left=0.0, wspace=0.4, right=0.96, top=0.97, bottom=0.1) ax_m = fig.add_axes([-0.01, 0.05, 0.25, 0.97], aspect=1, frameon=False, xticks=[], yticks=[]) ax_m.plot(cell_wca_data["cell_x"].T, cell_wca_data["cell_z"].T, c='k') [ ax_m.plot(cell_wca_data["cell_x"][idx].mean(), cell_wca_data["cell_z"][idx].mean(), 'o', c=idx_clr[idx], ms=13) for idx in plot_idxs ] ax_m.plot(elec_x, elec_z, elec_clr, marker='D') ax_top = 0.98 ax_h = 0.15 h_space = 0.1 ax_w = 0.17 ax_left = 0.37 num = 11 levels = np.logspace(-2.5, 0, num=num) scale_max = 100. levels_norm = scale_max * np.concatenate((-levels[::-1], levels)) bwr_cmap = plt.cm.get_cmap('bwr_r') colors_from_map = [ bwr_cmap(i * np.int(255 / (len(levels_norm) - 2))) for i in range(len(levels_norm) - 1) ] colors_from_map[num - 1] = (1.0, 1.0, 1.0, 1.0) spike_plot_time_idxs = [1030, 1151] summed_cdm_max = np.zeros(2) for plot_row, cell_data in enumerate([cell_woca_data, cell_wca_data]): ax_left += plot_row * 0.25 grid_LFP = cell_data["grid_LFP"] cdm = cell_data["cdm"] tvec = cell_data["tvec"] vmem = cell_data["vmem"] elec_LFP = cell_data["elec_LFP"] time_idx = spike_plot_time_idxs[plot_row] print(tvec[time_idx]) grid_LFP_ = grid_LFP[:, time_idx].reshape(grid_x.shape) ax_ = fig.add_axes([0.75, 0.55 - plot_row * 0.46, 0.3, 0.45], xticks=[], yticks=[], aspect=1, frameon=False) mark_subplots(ax_, [["D"], ["E"]][plot_row], ypos=0.95, xpos=0.35) ax_.plot(cell_data["cell_x"].T, cell_data["cell_z"].T, c='k') ep_intervals = ax_.contourf(grid_x, grid_z, grid_LFP_, zorder=-2, colors=colors_from_map, levels=levels_norm, extend='both') ax_.contour(grid_x, grid_z, grid_LFP_, colors='k', linewidths=(1), zorder=-2, levels=levels_norm) if plot_row == 1: cax = fig.add_axes([0.82, 0.12, 0.16, 0.01]) cbar = fig.colorbar(ep_intervals, cax=cax, orientation='horizontal', format='%.0E') cbar.set_ticks( np.array([-1, -0.1, -0.01, 0, 0.01, 0.1, 1]) * scale_max) cax.set_xticklabels(np.array( np.array([-1, -0.1, -0.01, 0, 0.01, 0.1, 1]) * scale_max, dtype=int), fontsize=11, rotation=45) cbar.set_label('$\phi$ (µV)', labelpad=-5) sum_tvec, summed_cdm = sum_jittered_cdm(cdm[2, :], dt, jitter_std, num_trials) summed_cdm_max[plot_row] = np.max(np.abs(summed_cdm)) ax_vm = fig.add_axes([ax_left, ax_top - ax_h, ax_w, ax_h], ylim=[-80, 50], xlim=[0, 100]) ax_eap = fig.add_axes( [ax_left, ax_top - 2 * ax_h - h_space, ax_w, ax_h], ylim=[-120, 40], xlim=[0, 100]) ax_cdm = fig.add_axes( [ax_left, ax_top - 3 * ax_h - 2 * h_space, ax_w, ax_h], ylim=[-0.5, 1], xlim=[0, 100], ) ax_cdm_sum = fig.add_axes( [ax_left, ax_top - 4 * ax_h - 3 * h_space, ax_w, ax_h], ylim=[-250, 100], xlabel="Time (ms)", xlim=[0, 140]) if plot_row == 0: ax_vm.set_ylabel("Membrane\npotential\n(mV)", labelpad=-3) ax_eap.set_ylabel("Extracellular\npotential\n(µV)", labelpad=-3) ax_cdm.set_ylabel("Curent dipole\nmoment\n(µA$\cdot$µm)", labelpad=-3) ax_cdm_sum.set_ylabel("Jittered sum\n(µA$\cdot$µm)", labelpad=-3) elif plot_row == 1: ax_vm.text(65, -5, "Ca$^{2+}$ spike", fontsize=11, color='orange') ax_vm.arrow(80, -10, -10, -18, color='orange', head_width=4) mark_subplots(ax_vm, [["B1"], ["C1"]][plot_row], xpos=-0.3, ypos=0.93) mark_subplots(ax_eap, [["B2"], ["C2"]][plot_row], xpos=-0.3, ypos=0.93) mark_subplots(ax_cdm, [["B3"], ["C3"]][plot_row], xpos=-0.35, ypos=0.97) mark_subplots(ax_cdm_sum, [["B4"], ["C4"]][plot_row], xpos=-0.3, ypos=0.93) [ax_vm.plot(tvec, vmem[idx], c=idx_clr[idx]) for idx in plot_idxs] ax_vm.axvline(tvec[time_idx], ls='--', c='gray') [ ax_eap.plot(tvec, elec_LFP[idx], c=elec_clr) for idx in range(len(elec_x)) ] ax_cdm.plot(tvec, 1e-3 * cdm[2, :], c='k') ax_cdm_sum.plot(sum_tvec, 1e-3 * summed_cdm, c='k') print( "Summed CDM max (abs), ratio", summed_cdm_max * 1e-3, ) mark_subplots([ax_m], xpos=0.1, ypos=0.95) simplify_axes(fig.axes) plt.savefig(join("figures", 'Figure5.pdf'))