Ejemplo n.º 1
0
 def _LexicalToKeywords(cls, text, lexical_re):
     match = lexical_re.match(text)
     if match is None:
         raise BadTypeValueError('Value "%s" not in %s lexical space' %
                                 (text, cls._ExpandedName))
     match_map = match.groupdict()
     kw = {}
     for (k, v) in match_map.iteritems():
         if (k in cls.__Fields) and (v is not None):
             kw[k] = types.IntType(v)
     if '-' == match_map.get('negYear', None):
         kw['year'] = -kw['year']
     if match_map.get('fracsec', None) is not None:
         kw['microsecond'] = types.IntType(
             1000000 * types.FloatType('0%s' % (match_map['fracsec'], )))
     else:
         # Discard any bogosity passed in by the caller
         kw.pop('microsecond', None)
     if match_map.get('tzinfo', None) is not None:
         kw['tzinfo'] = pyxb.utils.utility.UTCOffsetTimeZone(
             match_map['tzinfo'], flip=True)
     else:
         kw.pop('tzinfo', None)
     return kw
Ejemplo n.º 2
0
def CA_obsolete(res=None, returnFoldStat=False):
    """
    Calculates the classification Accuracy of orngTest.ExperimentResults in res
    The results res must be from a classifier
    """
    # If Called without arguments, return the type of problems this method can be used for:
    # 1 - Classification problems (Discrete Class)
    # 2 - Regression problems (Continuous Class)
    # 3 - Both Regression and Classification problems (Continuous or Discrete Class)
    if res == None:
        return {"type": CLASSIFICATION}

    if res.numberOfIterations > 1:
        CAs = [[0.0] * res.numberOfIterations
               for i in range(res.numberOfLearners)]
        nIter = [0] * res.numberOfIterations
        for tex in res.results:
            ac = tex.actualClass
            nIter[tex.iterationNumber] += 1
            for i, cls in enumerate(tex.classes):
                if cls == ac:
                    CAs[i][tex.iterationNumber] += 1
        CAs = [[x / ni for x, ni in zip(y, nIter)] for y in CAs]

        CAfoldList = CAs
        CA = [statc.mean(x) for x in CAs]
        CAstd = stats.stdev(CAfoldList[0])

        if returnFoldStat:
            return [round(statc.mean(x), 3) for x in CAs], CAfoldList
        else:
            return [round(statc.mean(x), 3) for x in CAs]

    else:
        CAs = [0.0] * res.numberOfLearners
        for tex in res.results:
            CAs = map(lambda res, cls, ac=tex.actualClass: res + types.IntType(
                cls == ac),
                      CAs,
                      tex.classes)
        return [round(x / (len(res.results)), 3) for x in CAs]
Ejemplo n.º 3
0
def makeEntity(xxx_todo_changeme):
    (name, num, description) = xxx_todo_changeme
    from nevow.stan import Entity
    e = Entity(name, num, description)
    __by_number[types.IntType(num)] = e
    globals()[name] = e
Ejemplo n.º 4
0
else:
    keys = [key for key in parameters]
for key in keys:
    valuesRange=eval(parameters[key][2])
    if key in paramKeys:
        idx = paramKeys.index(key)
    else:
        idx = None
    if parameters[key][1]=="values":
        if type(valuesRange[0]) == types.StringType:
            if useDefaults or not parameters[key][5]:
                testParameters[key] = str(parameters[key][4])
            elif len(valuesRange) == 1:
                testParameters[key] = str(valuesRange[0])
            else:
                testParameters[key] = str(valuesRange[types.IntType(round(vars[idx]))])
            if type(eval(parameters[key][0])) == types.ListType:
                if eval(parameters[key][0])[0] != types.TypeType:
		    if eval(parameters[key][0])[0] == types.BooleanType:
                        testParameters[key] = eval(parameters[key][0])[0](eval(testParameters[key]))
		    else:
                        testParameters[key] = eval(parameters[key][0])[0](testParameters[key])
            else:
                if eval(parameters[key][0]) != types.TypeType:
		    if eval(parameters[key][0]) == types.BooleanType:
                        testParameters[key] = eval(parameters[key][0])(eval(testParameters[key]))
		    else:
                        testParameters[key] = eval(parameters[key][0])(testParameters[key])
        
        else:
            if (len(valuesRange) != 1) and parameters[key][5] and (not useDefaults):
Ejemplo n.º 5
0
def makeEntity((name, num, description)):
    e = Entity(name, num, description)
    __by_number[types.IntType(num)] = e
    globals()[name] = e
Ejemplo n.º 6
0
 def __init__(self, intValue, unsigned=False, bitWidth=32):
     valueType = types.IntType(unsigned, bitWidth)
     valueType.assertValueHasType(intValue)
     PrimitiveValue.__init__(self, valueType, intValue)
