source: orange/source/orange/rconversions.cpp @ 7665:3aede63f569f

Revision 7665:3aede63f569f, 4.0 KB checked in by markotoplak, 3 years ago (diff)

Replaced Variable->name with setter and getter. Interface to Python remained the same.

Line 
1/*
2    This file is part of Orange.
3   
4    Copyright 1996-2010 Faculty of Computer and Information Science, University of Ljubljana
5    Contact: janez.demsar@fri.uni-lj.si
6
7    Orange is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Orange is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Orange.  If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <vector>
22#include "rconversions.hpp"
23#include "vars.hpp"
24#include "examplegen.hpp"
25
26
27void exampleGenerator2r(PExampleGenerator egen, const int &weightID, const char *contents, const int &multiTreatment,
28                        double *&X, double *&y, double *&w, int &rows, int &columns)
29{
30  bool hasClass, classVector, weightVector, classIsDiscrete;
31  vector<bool> include;
32
33  parseMatrixContents(egen, weightID, contents, multiTreatment,
34                          hasClass, classVector, weightVector, classIsDiscrete, columns, include);
35
36  rows = egen->numberOfExamples();
37
38  X = columns ? (double *)malloc(rows * columns * sizeof(double)) : NULL;
39  y = classVector ? (double *)malloc(rows * sizeof(double)) : NULL;
40  w = weightVector ? (double *)malloc(rows * sizeof(double)) : NULL;
41
42  double *Xi = X;
43  double *yi = y;
44  double *wi = w;
45
46  try {
47    int row = 0;
48    TExampleGenerator::iterator ei(egen->begin());
49    for(; ei; ++ei, row++, Xi += 1 - rows*columns) {
50      int col = 0;
51     
52      /* This is all optimized assuming that each symbol (A, C, W) only appears once.
53         If it would be common for them to appear more times, we could cache the values,
54         but since this is unlikely, caching would usually slow down the conversion */
55      for(const char *cp = contents; *cp && (*cp!='/'); cp++) {
56        switch (*cp) {
57          case 'A':
58          case 'a': {
59            const TVarList &attributes = egen->domain->attributes.getReference();
60            TVarList::const_iterator vi(attributes.begin()), ve(attributes.end());
61            TExample::iterator eei((*ei).begin());
62            vector<bool>::const_iterator bi(include.begin());
63            for(; vi != ve; eei++, vi++, bi++)
64              if (*bi) {
65                if ((*eei).isSpecial())
66                  raiseErrorWho("exampleGenerator2r", "value of attribute '%s' in example '%i' is undefined", (*vi)->get_name().c_str(), row);
67                *Xi = (*vi)->varType == TValue::FLOATVAR ? (*eei).floatV : float((*eei).intV);
68                Xi += rows;
69              }
70            break;
71          }
72
73          case 'C':
74          case 'c': 
75            if (hasClass) {
76              const TValue &classVal = (*ei).getClass();
77              if (classVal.isSpecial())
78                raiseErrorWho("exampleGenerator2r", "example %i has undefined class", row);
79              *Xi = classIsDiscrete ? float(classVal.intV) : classVal.floatV;
80              Xi += rows;
81            }
82            break;
83
84          case 'W':
85          case 'w': 
86            if (weightID)
87              *Xi = WEIGHT(*ei);
88              Xi += rows;
89            break;
90
91          case '0':
92            *Xi = 0.0;
93            Xi += rows;
94            break;
95
96          case '1':
97            *Xi = 1.0;
98            Xi += rows;
99            break;
100        }
101      }
102
103      if (y) {
104        const TValue &classVal = (*ei).getClass();
105        if (classVal.isSpecial())
106          raiseErrorWho("exampleGenerator2r", "example %i has undefined class", row);
107        *(yi++) = classIsDiscrete ? float(classVal.intV) : classVal.floatV;
108      }
109
110      if (w)
111        *(wi++) = WEIGHT(*ei);
112    }
113  }
114  catch (...) {
115    if (X)
116      free(X);
117    if (y)
118      free(y);
119    if (w)
120      free(w);
121    throw;
122  }
123}
Note: See TracBrowser for help on using the repository browser.