testProbUDF.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python3
  2. """
  3. Test the ProbUDF synapse
  4. File: NeuroML2/synapses/testProbUDF.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_probUDF") # 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="probUDFsyn")
  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 = "testProbUDF.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("ProbUDF.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", "TestPop/0/0/synapses:probUDFsyn:0/i")
  61. simulation.add_column_to_output_file("output1", "g", "TestPop/0/0/synapses:probUDFsyn:0/g")
  62. simulation.add_column_to_output_file("output1", "A", "TestPop/0/0/synapses:probUDFsyn:0/A")
  63. simulation.add_column_to_output_file("output1", "B", "TestPop/0/0/synapses:probUDFsyn:0/B")
  64. sim_filename = lems_simulation_file = simulation.save_to_file()
  65. data = run_lems_with_jneuroml_neuron(sim_filename, max_memory="8G", skip_run=False, nogui=True, compile_mods=True, load_saved_data=True)
  66. return data
  67. def plots(data):
  68. """Plot bits"""
  69. print("Generating plots")
  70. print(f"Data found: {data.keys()}")
  71. yvalues=[data['TestPop/0/0/v']]
  72. generate_plot(xvalues=numpy.array([data['t']] * len(yvalues)) * 1000,
  73. yvalues=numpy.array(yvalues) * 1000,
  74. title="Membrane potential (mV)",
  75. labels=["v"], show_plot_already=False)
  76. yvalues1=[data['TestPop/0/0/synapses:probUDFsyn:0/A'],
  77. data['TestPop/0/0/synapses:probUDFsyn:0/B'],
  78. ]
  79. generate_plot(xvalues=[data['t']] * len(yvalues1),
  80. yvalues=yvalues1,
  81. title="States",
  82. labels=[
  83. "A", "B"
  84. ], show_plot_already=False)
  85. # conductances, multiple by 10e6 to convert to uS to match NEURON mod file
  86. yvalues2=[data['TestPop/0/0/synapses:probUDFsyn:0/g']]
  87. generate_plot(xvalues=[data['t']] * len(yvalues2),
  88. yvalues=numpy.array(yvalues2),
  89. title="Conductances (S)",
  90. labels=["g"], show_plot_already=False)
  91. pyplot.show()
  92. if __name__ == "__main__":
  93. simulation_id = "test_probUDF"
  94. try:
  95. data = reload_saved_data(f"LEMS_{simulation_id}.xml")
  96. print("Data already exists, plotting directly. Remove data files to re-run simulation")
  97. except OSError:
  98. print("Data wasn't found. Re-running simulation")
  99. data = main(simulation_id)
  100. except Exception:
  101. print("Data wasn't found. Re-running simulation")
  102. data = main(simulation_id)
  103. plots(data)