-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoalib.i
572 lines (488 loc) · 16 KB
/
oalib.i
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* File: oalib.i
*
* Defines the Python interface to the OApackage
*
*/
%module(docstring="Python Orthogonal Array interface") oalib
// basic features, see http://realmike.org/blog/2010/07/18/python-extensions-in-cpp-using-swig/
%include "std_string.i"
%include "std_vector.i"
%include "std_deque.i"
%include "std_pair.i"
%include "exception.i"
%{
#define SWIG_FILE_WITH_INIT
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply ( array_link &al, long long* IN_ARRAY2, int DIM1, int DIM2 ) { (array_link &al, long long* pymatinput, int number_of_rows, int number_of_columns) }
%apply ( long long* IN_ARRAY2, int DIM1, int DIM2 ) { (long long* pymatinput, int number_of_rows, int number_of_columns) }
%apply ( long long* IN_ARRAY2, int DIM1, int DIM2 ) { (long long* pymatinput, int nrows, int ncols) }
%apply ( double* IN_ARRAY2, int DIM1, int DIM2 ) { (double* pymatdoubleinput, int nrows, int ncols) }
%apply (double* ARGOUT_ARRAY1, int DIM1) {(double* pymat1, int nrows)}
%apply (int* ARGOUT_ARRAY2, int DIM1, int DIM2) {(int* pymat2, int nrows, int ncols)}
%apply (int* ARGOUT_ARRAY1, int DIM1) {(int* pymat1, int n)}
%apply (array_t* ARGOUT_ARRAY1, int DIM1) {(array_t* pymat1, int n)}
%apply (int* ARGOUT_ARRAY1, int DIM1) {(int* rangevec, int n)}
%{
#include <utility>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <numpy/arrayobject.h>
#ifdef _WIN32
#else
#include <stdint.h>
#endif
#include "printfheader.h"
#include "oaoptions.h"
#include "mathtools.h"
#include "arraytools.h"
#include "tools.h"
#include "md5.h"
#include "pareto.h"
#include "arrayproperties.h"
#include "extend.h"
#include "lmc.h"
#include "Deff.h"
#include "graphtools.h"
#include "evenodd.h"
#include "conference.h"
#include "unittests.h"
#ifdef OADEV
#include "oadevelop.h"
#endif
%}
%typemap(in) Eigen::MatrixXd (Eigen::MatrixXd inputEigen)
{
/* note that Eigen is column-major by default and numpy is row major by default */
int rows = 0;
int cols = 0;
PyArrayObject *pp = (PyArrayObject *)($input);
rows = PyArray_DIM(pp,0);
cols = PyArray_DIM(pp,1);
PyArrayObject* temp;
PyArg_ParseTuple($input, "O", &temp);
inputEigen.resize(rows,cols);
inputEigen.fill(0);
double * values = ((double *) PyArray_DATA( pp ));
for (long int i = 0; i != rows; ++i){
for(long int j = 0; j != cols; ++j){
inputEigen(i,j) = values[i*rows+j];
}
}
}
%typemap(typecheck) Eigen::MatrixXd {
$1 = 1;
}
%typemap(typecheck) const Eigen::MatrixXd {
$1 = 1;
}
// see http://sourceforge.net/p/swig/mailman/message/32490448/
//http://mail.scipy.org/pipermail/numpy-discussion/2013-February/065637.html
%typemap(out) Eigen::VectorXd
{
npy_intp dims[1] = {$1.size()};
PyObject* array = PyArray_SimpleNew(1, dims, NPY_DOUBLE);
double* data = ((double *) (PyArray_DATA( (PyArrayObject*) array ) ) );
for (int i = 0; i != dims[0]; ++i){
*data++ = $1.data()[i];
}
$result = array;
}
%typemap(out) Eigen::MatrixXd
{
/* note that Eigen is column-major by default and numpy is row major by default */
const int verbose=0;
if (verbose) {
printf("typemap out for Eigen::MatrixXd: \n");
eigenInfo($1);
}
Eigen::MatrixXd mt = $1.transpose();
npy_intp dims[2] = {$1.rows(), $1.cols() };
PyObject* array = PyArray_SimpleNew(2, dims, NPY_DOUBLE);
double* data = ((double *) (PyArray_DATA( (PyArrayObject*) array ) ) );
for (int i = 0; i != dims[0]*dims[1]; ++i){
*data++ = mt.data()[i];
}
$result = array;
}
%pythoncode %{
import sys
import numpy as np
import copy
from typing import Optional, List
def reduceGraphNauty(G, colors : Optional[List] = None, verbose : int = 1) -> List:
""" Return vertex transformation reducing array to normal form
The reduction is calculated using `Nauty <http://users.cecs.anu.edu.au/~bdm/nauty/>`_
Args:
G (numpy array or array_link) : the graph in incidence matrix form
colors: an optional vertex coloring
verbose: Verbosity level
Returns:
Relabelling of the vertices
"""
if isinstance(G, np.ndarray):
al=array_link()
al.setarray(G)
else:
al = copy.copy(G)
if colors is None:
colors = [0] * G.shape[0]
vertex_permutation = _oalib.reduceNauty ( al, colors, verbose )
return vertex_permutation
def transformGraphMatrix(G, tr, verbose=1):
""" Apply a vertex permutation to a graph
Arguments
---------
G : Numpy array
the graph in incidence matrix form
tr : list
the vertex transformation as a list
Returns
-------
The transformed graph
"""
al=array_link()
al.setarray(G)
alt = _oalib.transformGraph(al, tr, verbose)
return np.array(alt)
%}
/* Convert from C --> Python */
%typemap(out) void * {
$result = PyLong_FromVoidPtr($1);
}
%extend array_link {
%insert("python") %{
def __getattr__(self, attr):
if attr=='__array_interface__':
a = dict()
a['version']=3
a['shape']=(self.n_rows, self.n_columns)
sizeofdata=_oalib.sizeof_array_t()
a['typestr']='<i%d' % sizeofdata # sizeof(array_t)
a['data']=(self.data(), True)
# convert from the OAP column-major style to Numpy row-major style
a['strides']=(sizeofdata, sizeofdata*self.n_rows)
return a
else:
raise AttributeError("%r object has no attribute %r" %
(self.__class__, attr))
@property
def shape(self):
return (self.n_rows, self.n_columns)
@property
def size(self):
return self.n_rows*self.n_columns
def showarray(self):
""" Show array """
# overridden to fix problems with ipython
sys.stdout.write(self.showarrayString())
def getarray(self, verbose=0, *args):
""" Return Numpy style array """
if verbose:
print('getting array: size %d %d' % (self.n_rows, self.n_columns))
x=self.getarraydata( int(self.n_rows*self.n_columns) )
return x.reshape((self.n_columns, self.n_rows)).transpose()
def setarray(self, X, verbose=0):
""" Update the array link object with a Numpy array
Args:
X (numpy array): array to be copied to the object
"""
self.init(X.shape[0], X.shape[1])
self.index=-1
iv = intVector(X.T.astype(int).flatten().tolist())
self.setarraydata(iv, X.size)
def _slice2range(self, slice, max_value):
""" Convert a python slice object to a range """
if isinstance(slice, int):
return [slice]
if slice.start is None:
start = 0
else:
start = slice.start
if slice.stop is None:
stop = max_value
else:
stop = slice.stop
if slice.step is None:
step = 1
else:
step = slice.step
return list(range(start, stop, step))
def _ranges2subarray(self, row_range, col_range):
""" From a list of row element and a list of column element construct a submatrix """
al=array_link(len(row_range), len(col_range), array_link.INDEX_DEFAULT )
for ii, row in enumerate(row_range):
for jj, col in enumerate(col_range):
al[ii, jj]=self.at(row, col)
return al
def __getitem__(self, index):
""" Return element of array """
if type(index)==int:
if index<0 or index > self.n_rows*self.n_columns:
raise IndexError('index out of bounds')
return self.at(index)
elif isinstance(index, slice):
indices=self._slice2range(index, self.n_rows*self.n_columns)
return np.array( [self.at(a) for a in indices])
else:
if len(index)==2:
index0=index[0]
index1=index[1]
if isinstance(index0, int) and isinstance(index1, int):
if index0<0 or index0 >= self.n_rows:
raise IndexError('index out of bounds')
if index1<0 or index1 >= self.n_columns:
raise IndexError('index out of bounds')
return self.at(index0, index1)
elif isinstance(index0, int) and isinstance(index1, slice):
row_range=[index0]
col_range=self._slice2range(index1, self.n_columns)
return self._ranges2subarray(row_range, col_range)
elif isinstance(index0, slice) and isinstance(index1, int):
row_range=self._slice2range(index0, self.n_rows)
col_range=[index1]
return self._ranges2subarray(row_range, col_range)
elif isinstance(index0, slice) and isinstance(index1, slice):
row_range=self._slice2range(index0, self.n_rows)
col_range=self._slice2range(index1, self.n_columns)
return self._ranges2subarray(row_range, col_range)
else:
raise NotImplementedError('slice indexing not supported')
else:
raise IndexError('invalid index')
def __setitem__(self, index, value):
""" Set specified value at specified index in the array """
if type(index)==int:
if index<0 or index > self.n_rows*self.n_columns:
raise IndexError('index out of bounds')
self.setvalue(index, 0, value)
else:
if len(index)==2:
a=index[0]
b=index[1]
if a<0 or a >= self.n_rows:
raise IndexError('index out of bounds')
if b<0 or b >= self.n_columns:
raise IndexError('index out of bounds')
self.setvalue(a, b, value)
else:
raise IndexError('invalid index')
%}
}
// see http://www.swig.org/Doc1.3/Python.html#Python_nn65
//%feature("autodoc", "docstring")
// to generate the oadoxy.i:
// doxygen Doxyfile; python doxy2swig.py -a -c docs/xml/index.xml oadoxy.i
// see also: http://www.enricozini.org/2007/tips/swig-doxygen-docstring/
// replaced by the -doxygen option of swig
%feature("autodoc", "1");
%include "oadoxy.i"
// see https://www.swig.org/Doc4.0/Python.html#Python_nn47
%include "cpointer.i"
%include "std_map.i"
// ignore variable argument length functions
%ignore printfstring;
// rename problem names
%rename(__lt__) ::operator<;
%rename(__gt__) ::operator>;
#pragma SWIG nowarn=454
namespace std {
%template(arraylist_t) deque<array_link>;
%template(jstructArray) vector<jstruct_t>;
%template(uint8Vector) std::vector<unsigned char>;
%template(charVector) std::vector<signed char>;
%template(intVector) std::vector<int>;
%template(longVector) std::vector<long>;
%template(longDeque) deque<long>;
%template(doubleVector) std::vector<double>;
%template(stringVector) std::vector<std::string>;
%template(map_int_long) std::map<int, long>;
};
%exception array_link::selectFirstColumns {
try {
$action
} catch (std::runtime_error& e) {
SWIG_exception(SWIG_RuntimeError, const_cast<char*>(e.what()));
}
}
%exception mycheck_handler {
try {
$action
} catch (std::runtime_error& e) {
SWIG_exception(SWIG_RuntimeError, const_cast<char*>(e.what()));
}
}
%exception throw_runtime_exception {
try {
$action
} catch (std::runtime_error& e) {
SWIG_exception(SWIG_RuntimeError, const_cast<char*>(e.what()));
}
}
%exception {
try {
$action
}
SWIG_CATCH_STDEXCEPT // catch std::exception
catch (...) {
SWIG_exception_fail(SWIG_UnknownError, "Unknown exception");
}
}
// prevent memory leaks
%newobject readarrayfile;
arraylist_t readarrayfile(const char *fname, int verbose=0, int *setcols = 0);
// do this before the real arraylink is included...
#ifdef SWIGPYTHON
%pythoncode %{
import numpy
%}
#endif
%include "oaoptions.h"
%include "mathtools.h"
%include "arraytools.h"
%include "tools.h"
%include "arrayproperties.h"
%include "md5.h"
%include "Deff.h"
%include "pareto.h"
%include "extend.h"
%include "lmc.h"
%include "Deff.h"
%include "graphtools.h"
%include "evenodd.h"
%include "conference.h"
%include "unittests.h"
#ifdef OADEV
%include "oadevelop.h"
#endif
%template(pairDoptimize) std::pair< std::vector< std::vector<double> > ,arraylist_t>;
%template(pairGraphColors) std::pair< array_link , std::vector<int> >;
%template(pairEigenMatrix) std::pair< MatrixFloat , MatrixFloat >;
%extend mvalue_t<double> {
%insert("python") %{
def __getattr__(self, attr):
if attr=='__array_interface__':
a = dict()
a['version']=3
a['shape']=(self.size(), )
sizeofdata=_oalib.sizeof_double()
a['typestr']='<f%d' % sizeofdata
a['data']=(np.array(self.values), True)
return a
else:
raise AttributeError("%r object has no attribute %r" %
(self.__class__, attr))
%}
}
%template(mvalue_t_long) mvalue_t<long>;
%template(mvalue_t_double) mvalue_t<double>;
%template(ParetoLongLong) Pareto<long,long>;
%template(ParetoMultiLongLong) Pareto<mvalue_t<long>,long>;
%template(ParetoMultiDoubleLong) Pareto<mvalue_t<double>,long>;
%template(ParetoDoubleLong) Pareto<double,long>;
%template(ParetoElementLong) pareto_element<mvalue_t<long>,long>;
#%template(ParetoMElementLong) pareto_element<mvalue_t<long>,long>;
%template(vector_mvalue_t_double) std::vector<mvalue_t<double> >;
%template(vector_mvalue_t_int) std::vector<mvalue_t<int> >;
%template(vector_mvalue_t_long) std::vector<mvalue_t<long> >;
%template(DequeParetoElementLong) std::deque<pareto_element<mvalue_t<long>,long> >;
%template(conference_columnVector) std::vector< conference_column >;
%template(calculateArrayParetoJ5) calculateArrayParetoJ5<array_link>;
%template(calculateArrayParetoJ5int) calculateArrayParetoJ5<int>;
%template(calculateArrayParetoJ5long) calculateArrayParetoJ5<long>;
%template(vector_vector_double) std::vector< std::vector<double> >;
%template(krawtchouk) krawtchouk<long>;
#%template(ndarray) ndarray<double>;
%template(choose_long) choose<long>;
%template(macwilliams_transform) macwilliams_transform<double>;
%pythoncode %{
# for legacy reasons and for name consistency
GWLPvalueVector = vector_mvalue_t_double
mvalueVector = vector_mvalue_t_long
%}
/* representation functions */
%extend arraydata_t {
public:
std::string __repr__() {
return $self->showstr();
}
}
%extend array_transformation_t {
public:
std::string __repr__() {
if($self->ad!=0)
return printfstring("array_transformation_t: transformation for array of size %d x %d", $self->ad->N, $self->ad->ncols);
else
return printfstring("array_transformation_t: no class defined");
}
}
%extend array_link {
public:
std::string __repr__() {
return $self->showstr();
}
}
%extend arrayfile::arrayfile_t {
public:
std::string __repr__() {
return $self->showstr();
}
}
%extend std::deque<array_link> {
public:
std::string __repr__() {
return printfstring("list of array_link objects with %d elements", $self->size() );
}
}
%extend jstruct_t {
public:
std::string __repr__() {
return $self->showstr();
}
}
%extend jstructconference_t {
public:
std::string __repr__() {
return $self->showstr();
}
}
#ifdef SWIGPYTHON
// Add module docstring
%pythoncode
%{
__doc__ = """
Python Orthogonal Array Interface
"""
%}
#endif
%extend ndarray {
%insert("python") %{
@property
def shape(self):
return tuple(self.dims)
def __getattr__(self, attr):
if attr=='__array_interface__':
a = dict()
a['version']=3
a['shape']=tuple(self.dims)
sizeofdata=self.sizeof_type()
is_floating_point = self.type_is_floating_point()
if is_floating_point:
a['typestr']='<f%d' % sizeofdata
else:
# assume signed integer type
a['typestr']='<i%d' % sizeofdata
a['data']=(self.data_pointer(), True)
# convert from the OAP column-major style to Numpy row-major style
a['strides'] = tuple(sizeofdata*p for p in list(self.cumprod)[:-1] )
return a
else:
raise AttributeError("%r object has no attribute %r" %
(self.__class__, attr))
%}
}
%template(ndarray_double) ndarray<double>;
%template(ndarray_long) ndarray<long>;