ceps.cpp 17.9 KB
Newer Older
André Anjos's avatar
André Anjos committed
1 2 3 4 5 6 7
/**
 * @author Andre Anjos <andre.anjos@idiap.ch>
 * @date Thu  6 Feb 09:00:05 2014
 *
 * @brief Bindings to the base class bob::ap::Ceps
 */

André Anjos's avatar
André Anjos committed
8 9
#include <bob.blitz/cppapi.h>
#include <bob.blitz/cleanup.h>
10
#include <bob.extension/defines.h>
André Anjos's avatar
André Anjos committed
11 12
#include "types.h"

André Anjos's avatar
André Anjos committed
13
PyDoc_STRVAR(s_ceps_str, BOB_EXT_MODULE_PREFIX ".Ceps");
André Anjos's avatar
André Anjos committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

PyDoc_STRVAR(s_ceps_doc,
"Ceps(sampling_frequency, [win_length_ms=20., [win_shift_ms=10., [n_filters=24, [n_ceps=19, [f_min=0., [f_max=4000., [delta_win=2, [pre_emphasis_coeff=0.95, [mel_scale=True, [dct_norm=True]]]]]]]]]]) -> new Ceps\n\
Ceps(other) -> new Ceps\n\
\n\
Objects of this class, after configuration, can extract the\n\
cepstral coefficients from 1D audio array/signals.\n\
\n\
Parameters:\n\
\n\
sampling_frequency\n\
  [float] the sampling frequency/frequency rate\n\
\n\
win_length_ms\n\
  [float] the window length in miliseconds\n\
\n\
win_shift_ms\n\
  [float] the window shift in miliseconds\n\
\n\
n_filters\n\
  [int] the number of filter bands\n\
\n\
n_ceps\n\
  [int] the number of cepstral coefficients\n\
\n\
f_min\n\
  [double] the minimum frequency of the filter bank\n\
\n\
f_max\n\
  [double] the maximum frequency of the filter bank\n\
\n\
delta_win\n\
  [int] The integer delta value used for computing the\n\
  first and second order derivatives\n\
\n\
pre_emphasis_coeff\n\
  [double] the coefficient used for the pre-emphasis\n\
\n\
mel_scale\n\
  [bool] tells whether cepstral features are extracted\n\
  on a linear (LFCC, set it to ``False``) or Mel (MFCC,\n\
  set it to ``True`` - the default)\n\
\n\
dct_norm\n\
  [bool] A factor by which the cepstral coefficients are\n\
  multiplied\n\
\n\
other\n\
  [Ceps] an object of which is or inherits from ``Ceps``\n\
  that will be deep-copied into a new instance.\n\
\n\
"
);

int PyBobApCeps_Check(PyObject* o) {
  return PyObject_IsInstance(o, reinterpret_cast<PyObject*>(&PyBobApCeps_Type));
}

static void PyBobApCeps_Delete (PyBobApCepsObject* o) {

74 75 76
  o->parent.parent.parent.cxx = 0; // FrameExtractor
  o->parent.parent.cxx = 0; // Energy
  o->parent.cxx = 0; // Spectrogram
André Anjos's avatar
André Anjos committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  delete o->cxx;
  Py_TYPE(o)->tp_free((PyObject*)o);

}

static int PyBobApCeps_InitCopy
(PyBobApCepsObject* self, PyObject* args, PyObject* kwds) {

  /* Parses input arguments in a single shot */
  static const char* const_kwlist[] = {"other", 0};
  static char** kwlist = const_cast<char**>(const_kwlist);

  PyObject* other = 0;

  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist,
        &PyBobApCeps_Type, &other)) return -1;

  auto copy = reinterpret_cast<PyBobApCepsObject*>(other);

  try {
    self->cxx = new bob::ap::Ceps(*(copy->cxx));
    if (!self->cxx) {
      PyErr_Format(PyExc_MemoryError, "cannot create new object of type `%s' - no more memory", Py_TYPE(self)->tp_name);
      return -1;
    }
    self->parent.parent.parent.cxx = self->cxx;
    self->parent.parent.cxx = self->cxx;
    self->parent.cxx = self->cxx;
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
    PyErr_Format(PyExc_RuntimeError, "cannot create new object of type `%s' - unknown exception thrown", Py_TYPE(self)->tp_name);
    return -1;
  }

  return 0;

}

