<commit_before>#!/usr/bin/env python
import sys
import quantities as pq
import os.path
from itertools import chain, repeat
import ninemlcatalog
from nineml import units as un
from nineml.user import Property
from nineml.user.multi.dynamics import MultiDynamics
from nineml.user import DynamicsProperties
from pype9.simulator.base.cells import (
    MultiDynamicsWithSynapses, DynamicsWithSynapsesProperties,
    ConnectionParameterSet, ConnectionPropertySet)
from pype9.simulator.neuron import (
    CellMetaClass as NeuronCellMetaClass,
    Simulation as NeuronSimulation)
argv = sys.argv[1:]  # Save argv before it is clobbered by the NEST init.
from pype9.simulator.nest import (  # @IgnorePep8
    CellMetaClass as NESTCellMetaClass,
    Simulation as NESTSimulation)
from pype9.utils.testing import Comparer, input_step, input_freq  # @IgnorePep8
from pype9.simulator.nest.units import UnitHandler as UnitHandlerNEST  # @IgnorePep8
if __name__ == '__main__':
    from pype9.utils.testing import DummyTestCase as TestCase  # @UnusedImport
else:
    from unittest import TestCase  # @Reimport

cell_metaclasses = {'neuron': NeuronCellMetaClass,
                    'nest': NESTCellMetaClass}

NEST_RNG_SEED = 1234567890
NEURON_RNG_SEED = 987654321


class TestDynamics(TestCase):

    liaf_initial_states = {'v': -65.0 * pq.mV, 'end_refractory': 0.0 * pq.ms}
    liaf_nest_translations = {
        # the conversion to g_leak is a bit of a hack because it is
        # actually Cm / g_leak
        'Cm': ('C_m', 1), 'tau': ('tau_m', 1),
        'refractory_period': ('t_ref', 1), 'e_leak': ('E_L', 1),
        'v_reset': ('V_reset', 1), 'v': ('V_m', 1),
        'v_threshold': ('V_th', 1), 'end_refractory': (None, 1)}
    liaf_neuron_translations = {
        'tau': ('pas.g', 0.25),  # Hack to translate tau_m to g_leak
        'Cm': ('cm', 1),
        'refractory_period': ('trefrac', 1), 'e_leak': ('pas.e', 1),
        'v_reset': ('vreset', 1), 'v_threshold': ('vthresh', 1),
        'end_refractory': (None, 1), 'v': ('v', 1)}

    def test_izhi(self, plot=False, print_comparisons=False,
                  simulators=['nest', 'neuron'], dt=0.001, duration=100.0,
                  build_mode='force', **kwargs):  # @UnusedVariable
        # Force compilation of code generation
        # Perform comparison in subprocess
        comparer = Comparer(
            nineml_model=ninemlcatalog.load(
                'neuron/Izhikevich', 'Izhikevich'),
            state_variable='V', dt=dt, simulators=simulators,
            properties=ninemlcatalog.load(
                'neuron/Izhikevich', 'SampleIzhikevich'),
            initial_states={'U': -14.0 * pq.mV / pq.ms, 'V': -65.0 * pq.mV},
            neuron_ref='Izhikevich', nest_ref='izhikevich',
            input_signal=input_step('Isyn', 0.02, 50, 100, dt),
            nest_translations={'V': ('V_m', 1), 'U': ('U_m', 1),
                               'weight': (None, 1), 'C_m': (None, 1),
                               'theta': ('V_th', 1),
                               'alpha': (None, 1), 'beta': (None, 1),
                               'zeta': (None, 1)},
            neuron_translations={'C_m': (None, 1), 'weight': (None, 1),
                                 'V': ('v', 1), 'U': ('u', 1),
                                 'alpha': (None, 1), 'beta': (None, 1),
                                 'zeta': (None, 1), 'theta': ('vthresh', 1)},
            neuron_build_args={'build_mode': build_mode},
            nest_build_args={'build_mode': build_mode},
            build_name='Izhikevich9ML')
        comparer.simulate(duration * un.ms, nest_rng_seed=NEST_RNG_SEED,
                          neuron_rng_seed=NEURON_RNG_SEED)
        comparisons = comparer.compare()
        if print_comparisons:
            for (name1, name2), diff in comparisons.iteritems():
                print '{} v {}: {}'.format(name1, name2, diff)
        if plot:
            comparer.plot()
        if 'nest' in simulators and 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', '9ML-neuron')], 0.4 * pq.mV,
                "Izhikevich NEURON 9ML simulation did not match NEST 9ML")
        if 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-neuron', 'Ref-neuron')], 0.0015 * pq.mV,
                "Izhikevich NEURON 9ML simulation did not match reference "
                "PyNN")
        if 'nest' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', 'Ref-nest')], 0.02 * pq.mV,
                "Izhikevich NEST 9ML simulation did not match reference "
                "built-in")

    def test_hh(self, plot=False, print_comparisons=False,
                simulators=['nest', 'neuron'], dt=0.001, duration=100.0,
                build_mode='force', **kwargs):  # @UnusedVariable
        # Perform comparison in subprocess
        comparer = Comparer(
            nineml_model=ninemlcatalog.load(
                'neuron/HodgkinHuxley', 'PyNNHodgkinHuxley'),
            state_variable='v', dt=dt, simulators=simulators,
            initial_states={'v': -65.0 * pq.mV, 'm': 0.0, 'h': 1.0, 'n': 0.0},
            properties=ninemlcatalog.load(
                'neuron/HodgkinHuxley', 'PyNNHodgkinHuxleyProperties'),
            neuron_ref='hh_traub', nest_ref='hh_cond_exp_traub',
            input_signal=input_step('iExt', 0.5, 50, 100, dt),
            nest_translations={
                'v': ('V_m', 1), 'm': ('Act_m', 1),
                'h': ('Act_h', 1), 'n': ('Inact_n', 1),
                'eNa': ('E_Na', 1), 'eK': ('E_K', 1), 'C': ('C_m', 1),
                'gLeak': ('g_L', 1), 'eLeak': ('E_L', 1),
                'gbarNa': ('g_Na', 1), 'gbarK': ('g_K', 1),
                'v_rest': ('V_T', 1), 'v_threshold': (None, 1),
                'm_alpha_A': (None, 1), 'm_alpha_V0': (None, 1),
                'm_alpha_K': (None, 1), 'm_beta_A': (None, 1),
                'm_beta_V0': (None, 1), 'm_beta_K': (None, 1),
                'h_alpha_A': (None, 1), 'h_alpha_V0': (None, 1),
                'h_alpha_K': (None, 1), 'h_beta_A': (None, 1),
                'h_beta_V0': (None, 1), 'h_beta_K': (None, 1),
                'n_alpha_A': (None, 1), 'n_alpha_V0': (None, 1),
                'n_alpha_K': (None, 1), 'n_beta_A': (None, 1),
                'n_beta_V0': (None, 1), 'n_beta_K': (None, 1)},
            neuron_translations={
                'eNa': ('ena', 1), 'eK': ('ek', 1), 'C': ('cm', 1),
                'gLeak': ('gl', 1), 'eLeak': ('el', 1),
                'gbarNa': ('gnabar', 1), 'gbarK': ('gkbar', 1),
                'v_rest': ('vT', 1), 'v_threshold': (None, 1),
                'm_alpha_A': (None, 1), 'm_alpha_V0': (None, 1),
                'm_alpha_K': (None, 1), 'm_beta_A': (None, 1),
                'm_beta_V0': (None, 1), 'm_beta_K': (None, 1),
                'h_alpha_A': (None, 1), 'h_alpha_V0': (None, 1),
                'h_alpha_K': (None, 1), 'h_beta_A': (None, 1),
                'h_beta_V0': (None, 1), 'h_beta_K': (None, 1),
                'n_alpha_A': (None, 1), 'n_alpha_V0': (None, 1),
                'n_alpha_K': (None, 1), 'n_beta_A': (None, 1),
                'n_beta_V0': (None, 1), 'n_beta_K': (None, 1)},
            neuron_build_args={'build_mode': build_mode},
            nest_build_args={'build_mode': build_mode})
        comparer.simulate(duration * un.ms, nest_rng_seed=NEST_RNG_SEED,
                          neuron_rng_seed=NEURON_RNG_SEED)
        comparisons = comparer.compare()
        if print_comparisons:
            for (name1, name2), diff in comparisons.iteritems():
                print '{} v {}: {}'.format(name1, name2, diff)
        if plot:
            comparer.plot()
        # FIXME: Need to work out what is happening with the reference NEURON
        if 'nest' in simulators and 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', '9ML-neuron')], 0.5 * pq.mV,
                "HH 9ML NEURON and NEST simulation did not match each other")
        if 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-neuron', 'Ref-neuron')], 0.55 * pq.mV,
                "HH 9ML NEURON simulation did not match reference built-in")
        if 'nest' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', 'Ref-nest')], 0.0015 * pq.mV,
                "HH 9ML NEST simulation did not match reference built-in")

    def test_liaf(self, plot=False, print_comparisons=False,
                  simulators=['nest', 'neuron'], dt=0.001, duration=100.0,
                  build_mode='force', **kwargs):  # @UnusedVariable
        # Perform comparison in subprocess
        comparer = Comparer(
            nineml_model=ninemlcatalog.load(
                'neuron/LeakyIntegrateAndFire',
                'PyNNLeakyIntegrateAndFire'),
            state_variable='v', dt=dt, simulators=simulators,
            properties=ninemlcatalog.load(
                'neuron/LeakyIntegrateAndFire',
                'PyNNLeakyIntegrateAndFireProperties'),
            initial_states=self.liaf_initial_states,
            initial_regime='subthreshold',
            neuron_ref='ResetRefrac', nest_ref='iaf_psc_alpha',
            input_signal=input_step('i_synaptic', 1, 50, 100, dt),
            nest_translations=self.liaf_nest_translations,
            neuron_translations=self.liaf_neuron_translations,
            neuron_build_args={'build_mode': build_mode},
            nest_build_args={'build_mode': build_mode},
            extra_mechanisms=['pas'])
        comparer.simulate(duration * un.ms, nest_rng_seed=NEST_RNG_SEED,
                          neuron_rng_seed=NEURON_RNG_SEED)
        comparisons = comparer.compare()
        if print_comparisons:<commit_msg>added more informative failure messages<commit_after>#!/usr/bin/env python
