testProbAMPANMDA.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python3
  2. """
  3. Test the ProbAMPANMDA synapse
  4. File: NeuroML2/synapses/testProbAMPANMDA.py
  5. Copyright 2023 Ankur Sinha
  6. Author: Ankur Sinha <sanjay DOT ankur AT gmail DOT com>
  7. """
  8. import sys
  9. import numpy
  10. import neuroml
  11. from neuroml.loaders import read_neuroml2_file
  12. from neuroml.utils import component_factory
  13. from pyneuroml.lems.LEMSSimulation import LEMSSimulation
  14. from pyneuroml.plot import generate_plot
  15. from pyneuroml.pynml import (reload_saved_data, run_lems_with_jneuroml_neuron,
  16. write_neuroml2_file)
  17. from matplotlib import pyplot
  18. def main(simulation_id):
  19. """main method that creates sim and runs it."""
  20. # a new document
  21. newdoc = component_factory(neuroml.NeuroMLDocument, id="test_probAMPANMDA") # type: neuroml.NeuroMLDocument
  22. # add a cell
  23. newdoc.add(neuroml.IncludeType(href="passiveCell.cell.nml"))
  24. # cell = read_neuroml2_file("passiveCell.cell.nml").cells[0]
  25. # newdoc.add(cell)
  26. net = newdoc.add(neuroml.Network, id="TestNet", validate=False)
  27. # Create a population of defined cells and add it to the model
  28. pop = net.add(neuroml.Population, id="TestPop", component="passiveCell",
  29. type="populationList", validate=False)
  30. pop.add(neuroml.Instance, id=0, location=neuroml.Location(x=0, y=0, z=0))
  31. # set up a spike array
  32. spikearray = newdoc.add(neuroml.SpikeArray, id="spikeArray", validate=False)
  33. idval = 0
  34. for t in [100, 130, 160]:
  35. spikearray.add(neuroml.Spike, id=idval, time=f"{t} ms")
  36. idval += 1
  37. stimpop = net.add("Population", id="SpikePop", component=spikearray.id, size=1)
  38. proj = net.add(neuroml.Projection, id="proj", presynaptic_population="SpikePop",
  39. postsynaptic_population="TestPop", synapse="probAMPANMDA")
  40. proj.add(neuroml.ConnectionWD, id=0, pre_cell_id="../SpikePop[0]",
  41. post_cell_id="../TestPop/0/0", weight="1e-3", delay="0 ms")
  42. # validate the current document
  43. newdoc.validate(recursive=True)
  44. nml_file = "testProbAMPANMDA.net.nml"
  45. # No longer valid NeuroML because it includes a new Component that the
  46. # Schema does not know about
  47. write_neuroml2_file(newdoc, nml_file, validate=True)
  48. # Create simulation, and record data
  49. simulation = LEMSSimulation(
  50. sim_id=simulation_id, duration=500, dt=0.01, simulation_seed=123
  51. )
  52. # Include the new Component
  53. simulation.include_lems_file("ProbAMPANMDA.synapse.xml",
  54. include_included=True)
  55. simulation.assign_simulation_target(net.id)
  56. simulation.include_neuroml2_file(nml_file)
  57. # record other variables
  58. simulation.create_output_file("output1", f"{simulation_id}.output.dat")
  59. simulation.add_column_to_output_file("output1", "v", "TestPop/0/0/v")
  60. simulation.add_column_to_output_file("output1", "i_AMPA", "TestPop/0/0/synapses:probAMPANMDA:0/i_AMPA")
  61. simulation.add_column_to_output_file("output1", "i_NMDA", "TestPop/0/0/synapses:probAMPANMDA:0/i_NMDA")
  62. simulation.add_column_to_output_file("output1", "g_AMPA", "TestPop/0/0/synapses:probAMPANMDA:0/g_AMPA")
  63. simulation.add_column_to_output_file("output1", "g_NMDA", "TestPop/0/0/synapses:probAMPANMDA:0/g_NMDA")
  64. simulation.add_column_to_output_file("output1", "A_AMPA", "TestPop/0/0/synapses:probAMPANMDA:0/A_AMPA")
  65. simulation.add_column_to_output_file("output1", "A_NMDA", "TestPop/0/0/synapses:probAMPANMDA:0/A_NMDA")
  66. simulation.add_column_to_output_file("output1", "B_AMPA", "TestPop/0/0/synapses:probAMPANMDA:0/B_AMPA")
  67. simulation.add_column_to_output_file("output1", "B_NMDA", "TestPop/0/0/synapses:probAMPANMDA:0/B_NMDA")
  68. sim_filename = lems_simulation_file = simulation.save_to_file()
  69. data = run_lems_with_jneuroml_neuron(sim_filename, max_memory="8G", skip_run=False, nogui=True, compile_mods=True, load_saved_data=True)
  70. return data
  71. def plots(data):
  72. """Plot bits"""
  73. print("Generating plots")
  74. print(f"Data found: {data.keys()}")
  75. yvalues=[data['TestPop/0/0/v']]
  76. generate_plot(xvalues=numpy.array([data['t']] * len(yvalues)) * 1000,
  77. yvalues=numpy.array(yvalues) * 1000,
  78. title="Membrane potential (mV)",
  79. labels=["v"], show_plot_already=False)
  80. yvalues1=[data['TestPop/0/0/synapses:probAMPANMDA:0/A_AMPA'],
  81. data['TestPop/0/0/synapses:probAMPANMDA:0/A_NMDA'],
  82. data['TestPop/0/0/synapses:probAMPANMDA:0/B_AMPA'],
  83. data['TestPop/0/0/synapses:probAMPANMDA:0/B_NMDA']]
  84. generate_plot(xvalues=[data['t']] * len(yvalues1),
  85. yvalues=yvalues1,
  86. title="States",
  87. labels=[
  88. "A_AMPA", "A_NMDA", "B_AMPA", "B_NMDA"
  89. ], show_plot_already=False)
  90. # conductances, multiple by 10e6 to convert to uS to match NEURON mod file
  91. yvalues2=[data['TestPop/0/0/synapses:probAMPANMDA:0/g_AMPA'],
  92. data['TestPop/0/0/synapses:probAMPANMDA:0/g_NMDA']
  93. ]
  94. generate_plot(xvalues=[data['t']] * len(yvalues2),
  95. yvalues=numpy.array(yvalues2),
  96. title="Conductances (S)",
  97. labels=["g_AMPA", "g_NMDA"], show_plot_already=False)
  98. pyplot.show()
  99. if __name__ == "__main__":
  100. simulation_id = "test_probAMPANMDA"
  101. try:
  102. data = reload_saved_data(f"LEMS_{simulation_id}.xml")
  103. print("Data already exists, plotting directly. Remove data files to re-run simulation")
  104. except OSError:
  105. print("Data wasn't found. Re-running simulation")
  106. data = main(simulation_id)
  107. except Exception:
  108. print("Data wasn't found. Re-running simulation")
  109. data = main(simulation_id)
  110. plots(data)