.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\plot_3_1D_grid_simple_example.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_3_1D_grid_simple_example.py: ********************** 3. 1D Grid Simulation ********************** This example illustrates how to setup and run a simple 1D grid simulation, also known as a fiber simulation with a row of cells. These simulations show how the cell models interact rather than examining them in isolation. 2D grids can be setup in the same fashion as 1D fibers, and there is also a more advanced tutorial on setting up a 2D grid showing the possible customizations. .. GENERATED FROM PYTHON SOURCE LINES 16-23 Setup & Run the Simulation --------------------------- Import PyLongQt and create a grid protocol, this protocol can function for 1 or 2D simulations. These simulations are similar to current clamp simulations except that they are preformed on a grid of connected cells, instead of on an individual cell. .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: default import PyLongQt as pylqt proto = pylqt.protoMap['Grid Protocol']() .. GENERATED FROM PYTHON SOURCE LINES 29-33 To configure the size of the Fiber we will add one row and the number of columns we would like to have for the simulation. Due to the detailed nature of many of the cell models, larger fibers/grids may be very slow and computationally demanding. .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: default n_cols = 10 proto.grid.addRow() proto.grid.addColumns(n_cols) .. GENERATED FROM PYTHON SOURCE LINES 39-41 We can also use the :py:meth:`Grid.simpleGrid` to get a representation of the grid that is easier to visualize .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: default proto.grid.simpleGrid() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), {'Inexcitable Cell': 0}) .. GENERATED FROM PYTHON SOURCE LINES 45-50 When new cells are added to the grid, they are automatically filled with a cell model called 'Inexcitable Cell', this cell acts like the edge of the grid. It is not excited by its neighbors and cannot pass any signal between them. To replace these cells with more interesting cells, we will create new cell objects and place them in the grid. .. GENERATED FROM PYTHON SOURCE LINES 52-53 To get the possible options for a node in a grid use .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: default proto.grid[0,0].cellOptions() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['Inexcitable Cell', 'Canine Ventricular (Hund-Rudy 2009)', 'Canine Ventricular Border Zone (Hund-Rudy 2009)', 'Coupled Inexcitable Cell', 'Human Atrial (Courtemanche 1998)', 'Human Atrial (Grandi 2011)', 'Human Atrial (Koivumaki 2011)', 'Human Atrial (Onal 2017)', 'Human Ventricular (Grandi 10)', 'Human Ventricular (Ten Tusscher 2004)', "Human Ventricular Endocardium (O'Hara-Rudy 2011)", "Human Ventricular Epicardium (O'Hara-Rudy 2011)", "Human Ventricular Mid Myocardial (O'Hara-Rudy 2011)", 'Mammalian Ventricular (Faber-Rudy 2000)', 'Mouse Sinus Node (Kharche 2011)', 'Mouse Ventricular (Bondarenko 2004)', 'Rabbit Sinus Node (Kurata 2008)'] .. GENERATED FROM PYTHON SOURCE LINES 57-58 rather than .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: default proto.cellOptions() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [''] .. GENERATED FROM PYTHON SOURCE LINES 62-67 as the cell cannot be set at the protocol level when there is a grid structure. .. note:: At this time all nodes will have the same options, so it is not necessary to check each node individually. .. GENERATED FROM PYTHON SOURCE LINES 67-74 .. code-block:: default for col in range(proto.grid.columnCount()): node = proto.grid[0,col] node.setCellByName('Canine Ventricular (Hund-Rudy 2009)') proto.grid.simpleGrid() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), {'Canine Ventricular (Hund-Rudy 2009)': 0}) .. GENERATED FROM PYTHON SOURCE LINES 75-79 Now that all the cells have been setup we can configure the stimulus settings. Many of the settings are the same, however now we must also select the cells that we want to stimulate. Typically this is one edge or one corner of the grid, but it can be any cells as desired. .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: default proto.stimNodes = {(0,0), (0,1)} .. GENERATED FROM PYTHON SOURCE LINES 83-87 To select which cells will be stimulated create a set of tuples with the positions of cells to be stimulated as shown above. The rest of the settings can be modified in the same fashion as before. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default proto.stimt = 1000 .. GENERATED FROM PYTHON SOURCE LINES 91-92 By detault the simulation will be 5000 ms .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: default proto.tMax .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 5000.0 .. GENERATED FROM PYTHON SOURCE LINES 96-101 Measures and traces can also be taken, the setup is largely similar to single cell simulations with the addition of selecting the nodes to be measured. These nodes can be selected using the :py:class:`MeasureManager` and are the same nodes used for saving traces and taking measurements. For this example we will selected every node. .. GENERATED FROM PYTHON SOURCE LINES 101-108 .. code-block:: default proto.measureMgr.dataNodes = {(0,i) for i in range(proto.grid.columnCount())} proto.measureMgr.addMeasure('vOld', {'peak', 'min', 'cl'}) proto.measureMgr.addMeasure('iNa', {'min', 'avg'}) proto.measureMgr.selection .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'iNa': {'min', 'avg'}, 'vOld': {'min', 'peak', 'cl'}} .. GENERATED FROM PYTHON SOURCE LINES 109-110 The traces can be set in the same way as before using :py:data:`Protocol.cell` .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: default proto.cell.variableSelection = {'t', 'vOld', 'iNa'} .. GENERATED FROM PYTHON SOURCE LINES 114-115 Now the simulation is all setup and can be run. .. GENERATED FROM PYTHON SOURCE LINES 115-121 .. code-block:: default sim_runner = pylqt.RunSim() sim_runner.setSims(proto) sim_runner.run() sim_runner.wait() .. GENERATED FROM PYTHON SOURCE LINES 122-126 Plot the Results ---------------- Disable future warnings to avoid excess outputs from plotting .. GENERATED FROM PYTHON SOURCE LINES 127-131 .. code-block:: default import warnings warnings.simplefilter(action='ignore', category=FutureWarning) .. GENERATED FROM PYTHON SOURCE LINES 132-133 The data can once again be read using :py:class:`DataReader`. .. GENERATED FROM PYTHON SOURCE LINES 133-140 .. code-block:: default import pandas as pd import numpy as np import matplotlib.pyplot as plt [trace], [meas] = pylqt.DataReader.readAsDataFrame(proto.datadir) .. GENERATED FROM PYTHON SOURCE LINES 141-143 The trace DataFrame's columns now have two levels, one for the cell's position and one for the variable name. .. GENERATED FROM PYTHON SOURCE LINES 143-146 .. code-block:: default trace.columns[:5] .. rst-class:: sphx-glr-script-out Out: .. code-block:: none MultiIndex([((0, 2), 'iNa'), ((0, 2), 't'), ((0, 2), 'vOld'), ((0, 5), 'iNa'), ((0, 5), 't')], names=['Cell', 'Variable']) .. GENERATED FROM PYTHON SOURCE LINES 147-149 Similarly the measure DataFrame's columns have an additional level for the cell's position .. GENERATED FROM PYTHON SOURCE LINES 149-153 .. code-block:: default meas.columns[:5] .. rst-class:: sphx-glr-script-out Out: .. code-block:: none MultiIndex([((0, 0), 'iNa', 'avg'), ((0, 0), 'iNa', 'min'), ((0, 0), 'vOld', 'cl'), ((0, 0), 'vOld', 'min'), ((0, 0), 'vOld', 'peak')], names=['Cell', 'Variable', 'Property']) .. GENERATED FROM PYTHON SOURCE LINES 154-156 We can use the trace Dataframe to produce traces of each of the action potenitals in the fiber and color them by their location in the fiber .. GENERATED FROM PYTHON SOURCE LINES 156-170 .. code-block:: default plt.figure() colors = plt.cm.jet_r(np.linspace(0,1,n_cols)) for i in range(n_cols): trace_subset = trace[trace[((0,i),'t')] < 4_250] plt.plot(trace_subset[((0,i),'t')], trace_subset[((0,i),'vOld')], color=colors[i], label=str(i)) plt.xlabel('Time (ms)') plt.ylabel('Voltage (mV)') _ = plt.legend() .. image:: /auto_examples/images/sphx_glr_plot_3_1D_grid_simple_example_001.png :alt: plot 3 1D grid simple example :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 171-172 Then let's create one more plot to show the action potential propagation. .. GENERATED FROM PYTHON SOURCE LINES 172-185 .. code-block:: default plt.figure() colors = plt.cm.jet_r(np.linspace(0,1,n_cols)) for i in range(n_cols): trace_subset = trace[trace[((0,i),'t')] < 4_010] plt.plot(trace_subset[((0,i),'t')], trace_subset[((0,i),'vOld')], color=colors[i], label=str(i)) plt.xlabel('Time (ms)') _ = plt.ylabel('Voltage (mV)') .. image:: /auto_examples/images/sphx_glr_plot_3_1D_grid_simple_example_002.png :alt: plot 3 1D grid simple example :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.446 seconds) .. _sphx_glr_download_auto_examples_plot_3_1D_grid_simple_example.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_3_1D_grid_simple_example.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_3_1D_grid_simple_example.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_