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

André Anjos's avatar
André Anjos committed
8
9
#define BOB_LEARN_ACTIVATION_MODULE
#include <bob.learn.activation/api.h>
10
11
12
13

#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
André Anjos's avatar
André Anjos committed
14
15
16
#include <bob.blitz/capi.h>
#include <bob.blitz/cleanup.h>
#include <bob.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
PyDoc_STRVAR(module_docstr, "classes for activation functors");

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

André Anjos's avatar
André Anjos committed
35
int PyBobLearnActivation_APIVersion = BOB_LEARN_ACTIVATION_API_VERSION;
36

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(BOB_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
69

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

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

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

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

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

  Py_INCREF(&PyBobLearnMultipliedHyperbolicTangentActivation_Type);
André Anjos's avatar
André Anjos committed
85
  if (PyModule_AddObject(m, "MultipliedHyperbolicTangent", (PyObject *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) return 0;
86

André Anjos's avatar
André Anjos committed
87
  static void* PyBobLearnActivation_API[PyBobLearnActivation_API_pointers];
88
89
90
91
92
93
94

  /* exhaustive list of C APIs */

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

André Anjos's avatar
André Anjos committed
95
  PyBobLearnActivation_API[PyBobLearnActivation_APIVersion_NUM] = (void *)&PyBobLearnActivation_APIVersion;
96
97

  /*************************************************
André Anjos's avatar
André Anjos committed
98
   * Bindings for bob.learn.activation.Activation *
99
100
   *************************************************/

André Anjos's avatar
André Anjos committed
101
  PyBobLearnActivation_API[PyBobLearnActivation_Type_NUM] = (void *)&PyBobLearnActivation_Type;
102

André Anjos's avatar
André Anjos committed
103
  PyBobLearnActivation_API[PyBobLearnActivation_Check_NUM] = (void *)&PyBobLearnActivation_Check;
104

André Anjos's avatar
André Anjos committed
105
  PyBobLearnActivation_API[PyBobLearnActivation_NewFromActivation_NUM] = (void *)&PyBobLearnActivation_NewFromActivation;
106

107
  /***********************************************
André Anjos's avatar
André Anjos committed
108
   * Bindings for bob.learn.activation.Identity *
109
110
   ***********************************************/

André Anjos's avatar
André Anjos committed
111
  PyBobLearnActivation_API[PyBobLearnIdentityActivation_Type_NUM] = (void *)&PyBobLearnIdentityActivation_Type;
112
113

  /*********************************************
André Anjos's avatar
André Anjos committed
114
   * Bindings for bob.learn.activation.Linear *
115
116
   *********************************************/

André Anjos's avatar
André Anjos committed
117
  PyBobLearnActivation_API[PyBobLearnLinearActivation_Type_NUM] = (void *)&PyBobLearnLinearActivation_Type;
118
119

  /***********************************************
André Anjos's avatar
André Anjos committed
120
   * Bindings for bob.learn.activation.Logistic *
121
122
   ***********************************************/

André Anjos's avatar
André Anjos committed
123
  PyBobLearnActivation_API[PyBobLearnLogisticActivation_Type_NUM] = (void *)&PyBobLearnLogisticActivation_Type;
124
125

  /********************************************************
André Anjos's avatar
André Anjos committed
126
   * Bindings for bob.learn.activation.HyperbolicTangent *
127
128
   ********************************************************/

André Anjos's avatar
André Anjos committed
129
  PyBobLearnActivation_API[PyBobLearnHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnHyperbolicTangentActivation_Type;
130
131

  /******************************************************************
André Anjos's avatar
André Anjos committed
132
   * Bindings for bob.learn.activation.MultipliedHyperbolicTangent *
133
134
   ******************************************************************/

André Anjos's avatar
André Anjos committed
135
  PyBobLearnActivation_API[PyBobLearnMultipliedHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type;
136
137
138
139
140

#if PY_VERSION_HEX >= 0x02070000

  /* defines the PyCapsule */

André Anjos's avatar
André Anjos committed
141
142
  PyObject* c_api_object = PyCapsule_New((void *)PyBobLearnActivation_API,
      BOB_EXT_MODULE_PREFIX "." BOB_EXT_MODULE_NAME "._C_API", 0);
143
144
145

#else

André Anjos's avatar
André Anjos committed
146
  PyObject* c_api_object = PyCObject_FromVoidPtr((void *)PyBobLearnActivation_API, 0);
147
148
149
150
151

#endif

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

152
  /* imports dependencies */
André Anjos's avatar
André Anjos committed
153
  if (import_bob_blitz() < 0) {
154
    PyErr_Print();
André Anjos's avatar
André Anjos committed
155
    PyErr_Format(PyExc_ImportError, "cannot import `%s'", BOB_EXT_MODULE_NAME);
156
157
158
    return 0;
  }

André Anjos's avatar
André Anjos committed
159
  if (import_bob_io_base() < 0) {
160
    PyErr_Print();
André Anjos's avatar
André Anjos committed
161
    PyErr_Format(PyExc_ImportError, "cannot import `%s'", BOB_EXT_MODULE_NAME);
162
163
    return 0;
  }
164

165
  return Py_BuildValue("O",m);
André Anjos's avatar
André Anjos committed
166
}
167

André Anjos's avatar
André Anjos committed
168
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) {
André Anjos's avatar
André Anjos committed
169
# if PY_VERSION_HEX >= 0x03000000
André Anjos's avatar
André Anjos committed
170
  return
André Anjos's avatar
André Anjos committed
171
# endif
André Anjos's avatar
André Anjos committed
172
    create_module();
173
}