import sys
import quantities as pq
import os.path
from itertools import chain, repeat
import ninemlcatalog
from nineml import units as un
from nineml.user import Property
from nineml.user.multi.dynamics import MultiDynamics
from nineml.user import DynamicsProperties
from pype9.simulator.base.cells import (
    MultiDynamicsWithSynapses, DynamicsWithSynapsesProperties,
    ConnectionParameterSet, ConnectionPropertySet)
from pype9.simulator.neuron import (
    CellMetaClass as NeuronCellMetaClass,
    Simulation as NeuronSimulation)
argv = sys.argv[1:]  # Save argv before it is clobbered by the NEST init.
from pype9.simulator.nest import (  # @IgnorePep8
    CellMetaClass as NESTCellMetaClass,
    Simulation as NESTSimulation)
from pype9.utils.testing import Comparer, input_step, input_freq  # @IgnorePep8
from pype9.simulator.nest.units import UnitHandler as UnitHandlerNEST  # @IgnorePep8
if __name__ == '__main__':
    from pype9.utils.testing import DummyTestCase as TestCase  # @UnusedImport
else:
    from unittest import TestCase  # @Reimport

cell_metaclasses = {'neuron': NeuronCellMetaClass,
                    'nest': NESTCellMetaClass}

NEST_RNG_SEED = 1234567890
NEURON_RNG_SEED = 987654321


