📄 lib_components.cpp
字号:
/*
This file is part of Orange.
Orange is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Orange is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Orange; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Authors: Janez Demsar, Blaz Zupan, 1996--2002
Contact: janez.demsar@fri.uni-lj.si
*/
/********************************
This file includes constructors and specialized methods for ML* object, defined in project Components
*********************************/
#ifdef _MSC_VER
#pragma warning (disable : 4786 4114 4018 4267 4244)
#endif
#include "vars.hpp"
#include "stringvars.hpp"
#include "distvars.hpp"
#include "domain.hpp"
#include "examples.hpp"
#include "examplegen.hpp"
#include "table.hpp"
#include "cls_value.hpp"
#include "cls_example.hpp"
#include "cls_orange.hpp"
#include "lib_kernel.hpp"
#include "callback.hpp"
#include "vectortemplates.hpp"
#include "converts.hpp"
#include "slist.hpp"
#include "externs.px"
bool convertFromPython(PyObject *, PContingency &, bool allowNull=false, PyTypeObject *type=NULL);
/* ************ COST ************ */
#include "cost.hpp"
PyObject *convertToPython(const PCostMatrix &matrix)
{
int dim = matrix->dimension;
PyObject *pycost = PyList_New(dim);
float *ci = matrix->costs;
for(int i = 0; i < dim; i++) {
PyObject *row = PyList_New(dim);
for(int j = 0; j < dim; j++)
PyList_SetItem(row, j, PyFloat_FromDouble(*ci++));
PyList_SetItem(pycost, i, row);
}
return pycost;
}
bool readCostMatrix(PyObject *arg, TCostMatrix *&matrix)
{
int dim;
const int arglength = PyObject_Length(arg);
if (matrix) {
dim = matrix->dimension;
if (dim != arglength) {
PyErr_Format(PyExc_TypeError, "invalid sequence length (expected %i, got %i)", dim, arglength);
return false;
}
}
else {
dim = arglength;
matrix = mlnew TCostMatrix(dim);
}
PyObject *iter = PyObject_GetIter(arg);
if (!iter)
PYERROR(PyExc_TypeError, "sequence expected", false);
int i, j;
for(i = 0; i<dim; i++) {
PyObject *item = PyIter_Next(iter);
if (!item) {
PyErr_Format(PyExc_TypeError, "matrix is too short (%i rows expected)", dim);
break;
}
PyObject *subiter = PyObject_GetIter(item);
Py_DECREF(item);
if (!subiter) {
PyErr_Format(PyExc_TypeError, "element %i is not a sequence", i);
break;
}
for(j = 0; j<dim; j++) {
PyObject *subitem = PyIter_Next(subiter);
if (!subitem) {
PyErr_Format(PyExc_TypeError, "element %i is too short (sequence with %i elements expected)", i, dim);
break;
}
float f;
bool ok = PyNumber_ToFloat(subitem, f);
Py_DECREF(subitem);
if (!ok) {
PyErr_Format(PyExc_TypeError, "element at (%i, %i) is not a number", i, j);
break;
}
// this cannot fail:
matrix->cost(i, j) = f;
}
if (j<dim) {
Py_DECREF(subiter);
break;
}
PyObject *subitem = PyIter_Next(subiter);
Py_DECREF(subiter);
if (subitem) {
PyErr_Format(PyExc_TypeError, "element %i is too long (sequence with %i elements expected)", i, dim);
Py_DECREF(subitem);
break;
}
}
Py_DECREF(iter);
if (i<dim) {
mldelete matrix;
return false;
}
return true;
}
PyObject *CostMatrix_new(PyTypeObject *type, PyObject *args) BASED_ON(Orange, "(list-of-list-of-prices) -> CostMatrix")
{
PyTRY
if (PyTuple_Size(args) == 1) {
PyObject *arg = PyTuple_GET_ITEM(args, 0);
if (PyInt_Check(arg))
return WrapNewOrange(mlnew TCostMatrix(PyInt_AsLong(arg)), type);
if (PyOrVariable_Check(arg))
return WrapNewOrange(mlnew TCostMatrix(PyOrange_AsVariable(arg)), type);
TCostMatrix *nm = NULL;
return readCostMatrix(arg, nm) ? WrapNewOrange(nm, type) : PYNULL;
}
if (PyTuple_Size(args) == 2) {
PyObject *arg1, *arg2;
arg1 = PyTuple_GetItem(args, 0);
arg2 = PyTuple_GetItem(args, 1);
float inside;
if (PyNumber_ToFloat(arg2, inside)) {
if (PyInt_Check(arg1))
return WrapNewOrange(mlnew TCostMatrix(PyInt_AsLong(arg1), inside), type);
if (PyOrVariable_Check(arg1))
return WrapNewOrange(mlnew TCostMatrix(PyOrange_AsVariable(arg1), inside), type);
}
if (PyOrVariable_Check(arg1)) {
TCostMatrix *nm = mlnew TCostMatrix(PyOrange_AsVariable(arg1));
return readCostMatrix(arg2, nm) ? WrapNewOrange(nm, type) : PYNULL;
}
}
PYERROR(PyExc_TypeError, "invalid arguments", PYNULL);
PyCATCH
}
PyObject *CostMatrix__reduce__(PyObject *self)
{
PyTRY
CAST_TO(TCostMatrix, matrix);
const int dim = matrix->dimension;
return Py_BuildValue("O(Os#i)N", getExportedFunction("__pickleLoaderCostMatrix"),
self->ob_type,
matrix->costs, dim*dim*sizeof(float),
dim,
packOrangeDictionary(self));
PyCATCH
}
PyObject *__pickleLoaderCostMatrix(PyObject *, PyObject *args) PYARGS(METH_VARARGS, "(type, packed_matrix, dimension)")
{
PyTRY
PyTypeObject *type;
char *buf;
int bufSize, dim;
if (!PyArg_ParseTuple(args, "Os#i:__pickleLoaderCostMatrix", &type, &buf, &bufSize, &dim))
return NULL;
TCostMatrix *cm = new TCostMatrix(dim);
memcpy(cm->costs, buf, bufSize);
return WrapNewOrange(cm, type);
PyCATCH
}
PyObject *CostMatrix_native(PyObject *self) PYARGS(METH_O, "() -> list of lists of floats")
{ return convertToPython(PyOrange_AsCostMatrix(self)); }
int getCostIndex(PyObject *arg, TCostMatrix *matrix, char *error)
{
if (PyInt_Check(arg)) {
int pred = PyInt_AsLong(arg);
if ((pred<0) || (pred >= matrix->dimension))
PYERROR(PyExc_IndexError, error, -1);
return pred;
}
else {
TValue val;
return convertFromPython(arg, val, matrix->classVar) ? int(val) : -1;
}
}
PyObject *CostMatrix_getcost(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(predicted, correct) -> float")
{
PyTRY
CAST_TO(TCostMatrix, matrix);
if (PyTuple_Size(args) != 2)
PYERROR(PyExc_TypeError, "two arguments expected", PYNULL);
PyObject *arg1 = PyTuple_GET_ITEM(args, 0);
PyObject *arg2 = PyTuple_GET_ITEM(args, 1);
int pred = getCostIndex(arg1, matrix, "predicted value out of range");
int corr = getCostIndex(arg2, matrix, "correct value out of range");
if ((pred==-1) || (corr==-1))
return PYNULL;
return PyFloat_FromDouble(matrix->cost(pred, corr));
PyCATCH
}
PyObject *CostMatrix_setcost(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(predicted, correct, cost) -> float")
{
PyTRY
CAST_TO(TCostMatrix, matrix);
PyObject *arg1, *arg2;
float cost;
if (!PyArg_ParseTuple(args, "OOf:CostMatrix.setcost", &arg1, &arg2, &cost))
return PYNULL;
int pred = getCostIndex(arg1, matrix, "predicted value out of range");
int corr = getCostIndex(arg2, matrix, "correct value out of range");
if ((pred==-1) || (corr==-1))
return PYNULL;
matrix->cost(pred, corr) = cost;
RETURN_NONE;
PyCATCH
}
/* ************ BASSTAT ************ */
#include "basstat.hpp"
PyObject *BasicAttrStat_new(PyTypeObject *type, PyObject *args, PyObject *) BASED_ON(Orange, "(variable, [examples, weightID, min=, max=, avg=, dev=, n=]) -> BasicAttrStat") ALLOWS_EMPTY
{ PyTRY
PyObject *pyvar = NULL;
PExampleGenerator egen;
int weightID = 0;
if (!PyArg_ParseTuple(args, "|OO&i:BasicAttrStat.__new__", &pyvar, pt_ExampleGenerator, &egen, &weightID))
return NULL;
if (!pyvar)
return WrapNewOrange(mlnew TBasicAttrStat(PVariable()), type);
if (!egen) {
if (!PyOrVariable_Check(pyvar)) {
PyErr_Format(PyExc_TypeError, "BasicAttrStat expects a 'Variable', not a '%s'", pyvar->ob_type->tp_name);
return NULL;
}
return WrapNewOrange(mlnew TBasicAttrStat(PyOrange_AsVariable(pyvar)), type);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -