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

Revision 7665:3aede63f569f, 4.9 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/*
22#include "gsl/gsl_matrix.h"
23#include "gsl/gsl_vector.h"
24*/
25#include <vector>
26
27#include "orange.hpp"
28#include "vars.hpp"
29#include "examplegen.hpp"
30
31/*
32#ifndef _DEBUG
33// I prefer to see exceptions when debugging
34extern "C" void my_gsl_error_handler(const char *reason, const char *file, int line, int)
35{ raiseErrorWho("GSL", "%s (%s:%i)", reason, file, line); }
36
37gsl_error_handler_t *fooerrh = gsl_set_error_handler(my_gsl_error_handler);
38#endif
39*/
40
41void parseMatrixContents(PExampleGenerator egen, const int &weightID, const char *contents, const int &multiTreatment,
42                         bool &hasClass, bool &classVector, bool &weightVector, bool &classIsDiscrete, int &columns,
43                         vector<bool> &include)
44{
45  hasClass = bool(egen->domain->classVar);
46
47  columns = 0;
48  int classIncluded = 0, attrsIncluded = 0, weightIncluded = 0;
49  bool attrsRequested = false, classRequested = false, weightRequested = false;
50  const char *cp;
51  for(cp = contents; *cp && (*cp!='/'); cp++) {
52    switch (*cp) {
53      case 'A': attrsRequested = true;
54      case 'a': attrsIncluded++;
55                break;
56
57      case 'C': classRequested = true;
58      case 'c': classIncluded++;
59                break;
60
61      case 'W': weightRequested = true;
62      case 'w': weightIncluded++;
63                break;
64
65      case '0':
66      case '1': columns++;
67                break;
68      default:
69        raiseErrorWho("parseMatrixContents", "unrecognized character '%c' in format string '%s')", *cp, contents);
70    }
71  }
72
73  classVector = false;
74  weightVector = false;
75
76  if (*cp)
77    while(*++cp)
78      switch (*cp) {
79        case 'A':
80        case 'a': raiseErrorWho("parseMatrixContents", "invalid format string (attributes on the right side)");
81
82        case '0':
83        case '1': raiseErrorWho("parseMatrixContents", "invalid format string (constants on the right side)");
84
85        case 'c': classVector = hasClass; break;
86        case 'C': classVector = true; break;
87
88        case 'w': weightVector = (weightID != 0); break;
89        case 'W': weightVector = true; break;
90        default:
91          raiseErrorWho("parseMatrixContents", "unrecognized character '%c' in format string '%s')", *cp, contents);
92      }
93
94
95  if (classIncluded || classVector) {
96    if (hasClass) {
97      TEnumVariable *eclassVar = egen->domain->classVar.AS(TEnumVariable);
98      classIsDiscrete = eclassVar != NULL;
99      if (classIsDiscrete) {
100        if ((eclassVar->values->size()>2) && (multiTreatment != 1))
101          raiseErrorWho("parseMatrixContents", "multinomial classes are allowed only when explicitly treated as ordinal"); 
102      }
103      else {
104        if (egen->domain->classVar->varType != TValue::FLOATVAR)
105          raiseErrorWho("parseMatrixContents", "unsupported class type");
106      } 
107
108      columns += classIncluded;
109    }
110    else if (classRequested || classVector)
111      raiseErrorWho("parseMatrixContents", "classless domain");
112  }
113
114
115  if (weightIncluded || weightVector) {
116    if (weightID)
117      columns += weightIncluded;
118  }
119
120  include.clear();
121
122  if (attrsIncluded) {
123    int attrs_in = 0;
124
125    const_PITERATE(TVarList, vi, egen->domain->attributes) {
126      if ((*vi)->varType == TValue::FLOATVAR) {
127        attrs_in++;
128        include.push_back(true);
129      }
130      else if ((*vi)->varType == TValue::INTVAR) {
131        if ((*vi).AS(TEnumVariable)->values->size() == 2) {
132          attrs_in++;
133          include.push_back(true);
134        }
135        else
136          switch (multiTreatment) {
137            case 0:
138              include.push_back(false);
139              break;
140
141            case 1:
142              attrs_in++;
143              include.push_back(true);
144              break;
145
146            default:
147              raiseErrorWho("parseMatrixContents", "attribute '%s' is multinomial", (*vi)->get_name().c_str());
148          }
149      }
150      else {
151        attrs_in++;
152        include.push_back(true);
153        raiseWarning(PyExc_OrangeKernelWarning, "attribute '%s' is of unsupported type", (*vi)->get_name().c_str());
154      }
155    }
156
157    if (attrsRequested && !attrs_in)
158      raiseErrorWho("parseMatrixContents", "the domain has no (useful) attributes");
159
160    columns += attrs_in * attrsIncluded;
161  }
162}
163
Note: See TracBrowser for help on using the repository browser.