Changeset 9532:2c6a8bb6ec89 in orange


Ignore:
Timestamp:
01/15/12 00:05:13 (2 years ago)
Author:
janezd <janez.demsar@…>
Branch:
default
Convert:
a1d3d929e242ed688f1d840f8d2fd47995e6da25
Message:

Conversion to numpy now supports multiple classes (Ticket #1012)

Location:
source/orange
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • source/orange/gslconversions.cpp

    r7665 r9532  
    4040 
    4141void parseMatrixContents(PExampleGenerator egen, const int &weightID, const char *contents, const int &multiTreatment, 
    42                          bool &hasClass, bool &classVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
     42                         bool &hasClass, bool &classVector, bool &multiclassVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
    4343                         vector<bool> &include) 
    4444{ 
     
    4646 
    4747  columns = 0; 
    48   int classIncluded = 0, attrsIncluded = 0, weightIncluded = 0; 
     48  int classIncluded = 0, attrsIncluded = 0, weightIncluded = 0, multiclassIncluded = 0; 
    4949  bool attrsRequested = false, classRequested = false, weightRequested = false; 
     50  bool multiclassRequested = false; 
    5051  const char *cp; 
    5152  for(cp = contents; *cp && (*cp!='/'); cp++) { 
     
    6162      case 'W': weightRequested = true; 
    6263      case 'w': weightIncluded++; 
     64                break; 
     65 
     66      case 'M': multiclassRequested = true; 
     67      case 'm': multiclassIncluded++; 
    6368                break; 
    6469 
     
    8893        case 'w': weightVector = (weightID != 0); break; 
    8994        case 'W': weightVector = true; break; 
     95 
     96        case 'm': multiclassVector = (egen->domain->classVars->size() != 0); break; 
     97        case 'M': multiclassVector = true; break; 
    9098        default: 
    9199          raiseErrorWho("parseMatrixContents", "unrecognized character '%c' in format string '%s')", *cp, contents); 
     
    116124    if (weightID) 
    117125      columns += weightIncluded; 
     126  } 
     127 
     128  if (multiclassIncluded || multiclassVector) { 
     129      columns += multiclassIncluded * egen->domain->classVars->size(); 
    118130  } 
    119131 
  • source/orange/gslconversions.hpp

    r6531 r9532  
    3535 
    3636void parseMatrixContents(PExampleGenerator egen, const int &weightID, const char *contents, const int &multiTreatment, 
    37                          bool &hasClass, bool &classVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
     37                         bool &hasClass, bool &classVector, bool &multiclassVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
    3838                         vector<bool> &include); 
    3939 
  • source/orange/lib_kernel.cpp

    r9529 r9532  
    32853285 
    32863286 
    3287 PyObject *packMatrixTuple(PyObject *X, PyObject *y, PyObject *w, char *contents) 
     3287PyObject *packMatrixTuple(PyObject *X, PyObject *y, PyObject *my, PyObject *w, char *contents) 
    32883288{ 
    32893289  int left = (*contents && *contents != '/') ? 1 : 0; 
     
    33073307        PyTuple_SetItem(res, left++, y); 
    33083308      } 
    3309       else { 
     3309      else if ((*cp == 'w') || (*cp == 'W')) { 
    33103310        Py_INCREF(w); 
    33113311        PyTuple_SetItem(res, left++, w); 
    33123312      } 
     3313      else { 
     3314        Py_INCREF(my); 
     3315        PyTuple_SetItem(res, left++, my); 
     3316      } 
    33133317 
    33143318  Py_DECREF(X); 
    33153319  Py_DECREF(y); 
     3320  Py_DECREF(my); 
    33163321  Py_DECREF(w); 
    33173322  return res; 
     
    33203325 
    33213326void parseMatrixContents(PExampleGenerator egen, const int &weightID, const char *contents, const int &multiTreatment, 
    3322                          bool &hasClass, bool &classVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
     3327                         bool &hasClass, bool &classVector, bool &multiclassVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
    33233328                         vector<bool> &include); 
    33243329 
     
    33833388    PExampleGenerator egen = PyOrange_AsExampleGenerator(self); 
    33843389 
    3385     bool hasClass, classVector, weightVector, classIsDiscrete; 
     3390    bool hasClass, classVector, weightVector, multiclassVector, classIsDiscrete; 
    33863391    vector<bool> include; 
    33873392    int columns; 
    33883393    parseMatrixContents(egen, weightID, contents, multinomialTreatment, 
    3389                             hasClass, classVector, weightVector, classIsDiscrete, columns, include); 
     3394                            hasClass, classVector, multiclassVector, weightVector, 
     3395                            classIsDiscrete, columns, include); 
    33903396 
    33913397    int rows = egen->numberOfExamples(); 
    33923398    PVariable classVar = egen->domain->classVar; 
    33933399 
    3394     PyObject *X, *y, *w, *mask = NULL, *masky = NULL; 
    3395     double *Xp, *yp, *wp; 
    3396     signed char *mp = NULL, *mpy = NULL; 
     3400    PyObject *X, *y, *w, *my, *mask = NULL, *masky = NULL, *maskmy = NULL; 
     3401    double *Xp, *yp, *myp, *wp; 
     3402    signed char *mp = NULL, *mpy = NULL, *mpmy = NULL; 
    33973403    if (columns) { 
    33983404      X = PyObject_CallFunction(mzeros, "(ii)s", rows, columns, "d"); 
     
    34303436    } 
    34313437 
     3438    if (multiclassVector) { 
     3439      my = PyObject_CallFunction(mzeros, "(ii)s", rows, egen->domain->classVars->size(), "d"); 
     3440      if (!my) 
     3441        return PYNULL; 
     3442      myp = (double *)((PyArrayObject *)my)->data; 
     3443 
     3444      if (maskedArray) { 
     3445        maskmy= PyObject_CallFunction(mzeros, "(ii)s", rows, egen->domain->classVars->size(), "b"); 
     3446        mpmy = (signed char *)((PyArrayObject *)maskmy)->data; 
     3447      } 
     3448    } 
     3449    else { 
     3450      my = Py_None; 
     3451      Py_INCREF(my); 
     3452      mpmy = NULL; 
     3453    } 
    34323454 
    34333455    if (weightVector) { 
     
    34723494              break; 
    34733495 
     3496            case 'M': 
     3497            case 'm': { 
     3498              const TVarList &classes = egen->domain->classVars.getReference(); 
     3499              TVarList::const_iterator vi(classes.begin()), ve(classes.end()); 
     3500              TValue *eei = (*ei).values_end; 
     3501              for(; vi != ve; eei++, vi++) 
     3502                if (!storeNumPyValue(Xp, *eei, mp, *vi, row)) 
     3503                  return PYNULL; 
     3504              break; 
     3505            } 
     3506 
    34743507            case 'W': 
    34753508            case 'w': 
     
    34993532        if (wp) 
    35003533          *wp++ = WEIGHT(*ei); 
     3534 
     3535        if (myp) { 
     3536            const TVarList &classes = egen->domain->classVars.getReference(); 
     3537            TVarList::const_iterator vi(classes.begin()), ve(classes.end()); 
     3538            TValue *eei = (*ei).values_end; 
     3539            for(; vi != ve; eei++, vi++) 
     3540              if (!storeNumPyValue(myp, *eei, mpmy, *vi, row)) 
     3541                return PYNULL; 
     3542        } 
    35013543      } 
    35023544 
    35033545      if (maskedArray) { 
    3504         PyObject *args, *maskedX = NULL, *maskedy = NULL; 
     3546        PyObject *args, *maskedX = NULL, *maskedy = NULL, *maskedmy = NULL; 
    35053547 
    35063548        bool err = false; 
     
    35323574        } 
    35333575 
     3576        if (!err && maskmy) { 
     3577          args = Py_BuildValue("OOiOO", my, Py_None, 1, Py_None, maskmy); 
     3578          maskedmy = PyObject_CallObject(*maskedArray, args); 
     3579          Py_DECREF(args); 
     3580          if (!maskedmy) { 
     3581            PyErr_Clear(); 
     3582            args = Py_BuildValue("OOOi", my, maskmy, Py_None, 1); 
     3583            maskedmy = PyObject_CallObject(*maskedArray, args); 
     3584            Py_DECREF(args); 
     3585          } 
     3586          err = !maskedmy; 
     3587        } 
     3588 
    35343589        if (err) { 
    35353590          Py_DECREF(X); 
     
    35393594          Py_XDECREF(mask); 
    35403595          Py_XDECREF(masky); 
     3596          Py_XDECREF(maskmy); 
    35413597          return PYNULL; 
    35423598        } 
     
    35533609          y = maskedy; 
    35543610        } 
    3555       } 
    3556  
    3557       return packMatrixTuple(X, y, w, contents); 
     3611 
     3612        if (maskmy) { 
     3613          Py_DECREF(my); 
     3614          Py_DECREF(maskmy); 
     3615          my = maskedmy; 
     3616        } 
     3617      } 
     3618 
     3619      return packMatrixTuple(X, y, my, w, contents); 
    35583620    } 
    35593621    catch (...) { 
     
    35633625      Py_XDECREF(mask); 
    35643626      Py_XDECREF(masky); 
     3627      Py_XDECREF(maskmy); 
    35653628      throw; 
    35663629    } 
  • source/orange/rconversions.cpp

    r7665 r9532  
    2828                        double *&X, double *&y, double *&w, int &rows, int &columns) 
    2929{ 
    30   bool hasClass, classVector, weightVector, classIsDiscrete; 
     30  bool hasClass, classVector, multiclassVector, weightVector, classIsDiscrete; 
    3131  vector<bool> include; 
    3232 
    3333  parseMatrixContents(egen, weightID, contents, multiTreatment, 
    34                           hasClass, classVector, weightVector, classIsDiscrete, columns, include); 
     34                          hasClass, classVector, multiclassVector, weightVector, classIsDiscrete, columns, include); 
     35 
     36  // this does not work if the domain includes multiple classes -- which is irrelevant 
     37  // since nobody calls this function anyway ;) 
    3538 
    3639  rows = egen->numberOfExamples(); 
  • source/orange/rconversions.hpp

    r6531 r9532  
    3131 
    3232void parseMatrixContents(PExampleGenerator egen, const int &weightID, const char *contents, const int &multiTreatment, 
    33                          bool &hasClass, bool &classVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
     33                         bool &hasClass, bool &classVector, bool &multiClassVector, bool &weightVector, bool &classIsDiscrete, int &columns, 
    3434                         vector<bool> &include); 
    3535 
Note: See TracChangeset for help on using the changeset viewer.