Ejemplo n.º 7
0
    def __new__(cls, *args, **kw):
        args = cls._ConvertArguments(args, kw)
        text = args[0]
        have_kw_update = False
        if isinstance(text, (str, unicode)):
            match = cls.__Lexical_re.match(text)
            if match is None:
                raise BadTypeValueError('Value "%s" not in %s lexical space' %
                                        (text, cls._ExpandedName))
            match_map = match.groupdict()
            if 'T' == match_map.get('Time', None):
                # Can't have T without additional time information
                raise BadTypeValueError('Value "%s" not in %s lexical space' %
                                        (text, cls._ExpandedName))

            negative_duration = ('-' == match_map.get('neg', None))

            fractional_seconds = 0.0
            if match_map.get('fracsec', None) is not None:
                fractional_seconds = types.FloatType('0%s' %
                                                     (match_map['fracsec'], ))
                usec = types.IntType(1000000 * fractional_seconds)
                if negative_duration:
                    kw['microseconds'] = -usec
                else:
                    kw['microseconds'] = usec
            else:
                # Discard any bogosity passed in by the caller
                kw.pop('microsecond', None)

            data = {}
            for fn in cls.__XSDFields:
                v = match_map.get(fn, 0)
                if v is None:
                    v = 0
                data[fn] = types.IntType(v)
                if fn in cls.__PythonFields:
                    if negative_duration:
                        kw[fn] = -data[fn]
                    else:
                        kw[fn] = data[fn]
            data['seconds'] += fractional_seconds
            have_kw_update = True
        elif isinstance(text, cls):
            data = text.durationData()
            negative_duration = text.negativeDuration()
        elif isinstance(text, datetime.timedelta):
            data = {
                'days': text.days,
                'seconds': text.seconds + (text.microseconds / 1000000.0)
            }
            negative_duration = (0 > data['days'])
            if negative_duration:
                data['days'] = 1 - data['days']
                data['seconds'] = 24 * 60 * 60.0 - data['seconds']
            data['minutes'] = 0
            data['hours'] = 0
        if not have_kw_update:
            rem_time = data['seconds']
            use_seconds = rem_time
            if (0 != (rem_time % 1)):
                data['microseconds'] = types.IntType(1000000 * (rem_time % 1))
                rem_time = rem_time // 1
            if 60 <= rem_time:
                data['seconds'] = rem_time % 60
                rem_time = data['minutes'] + (rem_time // 60)
            if 60 <= rem_time:
                data['minutes'] = rem_time % 60
                rem_time = data['hours'] + (rem_time // 60)
            data['hours'] = rem_time % 24
            data['days'] += (rem_time // 24)
            for fn in cls.__PythonFields:
                if fn in data:
                    if negative_duration:
                        kw[fn] = -data[fn]
                    else:
                        kw[fn] = data[fn]
                else:
                    kw.pop(fn, None)
            kw['microseconds'] = data.pop('microseconds', 0)
            data['seconds'] += kw['microseconds'] / 1000000.0

        rv = super(duration, cls).__new__(cls, **kw)
        rv.__durationData = data
        rv.__negativeDuration = negative_duration
        return rv
Ejemplo n.º 8
0
    def __call__(self, trainingData, weight=None):
        """Creates an PLS model from the data in trainingData. """
        if not AZBaseClasses.AZLearner.__call__(self, trainingData, weight):
            return None
        #Remove from the domain any unused values of discrete attributes including class
        trainingData = dataUtilities.getDataWithoutUnusedValues(
            trainingData, True)
        # Create path for the Orange data
        scratchdir = miscUtilities.createScratchDir(desc="PLS")
        OrngFile = os.path.join(scratchdir, "OrngData.tab")

        # Remove meta attributes from training data to make the imputer work with examples without the meta attributes.
        #dataUtilities.rmAllMeta(trainingData)
        if len(trainingData.domain.getmetas()) == 0:
            trainData = trainingData
        else:
            trainData = dataUtilities.getCopyWithoutMeta(trainingData)

# Create the imputer
        self.imputer = orange.ImputerConstructor_average(trainData)
        # Impute the data
        trainData = self.imputer(trainData)
        # Save the Data already imputed to an Orange formated file
        if self.verbose > 1:
            print time.asctime(), "Saving Orange Data to a tab file..."
        orange.saveTabDelimited(OrngFile, trainData)
        if self.verbose > 1: print time.asctime(), "done"

        # Create the PLS instance
        if self.verbose > 1: print time.asctime(), "Creating PLS Object..."
        learner = pls.PlsAPI()
        if self.verbose > 1: print time.asctime(), "done"

        # Assign the PLS parameters
        learner.SetParameter('v', str(self.verbose))
        learner.SetParameter('debug', str(int(self.verbose > 0)))
        learner.SetParameter('method', self.method)
        if types.IntType(self.k) > len(trainData.domain.attributes):
            learner.SetParameter('k', str(len(trainData.domain.attributes)))
            if self.verbose > 0:
                print "Warning! The number of components were more than the number of attributes."
            if self.verbose > 0:
                print "   Components were set to ", len(
                    trainData.domain.attributes)
        else:
            learner.SetParameter('k', self.k)
        learner.SetParameter('precision', self.precision)
        learner.SetParameter('sDir', scratchdir)  #AZOC.SCRATCHDIR)

        # Read the Orange Formated file and Train the Algorithm
        # TRAIN
        if self.verbose > 1: print time.asctime(), "Training..."
        learner.Train(OrngFile)
        if self.verbose > 1:
            print "Train finished at ", time.asctime()
            print "PLS trained in: " + str(
                learner.GetCPUTrainTime()) + " seconds"
            print "Method:     " + learner.GetParameter("method")
            print "Components: " + learner.GetParameter("k")
            print "Precision:  " + learner.GetParameter("precision")

        # Remove the scratch file
        if self.verbose == 0:
            miscUtilities.removeDir(scratchdir)
        else:
            print "The directory " + scratchdir + " was not deleted because DEBUG flag is ON"
        del trainData
        impData = self.imputer.defaults
        return PLSClassifier(
            classifier=learner,
            name="Classifier of " + self.name,
            classVar=trainingData.domain.classVar,
            imputeData=impData,
            verbose=self.verbose,
            varNames=[attr.name for attr in trainingData.domain.attributes],
            NTrainEx=len(trainingData),
            basicStat=self.basicStat,
            parameters=self.parameters)  #learner.GetClassVarName())#
Ejemplo n.º 9
0
def makeEntity((name, num, description)):
    from nevow.stan import Entity
    e = Entity(name, num, description)
    __by_number[types.IntType(num)] = e
    globals()[name] = e