class TestDynamics(TestCase):

    liaf_initial_states = {'v': -65.0 * pq.mV, 'end_refractory': 0.0 * pq.ms}
    liaf_nest_translations = {
        # the conversion to g_leak is a bit of a hack because it is
        # actually Cm / g_leak
        'Cm': ('C_m', 1), 'tau': ('tau_m', 1),
        'refractory_period': ('t_ref', 1), 'e_leak': ('E_L', 1),
        'v_reset': ('V_reset', 1), 'v': ('V_m', 1),
        'v_threshold': ('V_th', 1), 'end_refractory': (None, 1)}
    liaf_neuron_translations = {
        'tau': ('pas.g', 0.25),  # Hack to translate tau_m to g_leak
        'Cm': ('cm', 1),
        'refractory_period': ('trefrac', 1), 'e_leak': ('pas.e', 1),
        'v_reset': ('vreset', 1), 'v_threshold': ('vthresh', 1),
        'end_refractory': (None, 1), 'v': ('v', 1)}

    def test_izhi(self, plot=False, print_comparisons=False,
                  simulators=['nest', 'neuron'], dt=0.001, duration=100.0,
                  build_mode='force', **kwargs):  # @UnusedVariable
        # Force compilation of code generation
        # Perform comparison in subprocess
        comparer = Comparer(
            nineml_model=ninemlcatalog.load(
                'neuron/Izhikevich', 'Izhikevich'),
            state_variable='V', dt=dt, simulators=simulators,
            properties=ninemlcatalog.load(
                'neuron/Izhikevich', 'SampleIzhikevich'),
            initial_states={'U': -14.0 * pq.mV / pq.ms, 'V': -65.0 * pq.mV},
            neuron_ref='Izhikevich', nest_ref='izhikevich',
            input_signal=input_step('Isyn', 0.02, 50, 100, dt),
            nest_translations={'V': ('V_m', 1), 'U': ('U_m', 1),
                               'weight': (None, 1), 'C_m': (None, 1),
                               'theta': ('V_th', 1),
                               'alpha': (None, 1), 'beta': (None, 1),
                               'zeta': (None, 1)},
            neuron_translations={'C_m': (None, 1), 'weight': (None, 1),
                                 'V': ('v', 1), 'U': ('u', 1),
                                 'alpha': (None, 1), 'beta': (None, 1),
                                 'zeta': (None, 1), 'theta': ('vthresh', 1)},
            neuron_build_args={'build_mode': build_mode},
            nest_build_args={'build_mode': build_mode},
            build_name='Izhikevich9ML')
        comparer.simulate(duration * un.ms, nest_rng_seed=NEST_RNG_SEED,
                          neuron_rng_seed=NEURON_RNG_SEED)
        comparisons = comparer.compare()
        if print_comparisons:
            for (name1, name2), diff in comparisons.iteritems():
                print '{} v {}: {}'.format(name1, name2, diff)
        if plot:
            comparer.plot()
        if 'nest' in simulators and 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', '9ML-neuron')], 0.4 * pq.mV,
                "Izhikevich NEURON 9ML simulation did not match NEST 9ML")
        if 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-neuron', 'Ref-neuron')], 0.0015 * pq.mV,
                "Izhikevich NEURON 9ML simulation did not match reference "
                "PyNN")
        if 'nest' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', 'Ref-nest')], 0.02 * pq.mV,
                "Izhikevich NEST 9ML simulation did not match reference "
                "built-in")

    def test_hh(self, plot=False, print_comparisons=False,
                simulators=['nest', 'neuron'], dt=0.001, duration=100.0,
                build_mode='force', **kwargs):  # @UnusedVariable
        # Perform comparison in subprocess
        comparer = Comparer(
            nineml_model=ninemlcatalog.load(
                'neuron/HodgkinHuxley', 'PyNNHodgkinHuxley'),
            state_variable='v', dt=dt, simulators=simulators,
            initial_states={'v': -65.0 * pq.mV, 'm': 0.0, 'h': 1.0, 'n': 0.0},
            properties=ninemlcatalog.load(
                'neuron/HodgkinHuxley', 'PyNNHodgkinHuxleyProperties'),
            neuron_ref='hh_traub', nest_ref='hh_cond_exp_traub',
            input_signal=input_step('iExt', 0.5, 50, 100, dt),
            nest_translations={
                'v': ('V_m', 1), 'm': ('Act_m', 1),
                'h': ('Act_h', 1), 'n': ('Inact_n', 1),
                'eNa': ('E_Na', 1), 'eK': ('E_K', 1), 'C': ('C_m', 1),
                'gLeak': ('g_L', 1), 'eLeak': ('E_L', 1),
                'gbarNa': ('g_Na', 1), 'gbarK': ('g_K', 1),
                'v_rest': ('V_T', 1), 'v_threshold': (None, 1),
                'm_alpha_A': (None, 1), 'm_alpha_V0': (None, 1),
                'm_alpha_K': (None, 1), 'm_beta_A': (None, 1),
                'm_beta_V0': (None, 1), 'm_beta_K': (None, 1),
                'h_alpha_A': (None, 1), 'h_alpha_V0': (None, 1),
                'h_alpha_K': (None, 1), 'h_beta_A': (None, 1),
                'h_beta_V0': (None, 1), 'h_beta_K': (None, 1),
                'n_alpha_A': (None, 1), 'n_alpha_V0': (None, 1),
                'n_alpha_K': (None, 1), 'n_beta_A': (None, 1),
                'n_beta_V0': (None, 1), 'n_beta_K': (None, 1)},
            neuron_translations={
                'eNa': ('ena', 1), 'eK': ('ek', 1), 'C': ('cm', 1),
                'gLeak': ('gl', 1), 'eLeak': ('el', 1),
                'gbarNa': ('gnabar', 1), 'gbarK': ('gkbar', 1),
                'v_rest': ('vT', 1), 'v_threshold': (None, 1),
                'm_alpha_A': (None, 1), 'm_alpha_V0': (None, 1),
                'm_alpha_K': (None, 1), 'm_beta_A': (None, 1),
                'm_beta_V0': (None, 1), 'm_beta_K': (None, 1),
                'h_alpha_A': (None, 1), 'h_alpha_V0': (None, 1),
                'h_alpha_K': (None, 1), 'h_beta_A': (None, 1),
                'h_beta_V0': (None, 1), 'h_beta_K': (None, 1),
                'n_alpha_A': (None, 1), 'n_alpha_V0': (None, 1),
                'n_alpha_K': (None, 1), 'n_beta_A': (None, 1),
                'n_beta_V0': (None, 1), 'n_beta_K': (None, 1)},
            neuron_build_args={'build_mode': build_mode},
            nest_build_args={'build_mode': build_mode})
        comparer.simulate(duration * un.ms, nest_rng_seed=NEST_RNG_SEED,
                          neuron_rng_seed=NEURON_RNG_SEED)
        comparisons = comparer.compare()
        if print_comparisons:
            for (name1, name2), diff in comparisons.iteritems():
                print '{} v {}: {}'.format(name1, name2, diff)
        if plot:
            comparer.plot()
        # FIXME: Need to work out what is happening with the reference NEURON
        if 'nest' in simulators and 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', '9ML-neuron')], 0.5 * pq.mV,
                "HH 9ML NEURON and NEST simulation did not match each other "
                "within {} ({})".format(
                    0.5 * pq.mV, comparisons[('9ML-nest', '9ML-neuron')]))
        if 'neuron' in simulators:
            self.assertLess(
                comparisons[('9ML-neuron', 'Ref-neuron')], 0.55 * pq.mV,
                "HH 9ML NEURON simulation did not match reference built-in "
                "within {} ({})".format(
                    0.55 * pq.mV, comparisons[('9ML-neuron', 'Ref-neuron')]))
        if 'nest' in simulators:
            self.assertLess(
                comparisons[('9ML-nest', 'Ref-nest')], 0.0015 * pq.mV,
                "HH 9ML NEST simulation did not match reference built-in "
                "within {} ({})".format(
                    0.0015 * pq.mV, comparisons[('9ML-nest', 'Ref-nest')]))

    def test_liaf(self, plot=False, print_comparisons=False,
                  simulators=['nest', 'neuron'], dt=0.001, duration=100.0,
                  build_mode='force', **kwargs):  # @UnusedVariable
        # Perform comparison in subprocess
        comparer = Comparer(
            nineml_model=ninemlcatalog.load(
                'neuron/LeakyIntegrateAndFire',
                'PyNNLeakyIntegrateAndFire'),
            state_variable='v', dt=dt, simulators=simulators,
            properties=ninemlcatalog.load(
                'neuron/LeakyIntegrateAndFire',
                'PyNNLeakyIntegrateAndFireProperties'),
            initial_states=self.liaf_initial_states,
            initial_regime='subthreshold',
            neuron_ref='ResetRefrac', nest_ref='iaf_psc_alpha',
            input_signal=input_step('i_synaptic', 1, 50, 100, dt),
            nest_translations=self.liaf_nest_translations,
            neuron_translations=self.liaf_neuron_translations,
            neuron_build_args={'build_mode': build_mode},
            nest_build_args={'build_mode': build_mode},
            extra_mechanisms=['pas'])
        comparer.simulate(duration * un.ms, nest_rng_seed=NEST_RNG_SEED,
                          neuron_rng_seed=NEURON_RNG_SEED)
        comparisons = comparer.compare()
        if print_comparisons:
{'fraction_non_alphanumeric': 0.14820374261983388, 'fraction_numerical': 0.024917442209546684, 'mean_word_length': 2.197408414653655, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3934982', 'n_tokens_mistral': 7297, 'n_tokens_neox': 6739, 'n_words': 1495}