⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib_kernel.cpp

📁 orange源码 数据挖掘技术
💻 CPP
📖 第 1 页 / 共 5 页
字号:
int AttributedFloatList_setitem(TPyOrange *self, PyObject *index, PyObject *value)
{
  PyTRY 
    CAST_TO_err(TAttributedFloatList, aflist, -1)

    const int ind = AttributedList_getIndex(aflist->size(), aflist->attributes, index);
    if (ind == ILLEGAL_INT)
      return -1;

    return FloatList_setitem_sq(self, ind, value);
  PyCATCH_1
}



PyObject *AttributedBoolList_new(PyTypeObject *type, PyObject *args, PyObject *keywds) BASED_ON(BoolList, "(attributes, list)") ALLOWS_EMPTY
{
  PyObject *ob1 = NULL, *ob2 = NULL;
  if (!PyArg_UnpackTuple(args, "AttributedBoolList.new", 0, 2, &ob1, &ob2))
    return PYNULL;

  PyObject *wabl = ListOfUnwrappedMethods<PAttributedBoolList, TAttributedBoolList, bool>::_new(type, ob2 ? ob2 : ob1, keywds);

  if (ob2) {
    PVarList attributes = PVarList_FromArguments(ob1);
    if (!attributes)
      return PYNULL;

    PyOrange_AsAttributedBoolList(wabl)->attributes = attributes;
  }

  return wabl;
}


PyObject * /*no pyxtract!*/ BoolList_getitem_sq(TPyOrange *self, int index);
int        /*no pyxtract!*/ BoolList_setitem_sq(TPyOrange *self, int index, PyObject *item);


PyObject *AttributedBoolList_getitem(TPyOrange *self, PyObject *index)
{
  PyTRY 
    CAST_TO(TAttributedBoolList, aflist)

    const int ind = AttributedList_getIndex(aflist->size(), aflist->attributes, index);
    if (ind == ILLEGAL_INT)
      return PYNULL;

    return BoolList_getitem_sq(self, ind);
  PyCATCH
}


int AttributedBoolList_setitem(TPyOrange *self, PyObject *index, PyObject *value)
{
  PyTRY 
    CAST_TO_err(TAttributedBoolList, aflist, -1)

    const int ind = AttributedList_getIndex(aflist->size(), aflist->attributes, index);
    if (ind == ILLEGAL_INT)
      return -1;

    return BoolList_setitem_sq(self, ind, value);
  PyCATCH_1
}


/* ************ DOMAIN ************ */

#include "domain.hpp"

const TMetaDescriptor *metaDescriptorFromArg(TDomain &domain, PyObject *rar)
{
  TMetaDescriptor *desc = NULL;

  if (PyString_Check(rar))
    desc = domain.metas[string(PyString_AsString(rar))];

  else if (PyOrVariable_Check(rar))
    desc = domain.metas[PyOrange_AsVariable(rar)->name];

  else if (PyInt_Check(rar))
    desc = domain.metas[PyInt_AsLong(rar)];

  else
    PYERROR(PyExc_TypeError, "invalid meta descriptor", NULL);

  if (!desc)
    PYERROR(PyExc_AttributeError, "meta attribute does not exist", NULL);

  return desc;
}


PyObject *Domain_metaid(TPyOrange *self, PyObject *rar) PYARGS(METH_O, "(name | descriptor) -> int")
{ PyTRY
    const TMetaDescriptor *desc = metaDescriptorFromArg(SELF_AS(TDomain), rar);
    return desc ? PyInt_FromLong(desc->id) : NULL;
  PyCATCH
}


PyObject *Domain_isOptionalMeta(TPyOrange *self, PyObject *rar) PYARGS(METH_O, "(name | int | descriptor) -> bool")
{
  PyTRY
    const TMetaDescriptor *desc = metaDescriptorFromArg(SELF_AS(TDomain), rar);
    return desc ? PyBool_FromLong(desc->optional ? 1 : 0) : NULL;

  PyCATCH
}


PyObject *Domain_hasmeta(TPyOrange *self, PyObject *rar) PYARGS(METH_O, "(name | int | descriptor) -> bool")
{
  PyTRY
    CAST_TO(TDomain, domain)

    TMetaDescriptor *desc = NULL;

    if (PyString_Check(rar))
      desc = domain->metas[string(PyString_AsString(rar))];

    else if (PyOrVariable_Check(rar))
      desc = domain->metas[PyOrange_AsVariable(rar)->name];

    else if (PyInt_Check(rar))
      desc = domain->metas[PyInt_AsLong(rar)];

    else
      PYERROR(PyExc_TypeError, "invalid meta descriptor", NULL);

    return PyBool_FromLong(desc ? 1 : 0);
  PyCATCH
}


