main.cpp 5.75 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
62
63

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

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

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

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

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

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

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

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

  /* exhaustive list of C APIs */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#if PY_VERSION_HEX >= 0x02070000

  /* defines the PyCapsule */

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

#else

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

#endif

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

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

158
  return Py_BuildValue("O",m);
André Anjos's avatar
André Anjos committed
159
}
160

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