Skip to content

Commit 764aa8f

Browse files
author
Ezequiel Torres
committed
Solved numpy warnings printed in stderr
* Now the numpy warnings are printed in the project log #46
1 parent e57da85 commit 764aa8f

File tree

9 files changed

+149
-40
lines changed

9 files changed

+149
-40
lines changed

MLC/Application.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import MLC.Log.log as lg
23

34
from MLC.Common.PreevaluationManager import PreevaluationManager
@@ -18,6 +19,7 @@ class MLC_CALLBACKS:
1819

1920
class Application(object):
2021
def __init__(self, simulation, callbacks={}):
22+
self._set_numpy_parameters()
2123
self._config = Config.get_instance()
2224

2325
self._simulation = simulation
@@ -67,6 +69,16 @@ def __init__(self, simulation, callbacks={}):
6769
self.__callbacks_manager.subscribe(MLC_CALLBACKS.ON_NEW_GENERATION, self.show_best)
6870
self.__display_best = False
6971

72+
def _set_numpy_parameters(self):
73+
# Set printable resolution (don't alter numpy interval resolution)
74+
np.set_printoptions(precision=3)
75+
# Show full arrays, no matter what size do they have
76+
np.set_printoptions(threshold=np.inf)
77+
# Don't show scientific notation
78+
np.set_printoptions(suppress=True)
79+
# Transform printed warnings to real warnings
80+
np.seterr(all='raise')
81+
7082
def go(self, to_generation, from_generation=None, display_best=False):
7183
"""
7284
Start MLC2 problem solving (MLC2 Toolbox)

MLC/Common/Lisp_Tree_Expr/Operation_Nodes.py

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
13
import importlib
24

35
import 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+
1933
class 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

4566
class 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

6998
class 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

98134
class 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

141184
class 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

160209
class 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

179234
class 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

213274
class 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

239306
class 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

258331
class RootNode(Internal_Node):
@@ -285,6 +358,7 @@ def accept(self, visitor):
285358

286359

287360
class Op_Node_Factory:
361+
288362
@staticmethod
289363
def make(op):
290364
if op == 'root':

MLC/GUI/Autogenerated/untitled/untitled.pro.user

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.2.0, 2017-01-15T22:01:26. -->
3+
<!-- Written by QtCreator 4.1.0, 2017-01-16T12:04:30. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
7-
<value type="QByteArray">{350f9065-16e4-4481-9d13-d542c79185b9}</value>
7+
<value type="QByteArray">{89aaf96f-e2f6-40f4-bca5-8aa7dd8dc388}</value>
88
</data>
99
<data>
1010
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -59,14 +59,14 @@
5959
<data>
6060
<variable>ProjectExplorer.Project.Target.0</variable>
6161
<valuemap type="QVariantMap">
62-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.7.1 GCC 64bit</value>
63-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.7.1 GCC 64bit</value>
64-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.57.gcc_64_kit</value>
62+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
63+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
64+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{f92ecf44-e715-45cb-84a0-3754f1e7bcc1}</value>
6565
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
6666
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
6767
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
6868
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
69-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/build-untitled-Desktop_Qt_5_7_1_GCC_64bit-Debug</value>
69+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/media/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/build-untitled-Desktop-Debug</value>
7070
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
7171
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
7272
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -126,7 +126,7 @@
126126
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
127127
</valuemap>
128128
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
129-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/build-untitled-Desktop_Qt_5_7_1_GCC_64bit-Release</value>
129+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/media/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/build-untitled-Desktop-Release</value>
130130
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
131131
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
132132
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -186,7 +186,7 @@
186186
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
187187
</valuemap>
188188
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
189-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/build-untitled-Desktop_Qt_5_7_1_GCC_64bit-Profile</value>
189+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/media/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/build-untitled-Desktop-Profile</value>
190190
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
191191
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
192192
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -304,7 +304,7 @@
304304
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
305305
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">untitled</value>
306306
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
307-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/untitled/untitled.pro</value>
307+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/media/home/etorres/Facultad/TP_Profesional/MLC_GUI/MLC/GUI/Autogenerated/untitled/untitled.pro</value>
308308
<value type="bool" key="QmakeProjectManager.QmakeRunConfiguration.UseLibrarySearchPath">true</value>
309309
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
310310
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">untitled.pro</value>

main.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ def initialize_config():
1515

1616

1717
def main():
18-
# Set printable resolution (don't alter numpy interval resolution)
19-
np.set_printoptions(precision=9)
20-
# Show full arrays, no matter what size do they have
21-
np.set_printoptions(threshold=np.inf)
22-
# Don't show scientific notation
23-
np.set_printoptions(suppress=True)
24-
2518
# MATLAB random numbers, used in integration tests
2619
RandomManager.load_random_values("./tests/integration_tests/matlab_randoms.txt")
2720

templates/toy_problem.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ def individual_data(indiv):
5151

5252
def cost(indiv):
5353
x, y, y2, mlc_y3 = individual_data(indiv)
54-
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
54+
cost_mlc_y3 = None
55+
try:
56+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
57+
except FloatingPointError, err:
58+
np.seterr(all='ignore')
59+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
60+
np.seterr(all='raise')
5561
return cost_mlc_y3
5662

5763

tests/integration_tests/test_basic/default_evaluation_script.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ def individual_data(indiv):
5353

5454

5555
def cost(indiv):
56-
x, y, y2, np_y3 = individual_data(indiv)
57-
cost_np_y3 = float(np.sum((np_y3 - y2)**2))
58-
return cost_np_y3
56+
x, y, y2, mlc_y3 = individual_data(indiv)
57+
cost_mlc_y3 = None
58+
try:
59+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
60+
except FloatingPointError, err:
61+
np.seterr(all='ignore')
62+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
63+
np.seterr(all='raise')
64+
return cost_mlc_y3
5965

6066

6167
def show_best(index, indiv, cost, block=True):

tests/integration_tests/test_multiple_controls/default_evaluation_script.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ def individual_data(indiv):
5353

5454

5555
def cost(indiv):
56-
x, y, y2, np_y3 = individual_data(indiv)
57-
cost_np_y3 = float(np.sum((np_y3 - y2)**2))
58-
return cost_np_y3
56+
x, y, y2, mlc_y3 = individual_data(indiv)
57+
cost_mlc_y3 = None
58+
try:
59+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
60+
except FloatingPointError, err:
61+
np.seterr(all='ignore')
62+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
63+
np.seterr(all='raise')
64+
return cost_mlc_y3
5965

6066

6167
def show_best(index, indiv, cost, block=True):

tests/integration_tests/test_persist_reusing_simulations/default_evaluation_script.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ def individual_data(indiv):
5353

5454

5555
def cost(indiv):
56-
x, y, y2, np_y3 = individual_data(indiv)
57-
cost_np_y3 = float(np.sum((np_y3 - y2)**2))
58-
return cost_np_y3
56+
x, y, y2, mlc_y3 = individual_data(indiv)
57+
cost_mlc_y3 = None
58+
try:
59+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
60+
except FloatingPointError, err:
61+
np.seterr(all='ignore')
62+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
63+
np.seterr(all='raise')
64+
return cost_mlc_y3
5965

6066

6167
def show_best(index, indiv, cost, block=True):

tests/integration_tests/test_persist_simulation/default_evaluation_script.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ def individual_data(indiv):
5353

5454

5555
def cost(indiv):
56-
x, y, y2, np_y3 = individual_data(indiv)
57-
cost_np_y3 = float(np.sum((np_y3 - y2)**2))
58-
return cost_np_y3
56+
x, y, y2, mlc_y3 = individual_data(indiv)
57+
cost_mlc_y3 = None
58+
try:
59+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
60+
except FloatingPointError, err:
61+
np.seterr(all='ignore')
62+
cost_mlc_y3 = float(np.sum((mlc_y3 - y2)**2))
63+
np.seterr(all='raise')
64+
return cost_mlc_y3
5965

6066

6167
def show_best(index, indiv, cost, block=True):

0 commit comments

Comments
 (0)