PyObject *Domain_getmeta(TPyOrange *self, PyObject *rar) PYARGS(METH_O, "(name | int) -> Variable")
{ PyTRY
    const TMetaDescriptor *desc = metaDescriptorFromArg(SELF_AS(TDomain), rar);
    return desc ? WrapOrange(desc->variable) : NULL;
  PyCATCH
}


PyObject *Domain_getmetasLow(const TDomain &domain)
{
  PyObject *dict = PyDict_New();
  const_ITERATE(TMetaVector, mi, domain.metas)
    PyDict_SetItem(dict, PyInt_FromLong((*mi).id), WrapOrange((*mi).variable));
  return dict;
}


PyObject *Domain_getmetasLow(const TDomain &domain, const bool optional)
{
  PyObject *dict = PyDict_New();
  const_ITERATE(TMetaVector, mi, domain.metas)
    if (optional == (*mi).optional)
      PyDict_SetItem(dict, PyInt_FromLong((*mi).id), WrapOrange((*mi).variable));
  return dict;
}


PyObject *Domain_getmetas(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "([optional]) -> {int: Variable}")
{ PyTRY
    if (PyTuple_Size(args)) {
      bool opt;
      if (!PyArg_ParseTuple(args, "O&:Domain.getmetas", &getBool, &opt))
        return NULL;

      return Domain_getmetasLow(SELF_AS(TDomain), opt);
    }

    return Domain_getmetasLow(SELF_AS(TDomain));
  PyCATCH
}


PyObject *Domain_addmeta(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "(id, descriptor[, optional]) -> None")
{ PyTRY
    CAST_TO(TDomain, domain);

    int id;
    PVariable var;
    bool opt = false;
    if (!PyArg_ParseTuple(args, "iO&|O&", &id, cc_Variable, &var, &getBool, &opt))
      return PYNULL;

    domain->metas.push_back(TMetaDescriptor(id, var, opt));
    domain->domainHasChanged();
    RETURN_NONE;
  PyCATCH
}


bool convertMetasFromPython(PyObject *dict, TMetaVector &metas)
{
  int pos = 0;
  PyObject *pykey, *pyvalue;
  while (PyDict_Next(dict, &pos, &pykey, &pyvalue)) {
    if (!PyOrVariable_Check(pyvalue)) {
      PyErr_Format(PyExc_TypeError, "parsing meta attributes: dictionary value at position '%i' should be 'Variable', not '%s'", pos-1, pyvalue->ob_type->tp_name);
      return false;
    }
    if (!PyInt_Check(pykey) || (PyInt_AsLong(pykey)>=0))
      PYERROR(PyExc_TypeError, "parsing meta attributes: dictionary keys should be meta-ids (negative integers)", false);

    metas.push_back(TMetaDescriptor((int)PyInt_AsLong(pykey), PyOrange_AsVariable(pyvalue)));
  }

  return true;
}


PyObject *Domain_addmetasLow(TDomain &domain, PyObject *dict, const bool opt = false)
{
  TMetaVector metas;
  if (!convertMetasFromPython(dict, metas))
    return PYNULL;

  ITERATE(TMetaVector, mi, metas) {
    (*mi).optional = opt;
    domain.metas.push_back(*mi);
  }

  domain.domainHasChanged();

  RETURN_NONE;
}


PyObject *Domain_addmetas(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "({id: descriptor, id: descriptor, ...}[, optional]) -> None")
{ PyTRY
    PyObject *pymetadict;
    bool opt = false;
    if (!PyArg_ParseTuple(args, "O|O&", &pymetadict, &getBool, &opt))
      PYERROR(PyExc_AttributeError, "Domain.addmetas expects a dictionary with id's and descriptors, optionally follow by a boolean flag 'optional'", PYNULL);

    return Domain_addmetasLow(SELF_AS(TDomain), pymetadict, opt);
  PyCATCH
}


bool removeMeta(PyObject *rar, TMetaVector &metas)
{ TMetaVector::iterator mvi(metas.begin()), mve(metas.end());

  if (PyInt_Check(rar)) {
    int id = PyInt_AsLong(rar);
    while((mvi!=mve) && ((*mvi).id!=id))
      mvi++;
  }
  else if (PyOrVariable_Check(rar))
    while((mvi!=mve) && ((*mvi).variable!=PyOrange_AsVariable(rar)))
      mvi++;
  else if (PyString_Check(rar)) {
    char *metaname = PyString_AsString(rar);
    while((mvi!=mve) && ((*mvi).variable->name!=metaname))
      mvi++;
  }
  else
    mvi=mve;

  if (mvi==mve)
    PYERROR(PyExc_AttributeError, "meta value not found", false);

  metas.erase(mvi);
  return true;
}


