Skip to content

Commit

Permalink
Merge pull request #18 from angelmadames/master
Browse files Browse the repository at this point in the history
closes #11: Fix constant definition on C++ headers.
  • Loading branch information
FutureLinkOpenSource authored Oct 2, 2017
2 parents 85578b3 + aeb1733 commit 6cc8250
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 122 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
CHANGELOG
=========

1.1.0 - 2017-10-02

* Fixed build for Windows OS.
* Added Windows installation details to README.
* Added support for 2.7 - 3.6.
* Fixed functions that return arrays and functions that deal with "FANN::connection arrays". Patch by @troiganto.
* Created wheel files for FreeBSD, Windows x64 & Linux (py27 & py36).

1.0.7 - 2015-03-31

* Fixed build for ARM devices
Expand Down
19 changes: 18 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fann2

Python bindings for Fast Artificial Neural Networks 2.2.0 (FANN >= 2.2.0). These
are the original python bindings included with FANN 2.1.0 beta and updated to
include support for python 2.6-3.4.
include support for python 2.6-3.6.


DESCRIPTION
Expand All @@ -32,6 +32,12 @@ INSTALLATION
You can install fann2 from pkgsrc or from pypi, using either pip or
easy_install:

wheels
------

$ pip install ${wheel_file}


pypi
----

Expand Down Expand Up @@ -67,6 +73,17 @@ Get and install pkgsrc. See `pkgsrc quickstart

pkgin -y install py-fann2

Windows considerations
----------------------

Source installation
...................

- Install Visual C++ Build Tools;
- Install `FANN source code <https://github.com/libfann/fann>`_, using cmake;
- Copy "fanndouble.lib" from FANN installed files to ${python_libs_directory} as "doublefann.lib";
- Install swig for Windows (you will need to set an Enviroment Variable for "swig.exe");
- Run > python setup.py install from PowerShell/Command Prompt.

USAGE
=====
Expand Down
129 changes: 95 additions & 34 deletions fann2/fann2.i
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "fann_cpp_subclass.h"
%}

%define HELPER_ARRAY_TEMPLATE( templ , T, GetFunc, SetFunc, cast)
%define HELPER_ARRAY_TEMPLATE( templ , T, GetFunc, SetFunc, cast)
%typemap(in) templ<T> * (templ<T> temp){
// templ<T>* type_map in
int i;
Expand All @@ -35,7 +35,7 @@
if (PyNumber_Check(o)) {
$1->array[i] = (T) GetFunc(o);
} else {
PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers");
PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers");
Py_DECREF(o);
SWIG_fail;
}
Expand All @@ -44,49 +44,33 @@
}
%typemap(freearg) templ<T>* {
// templ<T>* type_map freearg
if ($1 && $1->array && $1->can_delete)
{
if ($1 && $1->array && $1->can_delete)
{
free($1->array);
}
}

%typemap(out) templ<T>* {
// templ* type_map out
$result= PyList_New( $1->array_len );
for (unsigned int i = 0; i < $1->array_len; i++)
for (unsigned int i = 0; i < $1->array_len; i++)
{
PyObject *o = SetFunc( (cast) $1->array[i]);
PyList_SetItem($result,i,o);
}
if ($1 && $1->array && $1->can_delete)
{
free($1->array);
}
if ($1) delete $1;

}

%typemap(argout) templ<T>* ARGOUT{
// templ* type_map out
$result= PyList_New( $1->array_len );
for (unsigned int i = 0; i < $1->array_len; i++)
if ($1 && $1->array && $1->can_delete)
{
PyObject *o = SetFunc( (cast) $1->array[i]);
PyList_SetItem($result,i,o);
}
if ($1 && $1->array && $1->can_delete)
{
free($1->array);
}
if ($1) delete $1;
}

%enddef

%define HELPER_ARRAY_ARRAY_TEMPLATE(templ, T, GetFunc, SetFunc, cast)
%define HELPER_ARRAY_ARRAY_TEMPLATE(templ, T, GetFunc, SetFunc, cast)
%typemap(in) templ< T >* ( templ<T> temp) {
// templ<T>* type_map
unsigned int i;
unsigned int i;
unsigned int j;
unsigned int dim;
unsigned int num;
Expand All @@ -101,7 +85,7 @@
$1=&temp;
num=PySequence_Length($input);
$1->array_num=num;

PyObject* o0=PySequence_GetItem($input,0);
if (!PySequence_Check(o0)) {
PyErr_SetString(PyExc_ValueError,"Expected an inner sequence");
Expand All @@ -110,10 +94,10 @@
}
dim=PySequence_Length(o0);
Py_DECREF(o0);

$1->array_len=dim;
$1->arrays = (T **) calloc(num,sizeof(T*));

for (j = 0; j< num; j++)
{
PyObject* o1=PySequence_GetItem($input,j);
Expand All @@ -133,7 +117,7 @@
if (PyNumber_Check(o)) {
$1->arrays[j][i] = (T) GetFunc(o);
} else {
PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers");
PyErr_SetString(PyExc_ValueError,"Sequence elements must be numbers");
Py_DECREF(o);
Py_DECREF(o1);
SWIG_fail;
Expand All @@ -146,33 +130,33 @@
%typemap(freearg) templ< T >* {
// templ* type_map freearg
unsigned int i;
if ($1 && $1->arrays && $1->can_delete)
if ($1 && $1->arrays && $1->can_delete)
{
for (i=0; i < $1->array_num;++i)
if ($1->arrays[i])
if ($1->arrays[i])
free($1->arrays[i]);
free($1->arrays);
}
}
%typemap(out) templ<T>* {
// templ* type_map out
$result= PyList_New( $1->array_num );
for (unsigned int j = 0; j < $1->array_num; ++j)
for (unsigned int j = 0; j < $1->array_num; ++j)
{
PyObject *l= PyList_New( $1->array_len );
PyList_SetItem($result,j,l);
for (unsigned int i = 0; i < $1->array_len; i++)
for (unsigned int i = 0; i < $1->array_len; i++)
{
PyObject *o = SetFunc($1->arrays[j][i] );
//PyObject *o = SetFunc($1->arrays[i][j] );
PyList_SetItem(l,i,o);
}
}
unsigned int i;
if ($1 && $1->arrays && $1->can_delete)
if ($1 && $1->arrays && $1->can_delete)
{
for (i=0; i < $1->array_num;++i)
if ($1->arrays[i])
if ($1->arrays[i])
free($1->arrays[i]);
free($1->arrays);
}
Expand All @@ -194,6 +178,83 @@ HELPER_ARRAY_TEMPLATE( FANN::helper_array, fann_type , PyFloat_AsDouble, PyFlo

HELPER_ARRAY_ARRAY_TEMPLATE( FANN::helper_array_array, fann_type , PyFloat_AsDouble, PyFloat_FromDouble, double );



// Begin typemap specialization for helper_array<connection>*.

%typemap(in) FANN::helper_array<FANN::connection> * (FANN::helper_array<FANN::connection> temp){
// FANN::helper_array<FANN::connection>* type_map in
int i;
if (!PySequence_Check($input)) {
PyErr_SetString(PyExc_ValueError,"Expected a sequence");
SWIG_fail;
}
if (PySequence_Length($input) == 0) {
PyErr_SetString(PyExc_ValueError,"Size mismatch. Expected some elements");
SWIG_fail;
}
$1=&temp;
$1->array_len=PySequence_Length($input);
$1->array = (FANN::connection *) malloc($1->array_len*sizeof(FANN::connection));
for (i = 0; i < PySequence_Length($input); i++) {
PyObject *o = PySequence_GetItem($input,i);
if (!PySequence_Check(o) || PySequence_Length(o) != 3) {
PyErr_SetString(PyExc_ValueError,"Connection must be a sequence of length 3");
SWIG_fail;
}
PyObject *from_neuron = PySequence_GetItem(o,0);
PyObject *to_neuron = PySequence_GetItem(o,1);
PyObject *weight = PySequence_GetItem(o,2);
if (PyInt_Check(from_neuron) && PyInt_Check(to_neuron) &&
PyNumber_Check(weight)) {
$1->array[i].from_neuron = (unsigned int) PyInt_AsLong(from_neuron);
$1->array[i].to_neuron = (unsigned int) PyInt_AsLong(to_neuron);
$1->array[i].weight = (fann_type) PyFloat_AsDouble(weight);
} else {
PyErr_SetString(PyExc_ValueError,"Connection elements must be (int, int, float)");
Py_DECREF(from_neuron);
Py_DECREF(to_neuron);
Py_DECREF(weight);
Py_DECREF(o);
SWIG_fail;
}
Py_DECREF(from_neuron);
Py_DECREF(to_neuron);
Py_DECREF(weight);
Py_DECREF(o);
}
}

%typemap(freearg) FANN::helper_array<connection>* {
// FANN::helper_array<connection>* type_map freearg
if ($1 && $1->array && $1->can_delete)
{
free($1->array);
}
}

%typemap(out) FANN::helper_array<connection>* {
// FANN::helper_array<connection>* type_map out
$result= PyList_New( $1->array_len );
for (unsigned int i = 0; i < $1->array_len; i++)
{
PyObject *o = PyTuple_Pack (3,
PyInt_FromLong( (long) ($1->array[i].from_neuron)),
PyInt_FromLong( (long) ($1->array[i].to_neuron)),
PyFloat_FromDouble( (double) ($1->array[i].weight)));
PyList_SetItem($result,i,o);
}
if ($1 && $1->array && $1->can_delete)
{
free($1->array);
}
if ($1) delete $1;
}

// End typemap specialization for helper_array<connection>*.



%rename(neural_net_parent) FANN::neural_net;
%rename(neural_net) FANN::Neural_net;

Expand Down
Loading

0 comments on commit 6cc8250

Please sign in to comment.