1"""
2
3Export of PyNN models to NeuroML 2
4See https://github.com/NeuroML/NeuroML2/issues/73 for more details
5
6Contact Padraig Gleeson for more details
7
8:copyright: Copyright 2006-2017 by the PyNN team, see AUTHORS.
9:license: CeCILL, see LICENSE for details.
10"""
11
12import logging
13from pyNN import common
14from pyNN.common.control import DEFAULT_MAX_DELAY, DEFAULT_TIMESTEP, DEFAULT_MIN_DELAY
15from pyNN.connectors import *
16from pyNN.recording import *
17from pyNN.neuroml import simulator
18from pyNN.neuroml.standardmodels import *
19from pyNN.neuroml.standardmodels.synapses import *
20from pyNN.neuroml.standardmodels.cells import *
21from pyNN.neuroml.standardmodels.electrodes import *
22from pyNN.neuroml.populations import Population, PopulationView, Assembly
23from pyNN.neuroml.projections import Projection
24
25from neo.io import get_io
26
27import neuroml
28
29logger = logging.getLogger("PyNN_NeuroML")
30
31save_format = 'xml'
32
33
34def list_standard_models():
35    """Return a list of all the StandardCellType classes available for this simulator."""
36    return [obj.__name__ for obj in globals().values() if isinstance(obj, type) and issubclass(obj, StandardCellType)]
37
38
39def setup(timestep=DEFAULT_TIMESTEP, min_delay=DEFAULT_MIN_DELAY,
40          **extra_params):
41    """ Set up for saving cell models and network structure to NeuroML """
42    common.setup(timestep, min_delay, **extra_params)
43    simulator.state.clear()
44    simulator.state.dt = timestep  # move to common.setup?
45    simulator.state.min_delay = min_delay
46    simulator.state.max_delay =  extra_params.get('max_delay', DEFAULT_MAX_DELAY)
47    simulator.state.mpi_rank = extra_params.get('rank', 0)
48    simulator.state.num_processes = extra_params.get('num_processes', 1)
49
50    logger.debug("Creating network in NeuroML document to store structure")
51    nml_doc = simulator._get_nml_doc(extra_params.get('reference', "PyNN_NeuroML2_Export"),reset=True)
52    global save_format
53    save_format = extra_params.get('save_format', "xml")
54
55    # Create network
56    net = neuroml.Network(id=nml_doc.id)
57    nml_doc.networks.append(net)
58
59    lems_sim = simulator._get_lems_sim(reset=True)
60    lems_sim.dt = '%s'%timestep
61
62    return rank()
63
64
65def end(compatible_output=True):
66    """Do any necessary cleaning up before exiting."""
67    for (population, variables, filename) in simulator.state.write_on_end:
68        io = get_io(filename)
69        population.write_data(io, variables)
70    simulator.state.write_on_end = []
71
72    nml_doc = simulator._get_nml_doc()
73
74    import neuroml.writers as writers
75    if save_format == 'xml':
76        nml_file = '%s.net.nml'%nml_doc.id
77        writers.NeuroMLWriter.write(nml_doc, nml_file)
78    elif save_format == 'hdf5':
79        nml_file = '%s.net.nml.h5'%nml_doc.id
80        writers.NeuroMLHdf5Writer.write(nml_doc, nml_file)
81
82    logger.info("Written NeuroML 2 file out to: "+nml_file)
83
84    lems_sim = simulator._get_lems_sim()
85    lems_sim.include_neuroml2_file("PyNN.xml", include_included=False)
86    lems_sim.include_neuroml2_file(nml_file)
87    lems_file = lems_sim.save_to_file()
88    logger.info("Written LEMS file (to simulate NeuroML file) to: "+lems_file)
89
90    # should have common implementation of end()
91
92
93run, run_until = common.build_run(simulator)
94run_for = run
95
96reset = common.build_reset(simulator)
97
98initialize = common.initialize
99
100get_current_time, get_time_step, get_min_delay, get_max_delay, \
101                    num_processes, rank = common.build_state_queries(simulator)
102
103
104create = common.build_create(Population)
105
106connect = common.build_connect(Projection, FixedProbabilityConnector, StaticSynapse)
107
108#set = common.set
109
110record = common.build_record(simulator)
111
112record_v = lambda source, filename: record(['v'], source, filename)
113
114record_gsyn = lambda source, filename: record(['gsyn_exc', 'gsyn_inh'], source, filename)
115