PyObject *Domain_removemeta(TPyOrange *self, PyObject *rar) PYARGS(METH_O, "({id0:desc0, id1:desc1, ...}) | ([id0|desc0, id1|desc1, ...]) -> None")
{ PyTRY
    CAST_TO(TDomain, domain);

    if (PyDict_Check(rar)) {
      int pos=0;
      PyObject *key, *value;
      TMetaVector newMetas=domain->metas;
      TMetaVector::iterator mve=domain->metas.end();

      while (PyDict_Next(rar, &pos, &key, &value)) {
        if (!PyInt_Check(key) || !PyOrVariable_Check(value))
          PYERROR(PyExc_AttributeError, "invalid arguments", PYNULL);

        long idx=PyInt_AsLong(key);
        TMetaVector::iterator mvi(newMetas.begin());
        for(; (mvi!=mve) && ( ((*mvi).id!=idx) || (*mvi).variable!=PyOrange_AsVariable(value)); mvi++);
        if (mvi==mve)
          PYERROR(PyExc_AttributeError, "meta not found", PYNULL);

        newMetas.erase(mvi);
      }
      domain->metas=newMetas;
      domain->domainHasChanged();
    }

    else if (PyList_Check(rar)) {
      TMetaVector newMetas=domain->metas;
      for(int pos=0, noel=PyList_Size(rar); pos!=noel; pos++)
        if (!removeMeta(PyList_GetItem(rar, pos), newMetas))
          return PYNULL;
      domain->metas=newMetas;
      domain->domainHasChanged();
    }
  
    else if (!removeMeta(rar, domain->metas))
      return PYNULL;

    RETURN_NONE;
  PyCATCH
}


PyObject *Domain_hasDiscreteAttributes(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(includeClass=0) -> int")
{
  PyTRY
      PyObject *includeClass = PYNULL;
      if (!PyArg_ParseTuple(args, "|O:Domain.hasDiscreteAttributes", &includeClass))
        return PYNULL;

      return PyInt_FromLong(SELF_AS(TDomain).hasDiscreteAttributes(!includeClass || PyObject_IsTrue(includeClass)!=0) ? 1 : 0);
  PyCATCH
}


PyObject *Domain_hasContinuousAttributes(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(includeClass=0) -> int")
{
  PyTRY
      PyObject *includeClass = PYNULL;
      if (!PyArg_ParseTuple(args, "|O:Domain.hasContinuousAttributes", &includeClass))
        return PYNULL;

      return PyInt_FromLong(SELF_AS(TDomain).hasContinuousAttributes(!includeClass || PyObject_IsTrue(includeClass)!=0) ? 1 : 0);
  PyCATCH
}


PyObject *Domain_hasOtherAttributes(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(includeClass=0) -> int")
{
  PyTRY
      PyObject *includeClass = PYNULL;
      if (!PyArg_ParseTuple(args, "|O:Domain.hasOtherAttributes", &includeClass))
        return PYNULL;

      return PyInt_FromLong(SELF_AS(TDomain).hasOtherAttributes(!includeClass || PyObject_IsTrue(includeClass)!=0) ? 1 : 0);
  PyCATCH
}


int Domain_len(TPyOrange *self)
{ PyTRY
    CAST_TO_err(TDomain, domain, -1);
    return domain->variables->size();
  PyCATCH_1
}


PyObject *Domain_index(PyObject *self, PyObject *arg) PYARGS(METH_O, "(variable) -> int")
{
  PyTRY
    CAST_TO(TDomain, domain);

    PVariable variable = varFromArg_byDomain(arg, domain, true);
    return variable ? PyInt_FromLong(domain->getVarNum(variable)) : PYNULL;
  PyCATCH
}


int Domain_contains(PyObject *self, PyObject *arg)
{
  PyTRY
    CAST_TO_err(TDomain, domain, -1);

    PVariable variable = varFromArg_byDomain(arg, domain, true);
    PyErr_Clear();
    return variable ? 1 : 0;
  PyCATCH_1
}

CONSTRUCTOR_KEYWORDS(Domain, "source")

PyObject *Domain_new(PyTypeObject *type, PyObject *args, PyObject *keywds) BASED_ON(Orange, "(list-of-attrs | domain [, hasClass | classVar | None] [,domain | list-of-attrs | source=domain])")

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -