def test_good_model(self): Dynamics(name='A', aliases=['A1:=P1 * SV2', 'A2 := ARP1 + SV2', 'A3 := SV1'], state_variables=[ StateVariable('SV1', dimension=un.voltage), StateVariable('SV2', dimension=un.current) ], regimes=[ Regime('dSV1/dt = -SV1 / P2', 'dSV2/dt = A3 / ARP2 + SV2 / P2', transitions=[ On('SV1 > P3', do=[OutputEvent('emit')]), On('spikein', do=[OutputEvent('emit')]) ], name='R1'), Regime(name='R2', transitions=On('(SV1 > C1) & (SV2 < P4)', to='R1')) ], analog_ports=[ AnalogReceivePort('ARP1', dimension=un.current), AnalogReceivePort('ARP2', dimension=(un.resistance * un.time)), AnalogSendPort('A1', dimension=un.voltage * un.current), AnalogSendPort('A2', dimension=un.current) ], parameters=[ Parameter('P1', dimension=un.voltage), Parameter('P2', dimension=un.time), Parameter('P3', dimension=un.voltage), Parameter('P4', dimension=un.current) ], constants=[Constant('C1', value=1.0, units=un.mV)])
def test_rename_symbol(self): a = Dynamics( name='A', aliases=['A1_a:=P1_a', 'A2_a := ARP1_a + SV2_a', 'A3_a := SV1_a'], regimes=[ Regime( 'dSV1_a/dt = -SV1_a / (P2_a*t)', 'dSV2_a/dt = SV1_a / (ARP1_a*t) + SV2_a / (P1_a*t)', transitions=[ On('SV1_a > P1_a', do=[OutputEvent('emit')]), On('spikein', do=[OutputEvent('emit')]) ], name='R1_a', ), Regime(name='R2_a', transitions=On('SV1_a > 1', to='R1_a')) ], analog_ports=[ AnalogReceivePort('ARP1_a'), AnalogReceivePort('ARP2_a'), AnalogSendPort('A1_a'), AnalogSendPort('A2_a') ], parameters=['P1_a', 'P2_a']) b = Dynamics( name='A', aliases=['A1_b:=P1_b', 'A2_b := ARP1_b + SV2_b', 'A3_b := SV1_b'], regimes=[ Regime( 'dSV1_b/dt = -SV1_b / (P2_b*t)', 'dSV2_b/dt = SV1_b / (ARP1_b*t) + SV2_b / (P1_b*t)', transitions=[ On('SV1_b > P1_b', do=[OutputEvent('emit')]), On('spikein', do=[OutputEvent('emit')]) ], name='R1_b', ), Regime(name='R2_b', transitions=On('SV1_b > 1', to='R1_b')) ], analog_ports=[ AnalogReceivePort('ARP1_b'), AnalogReceivePort('ARP2_b'), AnalogSendPort('A1_b'), AnalogSendPort('A2_b') ], parameters=['P1_b', 'P2_b']) for symbol in ('A1', 'A2', 'A3', 'SV1', 'SV2', 'ARP1', 'ARP2', 'P1', 'P2', 'R1', 'R2'): a.rename_symbol(symbol + '_a', symbol + '_b') a == b self.assertEqual( a, b, "Symbols were not renamed properly between classes:\n" "{}".format(a.find_mismatch(b)))
def setUp(self): self.a = Dynamics(name='A', aliases=['A1:=P1', 'A2 := ARP2', 'A3 := SV1'], regimes=[ Regime('dSV1/dt = -SV1 / (P2*t)', 'dSV2/dt = A2/t + A3/t + ARP1/t', name='R1', transitions=On('input', 'SV1 = SV1 + 1')) ], analog_ports=[ AnalogReceivePort('ARP1'), AnalogReceivePort('ARP2'), AnalogSendPort('A1'), AnalogSendPort('A2') ], parameters=['P1', 'P2']) self.b = Dynamics(name='B', aliases=['A1:=P1', 'A2 := ARP1 + SV2', 'A3 := SV1'], regimes=[ Regime( 'dSV1/dt = -SV1 / (P2*t)', 'dSV2/dt = SV1 / (ARP1*t) + SV2 / (P1*t)', transitions=[ On('SV1 > P1', do=[OutputEvent('emit')]), On('spikein', do=[OutputEvent('emit')]) ], aliases=[Alias('A1', 'P1 * 2')], name='R1', ), Regime(name='R2', transitions=On( 'SV1 > 1', 'SV1 = SV1 * random.normal()', to='R1')) ], analog_ports=[ AnalogReceivePort('ARP1'), AnalogReceivePort('ARP2'), AnalogSendPort('A1'), AnalogSendPort('A2') ], parameters=['P1', 'P2'])
def test_add(self): # Copy templates a = self.a.clone() b = self.b.clone() # Add missing items a.add(Alias('A4', 'SV1^3 + SV2^-3')) a.add(StateVariable('SV3')) a.regime('R1').add(TimeDerivative('SV3', '-SV3/t + P3/t')) a.regime('R1').on_event('spikein').add(StateAssignment('SV1', 'P1')) a.regime('R2').add( OnCondition('SV3 < 0.001', target_regime_name='R2', state_assignments=[StateAssignment('SV3', 1)])) a.add(Parameter('P3')) a.add(AnalogSendPort('SV3')) a.bind() a.validate() self.assertEqual( b, a, "Did not transform 'a' into 'b':\n {}".format(b.find_mismatch(a)))
def test_mismatch_with_declared(self): self.assertRaises( NineMLDimensionError, Dynamics, name='A', state_variables=[StateVariable('SV1', dimension=un.voltage)], regimes=[ Regime('dSV1/dt = SV1/t', name='R1'), ], analog_ports=[AnalogSendPort('SV1', dimension=un.current)], ) self.assertRaises( NineMLDimensionError, Dynamics, name='A', state_variables=[StateVariable('SV1', dimension=un.voltage)], regimes=[ Regime('dSV1/dt = SV1 * P1', name='R1'), ], parameters=[Parameter('P1', dimension=un.time)], ) self.assertRaises( NineMLDimensionError, Dynamics, name='A', state_variables=[StateVariable('SV1', dimension=un.voltage)], regimes=[ Regime('dSV1/dt = SV1/t', transitions=[ On('SV1 > P1', do=[StateAssignment('SV1', 'P2')]) ], name='R1'), ], parameters=[ Parameter('P1', dimension=un.voltage), Parameter('P2', dimension=un.time) ], )
def setUp(self): self.a = Dynamics(name='A', aliases=['A1:=P1', 'A2 := ARP1 + SV2', 'A3 := SV1'], regimes=[ Regime( 'dSV1/dt = -SV1 / (P2*t)', 'dSV2/dt = SV1 / (ARP1*t) + SV2 / (P1*t)', transitions=[ On('SV1 > P1', do=[OutputEvent('emit')]), On('spikein', do=[OutputEvent('emit')]) ], name='R1', ), Regime(name='R2', transitions=On('SV1 > 1', to='R1')) ], analog_ports=[ AnalogReceivePort('ARP1'), AnalogReceivePort('ARP2'), AnalogSendPort('A1'), AnalogSendPort('A2') ], parameters=['P1', 'P2']) self.b = Dynamics(name='A', aliases=[ 'A1:=P1', 'A2 := ARP1 + SV2', 'A3 := SV1', 'A4 := SV1^3 + SV2^-3' ], regimes=[ Regime( 'dSV1/dt = -SV1 / (P2*t)', 'dSV2/dt = SV1 / (ARP1*t) + SV2 / (P1*t)', 'dSV3/dt = -SV3/t + P3/t', transitions=[ On('SV1 > P1', do=[OutputEvent('emit')]), On('spikein', do=[ OutputEvent('emit'), StateAssignment('SV1', 'P1') ]) ], name='R1', ), Regime(name='R2', transitions=[ On('SV1 > 1', to='R1'), On('SV3 < 0.001', to='R2', do=[StateAssignment('SV3', 1)]) ]) ], analog_ports=[ AnalogReceivePort('ARP1'), AnalogReceivePort('ARP2'), AnalogSendPort('A1'), AnalogSendPort('A2'), AnalogSendPort('SV3') ], parameters=['P1', 'P2', 'P3'])
def test_assign_from_roles(self): role2name = { 'pre': 'pre_cell', 'post': 'post_cell', 'response': 'psr', 'plasticity': 'pls' } epc = EventPortConnection(send_port_name='ESP1', receive_port_name='ERP1', sender_role='pre', receiver_role='response') apc = AnalogPortConnection(send_port_name='SV1', receive_port_name='ARP1', sender_role='response', receiver_role='post') adpc = AnalogPortConnection(send_port_name='SV1', receive_port_name='ADP1', sender_role='response', receiver_role='post') # Manually bind the port connections and senders to avoid check # that they are bound. epc._receive_port = EventReceivePort('ERP1') epc._send_port = EventSendPort('ESP1') apc._receive_port = AnalogReceivePort('ARP1', dimension=un.voltage) apc._send_port = AnalogSendPort('SV1', dimension=un.voltage) adpc._receive_port = AnalogReducePort('ADP1', dimension=un.voltage) adpc._send_port = AnalogSendPort('SV1', dimension=un.voltage) epc._receiver = True epc._sender = True apc._receiver = True apc._sender = True adpc._receiver = True adpc._sender = True # Test the creation of a new port connection with the original # port connection roles mapped to names of sub-components internal_epc = epc.assign_names_from_roles(role2name) self.assertEqual(internal_epc.send_port_name, 'ESP1') self.assertEqual(internal_epc.receive_port_name, 'ERP1') self.assertEqual(internal_epc.sender_name, 'pre_cell') self.assertEqual(internal_epc.receiver_name, 'psr') internal_apc = apc.assign_names_from_roles(role2name) self.assertEqual(internal_apc.send_port_name, 'SV1') self.assertEqual(internal_apc.receive_port_name, 'ARP1') self.assertEqual(internal_apc.sender_name, 'psr') self.assertEqual(internal_apc.receiver_name, 'post_cell') internal_adpc = adpc.assign_names_from_roles(role2name) self.assertEqual(internal_adpc.send_port_name, 'SV1') self.assertEqual(internal_adpc.receive_port_name, 'ADP1') self.assertEqual(internal_adpc.sender_name, 'psr') self.assertEqual(internal_adpc.receiver_name, 'post_cell') # Test the appending of role names to port_connection ports for # use in the creation of multi-dynamics external_epc = epc.append_namespace_from_roles(role2name) self.assertEqual(external_epc.send_port_name, 'ESP1__pre_cell') self.assertEqual(external_epc.receive_port_name, 'ERP1__psr') external_apc = apc.append_namespace_from_roles(role2name) self.assertEqual(external_apc.send_port_name, 'SV1__psr') self.assertEqual(external_apc.receive_port_name, 'ARP1__post_cell') external_adpc = adpc.append_namespace_from_roles(role2name) self.assertEqual(external_adpc.send_port_name, 'SV1__psr') self.assertEqual(external_adpc.receive_port_name, 'ADP1__post_cell' + AnalogReducePortExposure.SUFFIX) # Bind the port connections to the projection "container" and then # call expose_ports to create port exposures for each of the # terminals epc.bind(projD, to_roles=True) epc_send_exp, epc_receive_exp = epc.expose_ports(role2name) self.assertEqual(epc_send_exp.name, 'ESP1__pre_cell') self.assertEqual(epc_send_exp.sub_component_name, 'pre_cell') self.assertEqual(epc_send_exp.port_name, 'ESP1') self.assertEqual(epc_receive_exp.name, 'ERP1__psr') self.assertEqual(epc_receive_exp.sub_component_name, 'psr') self.assertEqual(epc_receive_exp.port_name, 'ERP1') apc.bind(projD, to_roles=True) apc_send_exp, apc_receive_exp = apc.expose_ports(role2name) self.assertEqual(apc_send_exp.name, 'SV1__psr') self.assertEqual(apc_send_exp.sub_component_name, 'psr') self.assertEqual(apc_send_exp.port_name, 'SV1') self.assertEqual(apc_receive_exp.name, 'ARP1__post_cell') self.assertEqual(apc_receive_exp.sub_component_name, 'post_cell') self.assertEqual(apc_receive_exp.port_name, 'ARP1')