def test_str_expr_replacement(self): # Signature: name(cls, frm, to, expr_string, func_ok=False) # replaces all occurences of name 'frm' with 'to' in expr_string # ('frm' may not occur as a function name on the rhs) ... # 'to' can be an arbitrary string so this function can also be used for # argument substitution. # # Returns the resulting string. # from nineml.abstraction_layer.component.util import MathUtil t = 'b*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)' t = MathUtil.str_expr_replacement('b', 'B', t) self.assertEqual(t, 'B*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)') # 'e' is a builtin, so this function doesn't care. t = MathUtil.str_expr_replacement(frm='e', to='E', expr_string=t) self.assertEqual(t, 'B*c + d/(E*sin(f+g/E)) + b1 + e_ / exp(12*g)')
def test_str_expr_replacement(self): # Signature: name(cls, frm, to, expr_string, func_ok=False) # replaces all occurences of name 'frm' with 'to' in expr_string # ('frm' may not occur as a function name on the rhs) ... # 'to' can be an arbitrary string so this function can also be used for # argument substitution. # # Returns the resulting string. # from nineml.abstraction_layer.component.util import MathUtil t = "b*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)" t = MathUtil.str_expr_replacement("b", "B", t) self.assertEqual(t, "B*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)") # 'e' is a builtin, so this function doesn't care. t = MathUtil.str_expr_replacement(frm="e", to="E", expr_string=t) self.assertEqual(t, "B*c + d/(E*sin(f+g/E)) + b1 + e_ / exp(12*g)")
def test_get_prefixed_rhs_string(self): # Signature: name(cls, expr_obj, prefix='', exclude=None) # No Docstring # from nineml.abstraction_layer.component.util import MathUtil e = Alias.from_str("a := b*c + d/(e_*sin(f+g/e_)) + b1 + e_ / exp(12*g)") rhs_sub = MathUtil.get_prefixed_rhs_string(e, prefix="U_", exclude=["c", "e_"]) self.assertEqual(rhs_sub, "U_b*c + U_d/(e_*sin(U_f+U_g/e_)) + U_b1 + e_ / exp(12*U_g)")
def test_get_rhs_substituted(self): # Signature: name(cls, expr_obj, namemap) # No Docstring # from nineml.abstraction_layer.component.util import MathUtil e = Alias.from_str("a := b*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)") rhs_sub = MathUtil.get_rhs_substituted(e, {"b": "B", "e": "E"}) self.assertEqual(rhs_sub, "B*c + d/(E*sin(f+g/E)) + b1 + e_ / exp(12*g)")
def test_get_prefixed_rhs_string(self): # Signature: name(cls, expr_obj, prefix='', exclude=None) # No Docstring # from nineml.abstraction_layer.component.util import MathUtil e = Alias.from_str('a := b*c + d/(e_*sin(f+g/e_)) + b1 + e_ / exp(12*g)') rhs_sub = MathUtil.get_prefixed_rhs_string(e, prefix='U_', exclude=['c', 'e_']) self.assertEqual( rhs_sub, 'U_b*c + U_d/(e_*sin(U_f+U_g/e_)) + U_b1 + e_ / exp(12*U_g)' )
def test_get_rhs_substituted(self): # Signature: name(cls, expr_obj, namemap) # No Docstring # from nineml.abstraction_layer.component.util import MathUtil e = Alias.from_str('a := b*c + d/(e*sin(f+g/e)) + b1 + e_ / exp(12*g)') rhs_sub = MathUtil.get_rhs_substituted(e, {'b': 'B', 'e': 'E'}) self.assertEqual( rhs_sub, 'B*c + d/(E*sin(f+g/E)) + b1 + e_ / exp(12*g)' )
def action_assignment(self, assignment, **kwargs): rand_map = { 'normal' : r'nineml_random_normal(\1,\2)', 'uniform' : r'nineml_random_uniform(\1,\2)', 'binomial' : r'nineml_random_binomial(\1,\2)', 'poisson' : r'nineml_random_poisson(\1)', #'exponential' : r'nineml_random_exponential(\1)', 'exponential': r'exprand(\1)', } expr = assignment.rhs for atom in assignment.rhs_atoms_in_namespace('random'): if not atom in rand_map: err = 'Neuron Simulator does not support: %s'%atom raise nineml.exceptions.NineMLRuntimeError(err) expr = MathUtil.rename_function(expr, '%s.%s'%('random',atom), rand_map[atom] ) self.required_random_functions.add(atom) assignment.neuron_rhs = expr
def test_is_single_symbol(self): # Signature: name(cls, expr) # Returns ``True`` if the expression is a single symbol, possibly # surrounded with white-spaces # # >>> is_single_symbol('hello') # True # # >>> is_single_symbol('hello * world') # False self.assertTrue(MathUtil.is_single_symbol('t')) self.assertTrue(MathUtil.is_single_symbol('var_1')) self.assertTrue(MathUtil.is_single_symbol('var_long_name')) self.assertTrue(MathUtil.is_single_symbol('_myName')) self.assertFalse(MathUtil.is_single_symbol('r + y')) self.assertFalse(MathUtil.is_single_symbol('r+y')) self.assertFalse(MathUtil.is_single_symbol('sin(y)'))
def test_is_single_symbol(self): # Signature: name(cls, expr) # Returns ``True`` if the expression is a single symbol, possibly # surrounded with white-spaces # # >>> is_single_symbol('hello') # True # # >>> is_single_symbol('hello * world') # False self.assertTrue(MathUtil.is_single_symbol("t")) self.assertTrue(MathUtil.is_single_symbol("var_1")) self.assertTrue(MathUtil.is_single_symbol("var_long_name")) self.assertTrue(MathUtil.is_single_symbol("_myName")) self.assertFalse(MathUtil.is_single_symbol("r + y")) self.assertFalse(MathUtil.is_single_symbol("r+y")) self.assertFalse(MathUtil.is_single_symbol("sin(y)"))