1+ # -*- coding: utf-8 -*-
2+
13import importlib
24
35import MLC .Log .log as lg
@@ -16,6 +18,18 @@ def process_float(arg):
1618 return str_arg
1719
1820
21+ def execute_op_without_warnings (op , log_prefix , exception_msg , arg1 , arg2 = None ):
22+ result = None
23+ np .seterr (all = 'ignore' )
24+ if arg2 is None :
25+ result = op (arg1 )
26+ else :
27+ result = op (arg1 , arg2 )
28+ np .seterr (all = 'raise' )
29+ lg .logger_ .warn ("{0}{1}" .format (log_prefix , exception_msg ))
30+ return result
31+
32+
1933class Plus_Node (Internal_Node ):
2034
2135 def __init__ (self ):
@@ -39,7 +53,14 @@ def op_simplify(self):
3953 return self
4054
4155 def op_compute (self , arg_list ):
42- return arg_list [0 ] + arg_list [1 ]
56+ try :
57+ return arg_list [0 ] + arg_list [1 ]
58+ except FloatingPointError , err :
59+ return execute_op_without_warnings (op = np .add ,
60+ arg1 = arg_list [0 ],
61+ arg2 = arg_list [1 ],
62+ log_prefix = "[PLUS_NODE] Error: " ,
63+ exception_msg = err )
4364
4465
4566class Minus_Node (Internal_Node ):
@@ -63,7 +84,15 @@ def op_simplify(self):
6384 return self
6485
6586 def op_compute (self , arg_list ):
66- return arg_list [0 ] - arg_list [1 ]
87+ try :
88+ return arg_list [0 ] - arg_list [1 ]
89+ except FloatingPointError , err :
90+ return execute_op_without_warnings (op = np .subtract ,
91+ arg1 = arg_list [0 ],
92+ arg2 = arg_list [1 ],
93+ log_prefix = "[MINUS_NODE] Error: " ,
94+ exception_msg = err )
95+
6796
6897
6998class Mult_Node (Internal_Node ):
@@ -92,7 +121,14 @@ def op_simplify(self):
92121 return self
93122
94123 def op_compute (self , arg_list ):
95- return arg_list [0 ] * arg_list [1 ]
124+ try :
125+ return arg_list [0 ] * arg_list [1 ]
126+ except FloatingPointError , err :
127+ return execute_op_without_warnings (op = np .multiply ,
128+ arg1 = arg_list [0 ],
129+ arg2 = arg_list [1 ],
130+ log_prefix = "[MULTI_NODE] Error: " ,
131+ exception_msg = err )
96132
97133
98134class Division_Node (Internal_Node ):
@@ -135,7 +171,14 @@ def op_simplify(self):
135171 return self
136172
137173 def op_compute (self , arg_list ):
138- return self ._process_division (arg_list [0 ], arg_list [1 ])
174+ try :
175+ return self ._process_division (arg_list [0 ], arg_list [1 ])
176+ except FloatingPointError , err :
177+ return execute_op_without_warnings (op = self ._process_division ,
178+ arg1 = arg_list [0 ],
179+ arg2 = arg_list [1 ],
180+ log_prefix = "[DIV_NODE] Error: " ,
181+ exception_msg = err )
139182
140183
141184class Sine_Node (Internal_Node ):
@@ -154,7 +197,13 @@ def op_simplify(self):
154197 return self
155198
156199 def op_compute (self , arg_list ):
157- return np .sin (arg_list [0 ])
200+ try :
201+ return np .sin (arg_list [0 ])
202+ except FloatingPointError , err :
203+ return execute_op_without_warnings (op = np .sin ,
204+ arg1 = arg_list [0 ],
205+ log_prefix = "[SIN_NODE] Error: " ,
206+ exception_msg = err )
158207
159208
160209class Cosine_Node (Internal_Node ):
@@ -173,7 +222,13 @@ def op_simplify(self):
173222 return self
174223
175224 def op_compute (self , arg_list ):
176- return np .cos (arg_list [0 ])
225+ try :
226+ return np .cos (arg_list [0 ])
227+ except FloatingPointError , err :
228+ return execute_op_without_warnings (op = np .cos ,
229+ arg1 = arg_list [0 ],
230+ log_prefix = "[COS_NODE] Error: " ,
231+ exception_msg = err )
177232
178233
179234class Logarithm_Node (Internal_Node ):
@@ -207,7 +262,13 @@ def op_simplify(self):
207262 return self
208263
209264 def op_compute (self , arg_list ):
210- return np .log (self ._process_arg (arg_list [0 ]))
265+ try :
266+ return np .log (self ._process_arg (arg_list [0 ]))
267+ except FloatingPointError , err :
268+ return execute_op_without_warnings (op = lambda x : np .log (self ._process_arg (x )),
269+ arg1 = arg_list [0 ],
270+ log_prefix = "[LOG_NODE] Error: " ,
271+ exception_msg = err )
211272
212273
213274class Exponential_Node (Internal_Node ):
@@ -220,20 +281,26 @@ def formal(self):
220281
221282 def op_simplify (self ):
222283 if not self ._nodes [0 ].is_sensor ():
223- lg .logger_ .debug ("[EXP NODE ] Value: " + self ._nodes [0 ].to_string ())
284+ lg .logger_ .debug ("[EXP_NODE ] Value: " + self ._nodes [0 ].to_string ())
224285 try :
225286 arg = np .exp (float (self ._nodes [0 ].to_string ()))
226287 except OverflowError :
227288 # FIXME: See what to do with this expression, because there are problems when
228- # an infinite value is the argumento of a sinusoidal function
289+ # an infinite value is the argument of a sinusoidal function
229290 return Leaf_Node (process_float (float ("inf" )))
230291
231292 return Leaf_Node (process_float (arg ))
232293 else :
233294 return self
234295
235296 def op_compute (self , arg_list ):
236- return np .exp (arg_list [0 ])
297+ try :
298+ return np .exp (arg_list [0 ])
299+ except FloatingPointError , err :
300+ return execute_op_without_warnings (op = np .exp ,
301+ arg1 = arg_list [0 ],
302+ log_prefix = "[EXP_NODE] Error: " ,
303+ exception_msg = err )
237304
238305
239306class Tanh_Node (Internal_Node ):
@@ -252,7 +319,13 @@ def op_simplify(self):
252319 return self
253320
254321 def op_compute (self , arg_list ):
255- return np .tanh (arg_list [0 ])
322+ try :
323+ return np .tanh (arg_list [0 ])
324+ except FloatingPointError , err :
325+ return execute_op_without_warnings (op = np .exp ,
326+ arg1 = arg_list [0 ],
327+ log_prefix = "[TANH_NODE] Error: " ,
328+ exception_msg = err )
256329
257330
258331class RootNode (Internal_Node ):
@@ -285,6 +358,7 @@ def accept(self, visitor):
285358
286359
287360class Op_Node_Factory :
361+
288362 @staticmethod
289363 def make (op ):
290364 if op == 'root' :
0 commit comments