Changeset 10588:5ea62b7b37bc in orange


Ignore:
Timestamp:
03/20/12 22:50:26 (2 years ago)
Author:
Janez Demšar <janez.demsar@…>
Branch:
default
Message:

Fixed conversion of Examples from lists and of ExampleTables from numpy arrays to work for domains with multiple classes (fixes #1145)

Location:
source/orange
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • source/orange/cls_example.cpp

    r9415 r10588  
    6060  } 
    6161 
    62   if (Py_ssize_t(dom->variables->size()) != PyList_Size(lst)) { 
    63     PyErr_Format(PyExc_IndexError, "invalid list size (%i items expected)", dom->variables->size()); 
     62  int const nvars = dom->variables->size() + dom->classVars->size(); 
     63  if (Py_ssize_t(nvars) != PyList_Size(lst)) { 
     64    PyErr_Format(PyExc_IndexError, "invalid list size (got %i, expected %i items)", 
     65        PyList_Size(lst), nvars); 
    6466    return false; 
    6567  } 
     
    6769  Py_ssize_t pos = 0; 
    6870  TExample::iterator ei(example.begin()); 
    69   PITERATE(TVarList, vi, dom->variables) { 
     71  TVarList::iterator vi(dom->variables->begin()); 
     72  TVarList::const_iterator const ve(dom->variables->end()); 
     73  TVarList::const_iterator const ce(dom->classVars->end()); 
     74  while(vi != ce) { 
    7075    PyObject *li=PyList_GetItem(lst, pos++); 
    7176    if (!li) 
     
    107112      else 
    108113        ei++; 
     114    } 
     115    if (++vi == ve) { 
     116        vi = dom->classVars->begin(); 
    109117    } 
    110118  } 
  • source/orange/lib_kernel.cpp

    r10568 r10588  
    29262926        PYERROR(PyExc_AttributeError, "two-dimensional array expected for an ExampleTable", NULL); 
    29272927 
    2928      PVarList variables; 
    2929      TVarList::const_iterator vi, ve; 
     2928      PVarList variables; 
     2929      TVarList::const_iterator vi; 
    29302930 
    29312931      if (!domain) { 
     
    29382938        domain = mlnew TDomain(PVariable(), lvariables); 
    29392939        variables = domain->variables; 
    2940         ve = variables->end(); 
    29412940      } 
    29422941 
    29432942      else { 
    2944         if (array->dimensions[1] != domain->variables->size()) 
    2945           PYERROR(PyExc_AttributeError, "the number of columns in the array doesn't match the number of attributes", NULL); 
     2943          int const nvars = domain->variables->size() + domain->classVars->size(); 
     2944          if (array->dimensions[1] != nvars) { 
     2945              PyErr_Format(PyExc_AttributeError, 
     2946                  "the number of columns (%i) in the array doesn't match the number of attributes (%i)", 
     2947                  array->dimensions[1], nvars); 
     2948              return NULL; 
     2949          } 
    29462950 
    29472951       variables = domain->variables; 
    2948        ve = variables->end(); 
    2949        for(vi = variables->begin(); vi!=ve; vi++) 
    2950                   if (((*vi)->varType != TValue::INTVAR) && ((*vi)->varType != TValue::FLOATVAR)) 
    2951                       PYERROR(PyExc_TypeError, "cannot read the value of attribute '%s' from an array (unsupported attribute type)", NULL); 
    2952             } 
     2952       for(vi = variables->begin(); vi!=variables->end(); vi++) 
     2953          if (((*vi)->varType != TValue::INTVAR) && ((*vi)->varType != TValue::FLOATVAR)) 
     2954              PYERROR(PyExc_TypeError, "cannot read the value of attribute '%s' from an array (unsupported attribute type)", NULL); 
     2955        
     2956       for(vi = domain->classVars->begin(); vi!=domain->classVars->end(); vi++)  
     2957          if (((*vi)->varType != TValue::INTVAR) && ((*vi)->varType != TValue::FLOATVAR)) 
     2958              PYERROR(PyExc_TypeError, "cannot read the value of attribute '%s' from an array (unsupported attribute type)", NULL); 
     2959      } 
    29532960 
    29542961      const char arrayType = getArrayType(array); 
     
    29692976      const int &strideMaskCol = mask ? mask->strides[1] : strideCol; 
    29702977 
     2978      TVarList::const_iterator const vb(variables->begin()); 
     2979      TVarList::const_iterator const ve(variables->end()); 
     2980      TVarList::const_iterator const cb(domain->classVars->begin()); 
     2981      TVarList::const_iterator const ce(domain->classVars->end()); 
     2982 
    29712983      try { 
    29722984        TExample::iterator ei; 
     
    29802992 
    29812993          #define ARRAYTYPE(TYPE) \ 
    2982             for(ei = nex->begin(), vi = variables->begin(); vi!=ve; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) \ 
     2994            for(ei = nex->begin(), vi = vb; vi!=ve; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) \ 
     2995              if ((*vi)->varType == TValue::INTVAR) \ 
     2996                intValInit(*ei, *(TYPE *)elPtr, mask && !*maskPtr ? valueDK : valueRegular); \ 
     2997              else \ 
     2998                floatValInit(*ei, *(TYPE *)elPtr, mask && !*maskPtr ? valueDK : valueRegular); \ 
     2999            for(vi = cb; vi!=ce; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) \ 
    29833000              if ((*vi)->varType == TValue::INTVAR) \ 
    29843001                intValInit(*ei, *(TYPE *)elPtr, mask && !*maskPtr ? valueDK : valueRegular); \ 
     
    29993016 
    30003017            case 'f': 
    3001               for(ei = nex->begin(), vi = variables->begin(); vi!=ve; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) 
     3018              for(ei = nex->begin(), vi = vb; vi!=ve; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) 
     3019                if ((*vi)->varType == TValue::INTVAR) 
     3020                  intValInit(*ei, int(floor(0.5 + *(float *)elPtr)), mask && !*maskPtr ? valueDK : valueRegular); 
     3021                else 
     3022                  floatValInit(*ei, *(float *)elPtr, mask && !*maskPtr ? valueDK : valueRegular); 
     3023              for(vi = cb; vi!=ce; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) 
    30023024                if ((*vi)->varType == TValue::INTVAR) 
    30033025                  intValInit(*ei, int(floor(0.5 + *(float *)elPtr)), mask && !*maskPtr ? valueDK : valueRegular); 
     
    30083030            case 'd': 
    30093031              for(ei = nex->begin(), vi = variables->begin(); vi!=ve; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) 
     3032                if ((*vi)->varType == TValue::INTVAR) 
     3033                  intValInit(*ei, int(floor(0.5 + *(double *)elPtr)), mask && !*maskPtr ? valueDK : valueRegular); 
     3034                else 
     3035                  floatValInit(*ei, *(double *)elPtr, mask && !*maskPtr ? valueDK : valueRegular); 
     3036              for(vi = cb; vi!=ce; vi++, ei++, elPtr += strideCol, maskPtr += strideMaskCol) 
    30103037                if ((*vi)->varType == TValue::INTVAR) 
    30113038                  intValInit(*ei, int(floor(0.5 + *(double *)elPtr)), mask && !*maskPtr ? valueDK : valueRegular); 
Note: See TracChangeset for help on using the changeset viewer.