static int PyBobApCeps_InitParameters
(PyBobApCepsObject* self, PyObject *args, PyObject* kwds) {

  /* Parses input arguments in a single shot */
  static const char* const_kwlist[] = {
    "sampling_frequency",
    "win_length_ms",
    "win_shift_ms",
    "n_filters",
128
    "n_ceps",
André Anjos's avatar
André Anjos committed
129 130
    "f_min",
    "f_max",
131
    "delta_win",
André Anjos's avatar
André Anjos committed
132 133
    "pre_emphasis_coeff",
    "mel_scale",
134
    "dct_norm",
André Anjos's avatar
André Anjos committed
135 136 137 138 139 140 141
    0};
  static char** kwlist = const_cast<char**>(const_kwlist);

  double sampling_frequency = 0.;
  double win_length_ms = 20.;
  double win_shift_ms = 10.;
  Py_ssize_t n_filters = 24;
142
  Py_ssize_t n_ceps = 19;
André Anjos's avatar
André Anjos committed
143
  double f_min = 0.;
144 145 146
  double f_max = 4000.;
  Py_ssize_t delta_win = 2;
  double pre_emphasis_coeff = 0.95;
André Anjos's avatar
André Anjos committed
147
  PyObject* mel_scale = Py_True;
148 149 150 151 152
  PyObject* dct_norm = Py_True;
  if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|ddnnddndOO", kwlist,
        &sampling_frequency, &win_length_ms, &win_shift_ms, &n_filters,
        &n_ceps, &f_min, &f_max, &delta_win, &pre_emphasis_coeff,
        &mel_scale, &dct_norm))
André Anjos's avatar
André Anjos committed
153 154 155
    return -1;

  bool mel_scale_ = PyObject_IsTrue(mel_scale);
156
  bool dct_norm_ = PyObject_IsTrue(dct_norm);
André Anjos's avatar
André Anjos committed
157 158 159

  try {
    self->cxx = new bob::ap::Ceps(sampling_frequency,
160 161
        win_length_ms, win_shift_ms, n_filters, n_ceps, f_min, f_max,
        delta_win, pre_emphasis_coeff, mel_scale_, dct_norm_);
André Anjos's avatar
André Anjos committed
162 163 164 165
    if (!self->cxx) {
      PyErr_Format(PyExc_MemoryError, "cannot create new object of type `%s' - no more memory", Py_TYPE(self)->tp_name);
      return -1;
    }
166
    self->parent.parent.parent.cxx = self->cxx;
André Anjos's avatar
André Anjos committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    self->parent.parent.cxx = self->cxx;
    self->parent.cxx = self->cxx;
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
    PyErr_Format(PyExc_RuntimeError, "cannot create new object of type `%s' - unknown exception thrown", Py_TYPE(self)->tp_name);
    return -1;
  }

  return 0; ///< SUCCESS

}

static int PyBobApCeps_Init(PyBobApCepsObject* self,
    PyObject* args, PyObject* kwds) {

  Py_ssize_t nargs = (args?PyTuple_Size(args):0) + (kwds?PyDict_Size(kwds):0);

  switch (nargs) {

    case 1:

      {

        PyObject* arg = 0; ///< borrowed (don't delete)
        if (PyTuple_Size(args)) arg = PyTuple_GET_ITEM(args, 0);
        else {
          PyObject* tmp = PyDict_Values(kwds);
          auto tmp_ = make_safe(tmp);
          arg = PyList_GET_ITEM(tmp, 0);
        }

        if (PyBobApCeps_Check(arg)) {
          return PyBobApCeps_InitCopy(self, args, kwds);
        }

        else {
          return PyBobApCeps_InitParameters(self, args, kwds);
        }

        PyErr_Format(PyExc_TypeError, "cannot initialize `%s' with `%s' (see help)", Py_TYPE(self)->tp_name, Py_TYPE(arg)->tp_name);

      }

      break;

    default:

      return PyBobApCeps_InitParameters(self, args, kwds);

  }

  return -1;

}

