-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestAdaoExchange.cxx
369 lines (339 loc) · 11 KB
/
TestAdaoExchange.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (C) 2019 EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author: Anthony Geay, anthony.geay@edf.fr, EDF R&D
#include "TestAdaoExchange.hxx"
#include "AdaoExchangeLayer.hxx"
#include "AdaoExchangeLayerException.hxx"
#include "AdaoModelKeyVal.hxx"
#include "PyObjectRAII.hxx"
#include "py2cpp/py2cpp.hxx"
#include <vector>
#include <iterator>
#include "TestAdaoHelper.cxx"
// Functor a remplacer par un appel a un evaluateur parallele
class NonParallelFunctor
{
public:
NonParallelFunctor(std::function< std::vector<double>(const std::vector<double>&) > cppFunction):_cpp_function(cppFunction) { }
PyObject *operator()(PyObject *inp) const
{
PyGILState_STATE gstate(PyGILState_Ensure());
PyObjectRAII iterator(PyObjectRAII::FromNew(PyObject_GetIter(inp)));
if(iterator.isNull())
throw AdaoExchangeLayerException("Input object is not iterable !");
PyObject *item(nullptr);
PyObjectRAII numpyModule(PyObjectRAII::FromNew(PyImport_ImportModule("numpy")));
if(numpyModule.isNull())
throw AdaoExchangeLayerException("Failed to load numpy");
PyObjectRAII ravelFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(numpyModule,"ravel")));
std::vector< PyObjectRAII > pyrets;
while( item = PyIter_Next(iterator) )
{
PyObjectRAII item2(PyObjectRAII::FromNew(item));
{
PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(1)));
PyTuple_SetItem(args,0,item2.retn());
PyObjectRAII npyArray(PyObjectRAII::FromNew(PyObject_CallObject(ravelFunc,args)));
// Waiting management of npy arrays into py2cpp
PyObjectRAII lolistFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(npyArray,"tolist")));
PyObjectRAII listPy;
{
PyObjectRAII args2(PyObjectRAII::FromNew(PyTuple_New(0)));
listPy=PyObjectRAII::FromNew(PyObject_CallObject(lolistFunc,args2));
}
std::vector<double> vect;
{
py2cpp::PyPtr listPy2(listPy.retn());
py2cpp::fromPyPtr(listPy2,vect);
}
//
PyGILState_Release(gstate);
std::vector<double> res(_cpp_function(vect));// L'appel effectif est ici
gstate=PyGILState_Ensure();
//
py2cpp::PyPtr resPy(py2cpp::toPyPtr(res));
PyObjectRAII resPy2(PyObjectRAII::FromBorrowed(resPy.get()));
pyrets.push_back(resPy2);
}
}
std::size_t len(pyrets.size());
PyObjectRAII ret(PyObjectRAII::FromNew(PyList_New(len)));
for(std::size_t i=0;i<len;++i)
{
PyList_SetItem(ret,i,pyrets[i].retn());
}
//PyObject *tmp(PyObject_Repr(ret));
// std::cerr << PyUnicode_AsUTF8(tmp) << std::endl;
PyGILState_Release(gstate);
return ret.retn();
}
private:
std::function< std::vector<double>(const std::vector<double>&) > _cpp_function;
};
PyObjectRAII NumpyToListWaitingForPy2CppManagement(PyObject *npObj)
{
PyObjectRAII func(PyObjectRAII::FromNew(PyObject_GetAttrString(npObj,"tolist")));
if(func.isNull())
throw AdaoExchangeLayerException("input pyobject does not have tolist attribute !");
PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(0)));
PyObjectRAII ret(PyObjectRAII::FromNew(PyObject_CallObject(func,args)));
return ret;
}
void AdaoExchangeTest::setUp()
{
}
void AdaoExchangeTest::tearDown()
{
}
void AdaoExchangeTest::cleanUp()
{
}
using namespace AdaoModel;
void AdaoExchangeTest::test3DVar()
{
NonParallelFunctor functor(funcBase);
MainModel mm;
AdaoExchangeLayer adao;
adao.init();
// For bounds, Background/Vector, Observation/Vector
adao.setFunctionCallbackInModel(&mm);
Visitor2 visitorPythonObj(adao.getPythonContext());
{
AutoGIL agil;
mm.visitPythonLeaves(&visitorPythonObj);
}
//
adao.loadTemplate(&mm);
//
{
std::string sciptPyOfModelMaker(mm.pyStr());
//std::cerr << sciptPyOfModelMaker << std::endl;
}
adao.execute();
PyObject *listOfElts( nullptr );
while( adao.next(listOfElts) )
{
PyObject *resultOfChunk(functor(listOfElts));
adao.setResult(resultOfChunk);
}
PyObject *res(adao.getResult());
PyObjectRAII optimum(PyObjectRAII::FromNew(res));
PyObjectRAII optimum_4_py2cpp(NumpyToListWaitingForPy2CppManagement(optimum));
std::vector<double> vect;
{
py2cpp::PyPtr obj(optimum_4_py2cpp);
py2cpp::fromPyPtr(obj,vect);
}
CPPUNIT_ASSERT_EQUAL(3,(int)vect.size());
CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,vect[0],1e-7);
CPPUNIT_ASSERT_DOUBLES_EQUAL(3.,vect[1],1e-7);
CPPUNIT_ASSERT_DOUBLES_EQUAL(4.,vect[2],1e-7);
}
void AdaoExchangeTest::testBlue()
{
class TestBlueVisitor : public RecursiveVisitor
{
public:
void visit(GenericKeyVal *obj)
{
EnumAlgoKeyVal *objc(dynamic_cast<EnumAlgoKeyVal *>(obj));
if(objc)
objc->setVal(EnumAlgo::Blue);
}
void enterSubDir(DictKeyVal *subdir) { }
void exitSubDir(DictKeyVal *subdir) { }
};
NonParallelFunctor functor(funcBase);
MainModel mm;
//
TestBlueVisitor vis;
mm.visitAll(&vis);
//
AdaoExchangeLayer adao;
adao.init();
// For bounds, Background/Vector, Observation/Vector
adao.setFunctionCallbackInModel(&mm);
Visitor2 visitorPythonObj(adao.getPythonContext());
{
AutoGIL agil;
mm.visitPythonLeaves(&visitorPythonObj);
}
//
adao.loadTemplate(&mm);
//
{
std::string sciptPyOfModelMaker(mm.pyStr());
//std::cerr << sciptPyOfModelMaker << std::endl;
}
adao.execute();
PyObject *listOfElts( nullptr );
while( adao.next(listOfElts) )
{
PyObject *resultOfChunk(functor(listOfElts));
adao.setResult(resultOfChunk);
}
PyObject *res(adao.getResult());
PyObjectRAII optimum(PyObjectRAII::FromNew(res));
PyObjectRAII optimum_4_py2cpp(NumpyToListWaitingForPy2CppManagement(optimum));
std::vector<double> vect;
{
py2cpp::PyPtr obj(optimum_4_py2cpp);
py2cpp::fromPyPtr(obj,vect);
}
CPPUNIT_ASSERT_EQUAL(3,(int)vect.size());
CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,vect[0],1e-7);
CPPUNIT_ASSERT_DOUBLES_EQUAL(3.,vect[1],1e-7);
CPPUNIT_ASSERT_DOUBLES_EQUAL(4.,vect[2],1e-7);
}
void AdaoExchangeTest::testNonLinearLeastSquares()
{
class TestNonLinearLeastSquaresVisitor : public RecursiveVisitor
{
public:
void visit(GenericKeyVal *obj)
{
EnumAlgoKeyVal *objc(dynamic_cast<EnumAlgoKeyVal *>(obj));
if(objc)
objc->setVal(EnumAlgo::NonLinearLeastSquares);
}
void enterSubDir(DictKeyVal *subdir) { }
void exitSubDir(DictKeyVal *subdir) { }
};
NonParallelFunctor functor(funcBase);
MainModel mm;
//
TestNonLinearLeastSquaresVisitor vis;
mm.visitAll(&vis);
//
AdaoExchangeLayer adao;
adao.init();
// For bounds, Background/Vector, Observation/Vector
adao.setFunctionCallbackInModel(&mm);
Visitor2 visitorPythonObj(adao.getPythonContext());
{
AutoGIL agil;
mm.visitPythonLeaves(&visitorPythonObj);
}
//
adao.loadTemplate(&mm);
//
{
std::string sciptPyOfModelMaker(mm.pyStr());
//std::cerr << sciptPyOfModelMaker << std::endl;
}
adao.execute();
PyObject *listOfElts( nullptr );
while( adao.next(listOfElts) )
{
PyObject *resultOfChunk(functor(listOfElts));
adao.setResult(resultOfChunk);
}
PyObject *res(adao.getResult());
PyObjectRAII optimum(PyObjectRAII::FromNew(res));
PyObjectRAII optimum_4_py2cpp(NumpyToListWaitingForPy2CppManagement(optimum));
std::vector<double> vect;
{
py2cpp::PyPtr obj(optimum_4_py2cpp);
py2cpp::fromPyPtr(obj,vect);
}
CPPUNIT_ASSERT_EQUAL(3,(int)vect.size());
CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,vect[0],1e-7);
CPPUNIT_ASSERT_DOUBLES_EQUAL(3.,vect[1],1e-7);
CPPUNIT_ASSERT_DOUBLES_EQUAL(4.,vect[2],1e-7);
}
void AdaoExchangeTest::testCasCrue()
{
NonParallelFunctor functor(funcCrue);
MainModel mm;
AdaoExchangeLayer adao;
adao.init();
// For bounds, Background/Vector, Observation/Vector
adao.setFunctionCallbackInModel(&mm);
VisitorCruePython visitorPythonObj(adao.getPythonContext());
{
AutoGIL agil;
mm.visitPythonLeaves(&visitorPythonObj);
}
//
adao.loadTemplate(&mm);
//
{
std::string sciptPyOfModelMaker(mm.pyStr());
//std::cerr << sciptPyOfModelMaker << std::endl;
}
adao.execute();
PyObject *listOfElts( nullptr );
while( adao.next(listOfElts) )
{
PyObject *resultOfChunk(functor(listOfElts));
adao.setResult(resultOfChunk);
}
PyObject *res(adao.getResult());
PyObjectRAII optimum(PyObjectRAII::FromNew(res));
PyObjectRAII optimum_4_py2cpp(NumpyToListWaitingForPy2CppManagement(optimum));
std::vector<double> vect;
{
py2cpp::PyPtr obj(optimum_4_py2cpp);
py2cpp::fromPyPtr(obj,vect);
}
CPPUNIT_ASSERT_EQUAL(1,(int)vect.size());
CPPUNIT_ASSERT_DOUBLES_EQUAL(25.,vect[0],1e-3);
}
CPPUNIT_TEST_SUITE_REGISTRATION( AdaoExchangeTest );
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TextTestProgressListener.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestRunner.h>
#include <cppunit/TextTestRunner.h>
int main(int argc, char* argv[])
{
// --- Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// --- Add a listener that collects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
// --- Add a listener that print dots as test run.
#ifdef WIN32
CPPUNIT_NS::TextTestProgressListener progress;
#else
CPPUNIT_NS::BriefTestProgressListener progress;
#endif
controller.addListener( &progress );
// --- Get the top level suite from the registry
CPPUNIT_NS::Test *suite =
CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
// --- Adds the test to the list of test to run
CPPUNIT_NS::TestRunner runner;
runner.addTest( suite );
runner.run( controller);
// --- Print test in a compiler compatible format.
std::ofstream testFile;
testFile.open("test.log", std::ios::out | std::ios::app);
testFile << "------ ADAO exchange test log:" << std::endl;
CPPUNIT_NS::CompilerOutputter outputter( &result, testFile );
outputter.write();
// --- Run the tests.
bool wasSucessful = result.wasSuccessful();
testFile.close();
// --- Return error code 1 if the one of test failed.
return wasSucessful ? 0 : 1;
}