Skip to content
Snippets Groups Projects
Commit a741fae5 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

Tests are now executing - only missing callable for Energy

parent dbd6a518
Branches
Tags
No related merge requests found
......@@ -3,7 +3,7 @@
; Mon 16 Apr 08:29:18 2012 CEST
[buildout]
parts = xbob.blitz xbob.io xbob.ap scripts
parts = xbob.blitz xbob.sp xbob.ap scripts
eggs = xbob.ap
ipdb
extensions = mr.developer
......@@ -17,21 +17,21 @@ verbose = true
xbob.buildout = git git@github.com:bioidiap/xbob.buildout
xbob.extension = git git@github.com:bioidiap/xbob.extension branch=xbob
xbob.blitz = git git@github.com:bioidiap/xbob.blitz egg=false
xbob.io = git git@github.com:bioidiap/xbob.io egg=false
xbob.sp = git git@github.com:bioidiap/xbob.sp egg=false
[xbob.blitz]
recipe = xbob.buildout:develop
setup = src/xbob.blitz
eggs = xbob.buildout xbob.extension
[xbob.io]
[xbob.sp]
recipe = xbob.buildout:develop
setup = src/xbob.io
setup = src/xbob.sp
eggs = xbob.blitz
[xbob.ap]
recipe = xbob.buildout:develop
eggs = xbob.blitz xbob.io
eggs = xbob.blitz xbob.sp
[scripts]
recipe = xbob.buildout:scripts
......@@ -28,7 +28,7 @@ setup(
install_requires=[
'setuptools',
'xbob.blitz',
'xbob.io',
'xbob.sp', # for testing
],
nameapace_packages=[
......@@ -38,6 +38,7 @@ setup(
ext_modules = [
Extension("xbob.ap._library",
[
"xbob/ap/energy.cpp",
"xbob/ap/frame_extractor.cpp",
"xbob/ap/main.cpp",
],
......
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Thu 6 Feb 09:00:05 2014
*
* @brief Bindings to the base class bob::ap::Energy
*/
#include <xbob.blitz/cppapi.h>
#include <xbob.blitz/cleanup.h>
#include "types.h"
PyDoc_STRVAR(s_energy_str, XBOB_EXT_MODULE_PREFIX ".Energy");
PyDoc_STRVAR(s_energy_doc,
"Energy(sampling_frequency, [win_length_ms=20., [win_shift_ms=10.]]) -> new Energy\n\
Energy(other) -> new Energy\n\
\n\
Objects of this class, after configuration, can extract the energy\n\
of frames extracted from a 1D audio array/signal.\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\
other\n\
[Energy] an object of which is or inherits from ``Energy``\n\
that will be deep-copied into a new instance.\n\
\n\
"
);
int PyBobApEnergy_Check(PyObject* o) {
return PyObject_IsInstance(o, reinterpret_cast<PyObject*>(&PyBobApEnergy_Type));
}
static void PyBobApEnergy_Delete (PyBobApEnergyObject* o) {
o->parent.cxx = 0;
delete o->cxx;
Py_TYPE(o)->tp_free((PyObject*)o);
}
static int PyBobApEnergy_InitCopy
(PyBobApEnergyObject* self, PyObject* args, PyObject* kwds) {
/* Parses input arguments in a single shot */
static const char* const_kwlist[] = {"sampling_frequency, ", 0};
static char** kwlist = const_cast<char**>(const_kwlist);
PyObject* other = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist,
&PyBobApEnergy_Type, &other)) return -1;
auto copy = reinterpret_cast<PyBobApEnergyObject*>(other);
try {
self->cxx = new bob::ap::Energy(*(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.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 PyBobApEnergy_InitParameters
(PyBobApEnergyObject* 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",
0};
static char** kwlist = const_cast<char**>(const_kwlist);
double sampling_frequency = 0.;
double win_length_ms = 20.;
double win_shift_ms = 10.;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|dd", kwlist,
&sampling_frequency, &win_length_ms, &win_shift_ms)) return -1;
try {
self->cxx = new bob::ap::Energy(sampling_frequency, win_length_ms, win_shift_ms);
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.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 PyBobApEnergy_Init(PyBobApEnergyObject* 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 (PyBobApEnergy_Check(arg)) {
return PyBobApEnergy_InitCopy(self, args, kwds);
}
else {
return PyBobApEnergy_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 PyBobApEnergy_InitParameters(self, args, kwds);
}
return -1;
}
static PyObject* PyBobApEnergy_Repr(PyBobApEnergyObject* self) {
return
# if PY_VERSION_HEX >= 0x03000000
PyUnicode_FromFormat
# else
PyString_FromFormat
# endif
("%s(sampling_frequency=%f, win_length_ms=%f, win_shift_ms=%f)", Py_TYPE(self)->tp_name, self->cxx->getSamplingFrequency(), self->cxx->getWinLengthMs(), self->cxx->getWinShiftMs());
}
static PyObject* PyBobApEnergy_RichCompare (PyBobApEnergyObject* self,
PyObject* other, int op) {
if (!PyBobApEnergy_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<PyBobApEnergyObject*>(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;
}
}
PyDoc_STRVAR(s_energy_floor_str, "energy_floor");
PyDoc_STRVAR(s_energy_floor_doc,
"The energy flooring threshold"
);
static PyObject* PyBobApEnergy_GetEnergyFloor
(PyBobApEnergyObject* self, void* /*closure*/) {
return Py_BuildValue("d", self->cxx->getEnergyFloor());
}
static int PyBobApEnergy_SetEnergyFloor
(PyBobApEnergyObject* self, PyObject* o, void* /*closure*/) {
if (!PyNumber_Check(o)) {
PyErr_Format(PyExc_TypeError, "`%s' energy floor can only be set using a number, not `%s'", Py_TYPE(self)->tp_name, Py_TYPE(o)->tp_name);
return -1;
}
double d = PyFloat_AsDouble(o);
if (PyErr_Occurred()) return -1;
try {
self->cxx->setEnergyFloor(d);
}
catch (std::exception& ex) {
PyErr_SetString(PyExc_RuntimeError, ex.what());
return -1;
}
catch (...) {
PyErr_Format(PyExc_RuntimeError, "cannot reset `energy_floor' of %s: unknown exception caught", Py_TYPE(self)->tp_name);
return -1;
}
return 0;
}
static PyGetSetDef PyBobApEnergy_getseters[] = {
{
s_energy_floor_str,
(getter)PyBobApEnergy_GetEnergyFloor,
(setter)PyBobApEnergy_SetEnergyFloor,
s_energy_floor_doc,
0
},
{0} /* Sentinel */
};
PyTypeObject PyBobApEnergy_Type = {
PyVarObject_HEAD_INIT(0, 0)
s_energy_str, /*tp_name*/
sizeof(PyBobApEnergyObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)PyBobApEnergy_Delete, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
(reprfunc)PyBobApEnergy_Repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /* tp_call */
(reprfunc)PyBobApEnergy_Repr, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
s_energy_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
(richcmpfunc)PyBobApEnergy_RichCompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
PyBobApEnergy_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)PyBobApEnergy_Init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
......@@ -7,7 +7,7 @@
#include <xbob.blitz/cppapi.h>
#include <xbob.blitz/cleanup.h>
#include <bob/ap/FrameExtractor.h>
#include "types.h"
PyDoc_STRVAR(s_frame_extractor_str, XBOB_EXT_MODULE_PREFIX ".FrameExtractor");
......@@ -41,16 +41,6 @@ other\n\
"
);
/**
* Represents either an FrameExtractor
*/
typedef struct {
PyObject_HEAD
bob::ap::FrameExtractor* cxx;
} PyBobApFrameExtractorObject;
extern PyTypeObject PyBobApFrameExtractor_Type; //forward declaration
int PyBobApFrameExtractor_Check(PyObject* o) {
return PyObject_IsInstance(o, reinterpret_cast<PyObject*>(&PyBobApFrameExtractor_Type));
}
......
......@@ -10,8 +10,7 @@
#endif
#include <xbob.blitz/capi.h>
#include <xbob.blitz/cleanup.h>
extern PyTypeObject PyBobApFrameExtractor_Type;
#include "types.h"
static PyMethodDef module_methods[] = {
{0} /* Sentinel */
......@@ -35,6 +34,9 @@ static PyObject* create_module (void) {
PyBobApFrameExtractor_Type.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyBobApFrameExtractor_Type) < 0) return 0;
PyBobApEnergy_Type.tp_base = &PyBobApFrameExtractor_Type;
if (PyType_Ready(&PyBobApEnergy_Type) < 0) return 0;
# if PY_VERSION_HEX >= 0x03000000
PyObject* m = PyModule_Create(&module_definition);
# else
......@@ -50,6 +52,8 @@ static PyObject* create_module (void) {
Py_INCREF(&PyBobApFrameExtractor_Type);
if (PyModule_AddObject(m, "FrameExtractor", (PyObject *)&PyBobApFrameExtractor_Type) < 0) return 0;
Py_INCREF(&PyBobApEnergy_Type);
if (PyModule_AddObject(m, "Energy", (PyObject *)&PyBobApEnergy_Type) < 0) return 0;
/* imports xbob.blitz C-API + dependencies */
if (import_xbob_blitz() < 0) return 0;
......
......@@ -6,12 +6,13 @@
import os, sys
import unittest
import bob
import numpy
import array
import math
import time
from . import Energy
#############################################################################
# Tests blitz-based extrapolation implementation with values returned
#############################################################################
......@@ -77,8 +78,10 @@ def hamming_window(vector, hamming_kernel, win_length):
return vector
def log_filter_bank(x, n_filters, p_index, win_size):
from xbob.sp import fft
x1 = numpy.array(x, dtype=numpy.complex128)
complex_ = bob.sp.fft(x1)
complex_ = fft(x1)
for i in range(0, win_size / 2 + 1):
re = complex_[i].real
im = complex_[i].imag
......@@ -118,9 +121,9 @@ def energy_computation(obj, rate_wavsample, win_length_ms, win_shift_ms, n_filte
## Initialisation part ##
#########################
c = bob.ap.Energy(rate_wavsample[0], win_length_ms, win_shift_ms)
c = Energy(rate_wavsample[0], win_length_ms, win_shift_ms)
#ct = bob.ap.TestCeps(c)
#ct = TestCeps(c)
sf = rate_wavsample[0]
data = rate_wavsample[1]
......@@ -202,9 +205,9 @@ def energy_computation(obj, rate_wavsample, win_length_ms, win_shift_ms, n_filte
def energy_comparison_run(obj, rate_wavsample, win_length_ms, win_shift_ms, n_filters, n_ceps, dct_norm, f_min, f_max, delta_win,
pre_emphasis_coef, mel_scale, with_energy, with_delta, with_delta_delta):
c = bob.ap.Energy(rate_wavsample[0], win_length_ms, win_shift_ms)
c = Energy(rate_wavsample[0], win_length_ms, win_shift_ms)
#ct = bob.ap.TestCeps(c)
#ct = TestCeps(c)
A = c(rate_wavsample[1])
B = energy_computation(obj, rate_wavsample, win_length_ms, win_shift_ms, n_filters, n_ceps, dct_norm,
......
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Thu 13 Feb 2014 15:07:49 CET
*
* @brief All types that need cross-compilation on different units
*/
#ifndef XBOB_AP_TYPES_H
#define XBOB_AP_TYPES_H
#include <bob/ap/FrameExtractor.h>
#include <bob/ap/Energy.h>
/**
* Represents either an FrameExtractor
*/
typedef struct {
PyObject_HEAD
bob::ap::FrameExtractor* cxx;
} PyBobApFrameExtractorObject;
extern PyTypeObject PyBobApFrameExtractor_Type; //forward declaration
/**
* Represents either the Energy extractor
*/
typedef struct {
PyBobApFrameExtractorObject parent;
bob::ap::Energy* cxx;
} PyBobApEnergyObject;
extern PyTypeObject PyBobApEnergy_Type; //forward declaration
#endif /* XBOB_AP_TYPES_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment