main.cpp 5.86 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
#include <bob.blitz/capi.h>
#include <bob.blitz/cleanup.h>
16
#include <bob.core/api.h>
André Anjos's avatar
André Anjos committed
17
#include <bob.io.base/api.h>
18

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

André Anjos's avatar
André Anjos committed
23
24
25
26
27
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
28
  BOB_EXT_MODULE_NAME,
André Anjos's avatar
André Anjos committed
29
30
  module_docstr,
  -1,
31
  module_methods,
André Anjos's avatar
André Anjos committed
32
33
34
  0, 0, 0, 0
};
#endif
35

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

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

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

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

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

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

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

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

# if PY_VERSION_HEX >= 0x03000000
62
63
64
  PyObject* module = PyModule_Create(&module_definition);
  auto module_ = make_xsafe(module);
  const char* ret = "O";
André Anjos's avatar
André Anjos committed
65
# else
66
67
  PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr);
  const char* ret = "N";
André Anjos's avatar
André Anjos committed
68
# endif
69
  if (!module) return 0;
70
71
72

  /* register the types to python */
  Py_INCREF(&PyBobLearnActivation_Type);
73
  if (PyModule_AddObject(module, "Activation", (PyObject *)&PyBobLearnActivation_Type) < 0) return 0;
74
75

  Py_INCREF(&PyBobLearnIdentityActivation_Type);
76
  if (PyModule_AddObject(module, "Identity", (PyObject *)&PyBobLearnIdentityActivation_Type) < 0) return 0;
77
78

  Py_INCREF(&PyBobLearnLinearActivation_Type);
79
  if (PyModule_AddObject(module, "Linear", (PyObject *)&PyBobLearnLinearActivation_Type) < 0) return 0;
80
81

  Py_INCREF(&PyBobLearnLogisticActivation_Type);
82
  if (PyModule_AddObject(module, "Logistic", (PyObject *)&PyBobLearnLogisticActivation_Type) < 0) return 0;
83
84

  Py_INCREF(&PyBobLearnHyperbolicTangentActivation_Type);
85
  if (PyModule_AddObject(module, "HyperbolicTangent", (PyObject *)&PyBobLearnHyperbolicTangentActivation_Type) < 0) return 0;
86
87

  Py_INCREF(&PyBobLearnMultipliedHyperbolicTangentActivation_Type);
88
  if (PyModule_AddObject(module, "MultipliedHyperbolicTangent", (PyObject *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type) < 0) return 0;
89

André Anjos's avatar
André Anjos committed
90
  static void* PyBobLearnActivation_API[PyBobLearnActivation_API_pointers];
91
92
93
94
95
96
97

  /* exhaustive list of C APIs */

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

André Anjos's avatar
André Anjos committed
98
  PyBobLearnActivation_API[PyBobLearnActivation_APIVersion_NUM] = (void *)&PyBobLearnActivation_APIVersion;
99
100

  /*************************************************
André Anjos's avatar
André Anjos committed
101
   * Bindings for bob.learn.activation.Activation *
102
103
   *************************************************/

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

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

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

110
  /***********************************************
André Anjos's avatar
André Anjos committed
111
   * Bindings for bob.learn.activation.Identity *
112
113
   ***********************************************/

André Anjos's avatar
André Anjos committed
114
  PyBobLearnActivation_API[PyBobLearnIdentityActivation_Type_NUM] = (void *)&PyBobLearnIdentityActivation_Type;
115
116

  /*********************************************
André Anjos's avatar
André Anjos committed
117
   * Bindings for bob.learn.activation.Linear *
118
119
   *********************************************/

André Anjos's avatar
André Anjos committed
120
  PyBobLearnActivation_API[PyBobLearnLinearActivation_Type_NUM] = (void *)&PyBobLearnLinearActivation_Type;
121
122

  /***********************************************
André Anjos's avatar
André Anjos committed
123
   * Bindings for bob.learn.activation.Logistic *
124
125
   ***********************************************/

André Anjos's avatar
André Anjos committed
126
  PyBobLearnActivation_API[PyBobLearnLogisticActivation_Type_NUM] = (void *)&PyBobLearnLogisticActivation_Type;
127
128

  /********************************************************
André Anjos's avatar
André Anjos committed
129
   * Bindings for bob.learn.activation.HyperbolicTangent *
130
131
   ********************************************************/

André Anjos's avatar
André Anjos committed
132
  PyBobLearnActivation_API[PyBobLearnHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnHyperbolicTangentActivation_Type;
133
134

  /******************************************************************
André Anjos's avatar
André Anjos committed
135
   * Bindings for bob.learn.activation.MultipliedHyperbolicTangent *
136
137
   ******************************************************************/

André Anjos's avatar
André Anjos committed
138
  PyBobLearnActivation_API[PyBobLearnMultipliedHyperbolicTangentActivation_Type_NUM] = (void *)&PyBobLearnMultipliedHyperbolicTangentActivation_Type;
139
140
141
142
143

#if PY_VERSION_HEX >= 0x02070000

  /* defines the PyCapsule */

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

#else

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

#endif

153
  if (c_api_object) PyModule_AddObject(module, "_C_API", c_api_object);
154

155
  /* imports dependencies */
156
157
158
  if (import_bob_blitz() < 0) return 0;
  if (import_bob_core_logging() < 0) return 0;
  if (import_bob_io_base() < 0) return 0;
159

160
  return Py_BuildValue(ret, module);
André Anjos's avatar
André Anjos committed
161
}
162

André Anjos's avatar
André Anjos committed
163
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) {
André Anjos's avatar
André Anjos committed
164
# if PY_VERSION_HEX >= 0x03000000
André Anjos's avatar
André Anjos committed
165
  return
André Anjos's avatar
André Anjos committed
166
# endif
André Anjos's avatar
André Anjos committed
167
    create_module();
168
}