static PyObject* PyBobApCeps_Repr(PyBobApCepsObject* self) {
227 228
  static const int MAXSIZE = 256;
  char buffer[MAXSIZE];
André Anjos's avatar
André Anjos committed
229
  Py_ssize_t n_filters = self->cxx->getNFilters();
230 231 232
  Py_ssize_t n_ceps = self->cxx->getNCeps();
  Py_ssize_t delta_win = self->cxx->getDeltaWin();
  auto count = std::snprintf(buffer, MAXSIZE, "%s(sampling_frequency=%f, win_length_ms=%f, win_shift_ms=%f, n_filters=%" PY_FORMAT_SIZE_T "d, n_ceps=%" PY_FORMAT_SIZE_T "d, f_min=%f, f_max=%f, delta_win=%" PY_FORMAT_SIZE_T "d, pre_emphasis_coeff=%f, mel_scale=%s, dct_norm=%s)", Py_TYPE(self)->tp_name, self->cxx->getSamplingFrequency(), self->cxx->getWinLengthMs(), self->cxx->getWinShiftMs(), n_filters, n_ceps, self->cxx->getFMin(), self->cxx->getFMax(), delta_win, self->cxx->getPreEmphasisCoeff(), self->cxx->getMelScale()?"True":"False", self->cxx->getDctNorm()?"True":"False");
André Anjos's avatar
André Anjos committed
233 234
  return
# if PY_VERSION_HEX >= 0x03000000
235
  PyUnicode_FromStringAndSize
André Anjos's avatar
André Anjos committed
236
# else
237
  PyString_FromStringAndSize
André Anjos's avatar
André Anjos committed
238
# endif
239
    (buffer, (count<=MAXSIZE)?count:MAXSIZE);
André Anjos's avatar
André Anjos committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

static PyObject* PyBobApCeps_RichCompare (PyBobApCepsObject* self,
    PyObject* other, int op) {

  if (!PyBobApCeps_Check(other)) {
    PyErr_Format(PyExc_TypeError, "cannot compare `%s' with `%s'",
        Py_TYPE(self)->tp_name, Py_TYPE(other)->tp_name);
    return 0;
  }

  auto other_ = reinterpret_cast<PyBobApCepsObject*>(other);

  switch (op) {
    case Py_EQ:
      if (self->cxx->operator==(*other_->cxx)) Py_RETURN_TRUE;
      Py_RETURN_FALSE;
      break;
    case Py_NE:
      if (self->cxx->operator!=(*other_->cxx)) Py_RETURN_TRUE;
      Py_RETURN_FALSE;
      break;
    default:
      Py_INCREF(Py_NotImplemented);
      return Py_NotImplemented;
  }

}

269 270 271
PyDoc_STRVAR(s_n_ceps_str, "n_ceps");
PyDoc_STRVAR(s_n_ceps_doc,
"The number of cepstral coefficients"
André Anjos's avatar
André Anjos committed
272 273
);

274
static PyObject* PyBobApCeps_GetNCeps
André Anjos's avatar
André Anjos committed
275
(PyBobApCepsObject* self, void* /*closure*/) {
276
  return Py_BuildValue("n", self->cxx->getNCeps());
André Anjos's avatar
André Anjos committed
277 278
}

279
static int PyBobApCeps_SetNCeps
André Anjos's avatar
André Anjos committed
280 281
(PyBobApCepsObject* self, PyObject* o, void* /*closure*/) {

282
  if (!PyBob_NumberCheck(o)) {
283
    PyErr_Format(PyExc_TypeError, "`%s' n_ceps can only be set using a number, not `%s'", Py_TYPE(self)->tp_name, Py_TYPE(o)->tp_name);
André Anjos's avatar
André Anjos committed
284 285 286 287 288 289 290
    return -1;
  }

  Py_ssize_t n = PyNumber_AsSsize_t(o, PyExc_OverflowError);
  if (PyErr_Occurred()) return -1;

  try {
291
    self->cxx->setNCeps(n);
André Anjos's avatar
André Anjos committed
292 293 294 295 296 297
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
298
    PyErr_Format(PyExc_RuntimeError, "cannot reset `n_ceps' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
André Anjos's avatar
André Anjos committed
299 300 301 302 303 304 305
    return -1;
  }

  return 0;

}

306 307 308 309
PyDoc_STRVAR(s_delta_win_str, "delta_win");
PyDoc_STRVAR(s_delta_win_doc,
"The integer delta value used for computing the first and\n\
second order derivatives"
André Anjos's avatar
André Anjos committed
310 311
);

312
static PyObject* PyBobApCeps_GetDeltaWin
André Anjos's avatar
André Anjos committed
313
(PyBobApCepsObject* self, void* /*closure*/) {
314
  return Py_BuildValue("n", self->cxx->getDeltaWin());
André Anjos's avatar
André Anjos committed
315 316
}

317
static int PyBobApCeps_SetDeltaWin
André Anjos's avatar
André Anjos committed
318 319
(PyBobApCepsObject* self, PyObject* o, void* /*closure*/) {

320
  if (!PyBob_NumberCheck(o)) {
321
    PyErr_Format(PyExc_TypeError, "`%s' delta_win can only be set using a number, not `%s'", Py_TYPE(self)->tp_name, Py_TYPE(o)->tp_name);
André Anjos's avatar
André Anjos committed
322 323 324
    return -1;
  }

325
  Py_ssize_t n = PyNumber_AsSsize_t(o, PyExc_OverflowError);
André Anjos's avatar
André Anjos committed
326 327 328
  if (PyErr_Occurred()) return -1;

  try {
329
    self->cxx->setDeltaWin(n);
André Anjos's avatar
André Anjos committed
330 331 332 333 334 335
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
336
    PyErr_Format(PyExc_RuntimeError, "cannot reset `delta_win' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
André Anjos's avatar
André Anjos committed
337 338 339 340 341 342 343
    return -1;
  }

  return 0;

}

344 345 346
PyDoc_STRVAR(s_dct_norm_str, "dct_norm");
PyDoc_STRVAR(s_dct_norm_doc,
"A factor by which the cepstral coefficients are multiplied"
André Anjos's avatar
André Anjos committed
347 348
);

349
static PyObject* PyBobApCeps_GetDctNorm
André Anjos's avatar
André Anjos committed
350
(PyBobApCepsObject* self, void* /*closure*/) {
351
  if (self->cxx->getDctNorm()) Py_RETURN_TRUE;
André Anjos's avatar
André Anjos committed
352 353 354
  else Py_RETURN_FALSE;
}

355
static int PyBobApCeps_SetDctNorm
André Anjos's avatar
André Anjos committed
356 357 358 359 360 361
(PyBobApCepsObject* self, PyObject* o, void* /*closure*/) {

  bool b = PyObject_IsTrue(o);
  if (PyErr_Occurred()) return -1;

  try {
362
    self->cxx->setDctNorm(b);
André Anjos's avatar
André Anjos committed
363 364 365 366 367 368
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
369
    PyErr_Format(PyExc_RuntimeError, "cannot reset `dct_norm' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
André Anjos's avatar
André Anjos committed
370 371 372 373 374 375 376
    return -1;
  }

  return 0;

}

377 378 379
PyDoc_STRVAR(s_with_energy_str, "with_energy");
PyDoc_STRVAR(s_with_energy_doc,
"Tells if we add the energy to the output feature"
André Anjos's avatar
André Anjos committed
380 381
);

382
static PyObject* PyBobApCeps_GetWithEnergy
André Anjos's avatar
André Anjos committed
383
(PyBobApCepsObject* self, void* /*closure*/) {
384
  if (self->cxx->getWithEnergy()) Py_RETURN_TRUE;
André Anjos's avatar
André Anjos committed
385 386 387
  else Py_RETURN_FALSE;
}

388
static int PyBobApCeps_SetWithEnergy
André Anjos's avatar
André Anjos committed
389 390 391 392 393 394
(PyBobApCepsObject* self, PyObject* o, void* /*closure*/) {

  bool b = PyObject_IsTrue(o);
  if (PyErr_Occurred()) return -1;

  try {
395
    self->cxx->setWithEnergy(b);
André Anjos's avatar
André Anjos committed
396 397 398 399 400 401
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
402
    PyErr_Format(PyExc_RuntimeError, "cannot reset `with_energy' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
André Anjos's avatar
André Anjos committed
403 404 405 406 407 408 409
    return -1;
  }

  return 0;

}

410 411 412
PyDoc_STRVAR(s_with_delta_str, "with_delta");
PyDoc_STRVAR(s_with_delta_doc,
"Tells if we add the first derivatives to the output feature"
André Anjos's avatar
André Anjos committed
413 414
);

415
static PyObject* PyBobApCeps_GetWithDelta
André Anjos's avatar
André Anjos committed
416
(PyBobApCepsObject* self, void* /*closure*/) {
417
  if (self->cxx->getWithDelta()) Py_RETURN_TRUE;
André Anjos's avatar
André Anjos committed
418 419 420
  else Py_RETURN_FALSE;
}

421
static int PyBobApCeps_SetWithDelta
André Anjos's avatar
André Anjos committed
422 423 424 425 426 427
(PyBobApCepsObject* self, PyObject* o, void* /*closure*/) {

  bool b = PyObject_IsTrue(o);
  if (PyErr_Occurred()) return -1;

  try {
428
    self->cxx->setWithDelta(b);
André Anjos's avatar
André Anjos committed
429 430 431 432 433 434
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
435
    PyErr_Format(PyExc_RuntimeError, "cannot reset `with_delta' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
André Anjos's avatar
André Anjos committed
436 437 438 439 440 441 442
    return -1;
  }

  return 0;

}

443 444 445
PyDoc_STRVAR(s_with_delta_delta_str, "with_delta_delta");
PyDoc_STRVAR(s_with_delta_delta_doc,
"Tells if we add the second derivatives to the output feature"
André Anjos's avatar
André Anjos committed
446 447
);

448
static PyObject* PyBobApCeps_GetWithDeltaDelta
André Anjos's avatar
André Anjos committed
449
(PyBobApCepsObject* self, void* /*closure*/) {
450
  if (self->cxx->getWithDeltaDelta()) Py_RETURN_TRUE;
André Anjos's avatar
André Anjos committed
451 452 453
  else Py_RETURN_FALSE;
}

454
static int PyBobApCeps_SetWithDeltaDelta
André Anjos's avatar
André Anjos committed
455 456 457 458 459 460
(PyBobApCepsObject* self, PyObject* o, void* /*closure*/) {

  bool b = PyObject_IsTrue(o);
  if (PyErr_Occurred()) return -1;

  try {
461
    self->cxx->setWithDeltaDelta(b);
André Anjos's avatar
André Anjos committed
462 463 464 465 466 467
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return -1;
  }
  catch (...) {
468
    PyErr_Format(PyExc_RuntimeError, "cannot reset `with_delta_delta' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
André Anjos's avatar
André Anjos committed
469 470 471 472 473 474 475 476 477
    return -1;
  }

  return 0;

}

static PyGetSetDef PyBobApCeps_getseters[] = {
    {
478 479 480 481
      s_n_ceps_str,
      (getter)PyBobApCeps_GetNCeps,
      (setter)PyBobApCeps_SetNCeps,
      s_n_ceps_doc,
André Anjos's avatar
André Anjos committed
482 483 484
      0
    },
    {
485 486 487 488
      s_delta_win_str,
      (getter)PyBobApCeps_GetDeltaWin,
      (setter)PyBobApCeps_SetDeltaWin,
      s_delta_win_doc,
André Anjos's avatar
André Anjos committed
489 490 491
      0
    },
    {
492 493 494 495
      s_dct_norm_str,
      (getter)PyBobApCeps_GetDctNorm,
      (setter)PyBobApCeps_SetDctNorm,
      s_dct_norm_doc,
André Anjos's avatar
André Anjos committed
496 497 498
      0
    },
    {
499 500 501 502
      s_with_energy_str,
      (getter)PyBobApCeps_GetWithEnergy,
      (setter)PyBobApCeps_SetWithEnergy,
      s_with_energy_doc,
André Anjos's avatar
André Anjos committed
503 504 505
      0
    },
    {
506 507 508 509
      s_with_delta_str,
      (getter)PyBobApCeps_GetWithDelta,
      (setter)PyBobApCeps_SetWithDelta,
      s_with_delta_doc,
André Anjos's avatar
André Anjos committed
510 511 512
      0
    },
    {
513 514 515 516
      s_with_delta_delta_str,
      (getter)PyBobApCeps_GetWithDeltaDelta,
      (setter)PyBobApCeps_SetWithDeltaDelta,
      s_with_delta_delta_doc,
André Anjos's avatar
André Anjos committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
      0
    },
    {0}  /* Sentinel */
};

static PyObject* PyBobApCeps_Call
(PyBobApCepsObject* self, PyObject *args, PyObject* kwds) {

  /* Parses input arguments in a single shot */
  static const char* const_kwlist[] = {"input", "output", 0};
  static char** kwlist = const_cast<char**>(const_kwlist);

  PyBlitzArrayObject* input = 0;
  PyBlitzArrayObject* output = 0;
  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&", kwlist,
        &PyBlitzArray_Converter, &input,
        &PyBlitzArray_OutputConverter, &output
        )) return 0;

  auto input_ = make_safe(input);
  auto output_ = make_xsafe(output);

  if (input->type_num != NPY_FLOAT64) {
    PyErr_Format(PyExc_TypeError, "`%s' only supports 1D 64-bit float arrays for input array `input'", Py_TYPE(self)->tp_name);
    return 0;
  }

  if (input->ndim != 1) {
    PyErr_Format(PyExc_TypeError, "`%s' only supports 1D 64-bit float arrays for input array `input'", Py_TYPE(self)->tp_name);
    return 0;
  }

  auto bz_input = PyBlitzArrayCxx_AsBlitz<double,1>(input);

  if (output) {

    if (output->type_num != NPY_FLOAT64) {
      PyErr_Format(PyExc_TypeError, "`%s' only supports 2D 64-bit float arrays for output array `output'", Py_TYPE(self)->tp_name);
      return 0;
    }

    if (output->ndim != 2) {
      PyErr_Format(PyExc_TypeError, "`%s' only supports 2D 64-bit float arrays for output array `output'", Py_TYPE(self)->tp_name);
      return 0;
    }

  }

  else {

    Py_ssize_t length[2];
    auto s = self->cxx->getShape(*bz_input);
    length[0] = s(0);
    length[1] = s(1);
    output = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(NPY_FLOAT64, 2, length);
    if (!output) return 0;
    output_ = make_safe(output);

  }

  auto bz_output = PyBlitzArrayCxx_AsBlitz<double,2>(output);

  try {
    self->cxx->operator()(*bz_input, *bz_output);
  }
  catch (std::exception& ex) {
    PyErr_SetString(PyExc_RuntimeError, ex.what());
    return 0;
  }
  catch (...) {
    PyErr_Format(PyExc_RuntimeError, "cannot call object of type `%s' - unknown exception thrown", Py_TYPE(self)->tp_name);
    return 0;
  }

591
  return PyBlitzArray_NUMPY_WRAP(Py_BuildValue("O", output));
André Anjos's avatar
André Anjos committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

}

PyTypeObject PyBobApCeps_Type = {
    PyVarObject_HEAD_INIT(0, 0)
    s_ceps_str,                               /*tp_name*/
    sizeof(PyBobApCepsObject),                /*tp_basicsize*/
    0,                                        /*tp_itemsize*/
    (destructor)PyBobApCeps_Delete,           /*tp_dealloc*/
    0,                                        /*tp_print*/
    0,                                        /*tp_getattr*/
    0,                                        /*tp_setattr*/
    0,                                        /*tp_compare*/
    (reprfunc)PyBobApCeps_Repr,               /*tp_repr*/
    0,                                        /*tp_as_number*/
    0,                                        /*tp_as_sequence*/
    0,                                        /*tp_as_mapping*/
    0,                                        /*tp_hash */
    (ternaryfunc)PyBobApCeps_Call,            /* tp_call */
    (reprfunc)PyBobApCeps_Repr,               /*tp_str*/
    0,                                        /*tp_getattro*/
    0,                                        /*tp_setattro*/
    0,                                        /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    s_ceps_doc,                               /* tp_doc */
    0,		                                    /* tp_traverse */
    0,		                                    /* tp_clear */
    (richcmpfunc)PyBobApCeps_RichCompare,     /* tp_richcompare */
    0,		                                    /* tp_weaklistoffset */
    0,		                                    /* tp_iter */
    0,		                                    /* tp_iternext */
    0,                                        /* tp_methods */
    0,                                        /* tp_members */
    PyBobApCeps_getseters,                    /* tp_getset */
    0,                                        /* tp_base */
    0,                                        /* tp_dict */
    0,                                        /* tp_descr_get */
    0,                                        /* tp_descr_set */
    0,                                        /* tp_dictoffset */
    (initproc)PyBobApCeps_Init,               /* tp_init */
    0,                                        /* tp_alloc */
    0,                                        /* tp_new */
};