main.cpp 6.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * @author Andre Anjos <andre.anjos@idiap.ch>
 * @date Fri 13 Dec 2013 12:35:59 CET
 *
 * @brief Bindings to activation functors
 */

#define XBOB_LEARN_ACTIVATION_MODULE
#include <xbob.learn.activation/api.h>

#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
André Anjos's avatar
André Anjos committed
14
#include <xbob.blitz/capi.h>
André Anjos's avatar
André Anjos committed
15
#include <xbob.blitz/cleanup.h>
16
#include <xbob.io.base/api.h>
17

André Anjos's avatar
André Anjos committed
18
static PyMethodDef module_methods[] = {
19
20
21
    {0}  /* Sentinel */
};

André Anjos's avatar
André Anjos committed
22
23
24
25
26
27
28
29
PyDoc_STRVAR(module_docstr, "classes for activation functors");

#if PY_VERSION_HEX >= 0x03000000
static PyModuleDef module_definition = {
  PyModuleDef_HEAD_INIT,
  XBOB_EXT_MODULE_NAME,
  module_docstr,
  -1,
30
  module_methods,
André Anjos's avatar
André Anjos committed
31
32
33
  0, 0, 0, 0
};
#endif
34
35
36

int PyXbobLearnActivation_APIVersion = XBOB_LEARN_ACTIVATION_API_VERSION;

André Anjos's avatar
André Anjos committed
37
static PyObject* create_module (void) {
38
39

  PyBobLearnActivation_Type.tp_new = PyType_GenericNew;
André Anjos's avatar
André Anjos committed
40
  if (PyType_Ready(&PyBobLearnActivation_Type) < 0) return 0;
41
42

  PyBobLearnIdentityActivation_Type.tp_base = &PyBobLearnActivation_Type;
André Anjos's avatar
André Anjos committed
43
  if (PyType_Ready(&PyBobLearnIdentityActivation_Type) < 0) return 0;
44
45

  PyBobLearnLinearActivation_Type.tp_base = &PyBobLearnActivation_Type;
André Anjos's avatar
André Anjos committed
46
  if (PyType_Ready(&PyBobLearnLinearActivation_Type) < 0) return 0;
47
48

  PyBobLearnLogisticActivation_Type.tp_base = &PyBobLearnActivation_Type;
André Anjos's avatar
André Anjos committed
49
  if (PyType_Ready(&PyBobLearnLogisticActivation_Type) < 0) return 0;
50
51
52

  PyBobLearnHyperbolicTangentActivation_Type.tp_base =
    &PyBobLearnActivation_Type;
André Anjos's avatar
André Anjos committed
53
  if (PyType_Ready(&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0;
54
55
56
57

  PyBobLearnMultipliedHyperbolicTangentActivation_Type.tp_base =
    &PyBobLearnActivation_Type;
  if (PyType_Ready(&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0)
André Anjos's avatar
André Anjos committed
58
    return 0;
André Anjos's avatar
André Anjos committed
59
60
61
62

# if PY_VERSION_HEX >= 0x03000000
  PyObject* m = PyModule_Create(&module_definition);
# else
André Anjos's avatar
André Anjos committed
63
  PyObject* m = Py_InitModule3(XBOB_EXT_MODULE_NAME, module_methods, module_docstr);
André Anjos's avatar
André Anjos committed
64
# endif
André Anjos's avatar
André Anjos committed
65
66
  if (!m) return 0;
  auto m_ = make_safe(m);
67
68

  /* register some constants */
69
  if (PyModule_AddIntConstant(m, "__api_version__", XBOB_LEARN_ACTIVATION_API_VERSION) < 0) return 0;
André Anjos's avatar
André Anjos committed
70
  if (PyModule_AddStringConstant(m, "__version__", XBOB_EXT_MODULE_VERSION) < 0) return 0;
71
72
73

  /* register the types to python */
  Py_INCREF(&PyBobLearnActivation_Type);
André Anjos's avatar
André Anjos committed
74
  if (PyModule_AddObject(m, "Activation", (PyObject *)&PyBobLearnActivation_Type) < 0) return 0;
75
76

  Py_INCREF(&PyBobLearnIdentityActivation_Type);
André Anjos's avatar
André Anjos committed
77
  if (PyModule_AddObject(m, "Identity", (PyObject *)&PyBobLearnIdentityActivation_Type) < 0) return 0;
78
79

  Py_INCREF(&PyBobLearnLinearActivation_Type);
André Anjos's avatar
André Anjos committed
80
  if (PyModule_AddObject(m, "Linear", (PyObject *)&PyBobLearnLinearActivation_Type) < 0) return 0;
81
82

  Py_INCREF(&PyBobLearnLogisticActivation_Type);
André Anjos's avatar
André Anjos committed
83
  if (PyModule_AddObject(m, "Logistic", (PyObject *)&PyBobLearnLogisticActivation_Type) < 0) return 0;
84
85

  Py_INCREF(&PyBobLearnHyperbolicTangentActivation_Type);
André Anjos's avatar
André Anjos committed
86
  if (PyModule_AddObject(m, "HyperbolicTangent", (PyObject *)&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0;
87
88

  Py_INCREF(&PyBobLearnMultipliedHyperbolicTangentActivation_Type);
André Anjos's avatar
André Anjos committed
89
  if (PyModule_AddObject(m, "MultipliedHyperbolicTangent", (PyObject *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) return 0;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

  static void* PyXbobLearnActivation_API[PyXbobLearnActivation_API_pointers];

  /* exhaustive list of C APIs */

  /**************
   * Versioning *
   **************/

  PyXbobLearnActivation_API[PyXbobLearnActivation_APIVersion_NUM] = (void *)&PyXbobLearnActivation_APIVersion;

  /*************************************************
   * Bindings for xbob.learn.activation.Activation *
   *************************************************/

  PyXbobLearnActivation_API[PyBobLearnActivation_Type_NUM] = (void *)&PyBobLearnActivation_Type;

  PyXbobLearnActivation_API[PyBobLearnActivation_Check_NUM] = (void *)&PyBobLearnActivation_Check;

109
110
  PyXbobLearnActivation_API[PyBobLearnActivation_NewFromActivation_NUM] = (void *)&PyBobLearnActivation_NewFromActivation;

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
  /***********************************************
   * Bindings for xbob.learn.activation.Identity *
   ***********************************************/

  PyXbobLearnActivation_API[PyBobLearnIdentityActivation_Type_NUM] = (void *)&PyBobLearnIdentityActivation_Type;

  /*********************************************
   * Bindings for xbob.learn.activation.Linear *
   *********************************************/

  PyXbobLearnActivation_API[PyBobLearnLinearActivation_Type_NUM] = (void *)&PyBobLearnLinearActivation_Type;

  /***********************************************
   * Bindings for xbob.learn.activation.Logistic *
   ***********************************************/

  PyXbobLearnActivation_API[PyBobLearnLogisticActivation_Type_NUM] = (void *)&PyBobLearnLogisticActivation_Type;

  /********************************************************
   * Bindings for xbob.learn.activation.HyperbolicTangent *
   ********************************************************/

  PyXbobLearnActivation_API[PyBobLearnHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnHyperbolicTangentActivation_Type;

  /******************************************************************
   * Bindings for xbob.learn.activation.MultipliedHyperbolicTangent *
   ******************************************************************/

  PyXbobLearnActivation_API[PyBobLearnMultipliedHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type;

#if PY_VERSION_HEX >= 0x02070000

  /* defines the PyCapsule */

  PyObject* c_api_object = PyCapsule_New((void *)PyXbobLearnActivation_API,
      XBOB_EXT_MODULE_PREFIX "." XBOB_EXT_MODULE_NAME "._C_API", 0);

#else

  PyObject* c_api_object = PyCObject_FromVoidPtr((void *)PyXbobLearnActivation_API, 0);

#endif

  if (c_api_object) PyModule_AddObject(m, "_C_API", c_api_object);

156
157
158
159
160
161
162
163
164
165
166
167
  /* imports dependencies */
  if (import_xbob_blitz() < 0) {
    PyErr_Print();
    PyErr_Format(PyExc_ImportError, "cannot import `%s'", XBOB_EXT_MODULE_NAME);
    return 0;
  }

  if (import_xbob_io_base() < 0) {
    PyErr_Print();
    PyErr_Format(PyExc_ImportError, "cannot import `%s'", XBOB_EXT_MODULE_NAME);
    return 0;
  }
168

André Anjos's avatar
André Anjos committed
169
170
  Py_INCREF(m);
  return m;
171

André Anjos's avatar
André Anjos committed
172
}
173

André Anjos's avatar
André Anjos committed
174
PyMODINIT_FUNC XBOB_EXT_ENTRY_NAME (void) {
André Anjos's avatar
André Anjos committed
175
# if PY_VERSION_HEX >= 0x03000000
André Anjos's avatar
André Anjos committed
176
  return
André Anjos's avatar
André Anjos committed
177
# endif
André Anjos's avatar
André Anjos committed
178
    create_module();
179
}